4
* 2001-November-7 Jason Rohrer
7
* 2002-April-11 Jason Rohrer
8
* Added missing include, and fixed a bug.
10
* 2004-January-29 Jason Rohrer
11
* Fixed so that 0-point of time is the same as on other platforms.
13
* 2004-October-14 Jason Rohrer
14
* Fixed bug in second/millisecond callibration.
15
* Fixed bug in win32 time to ANSI time translation.
16
* Fixed daylight savings time bug.
20
#include "minorGems/system/Time.h"
30
* Windows implementation of Time.h.
32
* The 0-point should match the ANSI standard.
37
void Time::getCurrentTime( unsigned long *outSeconds,
38
unsigned long *outMilliseconds ) {
39
// convert from win32 broken-down time (which has msec resolution)
40
// to an ANSI time struct and then convert to an absolute time in
42
// This procedure ensures that the 0-point matches the ANSI standard.
45
// we cannot simply call ANSI time() to get the seconds and then rely
46
// on GetLocalTime to get the milliseconds, since the seconds value
47
// used by GetLocalTime is (strangely enough) not calibrated to the seconds
49
// In other words, it is possible for the time() seconds to advance
50
// at a different clock cycle than the GetLocalTime seconds.
52
// get time using a win32 call
53
SYSTEMTIME win32TimeStruct;
54
GetLocalTime( &win32TimeStruct );
56
// convert this win32 structure to the ANSI standard structure
57
struct tm ansiTimeStruct;
59
ansiTimeStruct.tm_sec = win32TimeStruct.wSecond;
60
ansiTimeStruct.tm_min = win32TimeStruct.wMinute;
61
ansiTimeStruct.tm_hour = win32TimeStruct.wHour;
62
ansiTimeStruct.tm_mday = win32TimeStruct.wDay;
63
// ANSI time struct has month in range [0..11]
64
ansiTimeStruct.tm_mon = win32TimeStruct.wMonth - 1;
65
// ANSI time struct has year that is an offset from 1900
66
ansiTimeStruct.tm_year = win32TimeStruct.wYear - 1900;
67
// unknown daylight savings time (dst) status
68
// if we fail to init this value, we can get inconsistent results
69
ansiTimeStruct.tm_isdst = -1;
71
unsigned long secondsSinceEpoch = mktime( &ansiTimeStruct );
73
*outSeconds = secondsSinceEpoch;
74
*outMilliseconds = (unsigned long)( win32TimeStruct.wMilliseconds );