handle all invalid cases in genlocale date/time formatter

Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
This commit is contained in:
Ivailo Monev 2019-08-12 00:50:49 +00:00
parent 556ddafa04
commit bec57eb4fe
3 changed files with 572 additions and 547 deletions

View file

@ -130,12 +130,30 @@ def todatetimeformat(fromformat):
'l',
'w',
'f',
'e',
'c',
'k',
'j',
'v',
]
replacementtags = {
"y" : "yyyy", # four-digit year without leading zeroes
"MMMMM" : "MMM", # narrow month name
"LLLLL" : "MMM", # stand-alone narrow month name
"E" : "ddd", "EE" : "ddd", "EEE" : "ddd", "EEEEE" : "ddd", "EEEE" : "dddd", # day of week
"e" : "ddd", "ee" : "ddd", "eee" : "ddd", "eeeee" : "ddd", "eeee" : "dddd", # local day of week
"c" : "ddd", "cc" : "ddd", "ccc" : "ddd", "ccccc" : "ddd", "cccc" : "dddd", # stand-alone local day of week
"a" : "AP", # AM/PM
"K" : "h", # Hour 0-11
"k" : "H", # Hour 1-24
"z" : "t", "zz" : "t", "zzz" : "t", "zzzz" : "t", # timezone
"Z" : "t", "ZZ" : "t", "ZZZ" : "t", "ZZZZ" : "t", # timezone
"v" : "t", "vv" : "t", "vvv" : "t", "vvvv" : "t", # timezone
"V" : "t", "VV" : "t", "VVV" : "t", "VVVV" : "t", # timezone
"L" : "M", # stand-alone month names. not supported
}
replacementregex = {
r"yyy{3,}" : "yyyy", # more that three digits hence convert to four-digit year
r"g{1,}": "", # modified julian day. not supported.
r"S{1,}" : "", # fractional seconds. not supported.
r"A{1,}" : "" # milliseconds in day. not supported.
}
possibleoccurences = [
'%s, ',
', %s',
@ -144,9 +162,14 @@ def todatetimeformat(fromformat):
'%s-',
'-%s',
'(%s)',
"('%s')",
'%s ',
' %s',
'%s',
]
result = fromformat
for key in replacementregex.keys():
result = re.sub(key, replacementregex[key], result)
for tag in unsupportedtags:
uppertag = tag.upper()
for occurence in possibleoccurences:
@ -158,6 +181,8 @@ def todatetimeformat(fromformat):
result = result.replace(occurence % (uppertag * 3), '')
result = result.replace(occurence % (uppertag * 2), '')
result = result.replace(occurence % uppertag, '')
for key in replacementtags.keys():
result = result.replace(key, replacementtags[key])
return result
def tomonthslist(fromxmlelements, initialvalues):
@ -386,8 +411,8 @@ def printlocaledata(frommap, key):
tochar(value['list_pattern_part_mid']),
tochar(value['list_pattern_part_end']),
tochar(value['list_pattern_part_two']),
tochar(todatetimeformat(value['short_date_format'])),
tochar(todatetimeformat(value['long_date_format'])),
tochar(value['short_date_format']),
tochar(value['long_date_format']),
tochar(value['short_time_format']),
tochar(value['long_time_format']),
tochar(value['am']),

File diff suppressed because it is too large Load diff

View file

