]>
git.saurik.com Git - wxWidgets.git/blob - src/common/datetime.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of time/date related classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "datetime.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
37 #include "wx/datetime.h"
39 // ============================================================================
40 // implementation of wxDateTime
41 // ============================================================================
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 Country
wxDateTime::ms_country
;
48 wxDateTime
wxDateTime::ms_InvDateTime
;
50 // ----------------------------------------------------------------------------
51 // constructors and assignment operators
52 // ----------------------------------------------------------------------------
54 wxDateTime
& wxDateTime::Set(const struct tm
& tm
)
56 time_t timet
= mktime(tm
);
57 if ( timet
== (time_t)(-1) )
59 wxFAIL_MSG(_T("Invalid time"));
61 return ms_InvDateTime
;
69 wxDateTime
& wxDateTime::Set(wxDateTime_t hour
,
72 wxDateTime_t millisec
)
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,
78 _T("Invalid time in wxDateTime::Set()") );
80 // get the current date from system
81 time_t timet
= GetTimeNow();
82 struct tm
*tm
= localtime(&timet
);
91 // and finally adjust milliseconds
92 return SetMillisecond(millisec
);
95 wxDateTime
& wxDateTime::Set(wxDateTime_t day
,
101 wxDateTime_t millisec
)
103 wxCHECK_MSG( hour
< 24 && second
< 62 && minute
< 60 && millisec
< 1000,
105 _T("Invalid time in wxDateTime::Set()") );
107 if ( year
== Inv_Year
)
108 year
= GetCurrentYear();
109 if ( month
== Inv_Month
)
110 month
= GetCurrentMonth();
112 wxCHECK_MSG( day
< GetNumberOfDays(month
, year
), ms_InvDateTime
,
113 _T("Invalid date in wxDateTime::Set()") );
115 // the range of time_t type (inclusive)
116 static const int yearMinInRange
= 1970;
117 static const int yearMaxInRange
= 2037;
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
)
123 // use the standard library version if the date is in range - this is
124 // probably much more efficient than our code
135 // and finally adjust milliseconds
136 return SetMillisecond(millisec
);
140 // do time calculations ourselves: we want to calculate the number of
141 // milliseconds between the given date and the epoch (necessarily