]> git.saurik.com Git - wxWidgets.git/blob - src/common/datetime.cpp
New event locking.
[wxWidgets.git] / src / common / datetime.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/datetime.h
3 // Purpose: implementation of time/date related classes
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 11.05.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "datetime.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/string.h"
33 #include "wx/intl.h"
34 #include "wx/log.h"
35 #endif // WX_PRECOMP
36
37 #include "wx/datetime.h"
38
39 // ============================================================================
40 // implementation of wxDateTime
41 // ============================================================================
42
43 // ----------------------------------------------------------------------------
44 // static data
45 // ----------------------------------------------------------------------------
46
47 Country wxDateTime::ms_country;
48 wxDateTime wxDateTime::ms_InvDateTime;
49
50 // ----------------------------------------------------------------------------
51 // constructors and assignment operators
52 // ----------------------------------------------------------------------------
53
54 wxDateTime& wxDateTime::Set(const struct tm& tm)
55 {
56 time_t timet = mktime(tm);
57 if ( timet == (time_t)(-1) )
58 {
59 wxFAIL_MSG(_T("Invalid time"));
60
61 return ms_InvDateTime;
62 }
63 else
64 {
65 return Set(timet);
66 }
67 }
68
69 wxDateTime& wxDateTime::Set(wxDateTime_t hour,
70 wxDateTime_t minute,
71 wxDateTime_t second,
72 wxDateTime_t millisec)
73 {
74 // we allow seconds to be 61 to account for the leap seconds, even if we
75 // don't use them really
76 wxCHECK_MSG( hour < 24 && second < 62 && minute < 60 && millisec < 1000,
77 ms_InvDateTime,
78 _T("Invalid time in wxDateTime::Set()") );
79
80 // get the current date from system
81 time_t timet = GetTimeNow();
82 struct tm *tm = localtime(&timet);
83
84 // adjust the time
85 tm->tm_hour = hour;
86 tm->tm_min = minute;
87 tm->tm_sec = second;
88
89 (void)Set(tm);
90
91 // and finally adjust milliseconds
92 return SetMillisecond(millisec);
93 }
94
95 wxDateTime& wxDateTime::Set(wxDateTime_t day,
96 Month month,
97 int year,
98 wxDateTime_t hour,
99 wxDateTime_t minute,
100 wxDateTime_t second,
101 wxDateTime_t millisec)
102 {
103 wxCHECK_MSG( hour < 24 && second < 62 && minute < 60 && millisec < 1000,
104 ms_InvDateTime,
105 _T("Invalid time in wxDateTime::Set()") );
106
107 if ( year == Inv_Year )
108 year = GetCurrentYear();
109 if ( month == Inv_Month )
110 month = GetCurrentMonth();
111
112 wxCHECK_MSG( day < GetNumberOfDays(month, year), ms_InvDateTime,
113 _T("Invalid date in wxDateTime::Set()") );
114
115 // the range of time_t type (inclusive)
116 static const int yearMinInRange = 1970;
117 static const int yearMaxInRange = 2037;
118
119 // test only the year instead of testing for the exact end of the Unix
120 // time_t range - it doesn't bring anything to do more precise checks
121 if ( year >= yearMaxInRange && year <= yearMaxInRange )
122 {
123 // use the standard library version if the date is in range - this is
124 // probably much more efficient than our code
125 struct tm tm;
126 tm.tm_year = year;
127 tm.tm_mon = month;
128 tm.tm_mday = day;
129 tm.tm_hour = hour;
130 tm.tm_min = minute;
131 tm.tm_sec = second;
132
133 (void)Set(tm);
134
135 // and finally adjust milliseconds
136 return SetMillisecond(millisec);
137 }
138 else
139 {
140 // do time calculations ourselves: we want to calculate the number of
141 // milliseconds between the given date and the epoch (necessarily
142 // negative)
143 }
144
145 return *this;
146 }