Add support for %F (ISO 8601 date) format specifier to wxDateTime

Recognize it when formatting and parsing dates.

Closes #24173.
This commit is contained in:
Vadim Zeitlin 2023-12-30 17:01:19 +01:00
parent ce1d317768
commit 7543e49c3c
2 changed files with 21 additions and 0 deletions

View file

@ -580,6 +580,10 @@ wxString wxDateTime::Format(const wxString& formatp, const TimeZone& tz) const
res += wxString::Format(fmt, tm.mday); res += wxString::Format(fmt, tm.mday);
break; break;
case wxT('F'): // ISO 8601 date
res += wxString::Format(wxT("%04d-%02d-%02d"), tm.year, tm.mon + 1, tm.mday);
break;
case wxT('g'): // 2-digit week-based year case wxT('g'): // 2-digit week-based year
res += wxString::Format(fmt, GetWeekBasedYear() % 100); res += wxString::Format(fmt, GetWeekBasedYear() % 100);
break; break;
@ -1229,6 +1233,22 @@ wxDateTime::ParseFormat(const wxString& date,
mday = (wxDateTime_t)num; mday = (wxDateTime_t)num;
break; break;
case wxT('F'): // ISO 8601 date
{
wxDateTime dt = ParseFormatAt(input, end, wxS("%Y-%m-%d"));
if ( !dt.IsValid() )
return false;
const Tm tm = dt.GetTm();
year = tm.year;
mon = tm.mon;
mday = tm.mday;
haveDay = haveMon = haveYear = true;
}
break;
case wxT('H'): // hour in 24h format (00-23) case wxT('H'): // hour in 24h format (00-23)
if ( !GetNumericToken(width, input, end, &num) || (num > 23) ) if ( !GetNumericToken(width, input, end, &num) || (num > 23) )
{ {

View file

@ -684,6 +684,7 @@ void DateTimeTestCase::TestTimeFormat()
{ CompareYear, "Date is %x, time is %X" }, // %x could use 2 digits { CompareYear, "Date is %x, time is %X" }, // %x could use 2 digits
{ CompareTime, "Time is %H:%M:%S or %I:%M:%S %p" }, { CompareTime, "Time is %H:%M:%S or %I:%M:%S %p" },
{ CompareNone, "The day of year: %j, the week of year: %W" }, { CompareNone, "The day of year: %j, the week of year: %W" },
{ CompareDate, "ISO date using short form: %F" },
{ CompareDate, "ISO date without separators: %Y%m%d" }, { CompareDate, "ISO date without separators: %Y%m%d" },
{ CompareBoth, "RFC 2822 string: %Y-%m-%d %H:%M:%S.%l %z" }, { CompareBoth, "RFC 2822 string: %Y-%m-%d %H:%M:%S.%l %z" },