added support for width and flags in wxDateTime::Format()
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5051 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
parent
341e7d2889
commit
77c3e48a9e
@ -1587,7 +1587,7 @@ wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const
|
|||||||
tmTimeOnly.tm_year = 76;
|
tmTimeOnly.tm_year = 76;
|
||||||
tmTimeOnly.tm_isdst = 0; // no DST, we adjust for tz ourselves
|
tmTimeOnly.tm_isdst = 0; // no DST, we adjust for tz ourselves
|
||||||
|
|
||||||
wxString tmp, res;
|
wxString tmp, res, fmt;
|
||||||
for ( const wxChar *p = format; *p; p++ )
|
for ( const wxChar *p = format; *p; p++ )
|
||||||
{
|
{
|
||||||
if ( *p != _T('%') )
|
if ( *p != _T('%') )
|
||||||
@ -1598,8 +1598,28 @@ wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// start of the format specification
|
// set the default format
|
||||||
switch ( *++p )
|
switch ( *++p )
|
||||||
|
{
|
||||||
|
case _T('Y'): // year has 4 digits
|
||||||
|
fmt = _T("%04d");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case _T('j'): // day of year has 3 digits
|
||||||
|
fmt = _T("%03d");
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
// it's either another valid format specifier in which case
|
||||||
|
// the format is "%02d" (for all the rest) or we have the
|
||||||
|
// field width preceding the format in which case it will
|
||||||
|
// override the default format anyhow
|
||||||
|
fmt = _T("%02d");
|
||||||
|
}
|
||||||
|
|
||||||
|
restart:
|
||||||
|
// start of the format specification
|
||||||
|
switch ( *p )
|
||||||
{
|
{
|
||||||
case _T('a'): // a weekday name
|
case _T('a'): // a weekday name
|
||||||
case _T('A'):
|
case _T('A'):
|
||||||
@ -1746,13 +1766,11 @@ wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case _T('d'): // day of a month (01-31)
|
case _T('d'): // day of a month (01-31)
|
||||||
tmp.Printf(_T("%02d"), tm.mday);
|
res += wxString::Format(fmt, tm.mday);
|
||||||
res += tmp;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case _T('H'): // hour in 24h format (00-23)
|
case _T('H'): // hour in 24h format (00-23)
|
||||||
tmp.Printf(_T("%02d"), tm.hour);
|
res += wxString::Format(fmt, tm.hour);
|
||||||
res += tmp;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case _T('I'): // hour in 12h format (01-12)
|
case _T('I'): // hour in 12h format (01-12)
|
||||||
@ -1760,24 +1778,20 @@ wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const
|
|||||||
// 24h -> 12h, 0h -> 12h too
|
// 24h -> 12h, 0h -> 12h too
|
||||||
int hour12 = tm.hour > 12 ? tm.hour - 12
|
int hour12 = tm.hour > 12 ? tm.hour - 12
|
||||||
: tm.hour ? tm.hour : 12;
|
: tm.hour ? tm.hour : 12;
|
||||||
tmp.Printf(_T("%02d"), hour12);
|
res += wxString::Format(fmt, hour12);
|
||||||
res += tmp;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case _T('j'): // day of the year
|
case _T('j'): // day of the year
|
||||||
tmp.Printf(_T("%03d"), GetDayOfYear(tz));
|
res += wxString::Format(fmt, GetDayOfYear(tz));
|
||||||
res += tmp;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case _T('m'): // month as a number (01-12)
|
case _T('m'): // month as a number (01-12)
|
||||||
tmp.Printf(_T("%02d"), tm.mon + 1);
|
res += wxString::Format(fmt, tm.mon + 1);
|
||||||
res += tmp;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case _T('M'): // minute as a decimal number (00-59)
|
case _T('M'): // minute as a decimal number (00-59)
|
||||||
tmp.Printf(_T("%02d"), tm.min);
|
res += wxString::Format(fmt, tm.min);
|
||||||
res += tmp;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case _T('p'): // AM or PM string
|
case _T('p'): // AM or PM string
|
||||||
@ -1785,24 +1799,22 @@ wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case _T('S'): // second as a decimal number (00-61)
|
case _T('S'): // second as a decimal number (00-61)
|
||||||
tmp.Printf(_T("%02d"), tm.sec);
|
res += wxString::Format(fmt, tm.sec);
|
||||||
res += tmp;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case _T('U'): // week number in the year (Sunday 1st week day)
|
case _T('U'): // week number in the year (Sunday 1st week day)
|
||||||
tmp.Printf(_T("%02d"),
|
{
|
||||||
(GetDayOfYear(tz) - tm.GetWeekDay() + 7) / 7);
|
int week = (GetDayOfYear(tz) - tm.GetWeekDay() + 7) / 7;
|
||||||
res += tmp;
|
res += wxString::Format(fmt, week);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case _T('W'): // week number in the year (Monday 1st week day)
|
case _T('W'): // week number in the year (Monday 1st week day)
|
||||||
tmp.Printf(_T("%02d"), GetWeekOfYear(tz));
|
res += wxString::Format(fmt, GetWeekOfYear(tz));
|
||||||
res += tmp;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case _T('w'): // weekday as a number (0-6), Sunday = 0
|
case _T('w'): // weekday as a number (0-6), Sunday = 0
|
||||||
tmp.Printf(_T("%d"), tm.GetWeekDay());
|
res += wxString::Format(fmt, tm.GetWeekDay());
|
||||||
res += tmp;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// case _T('x'): -- handled with "%c"
|
// case _T('x'): -- handled with "%c"
|
||||||
@ -1813,13 +1825,11 @@ wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case _T('y'): // year without century (00-99)
|
case _T('y'): // year without century (00-99)
|
||||||
tmp.Printf(_T("%02d"), tm.year % 100);
|
res += wxString::Format(fmt, tm.year % 100);
|
||||||
res += tmp;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case _T('Y'): // year with century
|
case _T('Y'): // year with century
|
||||||
tmp.Printf(_T("%04d"), tm.year);
|
res += wxString::Format(fmt, tm.year);
|
||||||
res += tmp;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case _T('Z'): // timezone name
|
case _T('Z'): // timezone name
|
||||||
@ -1827,6 +1837,24 @@ wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
// is it the format width?
|
||||||
|
fmt.Empty();
|
||||||
|
while ( *p == _T('-') || *p == _T('+') ||
|
||||||
|
*p == _T(' ') || wxIsdigit(*p) )
|
||||||
|
{
|
||||||
|
fmt += *p;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !fmt.IsEmpty() )
|
||||||
|
{
|
||||||
|
// we've only got the flags and width so far in fmt
|
||||||
|
fmt.Prepend(_T('%'));
|
||||||
|
fmt.Append(_T('d'));
|
||||||
|
|
||||||
|
goto restart;
|
||||||
|
}
|
||||||
|
|
||||||
|
// no, it wasn't the width
|
||||||
wxFAIL_MSG(_T("unknown format specificator"));
|
wxFAIL_MSG(_T("unknown format specificator"));
|
||||||
|
|
||||||
// fall through and just copy it nevertheless
|
// fall through and just copy it nevertheless
|
||||||
|
@ -1105,14 +1105,15 @@ wxString& wxString::operator<<(double d)
|
|||||||
/* static */
|
/* static */
|
||||||
wxString wxString::Format(const wxChar *pszFormat, ...)
|
wxString wxString::Format(const wxChar *pszFormat, ...)
|
||||||
{
|
{
|
||||||
va_list argptr;
|
va_list argptr;
|
||||||
va_start(argptr, pszFormat);
|
va_start(argptr, pszFormat);
|
||||||
|
|
||||||
wxString s = FormatV(pszFormat, argptr);
|
wxString s;
|
||||||
|
s.PrintfV(pszFormat, argptr);
|
||||||
|
|
||||||
va_end(argptr);
|
va_end(argptr);
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* static */
|
/* static */
|
||||||
|
Loading…
Reference in New Issue
Block a user