X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c4e1b7f244ce0187974d9a19d540733921add9b2..e0c5c96f67eaf13ccbd378616bdf98c8dba01e78:/src/common/datetime.cpp diff --git a/src/common/datetime.cpp b/src/common/datetime.cpp index 7dc3120194..9c14cc43db 100644 --- a/src/common/datetime.cpp +++ b/src/common/datetime.cpp @@ -13,7 +13,7 @@ // so long as the above copyright and this permission statement // are retained in all copies. // -// Licence: wxWindows license +// Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// /* @@ -52,7 +52,7 @@ // headers // ---------------------------------------------------------------------------- -#ifdef __GNUG__ +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "datetime.h" #endif @@ -75,18 +75,36 @@ #include "wx/tokenzr.h" #include "wx/module.h" -#define wxDEFINE_TIME_CONSTANTS // before including datetime.h - #include #include "wx/datetime.h" -#include "wx/timer.h" // for wxGetLocalTimeMillis() +#include "wx/stopwatch.h" // for wxGetLocalTimeMillis() + +const long wxDateTime::TIME_T_FACTOR = 1000l; + +#if wxUSE_EXTENDED_RTTI + +template<> void wxStringReadValue(const wxString &s , wxDateTime &data ) +{ + data.ParseFormat(s,wxT("%Y-%m-%d %H:%M:%S")) ; +} + +template<> void wxStringWriteValue(wxString &s , const wxDateTime &data ) +{ + s = data.Format(wxT("%Y-%m-%d %H:%M:%S")) ; +} + +wxCUSTOM_TYPE_INFO(wxDateTime, wxToStringConverter , wxFromStringConverter) + +#endif +// // ---------------------------------------------------------------------------- // conditional compilation // ---------------------------------------------------------------------------- -#if defined(HAVE_STRPTIME) && defined(__LINUX__) +#if defined(HAVE_STRPTIME) && defined(__GLIBC__) && \ + ((__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0)) // glibc 2.0.7 strptime() is broken - the following snippet causes it to // crash (instead of just failing): // @@ -97,13 +115,17 @@ #undef HAVE_STRPTIME #endif // broken strptime() -#ifndef WX_TIMEZONE +#if defined(__MWERKS__) && wxUSE_UNICODE + #include +#endif + +#if !defined(WX_TIMEZONE) && !defined(WX_GMTOFF_IN_TM) #if defined(__BORLANDC__) || defined(__MINGW32__) || defined(__VISAGECPP__) #define WX_TIMEZONE _timezone #elif defined(__MWERKS__) long wxmw_timezone = 28800; #define WX_TIMEZONE wxmw_timezone - #elif defined(__DJGPP__) + #elif defined(__DJGPP__) || defined(__WINE__) #include #include static long wxGetTimeZone() @@ -118,10 +140,12 @@ return timezone; } #define WX_TIMEZONE wxGetTimeZone() + #elif defined(__DARWIN__) + #define WX_GMTOFF_IN_TM #else // unknown platform - try timezone #define WX_TIMEZONE timezone #endif -#endif // !WX_TIMEZONE +#endif // !WX_TIMEZONE && !WX_GMTOFF_IN_TM // ---------------------------------------------------------------------------- // macros @@ -153,7 +177,7 @@ public: virtual void OnExit() { wxDateTimeHolidayAuthority::ClearAllAuthorities(); - wxDateTimeHolidayAuthority::ms_authorities.Clear(); + wxDateTimeHolidayAuthority::ms_authorities.clear(); } private: @@ -211,23 +235,10 @@ static const wxDateTime::wxDateTime_t gs_cumulatedDays[2][MONTHS_IN_YEAR] = // in the fine tradition of ANSI C we use our equivalent of (time_t)-1 to // indicate an invalid wxDateTime object - -static const wxDateTime gs_dtDefault; - -const wxDateTime& wxDefaultDateTime = gs_dtDefault; +const wxDateTime wxDefaultDateTime; wxDateTime::Country wxDateTime::ms_country = wxDateTime::Country_Unknown; -// ---------------------------------------------------------------------------- -// private globals -// ---------------------------------------------------------------------------- - -// a critical section is needed to protect GetTimeZone() static -// variable in MT case -#if wxUSE_THREADS - static wxCriticalSection gs_critsectTimezone; -#endif // wxUSE_THREADS - // ---------------------------------------------------------------------------- // private functions // ---------------------------------------------------------------------------- @@ -259,25 +270,36 @@ wxDateTime::wxDateTime_t GetNumOfDaysInMonth(int year, wxDateTime::Month month) return daysInMonth[wxDateTime::IsLeapYear(year)][month]; } -// ensure that the timezone variable is set by calling localtime +// returns the time zone in the C sense, i.e. the difference UTC - local +// (in seconds) static int GetTimeZone() { +#ifdef WX_GMTOFF_IN_TM // set to TRUE when the timezone is set static bool s_timezoneSet = FALSE; + static long gmtoffset = LONG_MAX; // invalid timezone - wxCRIT_SECT_LOCKER(lock, gs_critsectTimezone); - + // ensure that the timezone variable is set by calling localtime if ( !s_timezoneSet ) { // just call localtime() instead of figuring out whether this system // supports tzset(), _tzset() or something else - time_t t = 0; + time_t t = 0; + struct tm *tm; - (void)localtime(&t); + tm = localtime(&t); s_timezoneSet = TRUE; + + // note that GMT offset is the opposite of time zone and so to return + // consistent results in both WX_GMTOFF_IN_TM and !WX_GMTOFF_IN_TM + // cases we have to negate it + gmtoffset = -tm->tm_gmtoff; } + return (int)gmtoffset; +#else // !WX_GMTOFF_IN_TM return (int)WX_TIMEZONE; +#endif // WX_GMTOFF_IN_TM/!WX_GMTOFF_IN_TM } // return the integral part of the JDN for the midnight of the given date (to @@ -320,7 +342,7 @@ static long GetTruncatedJDN(wxDateTime::wxDateTime_t day, - JDN_OFFSET; } -// this function is a wrapper around strftime(3) +// this function is a wrapper around strftime(3) adding error checking static wxString CallStrftime(const wxChar *format, const tm* tm) { wxChar buf[4096]; @@ -333,6 +355,41 @@ static wxString CallStrftime(const wxChar *format, const tm* tm) return wxString(buf); } +#ifdef HAVE_STRPTIME + +// glibc2 doesn't define this in the headers unless _XOPEN_SOURCE is defined +// which, unfortunately, wreaks havoc elsewhere +#if defined(__GLIBC__) && (__GLIBC__ == 2) + extern "C" char *strptime(const char *, const char *, struct tm *); +#endif + +// Unicode-friendly strptime() wrapper +static const wxChar * +CallStrptime(const wxChar *input, const char *fmt, tm *tm) +{ + // the problem here is that strptime() returns pointer into the string we + // passed to it while we're really interested in the pointer into the + // original, Unicode, string so we try to transform the pointer back +#if wxUSE_UNICODE + wxCharBuffer inputMB(wxConvertWX2MB(input)); +#else // ASCII + const char * const inputMB = input; +#endif // Unicode/Ascii + + const char *result = strptime(inputMB, fmt, tm); + if ( !result ) + return NULL; + +#if wxUSE_UNICODE + // FIXME: this is wrong in presence of surrogates &c + return input + (result - inputMB.data()); +#else // ASCII + return result; +#endif // Unicode/Ascii +} + +#endif // HAVE_STRPTIME + // if year and/or month have invalid values, replace them with the current ones static void ReplaceDefaultYearMonthWithCurrent(int *year, wxDateTime::Month *month) @@ -782,14 +839,29 @@ void wxDateTime::GetAmPmStrings(wxString *am, wxString *pm) { tm tm; InitTm(tm); + wxChar buffer[64]; + // @Note: Do not call 'CallStrftime' here! CallStrftime checks the return code + // and causes an assertion failed if the buffer is to small (which is good) - OR - + // if strftime does not return anything because the format string is invalid - OR - + // if there are no 'am' / 'pm' tokens defined for the current locale (which is not good). + // wxDateTime::ParseTime will try several different formats to parse the time. + // As a result, GetAmPmStrings might get called, even if the current locale + // does not define any 'am' / 'pm' tokens. In this case, wxStrftime would + // assert, even though it is a perfectly legal use. if ( am ) { - *am = CallStrftime(_T("%p"), &tm); + if (wxStrftime(buffer, sizeof buffer, _T("%p"), &tm) > 0) + *am = wxString(buffer); + else + *am = wxString(); } if ( pm ) { tm.tm_hour = 13; - *pm = CallStrftime(_T("%p"), &tm); + if (wxStrftime(buffer, sizeof buffer, _T("%p"), &tm) > 0) + *pm = wxString(buffer); + else + *pm = wxString(); } } @@ -1237,6 +1309,83 @@ wxDateTime& wxDateTime::ResetTime() return *this; } +// ---------------------------------------------------------------------------- +// DOS Date and Time Format functions +// ---------------------------------------------------------------------------- +// the dos date and time value is an unsigned 32 bit value in the format: +// YYYYYYYMMMMDDDDDhhhhhmmmmmmsssss +// +// Y = year offset from 1980 (0-127) +// M = month (1-12) +// D = day of month (1-31) +// h = hour (0-23) +// m = minute (0-59) +// s = bisecond (0-29) each bisecond indicates two seconds +// ---------------------------------------------------------------------------- + +wxDateTime& wxDateTime::SetFromDOS(unsigned long ddt) +{ + struct tm tm; + InitTm(tm); + + long year = ddt & 0xFE000000; + year >>= 25; + year += 80; + tm.tm_year = year; + + long month = ddt & 0x1E00000; + month >>= 21; + month -= 1; + tm.tm_mon = month; + + long day = ddt & 0x1F0000; + day >>= 16; + tm.tm_mday = day; + + long hour = ddt & 0xF800; + hour >>= 11; + tm.tm_hour = hour; + + long minute = ddt & 0x7E0; + minute >>= 5; + tm.tm_min = minute; + + long second = ddt & 0x1F; + tm.tm_sec = second * 2; + + return Set(mktime(&tm)); +} + +unsigned long wxDateTime::GetAsDOS() const +{ + unsigned long ddt; + time_t ticks = GetTicks(); + struct tm *tm = localtime(&ticks); + + long year = tm->tm_year; + year -= 80; + year <<= 25; + + long month = tm->tm_mon; + month += 1; + month <<= 21; + + long day = tm->tm_mday; + day <<= 16; + + long hour = tm->tm_hour; + hour <<= 11; + + long minute = tm->tm_min; + minute <<= 5; + + long second = tm->tm_sec; + second /= 2; + + ddt = year | month | day | hour | minute | second; + return ddt; +} + // ---------------------------------------------------------------------------- // time_t <-> broken down time conversions // ---------------------------------------------------------------------------- @@ -1484,13 +1633,18 @@ wxDateTime& wxDateTime::Add(const wxDateSpan& diff) // Weekday and monthday stuff // ---------------------------------------------------------------------------- -bool wxDateTime::SetToTheWeek(wxDateTime_t numWeek, WeekDay weekday) +bool wxDateTime::SetToTheWeek(wxDateTime_t numWeek, + WeekDay weekday, + WeekFlags flags) { + wxASSERT_MSG( numWeek > 0, + _T("invalid week number: weeks are counted from 1") ); + int year = GetYear(); // Jan 4 always lies in the 1st week of the year Set(4, Jan, year); - SetToWeekDayInSameWeek(weekday) += wxDateSpan::Weeks(numWeek); + SetToWeekDayInSameWeek(weekday, flags) += wxDateSpan::Weeks(numWeek - 1); if ( GetYear() != year ) { @@ -1513,17 +1667,34 @@ wxDateTime& wxDateTime::SetToLastMonthDay(Month month, return Set(GetNumOfDaysInMonth(year, month), month, year); } -wxDateTime& wxDateTime::SetToWeekDayInSameWeek(WeekDay weekday) +wxDateTime& wxDateTime::SetToWeekDayInSameWeek(WeekDay weekday, WeekFlags flags) { wxDATETIME_CHECK( weekday != Inv_WeekDay, _T("invalid weekday") ); - WeekDay wdayThis = GetWeekDay(); + int wdayThis = GetWeekDay(); if ( weekday == wdayThis ) { // nothing to do return *this; } - else if ( weekday < wdayThis ) + + if ( flags == Default_First ) + { + flags = GetCountry() == USA ? Sunday_First : Monday_First; + } + + // the logic below based on comparing weekday and wdayThis works if Sun (0) + // is the first day in the week, but breaks down for Monday_First case so + // we adjust the week days in this case + if( flags == Monday_First ) + { + if ( wdayThis == Sun ) + wdayThis += 7; + } + //else: Sunday_First, nothing to do + + // go forward or back in time to the day we want + if ( weekday < wdayThis ) { return Subtract(wxDateSpan::Days(wdayThis - weekday)); } @@ -1716,7 +1887,7 @@ wxDateTime& wxDateTime::SetToYearDay(wxDateTime::wxDateTime_t yday) // for Dec, we can't compare with gs_cumulatedDays[mon + 1], but we // don't need it neither - because of the CHECK above we know that // yday lies in December then - if ( (mon == Dec) || (yday < gs_cumulatedDays[isLeap][mon + 1]) ) + if ( (mon == Dec) || (yday <= gs_cumulatedDays[isLeap][mon + 1]) ) { Set(yday - gs_cumulatedDays[isLeap][mon], mon, year); @@ -1891,6 +2062,10 @@ wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const fmt = _T("%03d"); break; + case _T('w'): // week day as number has only one + fmt = _T("%d"); + break; + default: // it's either another valid format specifier in which case // the format is "%02d" (for all the rest) or we have the @@ -2528,12 +2703,36 @@ const wxChar *wxDateTime::ParseFormat(const wxChar *date, // parse the optional width size_t width = 0; - while ( isdigit(*++fmt) ) + while ( wxIsdigit(*++fmt) ) { width *= 10; width += *fmt - _T('0'); } + // the default widths for the various fields + if ( !width ) + { + switch ( *fmt ) + { + case _T('Y'): // year has 4 digits + width = 4; + break; + + case _T('j'): // day of year has 3 digits + case _T('l'): // milliseconds have 3 digits + width = 3; + break; + + case _T('w'): // week day as number has only one + width = 1; + break; + + default: + // default for all other fields + width = 2; + } + } + // then the format itself switch ( *fmt ) { @@ -2682,6 +2881,8 @@ const wxChar *wxDateTime::ParseFormat(const wxChar *date, wxString am, pm, token = GetAlphaToken(input); GetAmPmStrings(&am, &pm); + if (am.IsEmpty() && pm.IsEmpty()) + return (wxChar *)NULL; // no am/pm strings defined if ( token.CmpNoCase(pm) == 0 ) { isPM = TRUE; @@ -2773,12 +2974,13 @@ const wxChar *wxDateTime::ParseFormat(const wxChar *date, case _T('x'): // locale default date representation #ifdef HAVE_STRPTIME - // try using strptime() - it may fail even if the input is + // try using strptime() -- it may fail even if the input is // correct but the date is out of range, so we will fall back - // to our generic code anyhow (FIXME !Unicode friendly) + // to our generic code anyhow { struct tm tm; - const wxChar *result = strptime(input, "%x", &tm); + + const wxChar *result = CallStrptime(input, "%x", &tm); if ( result ) { input = result; @@ -2843,7 +3045,7 @@ const wxChar *wxDateTime::ParseFormat(const wxChar *date, { // use strptime() to do it for us (FIXME !Unicode friendly) struct tm tm; - input = strptime(input, "%X", &tm); + input = CallStrptime(input, "%X", &tm); if ( !input ) { return (wxChar *)NULL; @@ -3018,6 +3220,14 @@ const wxChar *wxDateTime::ParseFormat(const wxChar *date, Set(tm); + // finally check that the week day is consistent -- if we had it + if ( haveWDay && GetWeekDay() != wday ) + { + wxLogDebug(_T("inconsistsnet week day in wxDateTime::ParseFormat()")); + + return NULL; + } + return input; } @@ -3025,11 +3235,48 @@ const wxChar *wxDateTime::ParseDateTime(const wxChar *date) { wxCHECK_MSG( date, (wxChar *)NULL, _T("NULL pointer in wxDateTime::Parse") ); - // there is a public domain version of getdate.y, but it only works for - // English... - wxFAIL_MSG(_T("TODO")); + // Set to current day and hour, so strings like '14:00' becomes today at + // 14, not some other random date + wxDateTime dtDate = wxDateTime::Today(); + wxDateTime dtTime = wxDateTime::Today(); + + const wxChar* pchTime; + + // Try to parse the beginning of the string as a date + const wxChar* pchDate = dtDate.ParseDate(date); + + // We got a date in the beginning, see if there is a time specified after the date + if ( pchDate ) + { + // Skip spaces, as the ParseTime() function fails on spaces + while ( wxIsspace(*pchDate) ) + pchDate++; + + pchTime = dtTime.ParseTime(pchDate); + } + else // no date in the beginning + { + // check and see if we have a time followed by a date + pchTime = dtTime.ParseTime(date); + if ( pchTime ) + { + while ( wxIsspace(*pchTime) ) + pchTime++; + + pchDate = dtDate.ParseDate(pchTime); + } + } + + // If we have a date specified, set our own data to the same date + if ( !pchDate || !pchTime ) + return NULL; - return (wxChar *)NULL; + Set(dtDate.GetDay(), dtDate.GetMonth(), dtDate.GetYear(), + dtTime.GetHour(), dtTime.GetMinute(), dtTime.GetSecond(), + dtTime.GetMillisecond()); + + // Return endpoint of scan + return pchDate > pchTime ? pchDate : pchTime; } const wxChar *wxDateTime::ParseDate(const wxChar *date) @@ -3095,7 +3342,7 @@ const wxChar *wxDateTime::ParseDate(const wxChar *date) // tokenize the string size_t nPosCur = 0; - static const wxChar *dateDelimiters = _T(".,/-\t\n "); + static const wxChar *dateDelimiters = _T(".,/-\t\r\n "); wxStringTokenizer tok(p, dateDelimiters); while ( tok.HasMoreTokens() ) { @@ -3120,18 +3367,27 @@ const wxChar *wxDateTime::ParseDate(const wxChar *date) } else // not the month { - wxDateTime_t maxDays = haveMon - ? GetNumOfDaysInMonth(haveYear ? year : Inv_Year, mon) - : 31; - - // can it be day? - if ( (val == 0) || (val > (unsigned long)maxDays) ) // cast to shut up compiler warning in BCC + if ( haveDay ) { + // this can only be the year isYear = TRUE; } - else + else // may be either day or year { - isDay = TRUE; + wxDateTime_t maxDays = haveMon + ? GetNumOfDaysInMonth(haveYear ? year : Inv_Year, mon) + : 31; + + // can it be day? + if ( (val == 0) || (val > (unsigned long)maxDays) ) + { + // no + isYear = TRUE; + } + else // yes, suppose it's the day + { + isDay = TRUE; + } } } @@ -3294,8 +3550,7 @@ const wxChar *wxDateTime::ParseDate(const wxChar *date) mon = (wxDateTime::Month)(day - 1); // we're in the current year then - if ( (year > 0) && - (unsigned)year <= GetNumOfDaysInMonth(Inv_Year, mon) ) + if ( (year > 0) && (year <= (int)GetNumOfDaysInMonth(Inv_Year, mon)) ) { day = year; @@ -3382,7 +3637,8 @@ const wxChar *wxDateTime::ParseTime(const wxChar *time) size_t len = timeString.length(); if ( timeString.CmpNoCase(wxString(time, len)) == 0 ) { - Set(stdTimes[n].hour, 0, 0); + // casts required by DigitalMars + Set(stdTimes[n].hour, wxDateTime_t(0), wxDateTime_t(0)); return time + len; } @@ -3636,7 +3892,7 @@ wxHolidayAuthoritiesArray wxDateTimeHolidayAuthority::ms_authorities; /* static */ bool wxDateTimeHolidayAuthority::IsHoliday(const wxDateTime& dt) { - size_t count = ms_authorities.GetCount(); + size_t count = ms_authorities.size(); for ( size_t n = 0; n < count; n++ ) { if ( ms_authorities[n]->DoIsHoliday(dt) ) @@ -3656,9 +3912,9 @@ wxDateTimeHolidayAuthority::GetHolidaysInRange(const wxDateTime& dtStart, { wxDateTimeArray hol; - holidays.Empty(); + holidays.Clear(); - size_t count = ms_authorities.GetCount(); + size_t count = ms_authorities.size(); for ( size_t nAuth = 0; nAuth < count; nAuth++ ) { ms_authorities[nAuth]->DoGetHolidaysInRange(dtStart, dtEnd, hol); @@ -3668,7 +3924,7 @@ wxDateTimeHolidayAuthority::GetHolidaysInRange(const wxDateTime& dtStart, holidays.Sort(wxDateTimeCompareFunc); - return holidays.GetCount(); + return holidays.size(); } /* static */ @@ -3680,7 +3936,12 @@ void wxDateTimeHolidayAuthority::ClearAllAuthorities() /* static */ void wxDateTimeHolidayAuthority::AddAuthority(wxDateTimeHolidayAuthority *auth) { - ms_authorities.Add(auth); + ms_authorities.push_back(auth); +} + +wxDateTimeHolidayAuthority::~wxDateTimeHolidayAuthority() +{ + // nothing to do here } // ----------------------------------------------------------------------------