ttimes.nim 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. discard """
  2. matrix: "--mm:refc; --mm:orc; --backend:js --jsbigint64:on; --backend:js --jsbigint64:off -d:nimStringHash2"
  3. """
  4. import times, strutils, unittest
  5. import std/assertions
  6. when not defined(js):
  7. import os
  8. proc staticTz(hours, minutes, seconds: int = 0): Timezone {.noSideEffect.} =
  9. let offset = hours * 3600 + minutes * 60 + seconds
  10. proc zonedTimeFromAdjTime(adjTime: Time): ZonedTime =
  11. result = default(ZonedTime)
  12. result.isDst = false
  13. result.utcOffset = offset
  14. result.time = adjTime + initDuration(seconds = offset)
  15. proc zonedTimeFromTime(time: Time): ZonedTime =
  16. result = default(ZonedTime)
  17. result.isDst = false
  18. result.utcOffset = offset
  19. result.time = time
  20. newTimezone("", zonedTimeFromTime, zonedTimeFromAdjTime)
  21. template parseTest(s, f, sExpected: string, ydExpected: int) =
  22. let
  23. parsed = s.parse(f, utc())
  24. parsedStr = $parsed
  25. check parsedStr == sExpected
  26. check parsed.yearday == ydExpected
  27. template parseTestExcp(s, f: string) =
  28. expect ValueError:
  29. let parsed = s.parse(f)
  30. template parseTestTimeOnly(s, f, sExpected: string) =
  31. check sExpected in $s.parse(f, utc())
  32. # because setting a specific timezone for testing is platform-specific, we use
  33. # explicit timezone offsets in all tests.
  34. template runTimezoneTests() =
  35. parseTest("Tuesday at 09:04am on Dec 15, 2015 +0",
  36. "dddd 'at' hh:mmtt 'on' MMM d, yyyy z", "2015-12-15T09:04:00Z", 348)
  37. # ANSIC = "Mon Jan _2 15:04:05 2006"
  38. parseTest("Thu Jan 12 15:04:05 2006 +0", "ddd MMM dd HH:mm:ss yyyy z",
  39. "2006-01-12T15:04:05Z", 11)
  40. # UnixDate = "Mon Jan _2 15:04:05 MST 2006"
  41. parseTest("Thu Jan 12 15:04:05 2006 +0", "ddd MMM dd HH:mm:ss yyyy z",
  42. "2006-01-12T15:04:05Z", 11)
  43. # RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
  44. parseTest("Mon Feb 29 15:04:05 -07:00 2016 +0", "ddd MMM dd HH:mm:ss zzz yyyy z",
  45. "2016-02-29T15:04:05Z", 59) # leap day
  46. # RFC822 = "02 Jan 06 15:04 MST"
  47. parseTest("12 Jan 16 15:04 +0", "dd MMM yy HH:mm z",
  48. "2016-01-12T15:04:00Z", 11)
  49. # RFC822Z = "02 Jan 06 15:04 -0700" # RFC822 with numeric zone
  50. parseTest("01 Mar 16 15:04 -07:00", "dd MMM yy HH:mm zzz",
  51. "2016-03-01T22:04:00Z", 60) # day after february in leap year
  52. # RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
  53. parseTest("Monday, 12-Jan-06 15:04:05 +0", "dddd, dd-MMM-yy HH:mm:ss z",
  54. "2006-01-12T15:04:05Z", 11)
  55. # RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
  56. parseTest("Sun, 01 Mar 2015 15:04:05 +0", "ddd, dd MMM yyyy HH:mm:ss z",
  57. "2015-03-01T15:04:05Z", 59) # day after february in non-leap year
  58. # RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" # RFC1123 with numeric zone
  59. parseTest("Thu, 12 Jan 2006 15:04:05 -07:00", "ddd, dd MMM yyyy HH:mm:ss zzz",
  60. "2006-01-12T22:04:05Z", 11)
  61. # RFC3339 = "2006-01-02T15:04:05Z07:00"
  62. parseTest("2006-01-12T15:04:05Z-07:00", "yyyy-MM-dd'T'HH:mm:ss'Z'zzz",
  63. "2006-01-12T22:04:05Z", 11)
  64. # RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
  65. parseTest("2006-01-12T15:04:05.999999999Z-07:00",
  66. "yyyy-MM-dd'T'HH:mm:ss.'999999999Z'zzz", "2006-01-12T22:04:05Z", 11)
  67. for tzFormat in ["z", "zz", "zzz"]:
  68. # formatting timezone as 'Z' for UTC
  69. parseTest("2001-01-12T22:04:05Z", "yyyy-MM-dd'T'HH:mm:ss" & tzFormat,
  70. "2001-01-12T22:04:05Z", 11)
  71. # timezone offset formats
  72. parseTest("2001-01-12T15:04:05 +7", "yyyy-MM-dd'T'HH:mm:ss z",
  73. "2001-01-12T08:04:05Z", 11)
  74. parseTest("2001-01-12T15:04:05 +07", "yyyy-MM-dd'T'HH:mm:ss zz",
  75. "2001-01-12T08:04:05Z", 11)
  76. parseTest("2001-01-12T15:04:05 +07:00", "yyyy-MM-dd'T'HH:mm:ss zzz",
  77. "2001-01-12T08:04:05Z", 11)
  78. parseTest("2001-01-12T15:04:05 +07:30:59", "yyyy-MM-dd'T'HH:mm:ss zzzz",
  79. "2001-01-12T07:33:06Z", 11)
  80. parseTest("2001-01-12T15:04:05 +0700", "yyyy-MM-dd'T'HH:mm:ss ZZZ",
  81. "2001-01-12T08:04:05Z", 11)
  82. parseTest("2001-01-12T15:04:05 +073059", "yyyy-MM-dd'T'HH:mm:ss ZZZZ",
  83. "2001-01-12T07:33:06Z", 11)
  84. # Kitchen = "3:04PM"
  85. parseTestTimeOnly("3:04PM", "h:mmtt", "15:04:00")
  86. # Bug with parse not setting DST properly if the current local DST differs from
  87. # the date being parsed. Need to test parse dates both in and out of DST. We
  88. # are testing that be relying on the fact that transforming a TimeInfo to a Time
  89. # and back again will correctly set the DST value. With the incorrect parse
  90. # behavior this will introduce a one hour offset from the named time and the
  91. # parsed time if the DST value differs between the current time and the date we
  92. # are parsing.
  93. let dstT1 = parse("2016-01-01 00:00:00", "yyyy-MM-dd HH:mm:ss")
  94. let dstT2 = parse("2016-06-01 00:00:00", "yyyy-MM-dd HH:mm:ss")
  95. check dstT1 == toTime(dstT1).local
  96. check dstT2 == toTime(dstT2).local
  97. block dstTest:
  98. # parsing will set isDST in relation to the local time. We take a date in
  99. # January and one in July to maximize the probability to hit one date with DST
  100. # and one without on the local machine. However, this is not guaranteed.
  101. let
  102. parsedJan = parse("2016-01-05 04:00:00+01:00", "yyyy-MM-dd HH:mm:sszzz")
  103. parsedJul = parse("2016-07-01 04:00:00+01:00", "yyyy-MM-dd HH:mm:sszzz")
  104. check toTime(parsedJan).toUnix == 1451962800
  105. check toTime(parsedJul).toUnix == 1467342000
  106. template usingTimezone(tz: string, body: untyped) =
  107. when defined(linux) or defined(macosx):
  108. let oldZone = getEnv("TZ")
  109. putEnv("TZ", tz)
  110. body
  111. putEnv("TZ", oldZone)
  112. block: # ttimes
  113. # Generate tests for multiple timezone files where available
  114. # Set the TZ env var for each test
  115. when defined(linux) or defined(macosx):
  116. let tz_dir = getEnv("TZDIR", "/usr/share/zoneinfo")
  117. const f = "yyyy-MM-dd HH:mm zzz"
  118. var tz_cnt = 0
  119. for timezone in walkFiles(tz_dir & "/**/*"):
  120. if symlinkExists(timezone) or timezone.endsWith(".tab") or
  121. timezone.endsWith(".list"):
  122. continue
  123. usingTimezone(timezone):
  124. test "test for " & timezone:
  125. tz_cnt.inc
  126. runTimezoneTests()
  127. test "enough timezone files tested":
  128. check tz_cnt > 10
  129. else:
  130. # not on Linux or macosx: run in the local timezone only
  131. test "parseTest":
  132. runTimezoneTests()
  133. block: # dst handling
  134. usingTimezone("Europe/Stockholm"):
  135. # In case of an impossible time, the time is moved to after the
  136. # impossible time period
  137. check initDateTime(26, mMar, 2017, 02, 30, 00).format(f) ==
  138. "2017-03-26 03:30 +02:00"
  139. # In case of an ambiguous time, the earlier time is chosen
  140. check initDateTime(29, mOct, 2017, 02, 00, 00).format(f) ==
  141. "2017-10-29 02:00 +02:00"
  142. # These are just dates on either side of the dst switch
  143. check initDateTime(29, mOct, 2017, 01, 00, 00).format(f) ==
  144. "2017-10-29 01:00 +02:00"
  145. check initDateTime(29, mOct, 2017, 01, 00, 00).isDst
  146. check initDateTime(29, mOct, 2017, 03, 01, 00).format(f) ==
  147. "2017-10-29 03:01 +01:00"
  148. check (not initDateTime(29, mOct, 2017, 03, 01, 00).isDst)
  149. check initDateTime(21, mOct, 2017, 01, 00, 00).format(f) ==
  150. "2017-10-21 01:00 +02:00"
  151. block: # issue #6520
  152. usingTimezone("Europe/Stockholm"):
  153. var local = fromUnix(1469275200).local
  154. var utc = fromUnix(1469275200).utc
  155. let claimedOffset = initDuration(seconds = local.utcOffset)
  156. local.utcOffset = 0
  157. check claimedOffset == utc.toTime - local.toTime
  158. block: # issue #5704
  159. usingTimezone("Asia/Seoul"):
  160. let diff = parse("19700101-000000", "yyyyMMdd-hhmmss").toTime -
  161. parse("19000101-000000", "yyyyMMdd-hhmmss").toTime
  162. check diff == initDuration(seconds = 2208986872)
  163. block: # issue #6465
  164. usingTimezone("Europe/Stockholm"):
  165. let dt = parse("2017-03-25 12:00", "yyyy-MM-dd hh:mm")
  166. check $(dt + initTimeInterval(days = 1)) == "2017-03-26T12:00:00+02:00"
  167. check $(dt + initDuration(days = 1)) == "2017-03-26T13:00:00+02:00"
  168. block: # adding/subtracting time across dst
  169. usingTimezone("Europe/Stockholm"):
  170. let dt1 = initDateTime(26, mMar, 2017, 03, 00, 00)
  171. check $(dt1 - 1.seconds) == "2017-03-26T01:59:59+01:00"
  172. var dt2 = initDateTime(29, mOct, 2017, 02, 59, 59)
  173. check $(dt2 + 1.seconds) == "2017-10-29T02:00:00+01:00"
  174. block: # datetime before epoch
  175. check $fromUnix(-2147483648).utc == "1901-12-13T20:45:52Z"
  176. block: # incorrect inputs: empty string
  177. parseTestExcp("", "yyyy-MM-dd")
  178. block: # incorrect inputs: year
  179. parseTestExcp("20-02-19", "yyyy-MM-dd")
  180. block: # incorrect inputs: month number
  181. parseTestExcp("2018-2-19", "yyyy-MM-dd")
  182. block: # incorrect inputs: month name
  183. parseTestExcp("2018-Fe", "yyyy-MMM-dd")
  184. block: # incorrect inputs: day
  185. parseTestExcp("2018-02-1", "yyyy-MM-dd")
  186. block: # incorrect inputs: day of week
  187. parseTestExcp("2018-Feb-Mo", "yyyy-MMM-ddd")
  188. block: # incorrect inputs: hour
  189. parseTestExcp("2018-02-19 1:30", "yyyy-MM-dd hh:mm")
  190. block: # incorrect inputs: minute
  191. parseTestExcp("2018-02-19 16:3", "yyyy-MM-dd hh:mm")
  192. block: # incorrect inputs: second
  193. parseTestExcp("2018-02-19 16:30:0", "yyyy-MM-dd hh:mm:ss")
  194. block: # incorrect inputs: timezone (z)
  195. parseTestExcp("2018-02-19 16:30:00 ", "yyyy-MM-dd hh:mm:ss z")
  196. block: # incorrect inputs: timezone (zz) 1
  197. parseTestExcp("2018-02-19 16:30:00 ", "yyyy-MM-dd hh:mm:ss zz")
  198. block: # incorrect inputs: timezone (zz) 2
  199. parseTestExcp("2018-02-19 16:30:00 +1", "yyyy-MM-dd hh:mm:ss zz")
  200. block: # incorrect inputs: timezone (zzz) 1
  201. parseTestExcp("2018-02-19 16:30:00 ", "yyyy-MM-dd hh:mm:ss zzz")
  202. block: # incorrect inputs: timezone (zzz) 2
  203. parseTestExcp("2018-02-19 16:30:00 +01:", "yyyy-MM-dd hh:mm:ss zzz")
  204. block: # incorrect inputs: timezone (zzz) 3
  205. parseTestExcp("2018-02-19 16:30:00 +01:0", "yyyy-MM-dd hh:mm:ss zzz")
  206. block: # incorrect inputs: year (yyyy/uuuu)
  207. parseTestExcp("-0001", "yyyy")
  208. parseTestExcp("-0001", "YYYY")
  209. parseTestExcp("1", "yyyy")
  210. parseTestExcp("12345", "yyyy")
  211. parseTestExcp("1", "uuuu")
  212. parseTestExcp("12345", "uuuu")
  213. parseTestExcp("-1 BC", "UUUU g")
  214. block: # incorrect inputs: invalid sign
  215. parseTestExcp("+1", "YYYY")
  216. parseTestExcp("+1", "dd")
  217. parseTestExcp("+1", "MM")
  218. parseTestExcp("+1", "hh")
  219. parseTestExcp("+1", "mm")
  220. parseTestExcp("+1", "ss")
  221. block: # _ as a separator
  222. discard parse("2000_01_01", "YYYY'_'MM'_'dd")
  223. block: # dynamic timezone
  224. let tz = staticTz(seconds = -9000)
  225. let dt = initDateTime(1, mJan, 2000, 12, 00, 00, tz)
  226. check dt.utcOffset == -9000
  227. check dt.isDst == false
  228. check $dt == "2000-01-01T12:00:00+02:30"
  229. check $dt.utc == "2000-01-01T09:30:00Z"
  230. check $dt.utc.inZone(tz) == $dt
  231. block: # isLeapYear
  232. check isLeapYear(2016)
  233. check (not isLeapYear(2015))
  234. check isLeapYear(2000)
  235. check (not isLeapYear(1900))
  236. block: # TimeInterval
  237. let t = fromUnix(876124714).utc # Mon 6 Oct 08:58:34 BST 1997
  238. # Interval tests
  239. let t2 = t - 2.years
  240. check t2.year == 1995
  241. let t3 = (t - 7.years - 34.minutes - 24.seconds)
  242. check t3.year == 1990
  243. check t3.minute == 24
  244. check t3.second == 10
  245. check (t + 1.hours).toTime.toUnix == t.toTime.toUnix + 60 * 60
  246. check (t - 1.hours).toTime.toUnix == t.toTime.toUnix - 60 * 60
  247. block: # TimeInterval - months
  248. var dt = initDateTime(1, mFeb, 2017, 00, 00, 00, utc())
  249. check $(dt - initTimeInterval(months = 1)) == "2017-01-01T00:00:00Z"
  250. dt = initDateTime(15, mMar, 2017, 00, 00, 00, utc())
  251. check $(dt - initTimeInterval(months = 1)) == "2017-02-15T00:00:00Z"
  252. dt = initDateTime(31, mMar, 2017, 00, 00, 00, utc())
  253. # This happens due to monthday overflow. It's consistent with Phobos.
  254. check $(dt - initTimeInterval(months = 1)) == "2017-03-03T00:00:00Z"
  255. block: # duration
  256. let d = initDuration
  257. check d(hours = 48) + d(days = 5) == d(weeks = 1)
  258. let dt = initDateTime(01, mFeb, 2000, 00, 00, 00, 0, utc()) + d(milliseconds = 1)
  259. check dt.nanosecond == convert(Milliseconds, Nanoseconds, 1)
  260. check d(seconds = 1, milliseconds = 500) * 2 == d(seconds = 3)
  261. check d(seconds = 3) div 2 == d(seconds = 1, milliseconds = 500)
  262. check d(milliseconds = 1001).inSeconds == 1
  263. check d(seconds = 1, milliseconds = 500) - d(milliseconds = 1250) ==
  264. d(milliseconds = 250)
  265. check d(seconds = 1, milliseconds = 1) < d(seconds = 1, milliseconds = 2)
  266. check d(seconds = 1) <= d(seconds = 1)
  267. check d(seconds = 0) - d(milliseconds = 1500) == d(milliseconds = -1500)
  268. check d(milliseconds = -1500) == d(seconds = -1, milliseconds = -500)
  269. check d(seconds = -1, milliseconds = 500) == d(milliseconds = -500)
  270. check initDuration(seconds = 1, nanoseconds = 2) <=
  271. initDuration(seconds = 1, nanoseconds = 3)
  272. check (initDuration(seconds = 1, nanoseconds = 3) <=
  273. initDuration(seconds = 1, nanoseconds = 1)).not
  274. block: # large/small dates
  275. discard initDateTime(1, mJan, -35_000, 12, 00, 00, utc())
  276. # with local tz
  277. discard initDateTime(1, mJan, -35_000, 12, 00, 00)
  278. discard initDateTime(1, mJan, 35_000, 12, 00, 00)
  279. # with duration/timeinterval
  280. let dt = initDateTime(1, mJan, -35_000, 12, 00, 00, utc()) +
  281. initDuration(seconds = 1)
  282. check dt.second == 1
  283. let dt2 = dt + 35_001.years
  284. check $dt2 == "0001-01-01T12:00:01Z"
  285. block: # compare datetimes
  286. var dt1 = now()
  287. var dt2 = dt1
  288. check dt1 == dt2
  289. check dt1 <= dt2
  290. dt2 = dt2 + 1.seconds
  291. check dt1 < dt2
  292. block: # adding/subtracting TimeInterval
  293. # add/subtract TimeIntervals and Time/TimeInfo
  294. let now = getTime().utc
  295. let isSpecial = now.isLeapDay
  296. check now + convert(Seconds, Nanoseconds, 1).nanoseconds == now + 1.seconds
  297. check now + 1.weeks == now + 7.days
  298. check now - 1.seconds == now - 3.seconds + 2.seconds
  299. check now + 65.seconds == now + 1.minutes + 5.seconds
  300. check now + 60.minutes == now + 1.hours
  301. check now + 24.hours == now + 1.days
  302. if not isSpecial:
  303. check now + 13.months == now + 1.years + 1.months
  304. check toUnix(fromUnix(0) + 2.seconds) == 2
  305. check toUnix(fromUnix(0) - 2.seconds) == -2
  306. var ti1 = now + 1.years
  307. ti1 = ti1 - 1.years
  308. if not isSpecial:
  309. check ti1 == now
  310. ti1 = ti1 + 1.days
  311. if not isSpecial:
  312. check ti1 == now + 1.days
  313. # Bug with adding a day to a Time
  314. let day = 24.hours
  315. let tomorrow = now + day
  316. check tomorrow - now == initDuration(days = 1)
  317. # Disabled for JS because it fails due to precision errors
  318. # (The JS target uses float64 for int64).
  319. when not defined(js):
  320. test "fromWinTime/toWinTime":
  321. check 0.fromUnix.toWinTime.fromWinTime.toUnix == 0
  322. check (-1).fromWinTime.nanosecond == convert(Seconds, Nanoseconds, 1) - 100
  323. check (-1).fromWinTime.toWinTime == -1
  324. # One nanosecond is discarded due to differences in time resolution
  325. check initTime(0, 101).toWinTime.fromWinTime.nanosecond == 100
  326. check initTime(0, 101).toWinTime.fromWinTime.nanosecond == 100
  327. block: # issue 7620
  328. let layout = "M/d/yyyy' 'h:mm:ss' 'tt' 'z"
  329. let t7620_am = parse("4/15/2017 12:01:02 AM +0", layout, utc())
  330. check t7620_am.format(layout) == "4/15/2017 12:01:02 AM Z"
  331. let t7620_pm = parse("4/15/2017 12:01:02 PM +0", layout, utc())
  332. check t7620_pm.format(layout) == "4/15/2017 12:01:02 PM Z"
  333. block: # format
  334. var dt = initDateTime(1, mJan, -0001,
  335. 17, 01, 02, 123_456_789,
  336. staticTz(hours = 1, minutes = 2, seconds = 3))
  337. check dt.format("d") == "1"
  338. check dt.format("dd") == "01"
  339. check dt.format("ddd") == "Fri"
  340. check dt.format("dddd") == "Friday"
  341. check dt.format("h") == "5"
  342. check dt.format("hh") == "05"
  343. check dt.format("H") == "17"
  344. check dt.format("HH") == "17"
  345. check dt.format("m") == "1"
  346. check dt.format("mm") == "01"
  347. check dt.format("M") == "1"
  348. check dt.format("MM") == "01"
  349. check dt.format("MMM") == "Jan"
  350. check dt.format("MMMM") == "January"
  351. check dt.format("s") == "2"
  352. check dt.format("ss") == "02"
  353. check dt.format("t") == "P"
  354. check dt.format("tt") == "PM"
  355. check dt.format("yy") == "02"
  356. check dt.format("yyyy") == "0002"
  357. check dt.format("YYYY") == "2"
  358. check dt.format("uuuu") == "-0001"
  359. check dt.format("UUUU") == "-1"
  360. check dt.format("z") == "-1"
  361. check dt.format("zz") == "-01"
  362. check dt.format("zzz") == "-01:02"
  363. check dt.format("zzzz") == "-01:02:03"
  364. check dt.format("g") == "BC"
  365. check dt.format("fff") == "123"
  366. check dt.format("ffffff") == "123456"
  367. check dt.format("fffffffff") == "123456789"
  368. dt.nanosecond = 1
  369. check dt.format("fff") == "000"
  370. check dt.format("ffffff") == "000000"
  371. check dt.format("fffffffff") == "000000001"
  372. dt.year = 12345
  373. check dt.format("yyyy") == "+12345"
  374. check dt.format("uuuu") == "+12345"
  375. dt.year = -12345
  376. check dt.format("yyyy") == "+12346"
  377. check dt.format("uuuu") == "-12345"
  378. expect ValueError:
  379. discard initTimeFormat("'")
  380. expect ValueError:
  381. discard initTimeFormat("'foo")
  382. expect ValueError:
  383. discard initTimeFormat("foo'")
  384. for tz in [
  385. (staticTz(seconds = 0), "+0", "+00", "+00:00"), # UTC
  386. (staticTz(seconds = -3600), "+1", "+01", "+01:00"), # CET
  387. (staticTz(seconds = -39600), "+11", "+11", "+11:00"), # two digits
  388. (staticTz(seconds = -1800), "+0", "+00", "+00:30"), # half an hour
  389. (staticTz(seconds = 7200), "-2", "-02", "-02:00"), # positive
  390. (staticTz(seconds = 38700), "-10", "-10", "-10:45")]: # positive with three quaters hour
  391. let dt = initDateTime(1, mJan, 2000, 00, 00, 00, tz[0])
  392. doAssert dt.format("z") == tz[1]
  393. doAssert dt.format("zz") == tz[2]
  394. doAssert dt.format("zzz") == tz[3]
  395. block: # format locale
  396. let loc = DateTimeLocale(
  397. MMM: ["Fir","Sec","Thi","Fou","Fif","Six","Sev","Eig","Nin","Ten","Ele","Twe"],
  398. MMMM: ["Firsty", "Secondy", "Thirdy", "Fourthy", "Fifthy", "Sixthy", "Seventhy", "Eighthy", "Ninthy", "Tenthy", "Eleventhy", "Twelfthy"],
  399. ddd: ["Red", "Ora.", "Yel.", "Gre.", "Blu.", "Vio.", "Whi."],
  400. dddd: ["Red", "Orange", "Yellow", "Green", "Blue", "Violet", "White"],
  401. )
  402. var dt = initDateTime(5, mJan, 2010, 17, 01, 02, utc())
  403. check dt.format("d", loc) == "5"
  404. check dt.format("dd", loc) == "05"
  405. check dt.format("ddd", loc) == "Ora."
  406. check dt.format("dddd", loc) == "Orange"
  407. check dt.format("M", loc) == "1"
  408. check dt.format("MM", loc) == "01"
  409. check dt.format("MMM", loc) == "Fir"
  410. check dt.format("MMMM", loc) == "Firsty"
  411. block: # parse
  412. check $parse("20180101", "yyyyMMdd", utc()) == "2018-01-01T00:00:00Z"
  413. parseTestExcp("+120180101", "yyyyMMdd")
  414. check parse("1", "YYYY", utc()).year == 1
  415. check parse("1 BC", "YYYY g", utc()).year == 0
  416. check parse("0001 BC", "yyyy g", utc()).year == 0
  417. check parse("+12345 BC", "yyyy g", utc()).year == -12344
  418. check parse("1 AD", "YYYY g", utc()).year == 1
  419. check parse("0001 AD", "yyyy g", utc()).year == 1
  420. check parse("+12345 AD", "yyyy g", utc()).year == 12345
  421. check parse("-1", "UUUU", utc()).year == -1
  422. check parse("-0001", "uuuu", utc()).year == -1
  423. discard parse("foobar", "'foobar'")
  424. discard parse("foo'bar", "'foo''''bar'")
  425. discard parse("'", "''")
  426. parseTestExcp("2000 A", "yyyy g")
  427. block: # parse locale
  428. let loc = DateTimeLocale(
  429. MMM: ["Fir","Sec","Thi","Fou","Fif","Six","Sev","Eig","Nin","Ten","Ele","Twe"],
  430. MMMM: ["Firsty", "Secondy", "Thirdy", "Fourthy", "Fifthy", "Sixthy", "Seventhy", "Eighthy", "Ninthy", "Tenthy", "Eleventhy", "Twelfthy"],
  431. ddd: ["Red", "Ora.", "Yel.", "Gre.", "Blu.", "Vio.", "Whi."],
  432. dddd: ["Red", "Orange", "Yellow", "Green", "Blue", "Violet", "White"],
  433. )
  434. check $parse("02 Fir 2019", "dd MMM yyyy", utc(), loc) == "2019-01-02T00:00:00Z"
  435. check $parse("Fourthy 6, 2017", "MMMM d, yyyy", utc(), loc) == "2017-04-06T00:00:00Z"
  436. block: # timezoneConversion
  437. var l = now()
  438. let u = l.utc
  439. l = u.local
  440. check l.timezone == local()
  441. check u.timezone == utc()
  442. block: # getDayOfWeek
  443. check getDayOfWeek(01, mJan, 0000) == dSat
  444. check getDayOfWeek(01, mJan, -0023) == dSat
  445. check getDayOfWeek(21, mSep, 1900) == dFri
  446. check getDayOfWeek(01, mJan, 1970) == dThu
  447. check getDayOfWeek(21, mSep, 1970) == dMon
  448. check getDayOfWeek(01, mJan, 2000) == dSat
  449. check getDayOfWeek(01, mJan, 2021) == dFri
  450. block: # between - simple
  451. let x = initDateTime(10, mJan, 2018, 13, 00, 00)
  452. let y = initDateTime(11, mJan, 2018, 12, 00, 00)
  453. doAssert x + between(x, y) == y
  454. block: # between - dst start
  455. usingTimezone("Europe/Stockholm"):
  456. let x = initDateTime(25, mMar, 2018, 00, 00, 00)
  457. let y = initDateTime(25, mMar, 2018, 04, 00, 00)
  458. doAssert x + between(x, y) == y
  459. block: # between - empty interval
  460. let x = now()
  461. let y = x
  462. doAssert x + between(x, y) == y
  463. block: # between - dst end
  464. usingTimezone("Europe/Stockholm"):
  465. let x = initDateTime(27, mOct, 2018, 02, 00, 00)
  466. let y = initDateTime(28, mOct, 2018, 01, 00, 00)
  467. doAssert x + between(x, y) == y
  468. block: # between - long day
  469. usingTimezone("Europe/Stockholm"):
  470. # This day is 25 hours long in Europe/Stockholm
  471. let x = initDateTime(28, mOct, 2018, 00, 30, 00)
  472. let y = initDateTime(29, mOct, 2018, 00, 00, 00)
  473. doAssert between(x, y) == 24.hours + 30.minutes
  474. doAssert x + between(x, y) == y
  475. block: # between - offset change edge case
  476. # This test case is important because in this case
  477. # `x + between(x.utc, y.utc) == y` is not true, which is very rare.
  478. usingTimezone("America/Belem"):
  479. let x = initDateTime(24, mOct, 1987, 00, 00, 00)
  480. let y = initDateTime(26, mOct, 1987, 23, 00, 00)
  481. doAssert x + between(x, y) == y
  482. doAssert y + between(y, x) == x
  483. block: # between - all units
  484. let x = initDateTime(1, mJan, 2000, 00, 00, 00, utc())
  485. let ti = initTimeInterval(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
  486. let y = x + ti
  487. doAssert between(x, y) == ti
  488. doAssert between(y, x) == -ti
  489. block: # between - monthday overflow
  490. let x = initDateTime(31, mJan, 2001, 00, 00, 00, utc())
  491. let y = initDateTime(1, mMar, 2001, 00, 00, 00, utc())
  492. doAssert x + between(x, y) == y
  493. block: # between - misc
  494. block:
  495. let x = initDateTime(31, mDec, 2000, 12, 00, 00, utc())
  496. let y = initDateTime(01, mJan, 2001, 00, 00, 00, utc())
  497. doAssert between(x, y) == 12.hours
  498. block:
  499. let x = initDateTime(31, mDec, 2000, 12, 00, 00, utc())
  500. let y = initDateTime(02, mJan, 2001, 00, 00, 00, utc())
  501. doAssert between(x, y) == 1.days + 12.hours
  502. block:
  503. let x = initDateTime(31, mDec, 1995, 00, 00, 00, utc())
  504. let y = initDateTime(01, mFeb, 2000, 00, 00, 00, utc())
  505. doAssert x + between(x, y) == y
  506. block:
  507. let x = initDateTime(01, mDec, 1995, 00, 00, 00, utc())
  508. let y = initDateTime(31, mJan, 2000, 00, 00, 00, utc())
  509. doAssert x + between(x, y) == y
  510. block:
  511. let x = initDateTime(31, mJan, 2000, 00, 00, 00, utc())
  512. let y = initDateTime(01, mFeb, 2000, 00, 00, 00, utc())
  513. doAssert x + between(x, y) == y
  514. block:
  515. let x = initDateTime(01, mJan, 1995, 12, 00, 00, utc())
  516. let y = initDateTime(01, mFeb, 1995, 00, 00, 00, utc())
  517. doAssert between(x, y) == 4.weeks + 2.days + 12.hours
  518. block:
  519. let x = initDateTime(31, mJan, 1995, 00, 00, 00, utc())
  520. let y = initDateTime(10, mFeb, 1995, 00, 00, 00, utc())
  521. doAssert x + between(x, y) == y
  522. block:
  523. let x = initDateTime(31, mJan, 1995, 00, 00, 00, utc())
  524. let y = initDateTime(10, mMar, 1995, 00, 00, 00, utc())
  525. doAssert x + between(x, y) == y
  526. doAssert between(x, y) == 1.months + 1.weeks
  527. block: # default DateTime https://github.com/nim-lang/RFCs/issues/211
  528. var num = 0
  529. for ai in Month: num.inc
  530. check num == 12
  531. var a: DateTime
  532. check a == DateTime.default
  533. check not a.isInitialized
  534. check $a == "Uninitialized DateTime"
  535. expect(AssertionDefect): discard getDayOfWeek(a.monthday, a.month, a.year)
  536. expect(AssertionDefect): discard a.toTime
  537. expect(AssertionDefect): discard a.utc()
  538. expect(AssertionDefect): discard a.local()
  539. expect(AssertionDefect): discard a.inZone(utc())
  540. expect(AssertionDefect): discard a + initDuration(seconds = 1)
  541. expect(AssertionDefect): discard a + initTimeInterval(seconds = 1)
  542. expect(AssertionDefect): discard a.isLeapDay
  543. expect(AssertionDefect): discard a < a
  544. expect(AssertionDefect): discard a <= a
  545. expect(AssertionDefect): discard getDateStr(a)
  546. expect(AssertionDefect): discard getClockStr(a)
  547. expect(AssertionDefect): discard a.format "yyyy"
  548. expect(AssertionDefect): discard a.format initTimeFormat("yyyy")
  549. expect(AssertionDefect): discard between(a, a)
  550. block: # inX procs
  551. doAssert initDuration(seconds = 1).inSeconds == 1
  552. doAssert initDuration(seconds = -1).inSeconds == -1
  553. doAssert initDuration(seconds = -1, nanoseconds = 1).inSeconds == 0
  554. doAssert initDuration(nanoseconds = -1).inSeconds == 0
  555. doAssert initDuration(milliseconds = 500).inMilliseconds == 500
  556. doAssert initDuration(milliseconds = -500).inMilliseconds == -500
  557. doAssert initDuration(nanoseconds = -999999999).inMilliseconds == -999
  558. block: # getIsoWeekAndYear
  559. doAssert getIsoWeekAndYear(initDateTime(04, mNov, 2019, 00, 00, 00)) == (isoweek: 45.IsoWeekRange, isoyear: 2019.IsoYear)
  560. doAssert initDateTime(dMon, 45, 2019.IsoYear, 00, 00, 00) == initDateTime(04, mNov, 2019, 00, 00, 00)
  561. doAssert getIsoWeekAndYear(initDateTime(28, mDec, 2019, 00, 00, 00)) == (isoweek: 52.IsoWeekRange, isoyear: 2019.IsoYear)
  562. doAssert initDateTime(dSat, 52, 2019.IsoYear, 00, 00, 00) == initDateTime(28, mDec, 2019, 00, 00, 00)
  563. doAssert getIsoWeekAndYear(initDateTime(29, mDec, 2019, 00, 00, 00)) == (isoweek: 52.IsoWeekRange, isoyear: 2019.IsoYear)
  564. doAssert initDateTime(dSun, 52, 2019.IsoYear, 00, 00, 00) == initDateTime(29, mDec, 2019, 00, 00, 00)
  565. doAssert getIsoWeekAndYear(initDateTime(30, mDec, 2019, 00, 00, 00)) == (isoweek: 01.IsoWeekRange, isoyear: 2020.IsoYear)
  566. doAssert initDateTime(dMon, 01, 2020.IsoYear, 00, 00, 00) == initDateTime(30, mDec, 2019, 00, 00, 00)
  567. doAssert getIsoWeekAndYear(initDateTime(31, mDec, 2019, 00, 00, 00)) == (isoweek: 01.IsoWeekRange, isoyear: 2020.IsoYear)
  568. doAssert initDateTime(dTue, 01, 2020.IsoYear, 00, 00, 00) == initDateTime(31, mDec, 2019, 00, 00, 00)
  569. doAssert getIsoWeekAndYear(initDateTime(01, mJan, 2020, 00, 00, 00)) == (isoweek: 01.IsoWeekRange, isoyear: 2020.IsoYear)
  570. doAssert initDateTime(dWed, 01, 2020.IsoYear, 00, 00, 00) == initDateTime(01, mJan, 2020, 00, 00, 00)
  571. doAssert getIsoWeekAndYear(initDateTime(02, mJan, 2020, 00, 00, 00)) == (isoweek: 01.IsoWeekRange, isoyear: 2020.IsoYear)
  572. doAssert initDateTime(dThu, 01, 2020.IsoYear, 00, 00, 00) == initDateTime(02, mJan, 2020, 00, 00, 00)
  573. doAssert getIsoWeekAndYear(initDateTime(05, mApr, 2020, 00, 00, 00)) == (isoweek: 14.IsoWeekRange, isoyear: 2020.IsoYear)
  574. doAssert initDateTime(dSun, 14, 2020.IsoYear, 00, 00, 00) == initDateTime(05, mApr, 2020, 00, 00, 00)
  575. doAssert getIsoWeekAndYear(initDateTime(06, mApr, 2020, 00, 00, 00)) == (isoweek: 15.IsoWeekRange, isoyear: 2020.IsoYear)
  576. doAssert initDateTime(dMon, 15, 2020.IsoYear, 00, 00, 00) == initDateTime(06, mApr, 2020, 00, 00, 00)
  577. doAssert getIsoWeekAndYear(initDateTime(10, mApr, 2020, 00, 00, 00)) == (isoweek: 15.IsoWeekRange, isoyear: 2020.IsoYear)
  578. doAssert initDateTime(dFri, 15, 2020.IsoYear, 00, 00, 00) == initDateTime(10, mApr, 2020, 00, 00, 00)
  579. doAssert getIsoWeekAndYear(initDateTime(12, mApr, 2020, 00, 00, 00)) == (isoweek: 15.IsoWeekRange, isoyear: 2020.IsoYear)
  580. doAssert initDateTime(dSun, 15, 2020.IsoYear, 00, 00, 00) == initDateTime(12, mApr, 2020, 00, 00, 00)
  581. doAssert getIsoWeekAndYear(initDateTime(13, mApr, 2020, 00, 00, 00)) == (isoweek: 16.IsoWeekRange, isoyear: 2020.IsoYear)
  582. doAssert initDateTime(dMon, 16, 2020.IsoYear, 00, 00, 00) == initDateTime(13, mApr, 2020, 00, 00, 00)
  583. doAssert getIsoWeekAndYear(initDateTime(15, mApr, 2020, 00, 00, 00)) == (isoweek: 16.IsoWeekRange, isoyear: 2020.IsoYear)
  584. doAssert initDateTime(dThu, 16, 2020.IsoYear, 00, 00, 00) == initDateTime(16, mApr, 2020, 00, 00, 00)
  585. doAssert getIsoWeekAndYear(initDateTime(17, mJul, 2020, 00, 00, 00)) == (isoweek: 29.IsoWeekRange, isoyear: 2020.IsoYear)
  586. doAssert initDateTime(dFri, 29, 2020.IsoYear, 00, 00, 00) == initDateTime(17, mJul, 2020, 00, 00, 00)
  587. doAssert getIsoWeekAndYear(initDateTime(19, mJul, 2020, 00, 00, 00)) == (isoweek: 29.IsoWeekRange, isoyear: 2020.IsoYear)
  588. doAssert initDateTime(dSun, 29, 2020.IsoYear, 00, 00, 00) == initDateTime(19, mJul, 2020, 00, 00, 00)
  589. doAssert getIsoWeekAndYear(initDateTime(20, mJul, 2020, 00, 00, 00)) == (isoweek: 30.IsoWeekRange, isoyear: 2020.IsoYear)
  590. doAssert initDateTime(dMon, 30, 2020.IsoYear, 00, 00, 00) == initDateTime(20, mJul, 2020, 00, 00, 00)
  591. doAssert getIsoWeekAndYear(initDateTime(23, mJul, 2020, 00, 00, 00)) == (isoweek: 30.IsoWeekRange, isoyear: 2020.IsoYear)
  592. doAssert initDateTime(dThu, 30, 2020.IsoYear, 00, 00, 00) == initDateTime(23, mJul, 2020, 00, 00, 00)
  593. doAssert getIsoWeekAndYear(initDateTime(31, mDec, 2020, 00, 00, 00)) == (isoweek: 53.IsoWeekRange, isoyear: 2020.IsoYear)
  594. doAssert initDateTime(dThu, 53, 2020.IsoYear, 00, 00, 00) == initDateTime(31, mDec, 2020, 00, 00, 00)
  595. doAssert getIsoWeekAndYear(initDateTime(01, mJan, 2021, 00, 00, 00)) == (isoweek: 53.IsoWeekRange, isoyear: 2020.IsoYear)
  596. doAssert initDateTime(dFri, 53, 2020.IsoYear, 00, 00, 00) == initDateTime(01, mJan, 2021, 00, 00, 00)
  597. doAssert getIsoWeekAndYear(initDateTime(02, mJan, 2021, 00, 00, 00)) == (isoweek: 53.IsoWeekRange, isoyear: 2020.IsoYear)
  598. doAssert initDateTime(dSat, 53, 2020.IsoYear, 00, 00, 00) == initDateTime(02, mJan, 2021, 00, 00, 00)
  599. doAssert getIsoWeekAndYear(initDateTime(03, mJan, 2021, 00, 00, 00)) == (isoweek: 53.IsoWeekRange, isoyear: 2020.IsoYear)
  600. doAssert initDateTime(dSun, 53, 2020.IsoYear, 00, 00, 00) == initDateTime(03, mJan, 2021, 00, 00, 00)
  601. doAssert getIsoWeekAndYear(initDateTime(04, mJan, 2021, 00, 00, 00)) == (isoweek: 01.IsoWeekRange, isoyear: 2021.IsoYear)
  602. doAssert initDateTime(dMon, 01, 2021.IsoYear, 00, 00, 00) == initDateTime(04, mJan, 2021, 00, 00, 00)
  603. doAssert getIsoWeekAndYear(initDateTime(01, mFeb, 2021, 00, 00, 00)) == (isoweek: 05.IsoWeekRange, isoyear: 2021.IsoYear)
  604. doAssert initDateTime(dMon, 05, 2021.IsoYear, 00, 00, 00) == initDateTime(01, mFeb, 2021, 00, 00, 00)
  605. doAssert getIsoWeekAndYear(initDateTime(01, mFeb, 2021, 01, 02, 03, 400_000_000, staticTz(hours=1))) == (isoweek: 05.IsoWeekRange, isoyear: 2021.IsoYear)
  606. doAssert initDateTime(dMon, 05, 2021.IsoYear, 01, 02, 03, 400_000_000, staticTz(hours=1)) == initDateTime(01, mFeb, 2021, 01, 02, 03, 400_000_000, staticTz(hours=1))
  607. doAssert getIsoWeekAndYear(initDateTime(01, mApr, +0001, 00, 00, 00)) == (isoweek: 13.IsoWeekRange, isoyear: 0001.IsoYear)
  608. doAssert initDateTime(dSun, 13, 0001.IsoYear, 00, 00, 00) == initDateTime(01, mApr, 0001, 00, 00, 00)
  609. doAssert getIsoWeekAndYear(initDateTime(01, mApr, +0000, 00, 00, 00)) == (isoweek: 13.IsoWeekRange, isoyear: 0000.IsoYear)
  610. doAssert initDateTime(dSat, 13, 0000.IsoYear, 00, 00, 00) == initDateTime(01, mApr, 0000, 00, 00, 00)
  611. doAssert getIsoWeekAndYear(initDateTime(01, mApr, -0001, 00, 00, 00)) == (isoweek: 13.IsoWeekRange, isoyear: (-0001).IsoYear)
  612. doAssert initDateTime(dThu, 13, (-0001).IsoYear, 00, 00, 00) == initDateTime(01, mApr, -0001, 00, 00, 00)
  613. doAssert getIsoWeekAndYear(initDateTime(01, mApr, -0002, 00, 00, 00)) == (isoweek: 14.IsoWeekRange, isoyear: (-0002).IsoYear)
  614. doAssert initDateTime(dWed, 14, (-0002).IsoYear, 00, 00, 00) == initDateTime(01, mApr, -0002, 00, 00, 00)
  615. doAssert getIsoWeekAndYear(initDateTime(01, mApr, -0753, 00, 00, 00)) == (isoweek: 14.IsoWeekRange, isoyear: (-0753).IsoYear)
  616. doAssert initDateTime(dMon, 14, (-0753).IsoYear, 00, 00, 00) == initDateTime(01, mApr, -0753, 00, 00, 00)
  617. block: # getWeeksInIsoYear
  618. doAssert getWeeksInIsoYear((-0014).IsoYear) == 52
  619. doAssert getWeeksInIsoYear((-0013).IsoYear) == 53
  620. doAssert getWeeksInIsoYear((-0012).IsoYear) == 52
  621. doAssert getWeeksInIsoYear((-0009).IsoYear) == 52
  622. doAssert getWeeksInIsoYear((-0008).IsoYear) == 53
  623. doAssert getWeeksInIsoYear((-0007).IsoYear) == 52
  624. doAssert getWeeksInIsoYear((-0003).IsoYear) == 52
  625. doAssert getWeeksInIsoYear((-0002).IsoYear) == 53
  626. doAssert getWeeksInIsoYear((-0001).IsoYear) == 52
  627. doAssert getWeeksInIsoYear(0003.IsoYear) == 52
  628. doAssert getWeeksInIsoYear(0004.IsoYear) == 53
  629. doAssert getWeeksInIsoYear(0005.IsoYear) == 52
  630. doAssert getWeeksInIsoYear(1653.IsoYear) == 52
  631. doAssert getWeeksInIsoYear(1654.IsoYear) == 53
  632. doAssert getWeeksInIsoYear(1655.IsoYear) == 52
  633. doAssert getWeeksInIsoYear(1997.IsoYear) == 52
  634. doAssert getWeeksInIsoYear(1998.IsoYear) == 53
  635. doAssert getWeeksInIsoYear(1999.IsoYear) == 52
  636. doAssert getWeeksInIsoYear(2008.IsoYear) == 52
  637. doAssert getWeeksInIsoYear(2009.IsoYear) == 53
  638. doAssert getWeeksInIsoYear(2010.IsoYear) == 52
  639. doAssert getWeeksInIsoYear(2014.IsoYear) == 52
  640. doAssert getWeeksInIsoYear(2015.IsoYear) == 53
  641. doAssert getWeeksInIsoYear(2016.IsoYear) == 52
  642. block: # parse and generate iso years
  643. # short calendar week with text
  644. parseTest("KW 23 2023", "'KW' VV GGGG",
  645. "2023-06-05T00:00:00Z", 155)
  646. parseTest("KW 5 2023", "'KW' V GGGG",
  647. "2023-01-30T00:00:00Z", 29)
  648. parseTest("KW 05 23 Saturday", "'KW' V GG dddd",
  649. "2023-02-04T00:00:00Z", 34)
  650. parseTest("KW 53 20 Fri", "'KW' VV GG ddd",
  651. "2021-01-01T00:00:00Z", 0)
  652. parseTestExcp("KW 23", "'KW' VV") # no year
  653. parseTestExcp("KW 23", "'KW' V") # no year
  654. parseTestExcp("KW 23", "'KW' GG") # no week
  655. parseTestExcp("KW 2023", "'KW' GGGG") # no week
  656. var dt = initDateTime(5, mJan, 2023, 0, 0, 0, utc())
  657. check dt.format("V") == "1"
  658. check dt.format("VV") == "01"
  659. check dt.format("GG") == "23"
  660. check dt.format("GGGG") == "2023"
  661. check dt.format("dddd 'KW'V GGGG") == "Thursday KW1 2023"
  662. block: # Can be used inside gcsafe proc
  663. proc test(): DateTime {.gcsafe.} =
  664. result = "1970".parse("yyyy")
  665. doAssert test().year == 1970
  666. block: # test FormatLiterals
  667. # since #23861
  668. block:
  669. let dt = dateTime(2024, mJul, 21, 17, 01, 02, 123_321_123, utc())
  670. check dt.format("ss.fff") == "02.123"
  671. check dt.format("fff.ffffff") == "123.123321"
  672. block:
  673. let dt = parse("2024.07.21", "yyyy.MM.dd")
  674. check dt.year == 2024
  675. check dt.month == mJul
  676. check dt.monthday == 21