X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1ef54dcf125ad687ccfd32195fe2f449910e7d5f..78e848cac9c973346dcf598f3c1b942b63dd2821:/src/common/datetime.cpp diff --git a/src/common/datetime.cpp b/src/common/datetime.cpp index e623c60294..d93cb2d179 100644 --- a/src/common/datetime.cpp +++ b/src/common/datetime.cpp @@ -20,7 +20,8 @@ * Implementation notes: * * 1. the time is stored as a 64bit integer containing the signed number of - * milliseconds since Jan 1. 1970 (the Unix Epoch) + * 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 @@ -33,6 +34,14 @@ * 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. */ // ============================================================================ @@ -244,7 +253,8 @@ wxDateTime::Tm::Tm() 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; @@ -321,7 +331,9 @@ wxDateTime::TimeZone::TimeZone(wxDateTime::TZ tz) switch ( tz ) { case wxDateTime::Local: - // leave offset to be 0 + // 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: @@ -336,7 +348,7 @@ wxDateTime::TimeZone::TimeZone(wxDateTime::TZ tz) case wxDateTime::GMT_3: case wxDateTime::GMT_2: case wxDateTime::GMT_1: - m_offset = -60*(wxDateTime::GMT0 - tz); + m_offset = -3600*(wxDateTime::GMT0 - tz); break; case wxDateTime::GMT0: @@ -352,12 +364,12 @@ wxDateTime::TimeZone::TimeZone(wxDateTime::TZ tz) case wxDateTime::GMT10: case wxDateTime::GMT11: case wxDateTime::GMT12: - m_offset = 60*(tz - wxDateTime::GMT0); + m_offset = 3600*(tz - wxDateTime::GMT0); break; case wxDateTime::A_CST: // Central Standard Time in use in Australia = UTC + 9.5 - m_offset = 9*60 + 30; + m_offset = 60*(9*60 + 30); break; default: @@ -533,33 +545,33 @@ wxString wxDateTime::GetWeekDayName(wxDateTime::WeekDay wday, bool abbr) // 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); - - // we want the time in GMT, mktime() takes the local time, so use timegm() - // if it's available -#ifdef HAVE_TIMEGM - time_t timet = timegm(&tm2); -#else // !HAVE_TIMEGM - // FIXME this almost surely doesn't work - tm2.tm_sec -= GetTimeZone(); - + struct tm tm2(tm); time_t timet = mktime(&tm2); - if ( tm2.tm_isdst ) + if ( timet == (time_t)-1 ) { - tm2.tm_hour += 1; + // 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); - } -#endif // HAVE_TIMEGM/!HAVE_TIMEGM + timet = mktime(&tm2); + if ( timet != (time_t)-1 ) + { + timet += GetTimeZone(); - if ( timet == (time_t)(-1) ) - { - wxFAIL_MSG(_T("Invalid time")); + return Set(timet); + } + } + + wxFAIL_MSG( _T("mktime() failed") ); return ms_InvDateTime; } @@ -584,7 +596,9 @@ wxDateTime& wxDateTime::Set(wxDateTime_t hour, // get the current date from system time_t timet = GetTimeNow(); - struct tm *tm = gmtime(&timet); + struct tm *tm = localtime(&timet); + + wxCHECK_MSG( tm, ms_InvDateTime, _T("localtime() failed") ); // adjust the time tm->tm_hour = hour; @@ -634,7 +648,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 (wrongly, probably) + tm.tm_isdst = -1; // mktime() will guess it (void)Set(tm); @@ -651,7 +665,8 @@ wxDateTime& wxDateTime::Set(wxDateTime_t day, m_time -= EPOCH_JDN; m_time *= SECONDS_PER_DAY * TIME_T_FACTOR; - Add(wxTimeSpan(hour, minute, second, millisec)); + // JDN corresponds to GMT, we take localtime + Add(wxTimeSpan(hour, minute, second + GetTimeZone(), millisec)); } return *this; @@ -673,7 +688,7 @@ wxDateTime& wxDateTime::Set(double jdn) // 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") ); @@ -681,24 +696,37 @@ wxDateTime::Tm wxDateTime::GetTm() const if ( time != (time_t)-1 ) { // use C RTL functions - tm *tm = gmtime(&time); + tm *tm; + if ( tz.GetOffset() == -GetTimeZone() ) + { + // we are working with local time + tm = localtime(&time); + } + else + { + time += tz.GetOffset(); + tm = gmtime(&time); + } // should never happen wxCHECK_MSG( tm, Tm(), _T("gmtime() failed") ); - return Tm(*tm); + return Tm(*tm, tz); } 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; + wxLongLong timeMidnight = m_time + tz.GetOffset() * 1000; + + long timeOnly = (timeMidnight % MILLISECONDS_PER_DAY).ToLong(); - long timeOnly = (m_time % MILLISECONDS_PER_DAY).ToLong(); + // we want to always have positive time and timeMidnight to be really + // the midnight before it if ( timeOnly < 0 ) { - timeOnly = MILLISECONDS_PER_DAY - timeOnly; + timeOnly = MILLISECONDS_PER_DAY + timeOnly; } timeMidnight -= timeOnly; @@ -950,7 +978,8 @@ bool wxDateTime::SetToWeekDay(WeekDay weekday, double wxDateTime::GetJulianDayNumber() const { - Tm tm(GetTm()); + // JDN are always expressed for the GMT dates + Tm tm(ToTimezone(GMT0).GetTm(GMT0)); double result = GetTruncatedJDN(tm.mday, tm.mon, tm.year); @@ -968,31 +997,51 @@ double wxDateTime::GetRataDie() const } // ---------------------------------------------------------------------------- -// timezone stuff +// timezone and DST stuff // ---------------------------------------------------------------------------- -wxDateTime& wxDateTime::MakeUTC() +int wxDateTime::IsDST(wxDateTime::Country country) const { - return Add(wxTimeSpan::Seconds(GetTimeZone())); + 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 + { + // wxFAIL_MSG( _T("TODO") ); + + return -1; + } } wxDateTime& wxDateTime::MakeTimezone(const TimeZone& tz) { - int minDiff = GetTimeZone() / SECONDS_IN_MINUTE + tz.GetOffset(); - return Add(wxTimeSpan::Minutes(minDiff)); -} + int secDiff = GetTimeZone() + tz.GetOffset(); -wxDateTime& wxDateTime::MakeLocalTime(const TimeZone& tz) -{ - int minDiff = GetTimeZone() / SECONDS_IN_MINUTE + tz.GetOffset(); - return Substract(wxTimeSpan::Minutes(minDiff)); + // 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") ); @@ -1000,7 +1049,18 @@ wxString wxDateTime::Format(const wxChar *format) const if ( time != (time_t)-1 ) { // use strftime() - tm *tm = gmtime(&time); + tm *tm; + if ( tz.GetOffset() == -GetTimeZone() ) + { + // we are working with local time + tm = localtime(&time); + } + else + { + time += tz.GetOffset(); + + tm = gmtime(&time); + } // should never happen wxCHECK_MSG( tm, _T(""), _T("gmtime() failed") ); @@ -1018,7 +1078,7 @@ wxString wxDateTime::Format(const wxChar *format) const // the real year modulo 28 (so the week days coincide for them) // find the YEAR - int yearReal = GetYear(); + int yearReal = GetYear(tz); int year = 1970 + yearReal % 28; wxString strYear; @@ -1039,7 +1099,7 @@ wxString wxDateTime::Format(const wxChar *format) const // use strftime() to format the same date but in supported year wxDateTime dt(*this); dt.SetYear(year); - wxString str = dt.Format(format); + wxString str = dt.Format(format, tz); // now replace the occurence of 1999 with the real year wxString strYearReal;