X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/444bc2b24d7eb9ab76a9ddbb9ca22e1b2d7909b4..bf2c43c76e2819be443ab1d830ab68d9569d66b1:/src/common/datetimefmt.cpp diff --git a/src/common/datetimefmt.cpp b/src/common/datetimefmt.cpp index c1dcd1fd2c..4855025050 100644 --- a/src/common/datetimefmt.cpp +++ b/src/common/datetimefmt.cpp @@ -4,7 +4,6 @@ // Author: Vadim Zeitlin // Modified by: // Created: 11.05.99 -// RCS-ID: $Id$ // Copyright: (c) 1999 Vadim Zeitlin // parts of code taken from sndcal library by Scott E. Lee: // @@ -34,7 +33,7 @@ #if !defined(wxUSE_DATETIME) || wxUSE_DATETIME #ifndef WX_PRECOMP - #ifdef __WXMSW__ + #ifdef __WINDOWS__ #include "wx/msw/wrapwin.h" #endif #include "wx/string.h" @@ -291,8 +290,14 @@ ParseFormatAt(wxString::const_iterator& p, const wxString str(p, end); wxString::const_iterator endParse; wxDateTime dt; - if ( dt.ParseFormat(str, fmt, &endParse) || - (!fmtAlt.empty() && dt.ParseFormat(str, fmtAlt, &endParse)) ) + + // Use a default date outside of the DST period to avoid problems with + // parsing the time differently depending on the today's date (which is used + // as the fall back date if none is explicitly specified). + static const wxDateTime dtDef(1, wxDateTime::Jan, 2012); + + if ( dt.ParseFormat(str, fmt, dtDef, &endParse) || + (!fmtAlt.empty() && dt.ParseFormat(str, fmtAlt, dtDef, &endParse)) ) { p += endParse - str.begin(); } @@ -326,7 +331,7 @@ wxString wxDateTime::Format(const wxString& formatp, const TimeZone& tz) const time_t time = GetTicks(); if ( (time != (time_t)-1) && !wxStrstr(format, wxT("%l")) -#ifdef __WXMSW__ +#ifdef __WINDOWS__ && !wxStrstr(format, wxT("%z")) #endif ) @@ -508,7 +513,7 @@ wxString wxDateTime::Format(const wxString& formatp, const TimeZone& tz) const // (indirectly) set the year correctly while ( (nLostWeekDays % 7) != 0 ) { - nLostWeekDays += year++ % 4 ? 1 : 2; + nLostWeekDays += (year++ % 4) ? 1 : 2; } // finally move the year below 2000 so that the 2-digit @@ -649,6 +654,18 @@ wxString wxDateTime::Format(const wxString& formatp, const TimeZone& tz) const case wxT('z'): // time zone as [-+]HHMM { int ofs = tz.GetOffset(); + + // The time zone offset does not include the DST, but + // we do need to take it into account when showing the + // time in the local time zone to the user. + if ( ofs == -wxGetTimeZone() && IsDST() == 1 ) + { + // FIXME: As elsewhere in wxDateTime, we assume + // that the DST is always 1 hour, but this is not + // true in general. + ofs += 3600; + } + if ( ofs < 0 ) { res += '-'; @@ -929,6 +946,26 @@ wxDateTime::ParseRfc822Date(const wxString& date, wxString::const_iterator *end) return true; } +const char* wxDateTime::ParseRfc822Date(const char* date) +{ + wxString::const_iterator end; + wxString dateStr(date); + if ( !ParseRfc822Date(dateStr, &end) ) + return NULL; + + return date + dateStr.IterOffsetInMBStr(end); +} + +const wchar_t* wxDateTime::ParseRfc822Date(const wchar_t* date) +{ + wxString::const_iterator end; + wxString dateStr(date); + if ( !ParseRfc822Date(dateStr, &end) ) + return NULL; + + return date + (end - dateStr.begin()); +} + bool wxDateTime::ParseFormat(const wxString& date, const wxString& format, @@ -1408,6 +1445,11 @@ wxDateTime::ParseFormat(const wxString& date, case wxT('z'): { + // check that we have something here at all + if ( input == end ) + return false; + + // and then check that it's either plus or minus sign bool minusFound; if ( *input == wxT('-') ) minusFound = true; @@ -1443,7 +1485,7 @@ wxDateTime::ParseFormat(const wxString& date, break; case wxT('%'): // a percent sign - if ( *input++ != wxT('%') ) + if ( input == end || *input++ != wxT('%') ) { // no match return false; @@ -1558,6 +1600,32 @@ wxDateTime::ParseFormat(const wxString& date, return true; } +const char* +wxDateTime::ParseFormat(const char* date, + const wxString& format, + const wxDateTime& dateDef) +{ + wxString::const_iterator end; + wxString dateStr(date); + if ( !ParseFormat(dateStr, format, dateDef, &end) ) + return NULL; + + return date + dateStr.IterOffsetInMBStr(end); +} + +const wchar_t* +wxDateTime::ParseFormat(const wchar_t* date, + const wxString& format, + const wxDateTime& dateDef) +{ + wxString::const_iterator end; + wxString dateStr(date); + if ( !ParseFormat(dateStr, format, dateDef, &end) ) + return NULL; + + return date + (end - dateStr.begin()); +} + bool wxDateTime::ParseDateTime(const wxString& date, wxString::const_iterator *end) { @@ -1611,6 +1679,26 @@ wxDateTime::ParseDateTime(const wxString& date, wxString::const_iterator *end) return true; } +const char* wxDateTime::ParseDateTime(const char* date) +{ + wxString::const_iterator end; + wxString dateStr(date); + if ( !ParseDateTime(dateStr, &end) ) + return NULL; + + return date + dateStr.IterOffsetInMBStr(end); +} + +const wchar_t* wxDateTime::ParseDateTime(const wchar_t* date) +{ + wxString::const_iterator end; + wxString dateStr(date); + if ( !ParseDateTime(dateStr, &end) ) + return NULL; + + return date + (end - dateStr.begin()); +} + bool wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) { @@ -1648,12 +1736,12 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) if ( len > lenRest ) continue; - const wxString::const_iterator pEnd = p + len; - if ( wxString(p, pEnd).CmpNoCase(dateStr) == 0 ) + const wxString::const_iterator pEndStr = p + len; + if ( wxString(p, pEndStr).CmpNoCase(dateStr) == 0 ) { // nothing can follow this, so stop here - p = pEnd; + p = pEndStr; int dayDiffFromToday = literalDates[n].dayDiffFromToday; *this = Today(); @@ -1662,7 +1750,7 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) *this += wxDateSpan::Days(dayDiffFromToday); } - *end = pEnd; + *end = pEndStr; return true; } @@ -1968,6 +2056,26 @@ wxDateTime::ParseDate(const wxString& date, wxString::const_iterator *end) return true; } +const char* wxDateTime::ParseDate(const char* date) +{ + wxString::const_iterator end; + wxString dateStr(date); + if ( !ParseDate(dateStr, &end) ) + return NULL; + + return date + dateStr.IterOffsetInMBStr(end); +} + +const wchar_t* wxDateTime::ParseDate(const wchar_t* date) +{ + wxString::const_iterator end; + wxString dateStr(date); + if ( !ParseDate(dateStr, &end) ) + return NULL; + + return date + (end - dateStr.begin()); +} + bool wxDateTime::ParseTime(const wxString& time, wxString::const_iterator *end) { @@ -2008,6 +2116,8 @@ wxDateTime::ParseTime(const wxString& time, wxString::const_iterator *end) "%H:%M:%S", // could be the same or 24 hour one so try it too "%I:%M %p", // 12hour with AM/PM but without seconds "%H:%M", // and a possibly 24 hour version without seconds + "%I %p", // just hour with AM/AM + "%H", // just hour in 24 hour version "%X", // possibly something from above or maybe something // completely different -- try it last @@ -2023,6 +2133,26 @@ wxDateTime::ParseTime(const wxString& time, wxString::const_iterator *end) return false; } +const char* wxDateTime::ParseTime(const char* date) +{ + wxString::const_iterator end; + wxString dateStr(date); + if ( !ParseTime(dateStr, &end) ) + return NULL; + + return date + dateStr.IterOffsetInMBStr(end); +} + +const wchar_t* wxDateTime::ParseTime(const wchar_t* date) +{ + wxString::const_iterator end; + wxString dateStr(date); + if ( !ParseTime(dateStr, &end) ) + return NULL; + + return date + (end - dateStr.begin()); +} + // ---------------------------------------------------------------------------- // Workdays and holidays support // ----------------------------------------------------------------------------