X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b76b015ed9231ffe128ddba9fb35e37b4be9764d..5ea6dbbfff67d1dfad08044ab721ba56425ef6e1:/src/common/datetime.cpp?ds=sidebyside diff --git a/src/common/datetime.cpp b/src/common/datetime.cpp index f254ddc7e5..93c8dc6b84 100644 --- a/src/common/datetime.cpp +++ b/src/common/datetime.cpp @@ -1,13 +1,48 @@ -///////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// // Name: wx/datetime.h // Purpose: implementation of time/date related classes // Author: Vadim Zeitlin // Modified by: // Created: 11.05.99 // RCS-ID: $Id$ -// Copyright: (c) 1998 Vadim Zeitlin +// Copyright: (c) 1999 Vadim Zeitlin +// parts of code taken from sndcal library by Scott E. Lee: +// +// Copyright 1993-1995, Scott E. Lee, all rights reserved. +// Permission granted to use, copy, modify, distribute and sell +// so long as the above copyright and this permission statement +// are retained in all copies. +// // Licence: wxWindows license -///////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +/* + * Implementation notes: + * + * 1. the time is stored as a 64bit integer containing the signed number of + * milliseconds since Jan 1. 1970 (the Unix Epoch) - so it is always + * expressed in GMT. + * + * 2. the range is thus something about 580 million years, but due to current + * algorithms limitations, only dates from Nov 24, 4714BC are handled + * + * 3. standard ANSI C functions are used to do time calculations whenever + * possible, i.e. when the date is in the range Jan 1, 1970 to 2038 + * + * 4. otherwise, the calculations are done by converting the date to/from JDN + * first (the range limitation mentioned above comes from here: the + * algorithm used by Scott E. Lee's code only works for positive JDNs, more + * or less) + * + * 5. the object constructed for the given DD-MM-YYYY HH:MM:SS corresponds to + * this moment in local time and may be converted to the object + * corresponding to the same date/time in another time zone by using + * ToTimezone() + * + * 6. the conversions to the current (or any other) timezone are done when the + * internal time representation is converted to the broken-down one in + * wxDateTime::Tm. + */ // ============================================================================ // declarations @@ -34,26 +69,136 @@ #include "wx/log.h" #endif // WX_PRECOMP +#include "wx/thread.h" + #define wxDEFINE_TIME_CONSTANTS #include "wx/datetime.h" +#ifndef WX_TIMEZONE + #define WX_TIMEZONE timezone +#endif + // ---------------------------------------------------------------------------- // constants // ---------------------------------------------------------------------------- -// the number of days in month in Julian/Gregorian calendar: the first line is -// for normal years, the second one is for the leap ones -static wxDateTime::wxDateTime_t gs_daysInMonth[2][12] = -{ - { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, - { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } -}; +// some trivial ones +static const int MONTHS_IN_YEAR = 12; + +static const int SECONDS_IN_MINUTE = 60; + +static const long SECONDS_PER_DAY = 86400l; + +static const long MILLISECONDS_PER_DAY = 86400000l; + +// this is the integral part of JDN of the midnight of Jan 1, 1970 +// (i.e. JDN(Jan 1, 1970) = 2440587.5) +static const int EPOCH_JDN = 2440587; + +// the date of JDN -0.5 (as we don't work with fractional parts, this is the +// reference date for us) is Nov 24, 4714BC +static const int JDN_0_YEAR = -4713; +static const int JDN_0_MONTH = wxDateTime::Nov; +static const int JDN_0_DAY = 24; + +// the constants used for JDN calculations +static const int JDN_OFFSET = 32046; +static const int DAYS_PER_5_MONTHS = 153; +static const int DAYS_PER_4_YEARS = 1461; +static const int DAYS_PER_400_YEARS = 146097; + +// ---------------------------------------------------------------------------- +// globals +// ---------------------------------------------------------------------------- + +// a critical section is needed to protect GetTimeZone() static +// variable in MT case +#ifdef wxUSE_THREADS + wxCriticalSection gs_critsectTimezone; +#endif // wxUSE_THREADS // ---------------------------------------------------------------------------- // private functions // ---------------------------------------------------------------------------- +// get the number of days in the given month of the given year +static inline +wxDateTime::wxDateTime_t GetNumOfDaysInMonth(int year, wxDateTime::Month month) +{ + // the number of days in month in Julian/Gregorian calendar: the first line + // is for normal years, the second one is for the leap ones + static wxDateTime::wxDateTime_t daysInMonth[2][MONTHS_IN_YEAR] = + { + { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, + { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } + }; + + return daysInMonth[wxDateTime::IsLeapYear(year)][month]; +} + +// ensure that the timezone variable is set by calling localtime +static int GetTimeZone() +{ + // set to TRUE when the timezone is set + static bool s_timezoneSet = FALSE; + + wxCRIT_SECT_LOCKER(lock, gs_critsectTimezone); + + if ( !s_timezoneSet ) + { + // just call localtime() instead of figuring out whether this system + // supports tzset(), _tzset() or something else + time_t t; + (void)localtime(&t); + + s_timezoneSet = TRUE; + } + + return (int)WX_TIMEZONE; +} + +// return the integral part of the JDN for the midnight of the given date (to +// get the real JDN you need to add 0.5, this is, in fact, JDN of the +// noon of the previous day) +static long GetTruncatedJDN(wxDateTime::wxDateTime_t day, + wxDateTime::Month mon, + int year) +{ + // CREDIT: code below is by Scott E. Lee (but bugs are mine) + + // check the date validity + wxASSERT_MSG( + (year > JDN_0_YEAR) || + ((year == JDN_0_YEAR) && (mon > JDN_0_MONTH)) || + ((year == JDN_0_YEAR) && (mon == JDN_0_MONTH) && (day >= JDN_0_DAY)), + _T("date out of range - can't convert to JDN") + ); + + // make the year positive to avoid problems with negative numbers division + year += 4800; + + // months are counted from March here + int month; + if ( mon >= wxDateTime::Mar ) + { + month = mon - 2; + } + else + { + month = mon + 10; + year--; + } + + // now we can simply add all the contributions together + return ((year / 100) * DAYS_PER_400_YEARS) / 4 + + ((year % 100) * DAYS_PER_4_YEARS) / 4 + + (month * DAYS_PER_5_MONTHS + 2) / 5 + + day + - JDN_OFFSET; +} + +// this function is a wrapper around strftime(3) static wxString CallStrftime(const wxChar *format, const tm* tm) { wxChar buf[1024]; @@ -66,6 +211,28 @@ static wxString CallStrftime(const wxChar *format, const tm* tm) return wxString(buf); } +// if year and/or month have invalid values, replace them with the current ones +static void ReplaceDefaultYearMonthWithCurrent(int *year, + wxDateTime::Month *month) +{ + struct tm *tmNow = NULL; + + if ( *year == wxDateTime::Inv_Year ) + { + tmNow = wxDateTime::GetTmNow(); + + *year = 1900 + tmNow->tm_year; + } + + if ( *month == wxDateTime::Inv_Month ) + { + if ( !tmNow ) + tmNow = wxDateTime::GetTmNow(); + + *month = (wxDateTime::Month)tmNow->tm_mon; + } +} + // ============================================================================ // implementation of wxDateTime // ============================================================================ @@ -86,17 +253,19 @@ wxDateTime::Tm::Tm() year = (wxDateTime_t)wxDateTime::Inv_Year; mon = wxDateTime::Inv_Month; mday = 0; - hour = min = sec = 0; + hour = min = sec = msec = 0; wday = wxDateTime::Inv_WeekDay; } -wxDateTime::Tm::Tm(const struct tm& tm) +wxDateTime::Tm::Tm(const struct tm& tm, const TimeZone& tz) + : m_tz(tz) { + msec = 0; sec = tm.tm_sec; min = tm.tm_min; hour = tm.tm_hour; mday = tm.tm_mday; - mon = tm.tm_mon; + mon = (wxDateTime::Month)tm.tm_mon; year = 1900 + tm.tm_year; wday = tm.tm_wday; yday = tm.tm_yday; @@ -105,14 +274,116 @@ wxDateTime::Tm::Tm(const struct tm& tm) bool wxDateTime::Tm::IsValid() const { // we allow for the leap seconds, although we don't use them (yet) - return (year != wxDateTime::Inv_Year) && (mon < 12) && - (mday < gs_daysInMonth[IsLeapYear(year)][mon]) && - (hour < 24) && (min < 60) && (sec < 62); + return (year != wxDateTime::Inv_Year) && (mon != wxDateTime::Inv_Month) && + (mday <= GetNumOfDaysInMonth(year, mon)) && + (hour < 24) && (min < 60) && (sec < 62) && (msec < 1000); } void wxDateTime::Tm::ComputeWeekDay() { - wxFAIL_MSG(_T("TODO")); + // compute the week day from day/month/year: we use the dumbest algorithm + // possible: just compute our JDN and then use the (simple to derive) + // formula: weekday = (JDN + 1.5) % 7 + wday = (wxDateTime::WeekDay)(GetTruncatedJDN(mday, mon, year) + 2) % 7; +} + +void wxDateTime::Tm::AddMonths(int monDiff) +{ + // normalize the months field + while ( monDiff < -mon ) + { + year--; + + monDiff += MONTHS_IN_YEAR; + } + + while ( monDiff + mon > MONTHS_IN_YEAR ) + { + year++; + + monDiff -= MONTHS_IN_YEAR; + } + + mon = (wxDateTime::Month)(mon + monDiff); + + wxASSERT_MSG( mon >= 0 && mon < MONTHS_IN_YEAR, _T("logic error") ); +} + +void wxDateTime::Tm::AddDays(int dayDiff) +{ + // normalize the days field + mday += dayDiff; + while ( mday < 1 ) + { + AddMonths(-1); + + mday += GetNumOfDaysInMonth(year, mon); + } + + while ( mday > GetNumOfDaysInMonth(year, mon) ) + { + mday -= GetNumOfDaysInMonth(year, mon); + + AddMonths(1); + } + + wxASSERT_MSG( mday > 0 && mday <= GetNumOfDaysInMonth(year, mon), + _T("logic error") ); +} + +// ---------------------------------------------------------------------------- +// class TimeZone +// ---------------------------------------------------------------------------- + +wxDateTime::TimeZone::TimeZone(wxDateTime::TZ tz) +{ + switch ( tz ) + { + case wxDateTime::Local: + // get the offset from C RTL: it returns the difference GMT-local + // while we want to have the offset _from_ GMT, hence the '-' + m_offset = -GetTimeZone(); + break; + + case wxDateTime::GMT_12: + case wxDateTime::GMT_11: + case wxDateTime::GMT_10: + case wxDateTime::GMT_9: + case wxDateTime::GMT_8: + case wxDateTime::GMT_7: + case wxDateTime::GMT_6: + case wxDateTime::GMT_5: + case wxDateTime::GMT_4: + case wxDateTime::GMT_3: + case wxDateTime::GMT_2: + case wxDateTime::GMT_1: + m_offset = -3600*(wxDateTime::GMT0 - tz); + break; + + case wxDateTime::GMT0: + case wxDateTime::GMT1: + case wxDateTime::GMT2: + case wxDateTime::GMT3: + case wxDateTime::GMT4: + case wxDateTime::GMT5: + case wxDateTime::GMT6: + case wxDateTime::GMT7: + case wxDateTime::GMT8: + case wxDateTime::GMT9: + case wxDateTime::GMT10: + case wxDateTime::GMT11: + case wxDateTime::GMT12: + m_offset = 3600*(tz - wxDateTime::GMT0); + break; + + case wxDateTime::A_CST: + // Central Standard Time in use in Australia = UTC + 9.5 + m_offset = 60*(9*60 + 30); + break; + + default: + wxFAIL_MSG( _T("unknown time zone") ); + } } // ---------------------------------------------------------------------------- @@ -122,6 +393,9 @@ void wxDateTime::Tm::ComputeWeekDay() /* static */ bool wxDateTime::IsLeapYear(int year, wxDateTime::Calendar cal) { + if ( year == Inv_Year ) + year = GetCurrentYear(); + if ( cal == Gregorian ) { // in Gregorian calendar leap years are those divisible by 4 except @@ -144,9 +418,9 @@ bool wxDateTime::IsLeapYear(int year, wxDateTime::Calendar cal) } /* static */ -void wxDateTime::SetCountry(wxDateTime::Country country) +int wxDateTime::GetCentury(int year) { - ms_country = country; + return year > 0 ? year / 100 : year / 100 - 1; } /* static */ @@ -197,12 +471,36 @@ wxDateTime::Month wxDateTime::GetCurrentMonth(wxDateTime::Calendar cal) return Inv_Month; } +/* static */ +wxDateTime::wxDateTime_t wxDateTime::GetNumberOfDays(int year, Calendar cal) +{ + if ( year == Inv_Year ) + { + // take the current year if none given + year = GetCurrentYear(); + } + + switch ( cal ) + { + case Gregorian: + case Julian: + return IsLeapYear(year) ? 366 : 365; + break; + + default: + wxFAIL_MSG(_T("unsupported calendar")); + break; + } + + return 0; +} + /* static */ wxDateTime::wxDateTime_t wxDateTime::GetNumberOfDays(wxDateTime::Month month, int year, wxDateTime::Calendar cal) { - wxCHECK_MSG( month < 12, 0, _T("invalid month") ); + wxCHECK_MSG( month < MONTHS_IN_YEAR, 0, _T("invalid month") ); if ( cal == Gregorian || cal == Julian ) { @@ -212,7 +510,7 @@ wxDateTime::wxDateTime_t wxDateTime::GetNumberOfDays(wxDateTime::Month month, year = GetCurrentYear(); } - return gs_daysInMonth[IsLeapYear(year)][month]; + return GetNumOfDaysInMonth(year, month); } else { @@ -240,25 +538,322 @@ wxString wxDateTime::GetWeekDayName(wxDateTime::WeekDay wday, bool abbr) // take some arbitrary Sunday tm tm = { 0, 0, 0, 28, Nov, 99 }; - // and offset it by the number of days needed to get + // and offset it by the number of days needed to get the correct wday tm.tm_mday += wday; + // call mktime() to normalize it... + (void)mktime(&tm); + + // ... and call strftime() return CallStrftime(abbr ? _T("%a") : _T("%A"), &tm); } +// ---------------------------------------------------------------------------- +// Country stuff: date calculations depend on the country (DST, work days, +// ...), so we need to know which rules to follow. +// ---------------------------------------------------------------------------- + +/* static */ +wxDateTime::Country wxDateTime::GetCountry() +{ + if ( ms_country == Country_Unknown ) + { + // try to guess from the time zone name + time_t t = time(NULL); + struct tm *tm = localtime(&t); + + wxString tz = CallStrftime(_T("%Z"), tm); + if ( tz == _T("WET") || tz == _T("WEST") ) + { + ms_country = UK; + } + else if ( tz == _T("CET") || tz == _T("CEST") ) + { + ms_country = Country_EEC; + } + else if ( tz == _T("MSK") || tz == _T("MSD") ) + { + ms_country = Russia; + } + else if ( tz == _T("AST") || tz == _T("ADT") || + tz == _T("EST") || tz == _T("EDT") || + tz == _T("CST") || tz == _T("CDT") || + tz == _T("MST") || tz == _T("MDT") || + tz == _T("PST") || tz == _T("PDT") ) + { + ms_country = USA; + } + else + { + // well, choose a default one + ms_country = USA; + } + } + + return ms_country; +} + +/* static */ +void wxDateTime::SetCountry(wxDateTime::Country country) +{ + ms_country = country; +} + +/* static */ +bool wxDateTime::IsWestEuropeanCountry(Country country) +{ + if ( country == Country_Default ) + { + country = GetCountry(); + } + + return (Country_WesternEurope_Start <= country) && + (country <= Country_WesternEurope_End); +} + +// ---------------------------------------------------------------------------- +// DST calculations: we use 3 different rules for the West European countries, +// USA and for the rest of the world. This is undoubtedly false for many +// countries, but I lack the necessary info (and the time to gather it), +// please add the other rules here! +// ---------------------------------------------------------------------------- + +/* static */ +bool wxDateTime::IsDSTApplicable(int year, Country country) +{ + if ( year == Inv_Year ) + { + // take the current year if none given + year = GetCurrentYear(); + } + + if ( country == Country_Default ) + { + country = GetCountry(); + } + + switch ( country ) + { + case USA: + case UK: + // DST was first observed in the US and UK during WWI, reused + // during WWII and used again since 1966 + return year >= 1966 || + (year >= 1942 && year <= 1945) || + (year == 1918 || year == 1919); + + default: + // assume that it started after WWII + return year > 1950; + } +} + +/* static */ +wxDateTime wxDateTime::GetBeginDST(int year, Country country) +{ + if ( year == Inv_Year ) + { + // take the current year if none given + year = GetCurrentYear(); + } + + if ( country == Country_Default ) + { + country = GetCountry(); + } + + if ( !IsDSTApplicable(year, country) ) + { + return ms_InvDateTime; + } + + wxDateTime dt; + + if ( IsWestEuropeanCountry(country) || (country == Russia) ) + { + // DST begins at 1 a.m. GMT on the last Sunday of March + if ( !dt.SetToLastWeekDay(Sun, Mar, year) ) + { + // weird... + wxFAIL_MSG( _T("no last Sunday in March?") ); + } + + dt += wxTimeSpan::Hours(1); + + dt.MakeGMT(); + } + else switch ( country ) + { + case USA: + switch ( year ) + { + case 1918: + case 1919: + // don't know for sure - assume it was in effect all year + + case 1943: + case 1944: + case 1945: + dt.Set(1, Jan, year); + break; + + case 1942: + // DST was installed Feb 2, 1942 by the Congress + dt.Set(2, Feb, year); + break; + + // Oil embargo changed the DST period in the US + case 1974: + dt.Set(6, Jan, 1974); + break; + + case 1975: + dt.Set(23, Feb, 1975); + break; + + default: + // before 1986, DST begun on the last Sunday of April, but + // in 1986 Reagan changed it to begin at 2 a.m. of the + // first Sunday in April + if ( year < 1986 ) + { + if ( !dt.SetToLastWeekDay(Sun, Apr, year) ) + { + // weird... + wxFAIL_MSG( _T("no first Sunday in April?") ); + } + } + else + { + if ( !dt.SetToWeekDay(Sun, 1, Apr, year) ) + { + // weird... + wxFAIL_MSG( _T("no first Sunday in April?") ); + } + } + + dt += wxTimeSpan::Hours(2); + + // TODO what about timezone?? + } + + break; + + default: + // assume Mar 30 as the start of the DST for the rest of the world + // - totally bogus, of course + dt.Set(30, Mar, year); + } + + return dt; +} + +/* static */ +wxDateTime wxDateTime::GetEndDST(int year, Country country) +{ + if ( year == Inv_Year ) + { + // take the current year if none given + year = GetCurrentYear(); + } + + if ( country == Country_Default ) + { + country = GetCountry(); + } + + if ( !IsDSTApplicable(year, country) ) + { + return ms_InvDateTime; + } + + wxDateTime dt; + + if ( IsWestEuropeanCountry(country) || (country == Russia) ) + { + // DST ends at 1 a.m. GMT on the last Sunday of October + if ( !dt.SetToLastWeekDay(Sun, Oct, year) ) + { + // weirder and weirder... + wxFAIL_MSG( _T("no last Sunday in October?") ); + } + + dt += wxTimeSpan::Hours(1); + + dt.MakeGMT(); + } + else switch ( country ) + { + case USA: + switch ( year ) + { + case 1918: + case 1919: + // don't know for sure - assume it was in effect all year + + case 1943: + case 1944: + dt.Set(31, Dec, year); + break; + + case 1945: + // the time was reset after the end of the WWII + dt.Set(30, Sep, year); + break; + + default: + // DST ends at 2 a.m. on the last Sunday of October + if ( !dt.SetToLastWeekDay(Sun, Oct, year) ) + { + // weirder and weirder... + wxFAIL_MSG( _T("no last Sunday in October?") ); + } + + dt += wxTimeSpan::Hours(2); + + // TODO what about timezone?? + } + break; + + default: + // assume October 26th as the end of the DST - totally bogus too + dt.Set(26, Oct, year); + } + + return dt; +} + // ---------------------------------------------------------------------------- // constructors and assignment operators // ---------------------------------------------------------------------------- -wxDateTime& wxDateTime::Set(const struct tm& tm1) +// the values in the tm structure contain the local time +wxDateTime& wxDateTime::Set(const struct tm& tm) { wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") ); - tm tm2(tm1); + struct tm tm2(tm); time_t timet = mktime(&tm2); - if ( timet == (time_t)(-1) ) + + if ( timet == (time_t)-1 ) { - wxFAIL_MSG(_T("Invalid time")); + // mktime() rather unintuitively fails for Jan 1, 1970 if the hour is + // less than timezone - try to make it work for this case + if ( tm2.tm_year == 70 && tm2.tm_mon == 0 && tm2.tm_mday == 1 ) + { + // add timezone to make sure that date is in range + tm2.tm_sec -= GetTimeZone(); + + timet = mktime(&tm2); + if ( timet != (time_t)-1 ) + { + timet += GetTimeZone(); + + return Set(timet); + } + } + + wxFAIL_MSG( _T("mktime() failed") ); return ms_InvDateTime; } @@ -285,6 +880,8 @@ wxDateTime& wxDateTime::Set(wxDateTime_t hour, time_t timet = GetTimeNow(); struct tm *tm = localtime(&timet); + wxCHECK_MSG( tm, ms_InvDateTime, _T("localtime() failed") ); + // adjust the time tm->tm_hour = hour; tm->tm_min = minute; @@ -310,12 +907,10 @@ wxDateTime& wxDateTime::Set(wxDateTime_t day, ms_InvDateTime, _T("Invalid time in wxDateTime::Set()") ); - if ( year == Inv_Year ) - year = GetCurrentYear(); - if ( month == Inv_Month ) - month = GetCurrentMonth(); + ReplaceDefaultYearMonthWithCurrent(&year, &month); - wxCHECK_MSG( day < GetNumberOfDays(month, year), ms_InvDateTime, + wxCHECK_MSG( (0 < day) && (day <= GetNumberOfDays(month, year)), + ms_InvDateTime, _T("Invalid date in wxDateTime::Set()") ); // the range of time_t type (inclusive) @@ -324,7 +919,7 @@ wxDateTime& wxDateTime::Set(wxDateTime_t day, // test only the year instead of testing for the exact end of the Unix // time_t range - it doesn't bring anything to do more precise checks - if ( year >= yearMaxInRange && year <= yearMaxInRange ) + if ( year >= yearMinInRange && year <= yearMaxInRange ) { // use the standard library version if the date is in range - this is // probably more efficient than our code @@ -335,6 +930,7 @@ wxDateTime& wxDateTime::Set(wxDateTime_t day, tm.tm_hour = hour; tm.tm_min = minute; tm.tm_sec = second; + tm.tm_isdst = -1; // mktime() will guess it (void)Set(tm); @@ -344,19 +940,37 @@ wxDateTime& wxDateTime::Set(wxDateTime_t day, else { // do time calculations ourselves: we want to calculate the number of - // milliseconds between the given date and the epoch (necessarily - // negative) - wxFAIL_MSG(_T("TODO")); + // milliseconds between the given date and the epoch + + // get the JDN for the midnight of this day + m_time = GetTruncatedJDN(day, month, year); + m_time -= EPOCH_JDN; + m_time *= SECONDS_PER_DAY * TIME_T_FACTOR; + + // JDN corresponds to GMT, we take localtime + Add(wxTimeSpan(hour, minute, second + GetTimeZone(), millisec)); } return *this; } +wxDateTime& wxDateTime::Set(double jdn) +{ + // so that m_time will be 0 for the midnight of Jan 1, 1970 which is jdn + // EPOCH_JDN + 0.5 + jdn -= EPOCH_JDN + 0.5; + + m_time = jdn; + m_time *= MILLISECONDS_PER_DAY; + + return *this; +} + // ---------------------------------------------------------------------------- // time_t <-> broken down time conversions // ---------------------------------------------------------------------------- -wxDateTime::Tm wxDateTime::GetTm() const +wxDateTime::Tm wxDateTime::GetTm(const TimeZone& tz) const { wxASSERT_MSG( IsValid(), _T("invalid wxDateTime") ); @@ -364,19 +978,117 @@ wxDateTime::Tm wxDateTime::GetTm() const if ( time != (time_t)-1 ) { // use C RTL functions - tm *tm = localtime(&time); + tm *tm; + if ( tz.GetOffset() == -GetTimeZone() ) + { + // we are working with local time + tm = localtime(&time); - // should never happen - wxCHECK_MSG( tm, Tm(), _T("localtime() failed") ); + // should never happen + wxCHECK_MSG( tm, Tm(), _T("gmtime() failed") ); + } + else + { + time += tz.GetOffset(); + if ( time >= 0 ) + { + tm = gmtime(&time); + + // should never happen + wxCHECK_MSG( tm, Tm(), _T("gmtime() failed") ); + } + else + { + tm = (struct tm *)NULL; + } + } - return Tm(*tm); + if ( tm ) + { + return Tm(*tm, tz); + } + //else: use generic code below } - else + + // remember the time and do the calculations with the date only - this + // eliminates rounding errors of the floating point arithmetics + + wxLongLong timeMidnight = m_time + tz.GetOffset() * 1000; + + long timeOnly = (timeMidnight % MILLISECONDS_PER_DAY).ToLong(); + + // we want to always have positive time and timeMidnight to be really + // the midnight before it + if ( timeOnly < 0 ) { - wxFAIL_MSG(_T("TODO")); + timeOnly = MILLISECONDS_PER_DAY + timeOnly; + } + + timeMidnight -= timeOnly; + + // calculate the Gregorian date from JDN for the midnight of our date: + // this will yield day, month (in 1..12 range) and year + + // actually, this is the JDN for the noon of the previous day + long jdn = (timeMidnight / MILLISECONDS_PER_DAY).ToLong() + EPOCH_JDN; + + // CREDIT: code below is by Scott E. Lee (but bugs are mine) - return Tm(); + wxASSERT_MSG( jdn > -2, _T("JDN out of range") ); + + // calculate the century + int temp = (jdn + JDN_OFFSET) * 4 - 1; + int century = temp / DAYS_PER_400_YEARS; + + // then the year and day of year (1 <= dayOfYear <= 366) + temp = ((temp % DAYS_PER_400_YEARS) / 4) * 4 + 3; + int year = (century * 100) + (temp / DAYS_PER_4_YEARS); + int dayOfYear = (temp % DAYS_PER_4_YEARS) / 4 + 1; + + // and finally the month and day of the month + temp = dayOfYear * 5 - 3; + int month = temp / DAYS_PER_5_MONTHS; + int day = (temp % DAYS_PER_5_MONTHS) / 5 + 1; + + // month is counted from March - convert to normal + if ( month < 10 ) + { + month += 3; } + else + { + year += 1; + month -= 9; + } + + // year is offset by 4800 + year -= 4800; + + // check that the algorithm gave us something reasonable + wxASSERT_MSG( (0 < month) && (month <= 12), _T("invalid month") ); + wxASSERT_MSG( (1 <= day) && (day < 32), _T("invalid day") ); + wxASSERT_MSG( (INT_MIN <= year) && (year <= INT_MAX), + _T("year range overflow") ); + + // construct Tm from these values + Tm tm; + tm.year = (int)year; + tm.mon = (Month)(month - 1); // algorithm yields 1 for January, not 0 + tm.mday = (wxDateTime_t)day; + tm.msec = timeOnly % 1000; + timeOnly -= tm.msec; + timeOnly /= 1000; // now we have time in seconds + + tm.sec = timeOnly % 60; + timeOnly -= tm.sec; + timeOnly /= 60; // now we have time in minutes + + tm.min = timeOnly % 60; + timeOnly -= tm.min; + + tm.hour = timeOnly / 60; + + return tm; } wxDateTime& wxDateTime::SetYear(int year) @@ -465,35 +1177,412 @@ wxDateTime& wxDateTime::Add(const wxDateSpan& diff) Tm tm(GetTm()); tm.year += diff.GetYears(); - tm.mon += diff.GetMonths(); - tm.mday += diff.GetTotalDays(); + tm.AddMonths(diff.GetMonths()); + tm.AddDays(diff.GetTotalDays()); Set(tm); return *this; } +// ---------------------------------------------------------------------------- +// Weekday and monthday stuff +// ---------------------------------------------------------------------------- + +wxDateTime& wxDateTime::SetToLastMonthDay(Month month, + int year) +{ + // take the current month/year if none specified + ReplaceDefaultYearMonthWithCurrent(&year, &month); + + return Set(GetNumOfDaysInMonth(year, month), month, year); +} + +bool wxDateTime::SetToWeekDay(WeekDay weekday, + int n, + Month month, + int year) +{ + wxCHECK_MSG( weekday != Inv_WeekDay, FALSE, _T("invalid weekday") ); + + // we don't check explicitly that -5 <= n <= 5 because we will return FALSE + // anyhow in such case - but may be should still give an assert for it? + + // take the current month/year if none specified + ReplaceDefaultYearMonthWithCurrent(&year, &month); + + wxDateTime dt; + + // TODO this probably could be optimised somehow... + + if ( n > 0 ) + { + // get the first day of the month + dt.Set(1, month, year); + + // get its wday + WeekDay wdayFirst = dt.GetWeekDay(); + + // go to the first weekday of the month + int diff = weekday - wdayFirst; + if ( diff < 0 ) + diff += 7; + + // add advance n-1 weeks more + diff += 7*(n - 1); + + dt += wxDateSpan::Days(diff); + } + else // count from the end of the month + { + // get the last day of the month + dt.SetToLastMonthDay(month, year); + + // get its wday + WeekDay wdayLast = dt.GetWeekDay(); + + // go to the last weekday of the month + int diff = wdayLast - weekday; + if ( diff < 0 ) + diff += 7; + + // and rewind n-1 weeks from there + diff += 7*(-n - 1); + + dt -= wxDateSpan::Days(diff); + } + + // check that it is still in the same month + if ( dt.GetMonth() == month ) + { + *this = dt; + + return TRUE; + } + else + { + // no such day in this month + return FALSE; + } +} + +wxDateTime::wxDateTime_t wxDateTime::GetDayOfYear(const TimeZone& tz) const +{ + // this array contains the cumulated number of days in all previous months + // for normal and leap years + static const wxDateTime_t cumulatedDays[2][MONTHS_IN_YEAR] = + { + { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }, + { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 } + }; + + Tm tm(GetTm(tz)); + + return cumulatedDays[IsLeapYear(tm.year)][tm.mon] + tm.mday; +} + +wxDateTime::wxDateTime_t wxDateTime::GetWeekOfYear(const TimeZone& tz) const +{ + // the first week of the year is the one which contains Jan, 4 (according + // to ISO standard rule), so the year day N0 = 4 + 7*W always lies in the + // week W+1. As any day N = 7*W + 4 + (N - 4)%7, it lies in the same week + // as N0 or in the next one. + + // TODO this surely may be optimized - I got confused while writing it + + wxDateTime_t nDayInYear = GetDayOfYear(tz); + + // the week day of the day lying in the first week + WeekDay wdayStart = wxDateTime(4, Jan, GetYear()).GetWeekDay(); + + wxDateTime_t week = (nDayInYear - 4) / 7 + 1; + + // notice that Sunday shoould be counted as 7, not 0 here! + if ( ((nDayInYear - 4) % 7) + (!wdayStart ? 7 : wdayStart) > 7 ) + { + week++; + } + + return week; +} + +// ---------------------------------------------------------------------------- +// Julian day number conversion and related stuff +// ---------------------------------------------------------------------------- + +double wxDateTime::GetJulianDayNumber() const +{ + // JDN are always expressed for the GMT dates + Tm tm(ToTimezone(GMT0).GetTm(GMT0)); + + double result = GetTruncatedJDN(tm.mday, tm.mon, tm.year); + + // add the part GetTruncatedJDN() neglected + result += 0.5; + + // and now add the time: 86400 sec = 1 JDN + return result + ((double)(60*(60*tm.hour + tm.min) + tm.sec)) / 86400; +} + +double wxDateTime::GetRataDie() const +{ + // March 1 of the year 0 is Rata Die day -306 and JDN 1721119.5 + return GetJulianDayNumber() - 1721119.5 - 306; +} + +// ---------------------------------------------------------------------------- +// timezone and DST stuff +// ---------------------------------------------------------------------------- + +int wxDateTime::IsDST(wxDateTime::Country country) const +{ + wxCHECK_MSG( country == Country_Default, -1, + _T("country support not implemented") ); + + // use the C RTL for the dates in the standard range + time_t timet = GetTicks(); + if ( timet != (time_t)-1 ) + { + tm *tm = localtime(&timet); + + wxCHECK_MSG( tm, -1, _T("localtime() failed") ); + + return tm->tm_isdst; + } + else + { + int year = GetYear(); + + if ( !IsDSTApplicable(year, country) ) + { + // no DST time in this year in this country + return -1; + } + + return IsBetween(GetBeginDST(year, country), GetEndDST(year, country)); + } +} + +wxDateTime& wxDateTime::MakeTimezone(const TimeZone& tz) +{ + int secDiff = GetTimeZone() + tz.GetOffset(); + + // we need to know whether DST is or not in effect for this date + if ( IsDST() == 1 ) + { + // FIXME we assume that the DST is always shifted by 1 hour + secDiff -= 3600; + } + + return Substract(wxTimeSpan::Seconds(secDiff)); +} + // ---------------------------------------------------------------------------- // wxDateTime to/from text representations // ---------------------------------------------------------------------------- -wxString wxDateTime::Format(const wxChar *format) const +wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const { + wxCHECK_MSG( format, _T(""), _T("NULL format in wxDateTime::Format") ); + time_t time = GetTicks(); if ( time != (time_t)-1 ) { // use strftime() - tm *tm = localtime(&time); + tm *tm; + if ( tz.GetOffset() == -GetTimeZone() ) + { + // we are working with local time + tm = localtime(&time); - // should never happen - wxCHECK_MSG( tm, _T(""), _T("localtime() failed") ); + // should never happen + wxCHECK_MSG( tm, wxEmptyString, _T("localtime() failed") ); + } + else + { + time += tz.GetOffset(); + + if ( time >= 0 ) + { + tm = gmtime(&time); + + // should never happen + wxCHECK_MSG( tm, wxEmptyString, _T("gmtime() failed") ); + } + else + { + tm = (struct tm *)NULL; + } + } - return CallStrftime(format, tm); + if ( tm ) + { + return CallStrftime(format, tm); + } + //else: use generic code below } - else + + // use a hack and still use strftime(): first find the YEAR which is a year + // in the strftime() range (1970 - 2038) whose Jan 1 falls on the same week + // day as the Jan 1 of the real year. Then make a copy of the format and + // replace all occurences of YEAR in it with some unique string not + // appearing anywhere else in it, then use strftime() to format the date in + // year YEAR and then replace YEAR back by the real year and the unique + // replacement string back with YEAR. Notice that "all occurences of YEAR" + // means all occurences of 4 digit as well as 2 digit form! + + // NB: may be it would be simpler to "honestly" reimplement strftime()? + + // find the YEAR: normally, for any year X, Jan 1 or the year X + 28 is the + // same weekday as Jan 1 of X (because the weekday advances by 1 for each + // normal X and by 2 for each leap X, hence by 5 every 4 years or by 35 + // which is 0 mod 7 every 28 years) but this rule breaks down if there are + // years between X and Y which are divisible by 4 but not leap (i.e. + // divisible by 100 but not 400), hence the correction. + + int yearReal = GetYear(tz); + int year = 1970 + yearReal % 28; + + int nCenturiesInBetween = (year / 100) - (yearReal / 100); + int nLostWeekDays = nCenturiesInBetween - (nCenturiesInBetween / 400); + + // we have to gain back the "lost" weekdays... + while ( (nLostWeekDays % 7) != 0 ) + { + nLostWeekDays += year++ % 4 ? 1 : 2; + } + + // at any rate, we can't go further than 1997 + 28! + wxASSERT_MSG( year < 2030, _T("logic error in wxDateTime::Format") ); + + wxString strYear, strYear2; + strYear.Printf(_T("%d"), year); + strYear2.Printf(_T("%d"), year % 100); + + // find two strings not occuring in format (this is surely not optimal way + // of doing it... improvements welcome!) + wxString fmt = format; + wxString replacement = (wxChar)-1; + while ( fmt.Find(replacement) != wxNOT_FOUND ) { - wxFAIL_MSG(_T("TODO")); + replacement << (wxChar)-1; + } - return _T(""); + wxString replacement2 = (wxChar)-2; + while ( fmt.Find(replacement) != wxNOT_FOUND ) + { + replacement << (wxChar)-2; } + + // replace all occurences of year with it + bool wasReplaced = fmt.Replace(strYear, replacement) > 0; + if ( !wasReplaced ) + wasReplaced = fmt.Replace(strYear2, replacement2) > 0; + + // use strftime() to format the same date but in supported year + wxDateTime dt(*this); + dt.SetYear(year); + wxString str = dt.Format(format, tz); + + // now replace the occurence of 1999 with the real year + wxString strYearReal, strYearReal2; + strYearReal.Printf(_T("%04d"), yearReal); + strYearReal2.Printf(_T("%02d"), yearReal % 100); + str.Replace(strYear, strYearReal); + str.Replace(strYear2, strYearReal2); + + // and replace back all occurences of replacement string + if ( wasReplaced ) + { + str.Replace(replacement2, strYear2); + str.Replace(replacement, strYear); + } + + return str; +} + +// ============================================================================ +// wxTimeSpan +// ============================================================================ + +// not all strftime(3) format specifiers make sense here because, for example, +// a time span doesn't have a year nor a timezone +// +// Here are the ones which are supported (all of them are supported by strftime +// as well): +// %H hour in 24 hour format +// %M minute (00 - 59) +// %S second (00 - 59) +// %% percent sign +// +// Also, for MFC CTimeSpan compatibility, we support +// %D number of days +// +// And, to be better than MFC :-), we also have +// %E number of wEeks +// %l milliseconds (000 - 999) +wxString wxTimeSpan::Format(const wxChar *format) const +{ + wxCHECK_MSG( format, _T(""), _T("NULL format in wxTimeSpan::Format") ); + + wxString str; + str.Alloc(strlen(format)); + + for ( const wxChar *pch = format; pch; pch++ ) + { + wxChar ch = *pch; + + if ( ch == '%' ) + { + wxString tmp; + + ch = *pch++; + switch ( ch ) + { + default: + wxFAIL_MSG( _T("invalid format character") ); + // fall through + + case '%': + // will get to str << ch below + break; + + case 'D': + tmp.Printf(_T("%d"), GetDays()); + break; + + case 'E': + tmp.Printf(_T("%d"), GetWeeks()); + break; + + case 'H': + tmp.Printf(_T("%02d"), GetHours()); + break; + + case 'l': + tmp.Printf(_T("%03d"), GetMilliseconds().GetValue()); + break; + + case 'M': + tmp.Printf(_T("%02d"), GetMinutes()); + break; + + case 'S': + tmp.Printf(_T("%02d"), GetSeconds().GetValue()); + break; + } + + if ( !!tmp ) + { + str += tmp; + + // skip str += ch below + continue; + } + } + + str += ch; + } + + return str; }