← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba

 

Paul J. Lucas has proposed merging lp:~paul-lucas/zorba/pjl-misc into lp:zorba.

Commit message:
Time code clean-up.

Requested reviews:
  Paul J. Lucas (paul-lucas)

For more details, see:
https://code.launchpad.net/~paul-lucas/zorba/pjl-misc/+merge/148075

Time code clean-up.
-- 
https://code.launchpad.net/~paul-lucas/zorba/pjl-misc/+merge/148075
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'src/context/dynamic_context.cpp'
--- src/context/dynamic_context.cpp	2013-01-08 08:34:08 +0000
+++ src/context/dynamic_context.cpp	2013-02-13 01:59:23 +0000
@@ -15,18 +15,9 @@
  */
 #include "stdafx.h"
 
-#ifdef WIN32
-#include <sys/types.h>
-#include <sys/timeb.h>
-#endif
+#include <assert.h>
+
 #include "common/common.h"
-#include <assert.h>
-#include <time.h>
-#include <sys/timeb.h>
-#ifdef UNIX
-#include <sys/time.h>
-#include <unistd.h>
-#endif
 #include "store/api/iterator.h"
 #include "store/api/temp_seq.h"
 #include "store/api/item_factory.h"
@@ -52,6 +43,7 @@
 
 #include "zorbautils/hashmap_itemp.h"
 #include "util/string_util.h"
+#include "util/time_util.h"
 
 #include "diagnostics/assert.h"
 #include "diagnostics/util_macros.h"
@@ -235,34 +227,24 @@
 ********************************************************************************/
 void dynamic_context::reset_current_date_time()
 {
-  int lTimeShift = 0;
-#if defined (WIN32)
-  struct _timeb timebuffer;
-  _ftime_s( &timebuffer );
-  struct ::tm gmtm;
-  localtime_s(&gmtm, &timebuffer.time); //thread safe localtime on Windows
-  lTimeShift = -timebuffer.timezone*60;
-  if (gmtm.tm_isdst != 0)
-    lTimeShift += 3600;
-#else
-  struct timeb timebuffer;
-  ftime( &timebuffer );
-  struct ::tm gmtm;
-  localtime_r(&timebuffer.time, &gmtm); //thread safe localtime on Linux
-  lTimeShift = gmtm.tm_gmtoff;
-#endif
-
-  set_implicit_timezone(lTimeShift);//in seconds
-
-  GENV_ITEMFACTORY->createDateTime(theCurrentDateTime,
-                                   static_cast<short>(gmtm.tm_year + 1900),
-                                   static_cast<short>(gmtm.tm_mon + 1),
-                                   static_cast<short>(gmtm.tm_mday),
-                                   static_cast<short>(gmtm.tm_hour),
-                                   static_cast<short>(gmtm.tm_min),
-                                   gmtm.tm_sec + timebuffer.millitm/1000.0,
-                                   static_cast<short>(theTimezone/3600));
-
+  time::sec_type sec;
+  time::usec_type usec;
+  time::get_epoch( &sec, &usec );
+  time::ztm tm;
+  time::get_localtime( &tm, sec );
+
+  set_implicit_timezone( tm.ZTM_GMTOFF );
+
+  GENV_ITEMFACTORY->createDateTime(
+    theCurrentDateTime,
+    static_cast<short>( tm.tm_year + TM_YEAR_BASE ),
+    static_cast<short>( tm.tm_mon + 1 ),
+    static_cast<short>( tm.tm_mday ),
+    static_cast<short>( tm.tm_hour ),
+    static_cast<short>( tm.tm_min ),
+    tm.tm_sec + usec / 1000000.0,
+    static_cast<short>( tm.ZTM_GMTOFF / 3600 )
+  );
 }
 
 

