~ubuntu-branches/ubuntu/precise/primrose/precise

« back to all changes in this revision

Viewing changes to minorGems/system/win32/TimeWin32.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Paul Wise
  • Date: 2009-04-06 19:26:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090406192656-cri7503gebyvfl8t
Tags: upstream-5+dfsg1
ImportĀ upstreamĀ versionĀ 5+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Modification History
 
3
 *
 
4
 * 2001-November-7              Jason Rohrer
 
5
 * Created.
 
6
 *
 
7
 * 2002-April-11                Jason Rohrer
 
8
 * Added missing include, and fixed a bug.
 
9
 *
 
10
 * 2004-January-29              Jason Rohrer
 
11
 * Fixed so that 0-point of time is the same as on other platforms.
 
12
 *
 
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.
 
17
 */
 
18
 
 
19
 
 
20
#include "minorGems/system/Time.h"
 
21
 
 
22
#include <windows.h>
 
23
#include <winbase.h>
 
24
#include <time.h>
 
25
#include <stdio.h>
 
26
 
 
27
 
 
28
 
 
29
/**
 
30
 * Windows implementation of Time.h.
 
31
 *
 
32
 * The 0-point should match the ANSI standard.
 
33
 */
 
34
 
 
35
 
 
36
 
 
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
 
41
    // seconds
 
42
    // This procedure ensures that the 0-point matches the ANSI standard.
 
43
 
 
44
    // note:
 
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
 
48
    // value of time().
 
49
    // In other words, it is possible for the time() seconds to advance
 
50
    // at a different clock cycle than the GetLocalTime seconds.
 
51
 
 
52
    // get time using a win32 call
 
53
    SYSTEMTIME win32TimeStruct;
 
54
    GetLocalTime( &win32TimeStruct );
 
55
 
 
56
    // convert this win32 structure to the ANSI standard structure
 
57
    struct tm ansiTimeStruct;
 
58
 
 
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;
 
70
    
 
71
    unsigned long secondsSinceEpoch = mktime( &ansiTimeStruct );
 
72
 
 
73
    *outSeconds = secondsSinceEpoch;
 
74
        *outMilliseconds = (unsigned long)( win32TimeStruct.wMilliseconds );    
 
75
        }
 
76
 
 
77