]> git.saurik.com Git - wxWidgets.git/blob - src/common/datetime.cpp
Mostly Mingw32/Cygwin corrections
[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) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // parts of code taken from sndcal library by Scott E. Lee:
10 //
11 // Copyright 1993-1995, Scott E. Lee, all rights reserved.
12 // Permission granted to use, copy, modify, distribute and sell
13 // so long as the above copyright and this permission statement
14 // are retained in all copies.
15 //
16 // Licence: wxWindows license
17 ///////////////////////////////////////////////////////////////////////////////
18
19 /*
20 * Implementation notes:
21 *
22 * 1. the time is stored as a 64bit integer containing the signed number of
23 * milliseconds since Jan 1. 1970 (the Unix Epoch) - so it is always
24 * expressed in GMT.
25 *
26 * 2. the range is thus something about 580 million years, but due to current
27 * algorithms limitations, only dates from Nov 24, 4714BC are handled
28 *
29 * 3. standard ANSI C functions are used to do time calculations whenever
30 * possible, i.e. when the date is in the range Jan 1, 1970 to 2038
31 *
32 * 4. otherwise, the calculations are done by converting the date to/from JDN
33 * first (the range limitation mentioned above comes from here: the
34 * algorithm used by Scott E. Lee's code only works for positive JDNs, more
35 * or less)
36 *
37 * 5. the object constructed for the given DD-MM-YYYY HH:MM:SS corresponds to
38 * this moment in local time and may be converted to the object
39 * corresponding to the same date/time in another time zone by using
40 * ToTimezone()
41 *
42 * 6. the conversions to the current (or any other) timezone are done when the
43 * internal time representation is converted to the broken-down one in
44 * wxDateTime::Tm.
45 */
46
47 // ============================================================================
48 // declarations
49 // ============================================================================
50
51 // ----------------------------------------------------------------------------
52 // headers
53 // ----------------------------------------------------------------------------
54
55 #ifdef __GNUG__
56 #pragma implementation "datetime.h"
57 #endif
58
59 // For compilers that support precompilation, includes "wx.h".
60 #include "wx/wxprec.h"
61
62 #ifdef __BORLANDC__
63 #pragma hdrstop
64 #endif
65
66 #ifndef WX_PRECOMP
67 #include "wx/string.h"
68 #include "wx/intl.h"
69 #include "wx/log.h"
70 #endif // WX_PRECOMP
71
72 #include "wx/thread.h"
73
74 #define wxDEFINE_TIME_CONSTANTS
75
76 #include "wx/datetime.h"
77
78 #ifndef WX_TIMEZONE
79 #define WX_TIMEZONE timezone
80 #endif
81
82 // Is this right? Just a guess. (JACS)
83 #ifdef __MINGW32__
84 #define timezone _timezone
85 #endif
86
87 // ----------------------------------------------------------------------------
88 // constants
89 // ----------------------------------------------------------------------------
90
91 // some trivial ones
92 static const int MONTHS_IN_YEAR = 12;
93
94 static const int SECONDS_IN_MINUTE = 60;
95
96 static const long SECONDS_PER_DAY = 86400l;
97
98 static const long MILLISECONDS_PER_DAY = 86400000l;
99
100 // this is the integral part of JDN of the midnight of Jan 1, 1970
101 // (i.e. JDN(Jan 1, 1970) = 2440587.5)
102 static const int EPOCH_JDN = 2440587;
103
104 // the date of JDN -0.5 (as we don't work with fractional parts, this is the
105 // reference date for us) is Nov 24, 4714BC
106 static const int JDN_0_YEAR = -4713;
107 static const int JDN_0_MONTH = wxDateTime::Nov;
108 static const int JDN_0_DAY = 24;
109
110 // the constants used for JDN calculations
111 static const int JDN_OFFSET = 32046;
112 static const int DAYS_PER_5_MONTHS = 153;
113 static const int DAYS_PER_4_YEARS = 1461;
114 static const int DAYS_PER_400_YEARS = 146097;
115
116 // ----------------------------------------------------------------------------
117 // globals
118 // ----------------------------------------------------------------------------
119
120 // a critical section is needed to protect GetTimeZone() static
121 // variable in MT case
122 #if wxUSE_THREADS
123 wxCriticalSection gs_critsectTimezone;
124 #endif // wxUSE_THREADS
125
126 // ----------------------------------------------------------------------------
127 // private functions
128 // ----------------------------------------------------------------------------
129
130 // get the number of days in the given month of the given year
131 static inline
132 wxDateTime::wxDateTime_t GetNumOfDaysInMonth(int year, wxDateTime::Month month)
133 {
134 // the number of days in month in Julian/Gregorian calendar: the first line
135 // is for normal years, the second one is for the leap ones
136 static wxDateTime::wxDateTime_t daysInMonth[2][MONTHS_IN_YEAR] =
137 {
138 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
139 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
140 };
141
142 return daysInMonth[wxDateTime::IsLeapYear(year)][month];
143 }
144
145 // ensure that the timezone variable is set by calling localtime
146 static int GetTimeZone()
147 {
148 // set to TRUE when the timezone is set
149 static bool s_timezoneSet = FALSE;
150
151 wxCRIT_SECT_LOCKER(lock, gs_critsectTimezone);
152
153 if ( !s_timezoneSet )
154 {
155 // just call localtime() instead of figuring out whether this system
156 // supports tzset(), _tzset() or something else
157 time_t t;
158 (void)localtime(&t);
159
160 s_timezoneSet = TRUE;
161 }
162
163 return (int)WX_TIMEZONE;
164 }
165
166 // return the integral part of the JDN for the midnight of the given date (to
167 // get the real JDN you need to add 0.5, this is, in fact, JDN of the
168 // noon of the previous day)
169 static long GetTruncatedJDN(wxDateTime::wxDateTime_t day,
170 wxDateTime::Month mon,
171 int year)
172 {
173 // CREDIT: code below is by Scott E. Lee (but bugs are mine)
174
175 // check the date validity
176 wxASSERT_MSG(
177 (year > JDN_0_YEAR) ||
178 ((year == JDN_0_YEAR) && (mon > JDN_0_MONTH)) ||
179 ((year == JDN_0_YEAR) && (mon == JDN_0_MONTH) && (day >= JDN_0_DAY)),
180 _T("date out of range - can't convert to JDN")
181 );
182
183 // make the year positive to avoid problems with negative numbers division
184 year += 4800;
185
186 // months are counted from March here
187 int month;
188 if ( mon >= wxDateTime::Mar )
189 {
190 month = mon - 2;
191 }
192 else
193 {
194 month = mon + 10;
195 year--;
196 }
197
198 // now we can simply add all the contributions together
199 return ((year / 100) * DAYS_PER_400_YEARS) / 4
200 + ((year % 100) * DAYS_PER_4_YEARS) / 4
201 + (month * DAYS_PER_5_MONTHS + 2) / 5
202 + day
203 - JDN_OFFSET;
204 }
205
206 // this function is a wrapper around strftime(3)
207 static wxString CallStrftime(const wxChar *format, const tm* tm)
208 {
209 wxChar buf[1024];
210 if ( !wxStrftime(buf, WXSIZEOF(buf), format, tm) )
211 {
212 // is ti really possible that 1024 is too short?
213 wxFAIL_MSG(_T("strftime() failed"));
214 }
215
216 return wxString(buf);
217 }
218
219 // if year and/or month have invalid values, replace them with the current ones
220 static void ReplaceDefaultYearMonthWithCurrent(int *year,
221 wxDateTime::Month *month)
222 {
223 struct tm *tmNow = NULL;
224
225 if ( *year == wxDateTime::Inv_Year )
226 {
227 tmNow = wxDateTime::GetTmNow();
228
229 *year = 1900 + tmNow->tm_year;
230 }
231
232 if ( *month == wxDateTime::Inv_Month )
233 {
234 if ( !tmNow )
235 tmNow = wxDateTime::GetTmNow();
236
237 *month = (wxDateTime::Month)tmNow->tm_mon;
238 }
239 }
240
241 // ============================================================================
242 // implementation of wxDateTime
243 // ============================================================================
244
245 // ----------------------------------------------------------------------------
246 // static data
247 // ----------------------------------------------------------------------------
248
249 wxDateTime::Country wxDateTime::ms_country = wxDateTime::Country_Unknown;
250 wxDateTime wxDateTime::ms_InvDateTime;
251
252 // ----------------------------------------------------------------------------
253 // struct Tm
254 // ----------------------------------------------------------------------------
255
256 wxDateTime::Tm::Tm()
257 {
258 year = (wxDateTime_t)wxDateTime::Inv_Year;
259 mon = wxDateTime::Inv_Month;
260 mday = 0;
261 hour = min = sec = msec = 0;
262 wday = wxDateTime::Inv_WeekDay;
263 }
264
265 wxDateTime::Tm::Tm(const struct tm& tm, const TimeZone& tz)
266 : m_tz(tz)
267 {
268 msec = 0;
269 sec = tm.tm_sec;
270 min = tm.tm_min;
271 hour = tm.tm_hour;
272 mday = tm.tm_mday;
273 mon = (wxDateTime::Month)tm.tm_mon;
274 year = 1900 + tm.tm_year;
275 wday = tm.tm_wday;
276 yday = tm.tm_yday;
277 }
278
279 bool wxDateTime::Tm::IsValid() const
280 {
281 // we allow for the leap seconds, although we don't use them (yet)
282 return (year != wxDateTime::Inv_Year) && (mon != wxDateTime::Inv_Month) &&
283 (mday <= GetNumOfDaysInMonth(year, mon)) &&
284 (hour < 24) && (min < 60) && (sec < 62) && (msec < 1000);
285 }
286
287 void wxDateTime::Tm::ComputeWeekDay()
288 {
289 // compute the week day from day/month/year: we use the dumbest algorithm
290 // possible: just compute our JDN and then use the (simple to derive)
291 // formula: weekday = (JDN + 1.5) % 7
292 wday = (wxDateTime::WeekDay)(GetTruncatedJDN(mday, mon, year) + 2) % 7;
293 }
294
295 void wxDateTime::Tm::AddMonths(int monDiff)
296 {
297 // normalize the months field
298 while ( monDiff < -mon )
299 {
300 year--;
301
302 monDiff += MONTHS_IN_YEAR;
303 }
304
305 while ( monDiff + mon > MONTHS_IN_YEAR )
306 {
307 year++;
308
309 monDiff -= MONTHS_IN_YEAR;
310 }
311
312 mon = (wxDateTime::Month)(mon + monDiff);
313
314 wxASSERT_MSG( mon >= 0 && mon < MONTHS_IN_YEAR, _T("logic error") );
315 }
316
317 void wxDateTime::Tm::AddDays(int dayDiff)
318 {
319 // normalize the days field
320 mday += dayDiff;
321 while ( mday < 1 )
322 {
323 AddMonths(-1);
324
325 mday += GetNumOfDaysInMonth(year, mon);
326 }
327
328 while ( mday > GetNumOfDaysInMonth(year, mon) )
329 {
330 mday -= GetNumOfDaysInMonth(year, mon);
331
332 AddMonths(1);
333 }
334
335 wxASSERT_MSG( mday > 0 && mday <= GetNumOfDaysInMonth(year, mon),
336 _T("logic error") );
337 }
338
339 // ----------------------------------------------------------------------------
340 // class TimeZone
341 // ----------------------------------------------------------------------------
342
343 wxDateTime::TimeZone::TimeZone(wxDateTime::TZ tz)
344 {
345 switch ( tz )
346 {
347 case wxDateTime::Local:
348 // get the offset from C RTL: it returns the difference GMT-local
349 // while we want to have the offset _from_ GMT, hence the '-'
350 m_offset = -GetTimeZone();
351 break;
352
353 case wxDateTime::GMT_12:
354 case wxDateTime::GMT_11:
355 case wxDateTime::GMT_10:
356 case wxDateTime::GMT_9:
357 case wxDateTime::GMT_8:
358 case wxDateTime::GMT_7:
359 case wxDateTime::GMT_6:
360 case wxDateTime::GMT_5:
361 case wxDateTime::GMT_4:
362 case wxDateTime::GMT_3:
363 case wxDateTime::GMT_2:
364 case wxDateTime::GMT_1:
365 m_offset = -3600*(wxDateTime::GMT0 - tz);
366 break;
367
368 case wxDateTime::GMT0:
369 case wxDateTime::GMT1:
370 case wxDateTime::GMT2:
371 case wxDateTime::GMT3:
372 case wxDateTime::GMT4:
373 case wxDateTime::GMT5:
374 case wxDateTime::GMT6:
375 case wxDateTime::GMT7:
376 case wxDateTime::GMT8:
377 case wxDateTime::GMT9:
378 case wxDateTime::GMT10:
379 case wxDateTime::GMT11:
380 case wxDateTime::GMT12:
381 m_offset = 3600*(tz - wxDateTime::GMT0);
382 break;
383
384 case wxDateTime::A_CST:
385 // Central Standard Time in use in Australia = UTC + 9.5
386 m_offset = 60*(9*60 + 30);
387 break;
388
389 default:
390 wxFAIL_MSG( _T("unknown time zone") );
391 }
392 }
393
394 // ----------------------------------------------------------------------------
395 // static functions
396 // ----------------------------------------------------------------------------
397
398 /* static */
399 bool wxDateTime::IsLeapYear(int year, wxDateTime::Calendar cal)
400 {
401 if ( year == Inv_Year )
402 year = GetCurrentYear();
403
404 if ( cal == Gregorian )
405 {
406 // in Gregorian calendar leap years are those divisible by 4 except
407 // those divisible by 100 unless they're also divisible by 400
408 // (in some countries, like Russia and Greece, additional corrections
409 // exist, but they won't manifest themselves until 2700)
410 return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
411 }
412 else if ( cal == Julian )
413 {
414 // in Julian calendar the rule is simpler
415 return year % 4 == 0;
416 }
417 else
418 {
419 wxFAIL_MSG(_T("unknown calendar"));
420
421 return FALSE;
422 }
423 }
424
425 /* static */
426 int wxDateTime::GetCentury(int year)
427 {
428 return year > 0 ? year / 100 : year / 100 - 1;
429 }
430
431 /* static */
432 int wxDateTime::ConvertYearToBC(int year)
433 {
434 // year 0 is BC 1
435 return year > 0 ? year : year - 1;
436 }
437
438 /* static */
439 int wxDateTime::GetCurrentYear(wxDateTime::Calendar cal)
440 {
441 switch ( cal )
442 {
443 case Gregorian:
444 return Now().GetYear();
445
446 case Julian:
447 wxFAIL_MSG(_T("TODO"));
448 break;
449
450 default:
451 wxFAIL_MSG(_T("unsupported calendar"));
452 break;
453 }
454
455 return Inv_Year;
456 }
457
458 /* static */
459 wxDateTime::Month wxDateTime::GetCurrentMonth(wxDateTime::Calendar cal)
460 {
461 switch ( cal )
462 {
463 case Gregorian:
464 return Now().GetMonth();
465 break;
466
467 case Julian:
468 wxFAIL_MSG(_T("TODO"));
469 break;
470
471 default:
472 wxFAIL_MSG(_T("unsupported calendar"));
473 break;
474 }
475
476 return Inv_Month;
477 }
478
479 /* static */
480 wxDateTime::wxDateTime_t wxDateTime::GetNumberOfDays(int year, Calendar cal)
481 {
482 if ( year == Inv_Year )
483 {
484 // take the current year if none given
485 year = GetCurrentYear();
486 }
487
488 switch ( cal )
489 {
490 case Gregorian:
491 case Julian:
492 return IsLeapYear(year) ? 366 : 365;
493 break;
494
495 default:
496 wxFAIL_MSG(_T("unsupported calendar"));
497 break;
498 }
499
500 return 0;
501 }
502
503 /* static */
504 wxDateTime::wxDateTime_t wxDateTime::GetNumberOfDays(wxDateTime::Month month,
505 int year,
506 wxDateTime::Calendar cal)
507 {
508 wxCHECK_MSG( month < MONTHS_IN_YEAR, 0, _T("invalid month") );
509
510 if ( cal == Gregorian || cal == Julian )
511 {
512 if ( year == Inv_Year )
513 {
514 // take the current year if none given
515 year = GetCurrentYear();
516 }
517
518 return GetNumOfDaysInMonth(year, month);
519 }
520 else
521 {
522 wxFAIL_MSG(_T("unsupported calendar"));
523
524 return 0;
525 }
526 }
527
528 /* static */
529 wxString wxDateTime::GetMonthName(wxDateTime::Month month, bool abbr)
530 {
531 wxCHECK_MSG( month != Inv_Month, _T(""), _T("invalid month") );
532
533 tm tm = { 0, 0, 0, 1, month, 76 }; // any year will do
534
535 return CallStrftime(abbr ? _T("%b") : _T("%B"), &tm);
536 }
537
538 /* static */
539 wxString wxDateTime::GetWeekDayName(wxDateTime::WeekDay wday, bool abbr)
540 {
541 wxCHECK_MSG( wday != Inv_WeekDay, _T(""), _T("invalid weekday") );
542
543 // take some arbitrary Sunday
544 tm tm = { 0, 0, 0, 28, Nov, 99 };
545
546 // and offset it by the number of days needed to get the correct wday
547 tm.tm_mday += wday;
548
549 // call mktime() to normalize it...
550 (void)mktime(&tm);
551
552 // ... and call strftime()
553 return CallStrftime(abbr ? _T("%a") : _T("%A"), &tm);
554 }
555
556 // ----------------------------------------------------------------------------
557 // Country stuff: date calculations depend on the country (DST, work days,
558 // ...), so we need to know which rules to follow.
559 // ----------------------------------------------------------------------------
560
561 /* static */
562 wxDateTime::Country wxDateTime::GetCountry()
563 {
564 if ( ms_country == Country_Unknown )
565 {
566 // try to guess from the time zone name
567 time_t t = time(NULL);
568 struct tm *tm = localtime(&t);
569
570 wxString tz = CallStrftime(_T("%Z"), tm);
571 if ( tz == _T("WET") || tz == _T("WEST") )
572 {
573 ms_country = UK;
574 }
575 else if ( tz == _T("CET") || tz == _T("CEST") )
576 {
577 ms_country = Country_EEC;
578 }
579 else if ( tz == _T("MSK") || tz == _T("MSD") )
580 {
581 ms_country = Russia;
582 }
583 else if ( tz == _T("AST") || tz == _T("ADT") ||
584 tz == _T("EST") || tz == _T("EDT") ||
585 tz == _T("CST") || tz == _T("CDT") ||
586 tz == _T("MST") || tz == _T("MDT") ||
587 tz == _T("PST") || tz == _T("PDT") )
588 {
589 ms_country = USA;
590 }
591 else
592 {
593 // well, choose a default one
594 ms_country = USA;
595 }
596 }
597
598 return ms_country;
599 }
600
601 /* static */
602 void wxDateTime::SetCountry(wxDateTime::Country country)
603 {
604 ms_country = country;
605 }
606
607 /* static */
608 bool wxDateTime::IsWestEuropeanCountry(Country country)
609 {
610 if ( country == Country_Default )
611 {
612 country = GetCountry();
613 }
614
615 return (Country_WesternEurope_Start <= country) &&
616 (country <= Country_WesternEurope_End);
617 }
618
619 // ----------------------------------------------------------------------------
620 // DST calculations: we use 3 different rules for the West European countries,
621 // USA and for the rest of the world. This is undoubtedly false for many
622 // countries, but I lack the necessary info (and the time to gather it),
623 // please add the other rules here!
624 // ----------------------------------------------------------------------------
625
626 /* static */
627 bool wxDateTime::IsDSTApplicable(int year, Country country)
628 {
629 if ( year == Inv_Year )
630 {
631 // take the current year if none given
632 year = GetCurrentYear();
633 }
634
635 if ( country == Country_Default )
636 {
637 country = GetCountry();
638 }
639
640 switch ( country )
641 {
642 case USA:
643 case UK:
644 // DST was first observed in the US and UK during WWI, reused
645 // during WWII and used again since 1966
646 return year >= 1966 ||
647 (year >= 1942 && year <= 1945) ||
648 (year == 1918 || year == 1919);
649
650 default:
651 // assume that it started after WWII
652 return year > 1950;
653 }
654 }
655
656 /* static */
657 wxDateTime wxDateTime::GetBeginDST(int year, Country country)
658 {
659 if ( year == Inv_Year )
660 {
661 // take the current year if none given
662 year = GetCurrentYear();
663 }
664
665 if ( country == Country_Default )
666 {
667 country = GetCountry();
668 }
669
670 if ( !IsDSTApplicable(year, country) )
671 {
672 return ms_InvDateTime;
673 }
674
675 wxDateTime dt;
676
677 if ( IsWestEuropeanCountry(country) || (country == Russia) )
678 {
679 // DST begins at 1 a.m. GMT on the last Sunday of March
680 if ( !dt.SetToLastWeekDay(Sun, Mar, year) )
681 {
682 // weird...
683 wxFAIL_MSG( _T("no last Sunday in March?") );
684 }
685
686 dt += wxTimeSpan::Hours(1);
687
688 dt.MakeGMT();
689 }
690 else switch ( country )
691 {
692 case USA:
693 switch ( year )
694 {
695 case 1918:
696 case 1919:
697 // don't know for sure - assume it was in effect all year
698
699 case 1943:
700 case 1944:
701 case 1945:
702 dt.Set(1, Jan, year);
703 break;
704
705 case 1942:
706 // DST was installed Feb 2, 1942 by the Congress
707 dt.Set(2, Feb, year);
708 break;
709
710 // Oil embargo changed the DST period in the US
711 case 1974:
712 dt.Set(6, Jan, 1974);
713 break;
714
715 case 1975:
716 dt.Set(23, Feb, 1975);
717 break;
718
719 default:
720 // before 1986, DST begun on the last Sunday of April, but
721 // in 1986 Reagan changed it to begin at 2 a.m. of the
722 // first Sunday in April
723 if ( year < 1986 )
724 {
725 if ( !dt.SetToLastWeekDay(Sun, Apr, year) )
726 {
727 // weird...
728 wxFAIL_MSG( _T("no first Sunday in April?") );
729 }
730 }
731 else
732 {
733 if ( !dt.SetToWeekDay(Sun, 1, Apr, year) )
734 {
735 // weird...
736 wxFAIL_MSG( _T("no first Sunday in April?") );
737 }
738 }
739
740 dt += wxTimeSpan::Hours(2);
741
742 // TODO what about timezone??
743 }
744
745 break;
746
747 default:
748 // assume Mar 30 as the start of the DST for the rest of the world
749 // - totally bogus, of course
750 dt.Set(30, Mar, year);
751 }
752
753 return dt;
754 }
755
756 /* static */
757 wxDateTime wxDateTime::GetEndDST(int year, Country country)
758 {
759 if ( year == Inv_Year )
760 {
761 // take the current year if none given
762 year = GetCurrentYear();
763 }
764
765 if ( country == Country_Default )
766 {
767 country = GetCountry();
768 }
769
770 if ( !IsDSTApplicable(year, country) )
771 {
772 return ms_InvDateTime;
773 }
774
775 wxDateTime dt;
776
777 if ( IsWestEuropeanCountry(country) || (country == Russia) )
778 {
779 // DST ends at 1 a.m. GMT on the last Sunday of October
780 if ( !dt.SetToLastWeekDay(Sun, Oct, year) )
781 {
782 // weirder and weirder...
783 wxFAIL_MSG( _T("no last Sunday in October?") );
784 }
785
786 dt += wxTimeSpan::Hours(1);
787
788 dt.MakeGMT();
789 }
790 else switch ( country )
791 {
792 case USA:
793 switch ( year )
794 {
795 case 1918:
796 case 1919:
797 // don't know for sure - assume it was in effect all year
798
799 case 1943:
800 case 1944:
801 dt.Set(31, Dec, year);
802 break;
803
804 case 1945:
805 // the time was reset after the end of the WWII
806 dt.Set(30, Sep, year);
807 break;
808
809 default:
810 // DST ends at 2 a.m. on the last Sunday of October
811 if ( !dt.SetToLastWeekDay(Sun, Oct, year) )
812 {
813 // weirder and weirder...
814 wxFAIL_MSG( _T("no last Sunday in October?") );
815 }
816
817 dt += wxTimeSpan::Hours(2);
818
819 // TODO what about timezone??
820 }
821 break;
822
823 default:
824 // assume October 26th as the end of the DST - totally bogus too
825 dt.Set(26, Oct, year);
826 }
827
828 return dt;
829 }
830
831 // ----------------------------------------------------------------------------
832 // constructors and assignment operators
833 // ----------------------------------------------------------------------------
834
835 // the values in the tm structure contain the local time
836 wxDateTime& wxDateTime::Set(const struct tm& tm)
837 {
838 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
839
840 struct tm tm2(tm);
841 time_t timet = mktime(&tm2);
842
843 if ( timet == (time_t)-1 )
844 {
845 // mktime() rather unintuitively fails for Jan 1, 1970 if the hour is
846 // less than timezone - try to make it work for this case
847 if ( tm2.tm_year == 70 && tm2.tm_mon == 0 && tm2.tm_mday == 1 )
848 {
849 // add timezone to make sure that date is in range
850 tm2.tm_sec -= GetTimeZone();
851
852 timet = mktime(&tm2);
853 if ( timet != (time_t)-1 )
854 {
855 timet += GetTimeZone();
856
857 return Set(timet);
858 }
859 }
860
861 wxFAIL_MSG( _T("mktime() failed") );
862
863 return ms_InvDateTime;
864 }
865 else
866 {
867 return Set(timet);
868 }
869 }
870
871 wxDateTime& wxDateTime::Set(wxDateTime_t hour,
872 wxDateTime_t minute,
873 wxDateTime_t second,
874 wxDateTime_t millisec)
875 {
876 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
877
878 // we allow seconds to be 61 to account for the leap seconds, even if we
879 // don't use them really
880 wxCHECK_MSG( hour < 24 && second < 62 && minute < 60 && millisec < 1000,
881 ms_InvDateTime,
882 _T("Invalid time in wxDateTime::Set()") );
883
884 // get the current date from system
885 time_t timet = GetTimeNow();
886 struct tm *tm = localtime(&timet);
887
888 wxCHECK_MSG( tm, ms_InvDateTime, _T("localtime() failed") );
889
890 // adjust the time
891 tm->tm_hour = hour;
892 tm->tm_min = minute;
893 tm->tm_sec = second;
894
895 (void)Set(*tm);
896
897 // and finally adjust milliseconds
898 return SetMillisecond(millisec);
899 }
900
901 wxDateTime& wxDateTime::Set(wxDateTime_t day,
902 Month month,
903 int year,
904 wxDateTime_t hour,
905 wxDateTime_t minute,
906 wxDateTime_t second,
907 wxDateTime_t millisec)
908 {
909 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
910
911 wxCHECK_MSG( hour < 24 && second < 62 && minute < 60 && millisec < 1000,
912 ms_InvDateTime,
913 _T("Invalid time in wxDateTime::Set()") );
914
915 ReplaceDefaultYearMonthWithCurrent(&year, &month);
916
917 wxCHECK_MSG( (0 < day) && (day <= GetNumberOfDays(month, year)),
918 ms_InvDateTime,
919 _T("Invalid date in wxDateTime::Set()") );
920
921 // the range of time_t type (inclusive)
922 static const int yearMinInRange = 1970;
923 static const int yearMaxInRange = 2037;
924
925 // test only the year instead of testing for the exact end of the Unix
926 // time_t range - it doesn't bring anything to do more precise checks
927 if ( year >= yearMinInRange && year <= yearMaxInRange )
928 {
929 // use the standard library version if the date is in range - this is
930 // probably more efficient than our code
931 struct tm tm;
932 tm.tm_year = year - 1900;
933 tm.tm_mon = month;
934 tm.tm_mday = day;
935 tm.tm_hour = hour;
936 tm.tm_min = minute;
937 tm.tm_sec = second;
938 tm.tm_isdst = -1; // mktime() will guess it
939
940 (void)Set(tm);
941
942 // and finally adjust milliseconds
943 return SetMillisecond(millisec);
944 }
945 else
946 {
947 // do time calculations ourselves: we want to calculate the number of
948 // milliseconds between the given date and the epoch
949
950 // get the JDN for the midnight of this day
951 m_time = GetTruncatedJDN(day, month, year);
952 m_time -= EPOCH_JDN;
953 m_time *= SECONDS_PER_DAY * TIME_T_FACTOR;
954
955 // JDN corresponds to GMT, we take localtime
956 Add(wxTimeSpan(hour, minute, second + GetTimeZone(), millisec));
957 }
958
959 return *this;
960 }
961
962 wxDateTime& wxDateTime::Set(double jdn)
963 {
964 // so that m_time will be 0 for the midnight of Jan 1, 1970 which is jdn
965 // EPOCH_JDN + 0.5
966 jdn -= EPOCH_JDN + 0.5;
967
968 m_time = jdn;
969 m_time *= MILLISECONDS_PER_DAY;
970
971 return *this;
972 }
973
974 // ----------------------------------------------------------------------------
975 // time_t <-> broken down time conversions
976 // ----------------------------------------------------------------------------
977
978 wxDateTime::Tm wxDateTime::GetTm(const TimeZone& tz) const
979 {
980 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
981
982 time_t time = GetTicks();
983 if ( time != (time_t)-1 )
984 {
985 // use C RTL functions
986 tm *tm;
987 if ( tz.GetOffset() == -GetTimeZone() )
988 {
989 // we are working with local time
990 tm = localtime(&time);
991
992 // should never happen
993 wxCHECK_MSG( tm, Tm(), _T("gmtime() failed") );
994 }
995 else
996 {
997 time += tz.GetOffset();
998 if ( time >= 0 )
999 {
1000 tm = gmtime(&time);
1001
1002 // should never happen
1003 wxCHECK_MSG( tm, Tm(), _T("gmtime() failed") );
1004 }
1005 else
1006 {
1007 tm = (struct tm *)NULL;
1008 }
1009 }
1010
1011 if ( tm )
1012 {
1013 return Tm(*tm, tz);
1014 }
1015 //else: use generic code below
1016 }
1017
1018 // remember the time and do the calculations with the date only - this
1019 // eliminates rounding errors of the floating point arithmetics
1020
1021 wxLongLong timeMidnight = m_time + tz.GetOffset() * 1000;
1022
1023 long timeOnly = (timeMidnight % MILLISECONDS_PER_DAY).ToLong();
1024
1025 // we want to always have positive time and timeMidnight to be really
1026 // the midnight before it
1027 if ( timeOnly < 0 )
1028 {
1029 timeOnly = MILLISECONDS_PER_DAY + timeOnly;
1030 }
1031
1032 timeMidnight -= timeOnly;
1033
1034 // calculate the Gregorian date from JDN for the midnight of our date:
1035 // this will yield day, month (in 1..12 range) and year
1036
1037 // actually, this is the JDN for the noon of the previous day
1038 long jdn = (timeMidnight / MILLISECONDS_PER_DAY).ToLong() + EPOCH_JDN;
1039
1040 // CREDIT: code below is by Scott E. Lee (but bugs are mine)
1041
1042 wxASSERT_MSG( jdn > -2, _T("JDN out of range") );
1043
1044 // calculate the century
1045 int temp = (jdn + JDN_OFFSET) * 4 - 1;
1046 int century = temp / DAYS_PER_400_YEARS;
1047
1048 // then the year and day of year (1 <= dayOfYear <= 366)
1049 temp = ((temp % DAYS_PER_400_YEARS) / 4) * 4 + 3;
1050 int year = (century * 100) + (temp / DAYS_PER_4_YEARS);
1051 int dayOfYear = (temp % DAYS_PER_4_YEARS) / 4 + 1;
1052
1053 // and finally the month and day of the month
1054 temp = dayOfYear * 5 - 3;
1055 int month = temp / DAYS_PER_5_MONTHS;
1056 int day = (temp % DAYS_PER_5_MONTHS) / 5 + 1;
1057
1058 // month is counted from March - convert to normal
1059 if ( month < 10 )
1060 {
1061 month += 3;
1062 }
1063 else
1064 {
1065 year += 1;
1066 month -= 9;
1067 }
1068
1069 // year is offset by 4800
1070 year -= 4800;
1071
1072 // check that the algorithm gave us something reasonable
1073 wxASSERT_MSG( (0 < month) && (month <= 12), _T("invalid month") );
1074 wxASSERT_MSG( (1 <= day) && (day < 32), _T("invalid day") );
1075 wxASSERT_MSG( (INT_MIN <= year) && (year <= INT_MAX),
1076 _T("year range overflow") );
1077
1078 // construct Tm from these values
1079 Tm tm;
1080 tm.year = (int)year;
1081 tm.mon = (Month)(month - 1); // algorithm yields 1 for January, not 0
1082 tm.mday = (wxDateTime_t)day;
1083 tm.msec = timeOnly % 1000;
1084 timeOnly -= tm.msec;
1085 timeOnly /= 1000; // now we have time in seconds
1086
1087 tm.sec = timeOnly % 60;
1088 timeOnly -= tm.sec;
1089 timeOnly /= 60; // now we have time in minutes
1090
1091 tm.min = timeOnly % 60;
1092 timeOnly -= tm.min;
1093
1094 tm.hour = timeOnly / 60;
1095
1096 return tm;
1097 }
1098
1099 wxDateTime& wxDateTime::SetYear(int year)
1100 {
1101 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1102
1103 Tm tm(GetTm());
1104 tm.year = year;
1105 Set(tm);
1106
1107 return *this;
1108 }
1109
1110 wxDateTime& wxDateTime::SetMonth(Month month)
1111 {
1112 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1113
1114 Tm tm(GetTm());
1115 tm.mon = month;
1116 Set(tm);
1117
1118 return *this;
1119 }
1120
1121 wxDateTime& wxDateTime::SetDay(wxDateTime_t mday)
1122 {
1123 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1124
1125 Tm tm(GetTm());
1126 tm.mday = mday;
1127 Set(tm);
1128
1129 return *this;
1130 }
1131
1132 wxDateTime& wxDateTime::SetHour(wxDateTime_t hour)
1133 {
1134 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1135
1136 Tm tm(GetTm());
1137 tm.hour = hour;
1138 Set(tm);
1139
1140 return *this;
1141 }
1142
1143 wxDateTime& wxDateTime::SetMinute(wxDateTime_t min)
1144 {
1145 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1146
1147 Tm tm(GetTm());
1148 tm.min = min;
1149 Set(tm);
1150
1151 return *this;
1152 }
1153
1154 wxDateTime& wxDateTime::SetSecond(wxDateTime_t sec)
1155 {
1156 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1157
1158 Tm tm(GetTm());
1159 tm.sec = sec;
1160 Set(tm);
1161
1162 return *this;
1163 }
1164
1165 wxDateTime& wxDateTime::SetMillisecond(wxDateTime_t millisecond)
1166 {
1167 wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") );
1168
1169 // we don't need to use GetTm() for this one
1170 m_time -= m_time % 1000l;
1171 m_time += millisecond;
1172
1173 return *this;
1174 }
1175
1176 // ----------------------------------------------------------------------------
1177 // wxDateTime arithmetics
1178 // ----------------------------------------------------------------------------
1179
1180 wxDateTime& wxDateTime::Add(const wxDateSpan& diff)
1181 {
1182 Tm tm(GetTm());
1183
1184 tm.year += diff.GetYears();
1185 tm.AddMonths(diff.GetMonths());
1186 tm.AddDays(diff.GetTotalDays());
1187
1188 Set(tm);
1189
1190 return *this;
1191 }
1192
1193 // ----------------------------------------------------------------------------
1194 // Weekday and monthday stuff
1195 // ----------------------------------------------------------------------------
1196
1197 wxDateTime& wxDateTime::SetToLastMonthDay(Month month,
1198 int year)
1199 {
1200 // take the current month/year if none specified
1201 ReplaceDefaultYearMonthWithCurrent(&year, &month);
1202
1203 return Set(GetNumOfDaysInMonth(year, month), month, year);
1204 }
1205
1206 bool wxDateTime::SetToWeekDay(WeekDay weekday,
1207 int n,
1208 Month month,
1209 int year)
1210 {
1211 wxCHECK_MSG( weekday != Inv_WeekDay, FALSE, _T("invalid weekday") );
1212
1213 // we don't check explicitly that -5 <= n <= 5 because we will return FALSE
1214 // anyhow in such case - but may be should still give an assert for it?
1215
1216 // take the current month/year if none specified
1217 ReplaceDefaultYearMonthWithCurrent(&year, &month);
1218
1219 wxDateTime dt;
1220
1221 // TODO this probably could be optimised somehow...
1222
1223 if ( n > 0 )
1224 {
1225 // get the first day of the month
1226 dt.Set(1, month, year);
1227
1228 // get its wday
1229 WeekDay wdayFirst = dt.GetWeekDay();
1230
1231 // go to the first weekday of the month
1232 int diff = weekday - wdayFirst;
1233 if ( diff < 0 )
1234 diff += 7;
1235
1236 // add advance n-1 weeks more
1237 diff += 7*(n - 1);
1238
1239 dt += wxDateSpan::Days(diff);
1240 }
1241 else // count from the end of the month
1242 {
1243 // get the last day of the month
1244 dt.SetToLastMonthDay(month, year);
1245
1246 // get its wday
1247 WeekDay wdayLast = dt.GetWeekDay();
1248
1249 // go to the last weekday of the month
1250 int diff = wdayLast - weekday;
1251 if ( diff < 0 )
1252 diff += 7;
1253
1254 // and rewind n-1 weeks from there
1255 diff += 7*(-n - 1);
1256
1257 dt -= wxDateSpan::Days(diff);
1258 }
1259
1260 // check that it is still in the same month
1261 if ( dt.GetMonth() == month )
1262 {
1263 *this = dt;
1264
1265 return TRUE;
1266 }
1267 else
1268 {
1269 // no such day in this month
1270 return FALSE;
1271 }
1272 }
1273
1274 wxDateTime::wxDateTime_t wxDateTime::GetDayOfYear(const TimeZone& tz) const
1275 {
1276 // this array contains the cumulated number of days in all previous months
1277 // for normal and leap years
1278 static const wxDateTime_t cumulatedDays[2][MONTHS_IN_YEAR] =
1279 {
1280 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 },
1281 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }
1282 };
1283
1284 Tm tm(GetTm(tz));
1285
1286 return cumulatedDays[IsLeapYear(tm.year)][tm.mon] + tm.mday;
1287 }
1288
1289 wxDateTime::wxDateTime_t wxDateTime::GetWeekOfYear(const TimeZone& tz) const
1290 {
1291 // the first week of the year is the one which contains Jan, 4 (according
1292 // to ISO standard rule), so the year day N0 = 4 + 7*W always lies in the
1293 // week W+1. As any day N = 7*W + 4 + (N - 4)%7, it lies in the same week
1294 // as N0 or in the next one.
1295
1296 // TODO this surely may be optimized - I got confused while writing it
1297
1298 wxDateTime_t nDayInYear = GetDayOfYear(tz);
1299
1300 // the week day of the day lying in the first week
1301 WeekDay wdayStart = wxDateTime(4, Jan, GetYear()).GetWeekDay();
1302
1303 wxDateTime_t week = (nDayInYear - 4) / 7 + 1;
1304
1305 // notice that Sunday shoould be counted as 7, not 0 here!
1306 if ( ((nDayInYear - 4) % 7) + (!wdayStart ? 7 : wdayStart) > 7 )
1307 {
1308 week++;
1309 }
1310
1311 return week;
1312 }
1313
1314 // ----------------------------------------------------------------------------
1315 // Julian day number conversion and related stuff
1316 // ----------------------------------------------------------------------------
1317
1318 double wxDateTime::GetJulianDayNumber() const
1319 {
1320 // JDN are always expressed for the GMT dates
1321 Tm tm(ToTimezone(GMT0).GetTm(GMT0));
1322
1323 double result = GetTruncatedJDN(tm.mday, tm.mon, tm.year);
1324
1325 // add the part GetTruncatedJDN() neglected
1326 result += 0.5;
1327
1328 // and now add the time: 86400 sec = 1 JDN
1329 return result + ((double)(60*(60*tm.hour + tm.min) + tm.sec)) / 86400;
1330 }
1331
1332 double wxDateTime::GetRataDie() const
1333 {
1334 // March 1 of the year 0 is Rata Die day -306 and JDN 1721119.5
1335 return GetJulianDayNumber() - 1721119.5 - 306;
1336 }
1337
1338 // ----------------------------------------------------------------------------
1339 // timezone and DST stuff
1340 // ----------------------------------------------------------------------------
1341
1342 int wxDateTime::IsDST(wxDateTime::Country country) const
1343 {
1344 wxCHECK_MSG( country == Country_Default, -1,
1345 _T("country support not implemented") );
1346
1347 // use the C RTL for the dates in the standard range
1348 time_t timet = GetTicks();
1349 if ( timet != (time_t)-1 )
1350 {
1351 tm *tm = localtime(&timet);
1352
1353 wxCHECK_MSG( tm, -1, _T("localtime() failed") );
1354
1355 return tm->tm_isdst;
1356 }
1357 else
1358 {
1359 int year = GetYear();
1360
1361 if ( !IsDSTApplicable(year, country) )
1362 {
1363 // no DST time in this year in this country
1364 return -1;
1365 }
1366
1367 return IsBetween(GetBeginDST(year, country), GetEndDST(year, country));
1368 }
1369 }
1370
1371 wxDateTime& wxDateTime::MakeTimezone(const TimeZone& tz)
1372 {
1373 int secDiff = GetTimeZone() + tz.GetOffset();
1374
1375 // we need to know whether DST is or not in effect for this date
1376 if ( IsDST() == 1 )
1377 {
1378 // FIXME we assume that the DST is always shifted by 1 hour
1379 secDiff -= 3600;
1380 }
1381
1382 return Substract(wxTimeSpan::Seconds(secDiff));
1383 }
1384
1385 // ----------------------------------------------------------------------------
1386 // wxDateTime to/from text representations
1387 // ----------------------------------------------------------------------------
1388
1389 wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const
1390 {
1391 wxCHECK_MSG( format, _T(""), _T("NULL format in wxDateTime::Format") );
1392
1393 time_t time = GetTicks();
1394 if ( time != (time_t)-1 )
1395 {
1396 // use strftime()
1397 tm *tm;
1398 if ( tz.GetOffset() == -GetTimeZone() )
1399 {
1400 // we are working with local time
1401 tm = localtime(&time);
1402
1403 // should never happen
1404 wxCHECK_MSG( tm, wxEmptyString, _T("localtime() failed") );
1405 }
1406 else
1407 {
1408 time += tz.GetOffset();
1409
1410 if ( time >= 0 )
1411 {
1412 tm = gmtime(&time);
1413
1414 // should never happen
1415 wxCHECK_MSG( tm, wxEmptyString, _T("gmtime() failed") );
1416 }
1417 else
1418 {
1419 tm = (struct tm *)NULL;
1420 }
1421 }
1422
1423 if ( tm )
1424 {
1425 return CallStrftime(format, tm);
1426 }
1427 //else: use generic code below
1428 }
1429
1430 // use a hack and still use strftime(): first find the YEAR which is a year
1431 // in the strftime() range (1970 - 2038) whose Jan 1 falls on the same week
1432 // day as the Jan 1 of the real year. Then make a copy of the format and
1433 // replace all occurences of YEAR in it with some unique string not
1434 // appearing anywhere else in it, then use strftime() to format the date in
1435 // year YEAR and then replace YEAR back by the real year and the unique
1436 // replacement string back with YEAR. Notice that "all occurences of YEAR"
1437 // means all occurences of 4 digit as well as 2 digit form!
1438
1439 // NB: may be it would be simpler to "honestly" reimplement strftime()?
1440
1441 // find the YEAR: normally, for any year X, Jan 1 or the year X + 28 is the
1442 // same weekday as Jan 1 of X (because the weekday advances by 1 for each
1443 // normal X and by 2 for each leap X, hence by 5 every 4 years or by 35
1444 // which is 0 mod 7 every 28 years) but this rule breaks down if there are
1445 // years between X and Y which are divisible by 4 but not leap (i.e.
1446 // divisible by 100 but not 400), hence the correction.
1447
1448 int yearReal = GetYear(tz);
1449 int year = 1970 + yearReal % 28;
1450
1451 int nCenturiesInBetween = (year / 100) - (yearReal / 100);
1452 int nLostWeekDays = nCenturiesInBetween - (nCenturiesInBetween / 400);
1453
1454 // we have to gain back the "lost" weekdays...
1455 while ( (nLostWeekDays % 7) != 0 )
1456 {
1457 nLostWeekDays += year++ % 4 ? 1 : 2;
1458 }
1459
1460 // at any rate, we can't go further than 1997 + 28!
1461 wxASSERT_MSG( year < 2030, _T("logic error in wxDateTime::Format") );
1462
1463 wxString strYear, strYear2;
1464 strYear.Printf(_T("%d"), year);
1465 strYear2.Printf(_T("%d"), year % 100);
1466
1467 // find two strings not occuring in format (this is surely not optimal way
1468 // of doing it... improvements welcome!)
1469 wxString fmt = format;
1470 wxString replacement = (wxChar)-1;
1471 while ( fmt.Find(replacement) != wxNOT_FOUND )
1472 {
1473 replacement << (wxChar)-1;
1474 }
1475
1476 wxString replacement2 = (wxChar)-2;
1477 while ( fmt.Find(replacement) != wxNOT_FOUND )
1478 {
1479 replacement << (wxChar)-2;
1480 }
1481
1482 // replace all occurences of year with it
1483 bool wasReplaced = fmt.Replace(strYear, replacement) > 0;
1484 if ( !wasReplaced )
1485 wasReplaced = fmt.Replace(strYear2, replacement2) > 0;
1486
1487 // use strftime() to format the same date but in supported year
1488 wxDateTime dt(*this);
1489 dt.SetYear(year);
1490 wxString str = dt.Format(format, tz);
1491
1492 // now replace the occurence of 1999 with the real year
1493 wxString strYearReal, strYearReal2;
1494 strYearReal.Printf(_T("%04d"), yearReal);
1495 strYearReal2.Printf(_T("%02d"), yearReal % 100);
1496 str.Replace(strYear, strYearReal);
1497 str.Replace(strYear2, strYearReal2);
1498
1499 // and replace back all occurences of replacement string
1500 if ( wasReplaced )
1501 {
1502 str.Replace(replacement2, strYear2);
1503 str.Replace(replacement, strYear);
1504 }
1505
1506 return str;
1507 }
1508
1509 // ============================================================================
1510 // wxTimeSpan
1511 // ============================================================================
1512
1513 // not all strftime(3) format specifiers make sense here because, for example,
1514 // a time span doesn't have a year nor a timezone
1515 //
1516 // Here are the ones which are supported (all of them are supported by strftime
1517 // as well):
1518 // %H hour in 24 hour format
1519 // %M minute (00 - 59)
1520 // %S second (00 - 59)
1521 // %% percent sign
1522 //
1523 // Also, for MFC CTimeSpan compatibility, we support
1524 // %D number of days
1525 //
1526 // And, to be better than MFC :-), we also have
1527 // %E number of wEeks
1528 // %l milliseconds (000 - 999)
1529 wxString wxTimeSpan::Format(const wxChar *format) const
1530 {
1531 wxCHECK_MSG( format, _T(""), _T("NULL format in wxTimeSpan::Format") );
1532
1533 wxString str;
1534 str.Alloc(strlen(format));
1535
1536 for ( const wxChar *pch = format; pch; pch++ )
1537 {
1538 wxChar ch = *pch;
1539
1540 if ( ch == '%' )
1541 {
1542 wxString tmp;
1543
1544 ch = *pch++;
1545 switch ( ch )
1546 {
1547 default:
1548 wxFAIL_MSG( _T("invalid format character") );
1549 // fall through
1550
1551 case '%':
1552 // will get to str << ch below
1553 break;
1554
1555 case 'D':
1556 tmp.Printf(_T("%d"), GetDays());
1557 break;
1558
1559 case 'E':
1560 tmp.Printf(_T("%d"), GetWeeks());
1561 break;
1562
1563 case 'H':
1564 tmp.Printf(_T("%02d"), GetHours());
1565 break;
1566
1567 case 'l':
1568 tmp.Printf(_T("%03d"), GetMilliseconds().GetValue());
1569 break;
1570
1571 case 'M':
1572 tmp.Printf(_T("%02d"), GetMinutes());
1573 break;
1574
1575 case 'S':
1576 tmp.Printf(_T("%02d"), GetSeconds().GetValue());
1577 break;
1578 }
1579
1580 if ( !!tmp )
1581 {
1582 str += tmp;
1583
1584 // skip str += ch below
1585 continue;
1586 }
1587 }
1588
1589 str += ch;
1590 }
1591
1592 return str;
1593 }