=== modified file 'src/runtime/datetime/datetime_impl.cpp'
--- src/runtime/datetime/datetime_impl.cpp	2013-01-29 06:10:09 +0000
+++ src/runtime/datetime/datetime_impl.cpp	2013-02-13 01:59:23 +0000
@@ -175,7 +175,7 @@
 
 bool CurrentDateTime::nextImpl( store::Item_t& result,
                                 PlanState &plan_state ) const {
-  time_t sec;
+  time::sec_type sec;
   time::usec_type usec;
   time::get_epoch( &sec, &usec );
   time::ztm tm;
@@ -201,7 +201,7 @@
 
 bool CurrentTime::nextImpl( store::Item_t& result,
                             PlanState &plan_state ) const {
-  time_t sec;
+  time::sec_type sec;
   time::usec_type usec;
   time::get_epoch( &sec, &usec );
   time::ztm tm;
@@ -234,7 +234,7 @@
   consumeNext( item, theChildren[0], plan_state );
   millis = item->getLongValue();
 
-  time::get_gmtime( &tm, static_cast<time_t>( millis / 1000 ) );
+  time::get_gmtime( &tm, static_cast<time::sec_type>( millis / 1000 ) );
   GENV_ITEMFACTORY->createDateTime(
     result,
     static_cast<short>( tm.tm_year + TM_YEAR_BASE ),
@@ -360,7 +360,7 @@
 
 bool Timestamp::nextImpl( store::Item_t& result,
                           PlanState &plan_state ) const {
-  time_t sec;
+  time::sec_type sec;
   time::get_epoch( &sec );
 
   PlanIteratorState *state;

=== modified file 'src/util/time_util.cpp'
--- src/util/time_util.cpp	2013-01-10 01:23:21 +0000
+++ src/util/time_util.cpp	2013-02-13 01:59:23 +0000
@@ -144,14 +144,14 @@
   return days[ mon ] + (mon == 1 /* Feb */ && is_leap_year( year ));
 }
 
-void get_epoch( time_t *sec, usec_type *usec ) {
+void get_epoch( sec_type *sec, usec_type *usec ) {
 #ifdef WIN32
   FILETIME ft;
   ::GetSystemTimeAsFileTime( &ft );
   unsigned __int64 temp = ((__int64)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
   temp /= 10;                           // nanosec -> usec
   temp -= DELTA_EPOCH_IN_USEC;          // 1601 -> 1970
-  *sec = (time_t)(temp / 1000000UL);    // usec -> sec
+  *sec = (sec_type)(temp / 1000000UL);  // usec -> sec
   if ( usec )
     *usec = (usec_type)(temp % 1000000UL);
 #else
@@ -163,18 +163,18 @@
 #endif /* WIN32 */
 }
 
-void get_gmtime( ztm *tm, time_t when ) {
+void get_gmtime( ztm *tm, sec_type when ) {
   if ( !when )
     get_epoch( &when );
 #ifdef WIN32
   ::gmtime_s( tm, &when );
+#else
+  ::gmtime_r( &when, tm );
+#endif /* WIN32 */
   tm->ZTM_GMTOFF = 0;
-#else
-  ::gmtime_r( &when, tm );
-#endif /* WIN32 */
 }
 
-void get_localtime( ztm *tm, time_t when ) {
+void get_localtime( ztm *tm, sec_type when ) {
   if ( !when )
     get_epoch( &when );
 #ifdef WIN32
@@ -182,6 +182,10 @@
   tm->ZTM_GMTOFF = - _timezone;         // seconds west -> east
 #else
   ::localtime_r( &when, tm );
+#if !defined(ZORBA_HAVE_STRUCT_TM_TM_GMTOFF) && \
+    !defined(ZORBA_HAVE_STRUCT_TM___TM_GMTOFF)
+  tm->ZTM_GMTOFF = ::timegm( tm ) - when;
+#endif
 #endif /* WIN32 */
 }
 
@@ -191,7 +195,7 @@
   ::GetTimeZoneInformation( &tz );
   return tz.Bias * -60;                 // minutes west -> seconds east
 #else
-  time_t const now = ::time( nullptr );
+  sec_type const now = ::time( nullptr );
   ztm tm;
   ::localtime_r( &now, &tm );
   return ::timegm( &tm ) - now;

=== modified file 'src/util/time_util.h'
--- src/util/time_util.h	2012-12-29 17:08:29 +0000
+++ src/util/time_util.h	2013-02-13 01:59:23 +0000
@@ -36,6 +36,8 @@
 
 ///////////////////////////////////////////////////////////////////////////////
 
+typedef time_t sec_type;
+
 #ifdef WIN32
 typedef unsigned long usec_type;
 #else
@@ -119,7 +121,7 @@
  * @param usec A pointer to the result in microseconds or \c null if this is
  * not desired.
  */
-void get_epoch( time_t *sec, usec_type *usec = nullptr );
+void get_epoch( sec_type *sec, usec_type *usec = nullptr );
 
 /**
  * Gets the Greenwich time and populates the given ztm structure.
@@ -128,7 +130,7 @@
  * @param when If &gt; 0, populates \a tm based on \a when number of seconds
  * since \e epoch; if 0, populates \a when based on \e now.
  */
-void get_gmtime( ztm *tm, time_t when = 0 );
+void get_gmtime( ztm *tm, sec_type when = 0 );
 
 /**
  * Gets the offset of the current timezone from Greenwich time.
@@ -145,7 +147,7 @@
  * @param when If &gt; 0, populates \a tm based on \a when number of seconds
  * since \e epoch; if 0, populates \a when based on \e now.
  */
-void get_localtime( ztm *tm, time_t when = 0 );
+void get_localtime( ztm *tm, sec_type when = 0 );
 
 /**
  * Checks whether the given year is a leap year.


Follow ups