X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5e3566ffafa9c2c564ee2100a1202aad0646b397..94e0018723919fe2ca2f5b5f0a42804c16dbf3a1:/src/common/datetime.cpp diff --git a/src/common/datetime.cpp b/src/common/datetime.cpp index 827cb51a22..49bfa98e1a 100644 --- a/src/common/datetime.cpp +++ b/src/common/datetime.cpp @@ -117,54 +117,18 @@ wxCUSTOM_TYPE_INFO(wxDateTime, wxToStringConverter , wxFromStringCon #include #endif -#if !defined(WX_TIMEZONE) && !defined(WX_GMTOFF_IN_TM) - #if defined(__WXPALMOS__) - #define WX_GMTOFF_IN_TM - #elif 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__) || defined(__WINE__) - #include - #include - static long wxGetTimeZone() - { - struct timeb tb; - ftime(&tb); - return tb.timezone; - } - #define WX_TIMEZONE wxGetTimeZone() - #elif defined(__DARWIN__) +#if defined(__DJGPP__) || defined(__WINE__) + #include + #include +#endif + +#ifndef WX_GMTOFF_IN_TM + // Define it for some systems which don't (always) use configure but are + // known to have tm_gmtoff field. + #if defined(__WXPALMOS__) || defined(__DARWIN__) #define WX_GMTOFF_IN_TM - #elif defined(__WXWINCE__) && defined(__VISUALC8__) - // _timezone is not present in dynamic run-time library - #if 0 - // Solution (1): use the function equivalent of _timezone - static long wxGetTimeZone() - { - long t; - _get_timezone(& t); - return t; - } - #define WX_TIMEZONE wxGetTimeZone() - #elif 1 - // Solution (2): using GetTimeZoneInformation - static long wxGetTimeZone() - { - TIME_ZONE_INFORMATION tzi; - ::GetTimeZoneInformation(&tzi); - return tzi.Bias; // x 60 - } - #define WX_TIMEZONE wxGetTimeZone() - #else - // Old method using _timezone: this symbol doesn't exist in the dynamic run-time library (i.e. using /MD) - #define WX_TIMEZONE _timezone - #endif - #else // unknown platform - try timezone - #define WX_TIMEZONE timezone #endif -#endif // !WX_TIMEZONE && !WX_GMTOFF_IN_TM +#endif // NB: VC8 safe time functions could/should be used for wxMSW as well probably #if defined(__WXWINCE__) && defined(__VISUALC8__) @@ -281,8 +245,6 @@ static const int SEC_PER_MIN = 60; static const int MIN_PER_HOUR = 60; -static const int HOURS_PER_DAY = 24; - static const long SECONDS_PER_DAY = 86400l; static const int DAYS_PER_WEEK = 7; @@ -323,8 +285,8 @@ const long wxDateTime::TIME_T_FACTOR = 1000l; // global data // ---------------------------------------------------------------------------- -const char *wxDefaultDateTimeFormat = "%c"; -const char *wxDefaultTimeSpanFormat = "%H:%M:%S"; +const char wxDefaultDateTimeFormat[] = "%c"; +const char wxDefaultTimeSpanFormat[] = "%H:%M:%S"; // in the fine tradition of ANSI C we use our equivalent of (time_t)-1 to // indicate an invalid wxDateTime object @@ -356,7 +318,7 @@ 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] = + static const 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 } @@ -392,8 +354,33 @@ int GetTimeZone() gmtoffset = -tm.tm_gmtoff; } return (int)gmtoffset; -#else // !WX_GMTOFF_IN_TM +#elif defined(__DJGPP__) || defined(__WINE__) + struct timeb tb; + ftime(&tb); + return tb.timezone*60; +#elif defined(__VISUALC__) + // We must initialize the time zone information before using it (this will + // be done only once internally). + _tzset(); + + // Starting with VC++ 8 timezone variable is deprecated and is not even + // available in some standard library version so use the new function for + // accessing it instead. + #if wxCHECK_VISUALC_VERSION(8) + long t; + _get_timezone(&t); + return t; + #else // VC++ < 8 + return timezone; + #endif +#elif defined(WX_TIMEZONE) // If WX_TIMEZONE was defined by configure, use it. return WX_TIMEZONE; +#elif defined(__BORLANDC__) || defined(__MINGW32__) || defined(__VISAGECPP__) + return _timezone; +#elif defined(__MWERKS__) + return 28800; +#else // unknown platform -- assume it has timezone + return timezone; #endif // WX_GMTOFF_IN_TM/!WX_GMTOFF_IN_TM } @@ -510,8 +497,12 @@ wxDateTime::Tm::Tm() { year = (wxDateTime_t)wxDateTime::Inv_Year; mon = wxDateTime::Inv_Month; - mday = 0; - hour = min = sec = msec = 0; + mday = + yday = 0; + hour = + min = + sec = + msec = 0; wday = wxDateTime::Inv_WeekDay; } @@ -531,9 +522,17 @@ wxDateTime::Tm::Tm(const struct tm& tm, const TimeZone& tz) bool wxDateTime::Tm::IsValid() const { + if ( mon == wxDateTime::Inv_Month ) + return false; + + // We need to check this here to avoid crashing in GetNumOfDaysInMonth() if + // somebody passed us "(wxDateTime::Month)1000". + wxCHECK_MSG( mon >= wxDateTime::Jan && mon < wxDateTime::Inv_Month, false, + wxS("Invalid month value") ); + // we allow for the leap seconds, although we don't use them (yet) return (year != wxDateTime::Inv_Year) && (mon != wxDateTime::Inv_Month) && - (mday <= GetNumOfDaysInMonth(year, mon)) && + (mday > 0 && mday <= GetNumOfDaysInMonth(year, mon)) && (hour < 24) && (min < 60) && (sec < 62) && (msec < 1000); } @@ -820,7 +819,7 @@ wxString wxDateTime::GetEnglishMonthName(Month month, NameFlags flags) { wxCHECK_MSG( month != Inv_Month, wxEmptyString, "invalid month" ); - static const char *monthNames[2][MONTHS_IN_YEAR] = + static const char *const monthNames[2][MONTHS_IN_YEAR] = { { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }, @@ -859,7 +858,7 @@ wxString wxDateTime::GetEnglishWeekDayName(WeekDay wday, NameFlags flags) { wxCHECK_MSG( wday != Inv_WeekDay, wxEmptyString, wxT("invalid weekday") ); - static const char *weekdayNames[2][DAYS_PER_WEEK] = + static const char *const weekdayNames[2][DAYS_PER_WEEK] = { { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }, @@ -1614,6 +1613,7 @@ wxDateTime::Tm wxDateTime::GetTm(const TimeZone& tz) const // construct Tm from these values Tm tm; tm.year = (int)year; + tm.yday = (wxDateTime_t)(dayOfYear - 1); // use C convention for day number tm.mon = (Month)(month - 1); // algorithm yields 1 for January, not 0 tm.mday = (wxDateTime_t)day; tm.msec = (wxDateTime_t)(timeOnly % 1000); @@ -2054,16 +2054,22 @@ wxDateTime::wxDateTime_t wxDateTime::GetWeekOfMonth(wxDateTime::WeekFlags flags, const TimeZone& tz) const { Tm tm = GetTm(tz); - wxDateTime dtMonthStart = wxDateTime(1, tm.mon, tm.year); - int nWeek = GetWeekOfYear(flags) - dtMonthStart.GetWeekOfYear(flags) + 1; - if ( nWeek < 0 ) + const wxDateTime dateFirst = wxDateTime(1, tm.mon, tm.year); + const wxDateTime::WeekDay wdFirst = dateFirst.GetWeekDay(); + + if ( flags == Default_First ) { - // this may happen for January when Jan, 1 is the last week of the - // previous year - nWeek += IsLeapYear(tm.year - 1) ? 53 : 52; + flags = GetCountry() == USA ? Sunday_First : Monday_First; } - return (wxDateTime::wxDateTime_t)nWeek; + // compute offset of dateFirst from the beginning of the week + int firstOffset; + if ( flags == Sunday_First ) + firstOffset = wdFirst - Sun; + else + firstOffset = wdFirst == Sun ? DAYS_PER_WEEK - 1 : wdFirst - Mon; + + return (wxDateTime::wxDateTime_t)((tm.mday - 1 + firstOffset)/7 + 1); } wxDateTime& wxDateTime::SetToYearDay(wxDateTime::wxDateTime_t yday) @@ -2343,6 +2349,14 @@ wxDateTime& wxDateTime::SetFromMSWSysTime(const SYSTEMTIME& st) st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); } +wxDateTime& wxDateTime::SetFromMSWSysDate(const SYSTEMTIME& st) +{ + return Set(st.wDay, + static_cast(wxDateTime::Jan + st.wMonth - 1), + st.wYear, + 0, 0, 0, 0); +} + void wxDateTime::GetAsMSWSysTime(SYSTEMTIME* st) const { const wxDateTime::Tm tm(GetTm()); @@ -2357,6 +2371,22 @@ void wxDateTime::GetAsMSWSysTime(SYSTEMTIME* st) const st->wSecond = tm.sec; st->wMilliseconds = tm.msec; } + +void wxDateTime::GetAsMSWSysDate(SYSTEMTIME* st) const +{ + const wxDateTime::Tm tm(GetTm()); + + st->wYear = (WXWORD)tm.year; + st->wMonth = (WXWORD)(tm.mon - wxDateTime::Jan + 1); + st->wDay = tm.mday; + + st->wDayOfWeek = + st->wHour = + st->wMinute = + st->wSecond = + st->wMilliseconds = 0; +} + #endif // __WXMSW__ #endif // wxUSE_DATETIME