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