summary refs log tree commit diff stats
path: root/tests/stdlib
diff options
context:
space:
mode:
authorMatt Haggard <haggardii@gmail.com>2019-05-08 05:48:04 -0600
committerAndreas Rumpf <rumpf_a@web.de>2019-05-08 13:48:04 +0200
commit8180d443b9938f135dd280bcd5e1727766cd7468 (patch)
tree06af3c786b845ce594285093dced3299388e2974 /tests/stdlib
parent072566d5f44a3f771203bae616ca3df2f4884a2b (diff)
downloadNim-8180d443b9938f135dd280bcd5e1727766cd7468.tar.gz
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
Diffstat (limited to 'tests/stdlib')
-rw-r--r--tests/stdlib/ttimes.nim27
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/stdlib/ttimes.nim b/tests/stdlib/ttimes.nim
index b29f5090b..ef6712171 100644
--- a/tests/stdlib/ttimes.nim
+++ b/tests/stdlib/ttimes.nim
@@ -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)