- // this is a simplified version of ParseDateTime() which understands only
- // "today" (for wxDate compatibility) and digits only otherwise (and not
- // all esoteric constructions ParseDateTime() knows about)
-
- wxCHECK_MSG( date, (wxChar *)NULL, _T("NULL pointer in wxDateTime::Parse") );
-
- const wxChar *p = date;
- while ( wxIsspace(*p) )
- p++;
-
- // some special cases
- static struct
- {
- const wxChar *str;
- int dayDiffFromToday;
- } literalDates[] =
- {
- { wxTRANSLATE("today"), 0 },
- { wxTRANSLATE("yesterday"), -1 },
- { wxTRANSLATE("tomorrow"), 1 },
- };
-
- for ( size_t n = 0; n < WXSIZEOF(literalDates); n++ )
- {
- wxString date = wxGetTranslation(literalDates[n].str);
- size_t len = date.length();
- if ( wxStrlen(p) >= len && (wxString(p, len).CmpNoCase(date) == 0) )
- {
- // nothing can follow this, so stop here
- p += len;
-
- int dayDiffFromToday = literalDates[n].dayDiffFromToday;
- *this = Today();
- if ( dayDiffFromToday )
- {
- *this += wxDateSpan::Days(dayDiffFromToday);
- }
-
- return p;
- }
- }
-
- // We try to guess what we have here: for each new (numeric) token, we
- // determine if it can be a month, day or a year. Of course, there is an
- // ambiguity as some numbers may be days as well as months, so we also
- // have the ability to back track.
-
- // what do we have?
- bool haveDay = FALSE, // the months day?
- haveWDay = FALSE, // the day of week?
- haveMon = FALSE, // the month?
- haveYear = FALSE; // the year?
-
- // and the value of the items we have (init them to get rid of warnings)
- WeekDay wday = Inv_WeekDay;
- wxDateTime_t day = 0;
- wxDateTime::Month mon = Inv_Month;
- int year = 0;
-
- // tokenize the string
- size_t nPosCur = 0;
- static const wxChar *dateDelimiters = _T(".,/-\t\n ");
- wxStringTokenizer tok(p, dateDelimiters);
- while ( tok.HasMoreTokens() )
- {
- wxString token = tok.GetNextToken();
- if ( !token )
- continue;
-
- // is it a number?
- unsigned long val;
- if ( token.ToULong(&val) )
- {
- // guess what this number is
-
- bool isDay = FALSE,
- isMonth = FALSE,
- isYear = FALSE;
-
- if ( !haveMon && val > 0 && val <= 12 )
- {
- // assume it is month
- isMonth = TRUE;
- }
- else // not the month
- {
- wxDateTime_t maxDays = haveMon
- ? GetNumOfDaysInMonth(haveYear ? year : Inv_Year, mon)
- : 31;
-
- // can it be day?
- if ( (val == 0) || (val > maxDays) )
- {
- isYear = TRUE;
- }
- else
- {
- isDay = TRUE;
- }
- }
-
- if ( isYear )
- {
- if ( haveYear )
- break;
-
- haveYear = TRUE;
-
- year = (wxDateTime_t)val;
- }
- else if ( isDay )
- {
- if ( haveDay )
- break;
-
- haveDay = TRUE;
-
- day = (wxDateTime_t)val;
- }
- else if ( isMonth )
- {
- haveMon = TRUE;
-
- mon = (Month)(val - 1);
- }
- }
- else // not a number
- {
- // be careful not to overwrite the current mon value
- Month mon2 = GetMonthFromName(token, Name_Full | Name_Abbr);
- if ( mon2 != Inv_Month )
- {
- // it's a month
- if ( haveMon )
- {
- // but we already have a month - maybe we guessed wrong?
- if ( !haveDay )
- {
- // no need to check in month range as always < 12, but
- // the days are counted from 1 unlike the months
- day = (wxDateTime_t)mon + 1;
- haveDay = TRUE;
- }
- else
- {
- // could possible be the year (doesn't the year come
- // before the month in the japanese format?) (FIXME)
- break;
- }
- }
-
- mon = mon2;
-
- haveMon = TRUE;
- }
- else // not a valid month name
- {
- wday = GetWeekDayFromName(token, Name_Full | Name_Abbr);
- if ( wday != Inv_WeekDay )
- {
- // a week day
- if ( haveWDay )
- {
- break;
- }
-
- haveWDay = TRUE;
- }
- else // not a valid weekday name
- {
- // try the ordinals
- static const wxChar *ordinals[] =
- {
- wxTRANSLATE("first"),
- wxTRANSLATE("second"),
- wxTRANSLATE("third"),
- wxTRANSLATE("fourth"),
- wxTRANSLATE("fifth"),
- wxTRANSLATE("sixth"),
- wxTRANSLATE("seventh"),
- wxTRANSLATE("eighth"),
- wxTRANSLATE("ninth"),
- wxTRANSLATE("tenth"),
- wxTRANSLATE("eleventh"),
- wxTRANSLATE("twelfth"),
- wxTRANSLATE("thirteenth"),
- wxTRANSLATE("fourteenth"),
- wxTRANSLATE("fifteenth"),
- wxTRANSLATE("sixteenth"),
- wxTRANSLATE("seventeenth"),
- wxTRANSLATE("eighteenth"),
- wxTRANSLATE("nineteenth"),
- wxTRANSLATE("twentieth"),
- // that's enough - otherwise we'd have problems with
- // composite (or not) ordinals
- };
-
- size_t n;
- for ( n = 0; n < WXSIZEOF(ordinals); n++ )
- {
- if ( token.CmpNoCase(ordinals[n]) == 0 )
- {
- break;
- }
- }
-
- if ( n == WXSIZEOF(ordinals) )
- {
- // stop here - something unknown
- break;
- }
-
- // it's a day
- if ( haveDay )
- {
- // don't try anything here (as in case of numeric day
- // above) - the symbolic day spec should always
- // precede the month/year
- break;
- }
-
- haveDay = TRUE;
-
- day = n + 1;
- }
- }
- }
-
- nPosCur = tok.GetPosition();
- }
-
- // either no more tokens or the scan was stopped by something we couldn't
- // parse - in any case, see if we can construct a date from what we have
- if ( !haveDay && !haveWDay )
- {
- wxLogDebug(_T("ParseDate: no day, no weekday hence no date."));
-
- return (wxChar *)NULL;
- }
-
- if ( haveWDay && (haveMon || haveYear || haveDay) &&
- !(haveDay && haveMon && haveYear) )
- {
- // without adjectives (which we don't support here) the week day only
- // makes sense completely separately or with the full date
- // specification (what would "Wed 1999" mean?)
- return (wxChar *)NULL;
- }
-
- if ( !haveWDay && haveYear && !(haveDay && haveMon) )
- {
- // may be we have month and day instead of day and year?
- if ( haveDay && !haveMon )
- {
- if ( day <= 12 )
- {
- // exchange day and month
- mon = (wxDateTime::Month)(day - 1);
-
- // we're in the current year then
- if ( year <= GetNumOfDaysInMonth(Inv_Year, mon) )
- {
- day = year;
-
- haveMon = TRUE;
- haveYear = FALSE;
- }
- //else: no, can't exchange, leave haveMon == FALSE
- }
- }
-
- if ( !haveMon )
- {
- // if we give the year, month and day must be given too
- wxLogDebug(_T("ParseDate: day and month should be specified if year is."));
-
- return (wxChar *)NULL;
- }
- }
-
- if ( !haveMon )
- {
- mon = GetCurrentMonth();
- }
-
- if ( !haveYear )
- {
- year = GetCurrentYear();
- }
-
- if ( haveDay )
- {
- Set(day, mon, year);
-
- if ( haveWDay )
- {
- // check that it is really the same
- if ( GetWeekDay() != wday )
- {
- // inconsistency detected
- wxLogDebug(_T("ParseDate: inconsistent day/weekday."));
-
- return (wxChar *)NULL;
- }
- }
- }
- else // haveWDay
- {
- *this = Today();
-
- SetToWeekDayInSameWeek(wday);
- }
-
- // return the pointer to the first unparsed char
- p += nPosCur;
- if ( nPosCur && wxStrchr(dateDelimiters, *(p - 1)) )
- {
- // if we couldn't parse the token after the delimiter, put back the
- // delimiter as well
- p--;
- }
-
- return p;
-}
-
-const wxChar *wxDateTime::ParseTime(const wxChar *time)
-{
- wxCHECK_MSG( time, (wxChar *)NULL, _T("NULL pointer in wxDateTime::Parse") );
-
- // first try some extra things
- static const struct
- {
- const wxChar *name;
- wxDateTime_t hour;
- } stdTimes[] =
- {
- { wxTRANSLATE("noon"), 12 },
- { wxTRANSLATE("midnight"), 00 },
- // anything else?
- };
-
- for ( size_t n = 0; n < WXSIZEOF(stdTimes); n++ )
- {
- wxString timeString = wxGetTranslation(stdTimes[n].name);
- size_t len = timeString.length();
- if ( timeString.CmpNoCase(wxString(time, len)) == 0 )
- {
- Set(stdTimes[n].hour, 0, 0);
-
- return time + len;
- }
- }
-
- // try all time formats we may think about starting with the standard one
- const wxChar *result = ParseFormat(time, _T("%X"));
- if ( !result )
- {
- // normally, it's the same, but why not try it?
- result = ParseFormat(time, _T("%H:%M:%S"));
- }
-
- if ( !result )
- {
- // 12hour with AM/PM?
- result = ParseFormat(time, _T("%I:%M:%S %p"));
- }
-
- if ( !result )
- {
- // without seconds?
- result = ParseFormat(time, _T("%H:%M"));
- }
-
- if ( !result )
- {
- // 12hour with AM/PM but without seconds?
- result = ParseFormat(time, _T("%I:%M %p"));
- }
-
- if ( !result )
- {
- // just the hour?
- result = ParseFormat(time, _T("%H"));
- }
-
- if ( !result )
- {
- // just the hour and AM/PM?
- result = ParseFormat(time, _T("%I %p"));
- }
-
- // TODO: parse timezones
-
- return result;
-}
-
-// ----------------------------------------------------------------------------
-// Workdays and holidays support
-// ----------------------------------------------------------------------------
-
-bool wxDateTime::IsWorkDay(Country WXUNUSED(country)) const
-{
- return !wxDateTimeHolidayAuthority::IsHoliday(*this);
-}
-
-// ============================================================================
-// 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(wxStrlen(format));
-
- for ( const wxChar *pch = format; *pch; pch++ )
- {
- wxChar ch = *pch;
-
- if ( ch == _T('%') )
- {
- wxString tmp;
-
- ch = *++pch; // get the format spec char
- switch ( ch )
- {
- default:
- wxFAIL_MSG( _T("invalid format character") );
- // fall through
-
- case _T('%'):
- // will get to str << ch below
- break;
-
- case _T('D'):
- tmp.Printf(_T("%d"), GetDays());
- break;
-
- case _T('E'):
- tmp.Printf(_T("%d"), GetWeeks());
- break;
-
- case _T('H'):
- tmp.Printf(_T("%02d"), GetHours());
- break;
-
- case _T('l'):
- tmp.Printf(_T("%03ld"), GetMilliseconds().ToLong());
- break;
-
- case _T('M'):
- tmp.Printf(_T("%02d"), GetMinutes());
- break;
-
- case _T('S'):
- tmp.Printf(_T("%02ld"), GetSeconds().ToLong());
- break;
- }
-
- if ( !!tmp )
- {
- str += tmp;
-
- // skip str += ch below
- continue;
- }
- }
-
- str += ch;
- }
-
- return str;
-}
-
-// ============================================================================
-// wxDateTimeHolidayAuthority and related classes
-// ============================================================================
-
-#include "wx/arrimpl.cpp"
-
-WX_DEFINE_OBJARRAY(wxDateTimeArray);
-
-static int wxCMPFUNC_CONV
-wxDateTimeCompareFunc(wxDateTime **first, wxDateTime **second)
-{
- wxDateTime dt1 = **first,
- dt2 = **second;
-
- return dt1 == dt2 ? 0 : dt1 < dt2 ? -1 : +1;
-}
-
-// ----------------------------------------------------------------------------
-// wxDateTimeHolidayAuthority
-// ----------------------------------------------------------------------------
-
-wxHolidayAuthoritiesArray wxDateTimeHolidayAuthority::ms_authorities;
-
-/* static */
-bool wxDateTimeHolidayAuthority::IsHoliday(const wxDateTime& dt)
-{
- size_t count = ms_authorities.GetCount();
- for ( size_t n = 0; n < count; n++ )
- {
- if ( ms_authorities[n]->DoIsHoliday(dt) )
- {
- return TRUE;
- }
- }
-
- return FALSE;
-}
-
-/* static */
-size_t
-wxDateTimeHolidayAuthority::GetHolidaysInRange(const wxDateTime& dtStart,
- const wxDateTime& dtEnd,
- wxDateTimeArray& holidays)
-{
- wxDateTimeArray hol;
-
- holidays.Empty();
-
- size_t count = ms_authorities.GetCount();
- for ( size_t nAuth = 0; nAuth < count; nAuth++ )
- {
- ms_authorities[nAuth]->DoGetHolidaysInRange(dtStart, dtEnd, hol);
-
- WX_APPEND_ARRAY(holidays, hol);
- }
-
- holidays.Sort(wxDateTimeCompareFunc);
-
- return holidays.GetCount();
-}
-
-/* static */
-void wxDateTimeHolidayAuthority::ClearAllAuthorities()
-{
- WX_CLEAR_ARRAY(ms_authorities);