//============================================================================= // drt/sys/itime1.cpp // DRT primitive date and time classes. // This contains the O/S-dependent implementation code for class DrtTime. // // History // 0.01, 1999-02-27, David R Tribble. // First cut. // // Copyright ©1999, by David R. Tribble, all rights reserved. // See "drt/sys/copyr.txt" for more information. //----------------------------------------------------------------------------- // Identification static const char id[] = "@(#)drt/sys/itime1.cpp 0.01"; // System includes #include #define drt_std_assert_h 1 #include #define drt_std_time_h 1 // Special includes #include "sdefs.hpp" // System-specific includes #if DRT_OS_UNIX #ifndef drt_std_sys_time_h #include #define drt_std_sys_time_h 1 #endif #elif DRT_OS_WIN32 #ifndef drt_std_windows_h #define WIN32_LEAN_AND_MEAN 1 #include #define drt_std_windows_h 1 #endif #endif // Local includes #include "sdebug.hpp" #include "stime.hpp" // Local wrappers drt_namespace_begin //----------------------------------------------------------------------------- // Shared class functions //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // DrtTime::now() // Determine the current date and time, and set 'r' to this value. // // Returns // True if successful, otherwise false. //----------------------------------------------------------------------------- /*static*/ DrtTime DrtTime::now() { #if DrtTime_VS/100 != 1 #error DrtTime_VS has changed #endif DrtTrace dbg(s_grp, "now"); DrtTime t; #if DRT_OS_UNIX //--------------------------------------------------------------------- // The BSD gettimeofday() function fills a 'timeval' structure with the // current time_t value (which is the number of seconds since // AD1970-01-01 00:00:00 Z) and a microsecond count. These time values // are directly convertible into DrtTime epoch tick counts. // // If the gettimeofday() function is not available, we can use the // POSIX.4 clock_gettime() function instead. //--------------------------------------------------------------------- // Retrieve the current (system) date struct timeval tv; // Get the current system time (secs & usecs) #if DRT_USE_POSIX_GETTIMEOFDAY clock_gettimeofday(&tv, null); #else gettimeofday(&tv, null); #endif // Convert to DrtTime form t.m_ticks = AD1970; t.m_ticks += tv.tv_sec * (drt_int64_t)TICKS_PER_SEC; t.m_ticks += tv.tv_usec * (drt_int64_t)(TICKS_PER_SEC/1000000); return (t); #elif DRT_OS_WIN32 //------------------------------------------------------------------------- // The Microsoft Win32 API can return the current system time in "file // timestamp" format, which is a 64-bit value representing the number of // 100-nanosecond ticks since AD1601-01-01 00:00:00 Z. This time value is // directly convertible into a DrtTime epoch tick count. //------------------------------------------------------------------------- // Retrieve the current (system) date FILETIME ft; // Get the current system time GetSystemTimeAsFileTime(&ft); // Not GetSystemTime(&st) // Convert to DrtTime form drt_int64_t x; x = ((drt_int64_t)ft.dwHighDateTime << 32) + ft.dwLowDateTime; t.m_ticks = AD1601; t.m_ticks += x; return (t); #elif DRT_OS_ISO //------------------------------------------------------------------------- // If all else fails, we can use the ISO C/C++ time() function to get the // current system time. // // Unfortunately, the only thing we know (portably) about the value is that // the time_t type is arithmetic. This isn't much help for converting the // value into DrtTime epoch tick counts. // // We can, however, convert the time_t value into a broken-down 'tm' // structure, and then convert that back into a DrtTime value. What a // pain. //------------------------------------------------------------------------- // Retrieve the current (system) date time_t t; struct tm st; // Get the current system time (secs & usecs) time(&t); REDO // Convert to broken-down (expanded) form st = *gmtime(&t); REDO r->m_year = st.tm_year+1900; r->m_mon = st.tm_mon; r->m_mday = st.tm_mday; r->m_hour = st.tm_hour; r->m_min = st.tm_min; r->m_sec = st.tm_sec; r->m_usec = 0; return (true); #else #error Unknown O/S return (0); #endif } drt_namespace_end //============================================================================= // Test code //----------------------------------------------------------------------------- #if TEST //----------------------------------------------------------------------------- // ::main() // Test driver. //----------------------------------------------------------------------------- int main(int argc, char **argv) { printf("Create a DrtTime object...\n"); drt_namespace::DrtTime t; printf("Get the current time and time...\n"); t = t.now(); { union /*anonymous*/ { drt_int64_t t; unsigned char b[sizeof(drt_int64_t)]; } u; u.t = t.ticks(); #if DRT_BYTE_HL for (int i = 0; i < sizeof(u.t); i++) printf(" %02X", u.b[i]); #else for (int i = sizeof(u.t)-1; i >= 0; i--) printf(" %02X", u.b[i]); #endif printf("\n"); } return (0); } #endif // TEST // End itime1.cpp