]> git.saurik.com Git - wxWidgets.git/blame - src/common/time.cpp
Added wxGetUTCTimeMillis() and wxGetUTCTimeUSec().
[wxWidgets.git] / src / common / time.cpp
CommitLineData
59068d79
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/common/time.cpp
3// Purpose: Implementation of time-related functions.
4// Author: Vadim Zeitlin
5// Created: 2011-11-26
6// RCS-ID: $Id: wxhead.cpp,v 1.11 2010-04-22 12:44:51 zeitlin Exp $
7// Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// for compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#include "wx/time.h"
27
28#ifndef WX_GMTOFF_IN_TM
29 // Define it for some systems which don't (always) use configure but are
30 // known to have tm_gmtoff field.
31 #if defined(__WXPALMOS__) || defined(__DARWIN__)
32 #define WX_GMTOFF_IN_TM
33 #endif
34#endif
35
36// ============================================================================
37// implementation
38// ============================================================================
39
40// returns the time zone in the C sense, i.e. the difference UTC - local
41// (in seconds)
42int wxGetTimeZone()
43{
44#ifdef WX_GMTOFF_IN_TM
45 // set to true when the timezone is set
46 static bool s_timezoneSet = false;
47 static long gmtoffset = LONG_MAX; // invalid timezone
48
49 // ensure that the timezone variable is set by calling wxLocaltime_r
50 if ( !s_timezoneSet )
51 {
52 // just call wxLocaltime_r() instead of figuring out whether this
53 // system supports tzset(), _tzset() or something else
54 time_t t = time(NULL);
55 struct tm tm;
56
57 wxLocaltime_r(&t, &tm);
58 s_timezoneSet = true;
59
60 // note that GMT offset is the opposite of time zone and so to return
61 // consistent results in both WX_GMTOFF_IN_TM and !WX_GMTOFF_IN_TM
62 // cases we have to negate it
63 gmtoffset = -tm.tm_gmtoff;
64
65 // this function is supposed to return the same value whether DST is
66 // enabled or not, so we need to use an additional offset if DST is on
67 // as tm_gmtoff already does include it
68 if ( tm.tm_isdst )
69 gmtoffset += 3600;
70 }
71 return (int)gmtoffset;
72#elif defined(__DJGPP__) || defined(__WINE__)
73 struct timeb tb;
74 ftime(&tb);
75 return tb.timezone*60;
76#elif defined(__VISUALC__)
77 // We must initialize the time zone information before using it (this will
78 // be done only once internally).
79 _tzset();
80
81 // Starting with VC++ 8 timezone variable is deprecated and is not even
82 // available in some standard library version so use the new function for
83 // accessing it instead.
84 #if wxCHECK_VISUALC_VERSION(8)
85 long t;
86 _get_timezone(&t);
87 return t;
88 #else // VC++ < 8
89 return timezone;
90 #endif
91#elif defined(WX_TIMEZONE) // If WX_TIMEZONE was defined by configure, use it.
92 return WX_TIMEZONE;
93#elif defined(__BORLANDC__) || defined(__MINGW32__) || defined(__VISAGECPP__)
94 return _timezone;
95#elif defined(__MWERKS__)
96 return 28800;
97#else // unknown platform -- assume it has timezone
98 return timezone;
99#endif // WX_GMTOFF_IN_TM/!WX_GMTOFF_IN_TM
100}