mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-22 15:25:22 +00:00
Allow for locale-based parsing/formatting of dates (#11170)
* Allow for locale-based parsing/formatting of dates * Updates based on review feedback of PR 11170 DateTimeLocale arrays are now indexed by Month and WeekDay enums. More sane date used for testing. Documentation newline. Case change of DefaultLocale (and make it public) * Add changelog entry for DateTimeLocale addition to times module * Use pattern symbols for DateTimeLocale attribute names
This commit is contained in:
committed by
Andreas Rumpf
parent
072566d5f4
commit
8180d443b9
@@ -452,6 +452,23 @@ suite "ttimes":
|
||||
doAssert dt.format("zz") == tz[2]
|
||||
doAssert dt.format("zzz") == tz[3]
|
||||
|
||||
test "format locale":
|
||||
let loc = DateTimeLocale(
|
||||
MMM: ["Fir","Sec","Thi","Fou","Fif","Six","Sev","Eig","Nin","Ten","Ele","Twe"],
|
||||
MMMM: ["Firsty", "Secondy", "Thirdy", "Fourthy", "Fifthy", "Sixthy", "Seventhy", "Eighthy", "Ninthy", "Tenthy", "Eleventhy", "Twelfthy"],
|
||||
ddd: ["Red", "Ora.", "Yel.", "Gre.", "Blu.", "Vio.", "Whi."],
|
||||
dddd: ["Red", "Orange", "Yellow", "Green", "Blue", "Violet", "White"],
|
||||
)
|
||||
var dt = initDateTime(5, mJan, 2010, 17, 01, 02, utc())
|
||||
check dt.format("d", loc) == "5"
|
||||
check dt.format("dd", loc) == "05"
|
||||
check dt.format("ddd", loc) == "Ora."
|
||||
check dt.format("dddd", loc) == "Orange"
|
||||
check dt.format("M", loc) == "1"
|
||||
check dt.format("MM", loc) == "01"
|
||||
check dt.format("MMM", loc) == "Fir"
|
||||
check dt.format("MMMM", loc) == "Firsty"
|
||||
|
||||
test "parse":
|
||||
check $parse("20180101", "yyyyMMdd", utc()) == "2018-01-01T00:00:00Z"
|
||||
parseTestExcp("+120180101", "yyyyMMdd")
|
||||
@@ -473,6 +490,16 @@ suite "ttimes":
|
||||
|
||||
parseTestExcp("2000 A", "yyyy g")
|
||||
|
||||
test "parse locale":
|
||||
let loc = DateTimeLocale(
|
||||
MMM: ["Fir","Sec","Thi","Fou","Fif","Six","Sev","Eig","Nin","Ten","Ele","Twe"],
|
||||
MMMM: ["Firsty", "Secondy", "Thirdy", "Fourthy", "Fifthy", "Sixthy", "Seventhy", "Eighthy", "Ninthy", "Tenthy", "Eleventhy", "Twelfthy"],
|
||||
ddd: ["Red", "Ora.", "Yel.", "Gre.", "Blu.", "Vio.", "Whi."],
|
||||
dddd: ["Red", "Orange", "Yellow", "Green", "Blue", "Violet", "White"],
|
||||
)
|
||||
check $parse("02 Fir 2019", "dd MMM yyyy", utc(), loc) == "2019-01-02T00:00:00Z"
|
||||
check $parse("Fourthy 6, 2017", "MMMM d, yyyy", utc(), loc) == "2017-04-06T00:00:00Z"
|
||||
|
||||
test "countLeapYears":
|
||||
# 1920, 2004 and 2020 are leap years, and should be counted starting at the following year
|
||||
check countLeapYears(1920) + 1 == countLeapYears(1921)
|
||||
|
||||
Reference in New Issue
Block a user