@ -1641,16 +1641,16 @@ void tst_QLocale::dateFormat()
QCOMPARE(c.dateFormat(QLocale::NarrowFormat), c.dateFormat(QLocale::ShortFormat));
const QLocale nn("nn_NO");
QCOMPARE(nn.dateFormat(QLocale::NarrowFormat), QLatin1String("dd.MM.y"));
QCOMPARE(nn.dateFormat(QLocale::ShortFormat), QLatin1String("dd.MM.y"));
QCOMPARE(nn.dateFormat(QLocale::LongFormat), QLatin1String("d. MMMM y"));
QCOMPARE(nn.dateFormat(QLocale::NarrowFormat), QLatin1String("dd.MM.yyyy"));
QCOMPARE(nn.dateFormat(QLocale::ShortFormat), QLatin1String("dd.MM.yyyy"));
QCOMPARE(nn.dateFormat(QLocale::LongFormat), QLatin1String("d. MMMM yyyy"));
const QLocale ca("en_CA");
QCOMPARE(ca.dateFormat(QLocale::ShortFormat), QLatin1String("y-MM-dd"));
QCOMPARE(ca.dateFormat(QLocale::LongFormat), QLatin1String("MMMM d, y"));
QCOMPARE(ca.dateFormat(QLocale::ShortFormat), QLatin1String("yyyy-MM-dd"));
QCOMPARE(ca.dateFormat(QLocale::LongFormat), QLatin1String("MMMM d, yyyy"));
const QLocale ja("ja_JP");
QCOMPARE(ja.dateFormat(QLocale::ShortFormat), QLatin1String("y/MM/dd"));
QCOMPARE(ja.dateFormat(QLocale::ShortFormat), QLatin1String("yyyy/MM/dd"));
}
void tst_QLocale::timeFormat()
@ -1662,15 +1662,15 @@ void tst_QLocale::timeFormat()
const QLocale nn("nn_NO");
QCOMPARE(nn.timeFormat(QLocale::NarrowFormat), QLatin1String("HH:mm"));
QCOMPARE(nn.timeFormat(QLocale::ShortFormat), QLatin1String("HH:mm"));
QCOMPARE(nn.timeFormat(QLocale::LongFormat), QLatin1String("HH:mm:ss z"));
QCOMPARE(nn.timeFormat(QLocale::LongFormat), QLatin1String("HH:mm:ss t"));
const QLocale en("en_FJ");
QCOMPARE(en.timeFormat(QLocale::ShortFormat), QLatin1String("h:mm a"));
QCOMPARE(en.timeFormat(QLocale::LongFormat), QLatin1String("h:mm:ss a z"));
QCOMPARE(en.timeFormat(QLocale::ShortFormat), QLatin1String("h:mm AP"));
QCOMPARE(en.timeFormat(QLocale::LongFormat), QLatin1String("h:mm:ss AP t"));
const QLocale cat("ca_ES");
QCOMPARE(cat.timeFormat(QLocale::ShortFormat), QLatin1String("H:mm"));
QCOMPARE(cat.timeFormat(QLocale::LongFormat), QLatin1String("H:mm:ss z"));
QCOMPARE(cat.timeFormat(QLocale::LongFormat), QLatin1String("H:mm:ss t"));
}
void tst_QLocale::dateTimeFormat()
@ -1680,9 +1680,9 @@ void tst_QLocale::dateTimeFormat()
QCOMPARE(c.dateTimeFormat(QLocale::NarrowFormat), c.dateTimeFormat(QLocale::ShortFormat));
const QLocale nn("nn_NO");
QCOMPARE(nn.dateTimeFormat(QLocale::NarrowFormat), QLatin1String("dd.MM.y HH:mm"));
QCOMPARE(nn.dateTimeFormat(QLocale::ShortFormat), QLatin1String("dd.MM.y HH:mm"));
QCOMPARE(nn.dateTimeFormat(QLocale::LongFormat), QLatin1String("d. MMMM y HH:mm:ss z"));
QCOMPARE(nn.dateTimeFormat(QLocale::NarrowFormat), QLatin1String("dd.MM.yyyy HH:mm"));
QCOMPARE(nn.dateTimeFormat(QLocale::ShortFormat), QLatin1String("dd.MM.yyyy HH:mm"));
QCOMPARE(nn.dateTimeFormat(QLocale::LongFormat), QLatin1String("d. MMMM yyyy HH:mm:ss t"));
}
void tst_QLocale::monthName()