ttimes.nim 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. discard """
  2. target: "c js"
  3. """
  4. import times, strutils, unittest
  5. when not defined(js):
  6. import os
  7. # Normally testament configures unittest with environment variables,
  8. # but that doesn't work for the JS target. So instead we must set the correct
  9. # settings here.
  10. addOutputFormatter(
  11. newConsoleOutputFormatter(PRINT_FAILURES, colorOutput = false))
  12. proc staticTz(hours, minutes, seconds: int = 0): Timezone {.noSideEffect.} =
  13. let offset = hours * 3600 + minutes * 60 + seconds
  14. proc zonedTimeFromAdjTime(adjTime: Time): ZonedTime {.locks: 0.} =
  15. result.isDst = false
  16. result.utcOffset = offset
  17. result.time = adjTime + initDuration(seconds = offset)
  18. proc zonedTimeFromTime(time: Time): ZonedTime {.locks: 0.}=
  19. result.isDst = false
  20. result.utcOffset = offset
  21. result.time = time
  22. newTimezone("", zonedTimeFromTime, zonedTImeFromAdjTime)
  23. template parseTest(s, f, sExpected: string, ydExpected: int) =
  24. let
  25. parsed = s.parse(f, utc())
  26. parsedStr = $parsed
  27. check parsedStr == sExpected
  28. check parsed.yearday == ydExpected
  29. template parseTestExcp(s, f: string) =
  30. expect ValueError:
  31. let parsed = s.parse(f)
  32. template parseTestTimeOnly(s, f, sExpected: string) =
  33. check sExpected in $s.parse(f, utc())
  34. # because setting a specific timezone for testing is platform-specific, we use
  35. # explicit timezone offsets in all tests.
  36. template runTimezoneTests() =
  37. parseTest("Tuesday at 09:04am on Dec 15, 2015 +0",
  38. "dddd 'at' hh:mmtt 'on' MMM d, yyyy z", "2015-12-15T09:04:00Z", 348)
  39. # ANSIC = "Mon Jan _2 15:04:05 2006"
  40. parseTest("Thu Jan 12 15:04:05 2006 +0", "ddd MMM dd HH:mm:ss yyyy z",
  41. "2006-01-12T15:04:05Z", 11)
  42. # UnixDate = "Mon Jan _2 15:04:05 MST 2006"
  43. parseTest("Thu Jan 12 15:04:05 2006 +0", "ddd MMM dd HH:mm:ss yyyy z",
  44. "2006-01-12T15:04:05Z", 11)
  45. # RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
  46. parseTest("Mon Feb 29 15:04:05 -07:00 2016 +0", "ddd MMM dd HH:mm:ss zzz yyyy z",
  47. "2016-02-29T15:04:05Z", 59) # leap day
  48. # RFC822 = "02 Jan 06 15:04 MST"
  49. parseTest("12 Jan 16 15:04 +0", "dd MMM yy HH:mm z",
  50. "2016-01-12T15:04:00Z", 11)
  51. # RFC822Z = "02 Jan 06 15:04 -0700" # RFC822 with numeric zone
  52. parseTest("01 Mar 16 15:04 -07:00", "dd MMM yy HH:mm zzz",
  53. "2016-03-01T22:04:00Z", 60) # day after february in leap year
  54. # RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
  55. parseTest("Monday, 12-Jan-06 15:04:05 +0", "dddd, dd-MMM-yy HH:mm:ss z",
  56. "2006-01-12T15:04:05Z", 11)
  57. # RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
  58. parseTest("Sun, 01 Mar 2015 15:04:05 +0", "ddd, dd MMM yyyy HH:mm:ss z",
  59. "2015-03-01T15:04:05Z", 59) # day after february in non-leap year
  60. # RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" # RFC1123 with numeric zone
  61. parseTest("Thu, 12 Jan 2006 15:04:05 -07:00", "ddd, dd MMM yyyy HH:mm:ss zzz",
  62. "2006-01-12T22:04:05Z", 11)
  63. # RFC3339 = "2006-01-02T15:04:05Z07:00"
  64. parseTest("2006-01-12T15:04:05Z-07:00", "yyyy-MM-dd'T'HH:mm:ss'Z'zzz",
  65. "2006-01-12T22:04:05Z", 11)
  66. # RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
  67. parseTest("2006-01-12T15:04:05.999999999Z-07:00",
  68. "yyyy-MM-dd'T'HH:mm:ss'.999999999Z'zzz", "2006-01-12T22:04:05Z", 11)
  69. for tzFormat in ["z", "zz", "zzz"]:
  70. # formatting timezone as 'Z' for UTC
  71. parseTest("2001-01-12T22:04:05Z", "yyyy-MM-dd'T'HH:mm:ss" & tzFormat,
  72. "2001-01-12T22:04:05Z", 11)
  73. # timezone offset formats
  74. parseTest("2001-01-12T15:04:05 +7", "yyyy-MM-dd'T'HH:mm:ss z",
  75. "2001-01-12T08:04:05Z", 11)
  76. parseTest("2001-01-12T15:04:05 +07", "yyyy-MM-dd'T'HH:mm:ss zz",
  77. "2001-01-12T08:04:05Z", 11)
  78. parseTest("2001-01-12T15:04:05 +07:00", "yyyy-MM-dd'T'HH:mm:ss zzz",
  79. "2001-01-12T08:04:05Z", 11)
  80. parseTest("2001-01-12T15:04:05 +07:30:59", "yyyy-MM-dd'T'HH:mm:ss zzzz",
  81. "2001-01-12T07:33:06Z", 11)
  82. # Kitchen = "3:04PM"
  83. parseTestTimeOnly("3:04PM", "h:mmtt", "15:04:00")
  84. # Bug with parse not setting DST properly if the current local DST differs from
  85. # the date being parsed. Need to test parse dates both in and out of DST. We
  86. # are testing that be relying on the fact that tranforming a TimeInfo to a Time
  87. # and back again will correctly set the DST value. With the incorrect parse
  88. # behavior this will introduce a one hour offset from the named time and the
  89. # parsed time if the DST value differs between the current time and the date we
  90. # are parsing.
  91. let dstT1 = parse("2016-01-01 00:00:00", "yyyy-MM-dd HH:mm:ss")
  92. let dstT2 = parse("2016-06-01 00:00:00", "yyyy-MM-dd HH:mm:ss")
  93. check dstT1 == toTime(dstT1).local
  94. check dstT2 == toTime(dstT2).local
  95. block dstTest:
  96. # parsing will set isDST in relation to the local time. We take a date in
  97. # January and one in July to maximize the probability to hit one date with DST
  98. # and one without on the local machine. However, this is not guaranteed.
  99. let
  100. parsedJan = parse("2016-01-05 04:00:00+01:00", "yyyy-MM-dd HH:mm:sszzz")
  101. parsedJul = parse("2016-07-01 04:00:00+01:00", "yyyy-MM-dd HH:mm:sszzz")
  102. check toTime(parsedJan).toUnix == 1451962800
  103. check toTime(parsedJul).toUnix == 1467342000
  104. suite "ttimes":
  105. # Generate tests for multiple timezone files where available
  106. # Set the TZ env var for each test
  107. when defined(linux) or defined(macosx):
  108. let tz_dir = getEnv("TZDIR", "/usr/share/zoneinfo")
  109. const f = "yyyy-MM-dd HH:mm zzz"
  110. let orig_tz = getEnv("TZ")
  111. var tz_cnt = 0
  112. for tz_fn in walkFiles(tz_dir & "/**/*"):
  113. if symlinkExists(tz_fn) or tz_fn.endsWith(".tab") or
  114. tz_fn.endsWith(".list"):
  115. continue
  116. test "test for " & tz_fn:
  117. tz_cnt.inc
  118. putEnv("TZ", tz_fn)
  119. runTimezoneTests()
  120. test "enough timezone files tested":
  121. check tz_cnt > 10
  122. test "dst handling":
  123. putEnv("TZ", "Europe/Stockholm")
  124. # In case of an impossible time, the time is moved to after the impossible time period
  125. check initDateTime(26, mMar, 2017, 02, 30, 00).format(f) == "2017-03-26 03:30 +02:00"
  126. # In case of an ambiguous time, the earlier time is choosen
  127. check initDateTime(29, mOct, 2017, 02, 00, 00).format(f) == "2017-10-29 02:00 +02:00"
  128. # These are just dates on either side of the dst switch
  129. check initDateTime(29, mOct, 2017, 01, 00, 00).format(f) == "2017-10-29 01:00 +02:00"
  130. check initDateTime(29, mOct, 2017, 01, 00, 00).isDst
  131. check initDateTime(29, mOct, 2017, 03, 01, 00).format(f) == "2017-10-29 03:01 +01:00"
  132. check (not initDateTime(29, mOct, 2017, 03, 01, 00).isDst)
  133. check initDateTime(21, mOct, 2017, 01, 00, 00).format(f) == "2017-10-21 01:00 +02:00"
  134. test "issue #6520":
  135. putEnv("TZ", "Europe/Stockholm")
  136. var local = fromUnix(1469275200).local
  137. var utc = fromUnix(1469275200).utc
  138. let claimedOffset = initDuration(seconds = local.utcOffset)
  139. local.utcOffset = 0
  140. check claimedOffset == utc.toTime - local.toTime
  141. test "issue #5704":
  142. putEnv("TZ", "Asia/Seoul")
  143. let diff = parse("19700101-000000", "yyyyMMdd-hhmmss").toTime - parse("19000101-000000", "yyyyMMdd-hhmmss").toTime
  144. check diff == initDuration(seconds = 2208986872)
  145. test "issue #6465":
  146. putEnv("TZ", "Europe/Stockholm")
  147. let dt = parse("2017-03-25 12:00", "yyyy-MM-dd hh:mm")
  148. check $(dt + initTimeInterval(days = 1)) == "2017-03-26T12:00:00+02:00"
  149. check $(dt + initDuration(days = 1)) == "2017-03-26T13:00:00+02:00"
  150. test "datetime before epoch":
  151. check $fromUnix(-2147483648).utc == "1901-12-13T20:45:52Z"
  152. test "adding/subtracting time across dst":
  153. putenv("TZ", "Europe/Stockholm")
  154. let dt1 = initDateTime(26, mMar, 2017, 03, 00, 00)
  155. check $(dt1 - 1.seconds) == "2017-03-26T01:59:59+01:00"
  156. var dt2 = initDateTime(29, mOct, 2017, 02, 59, 59)
  157. check $(dt2 + 1.seconds) == "2017-10-29T02:00:00+01:00"
  158. putEnv("TZ", orig_tz)
  159. else:
  160. # not on Linux or macosx: run in the local timezone only
  161. test "parseTest":
  162. runTimezoneTests()
  163. test "incorrect inputs: empty string":
  164. parseTestExcp("", "yyyy-MM-dd")
  165. test "incorrect inputs: year":
  166. parseTestExcp("20-02-19", "yyyy-MM-dd")
  167. test "incorrect inputs: month number":
  168. parseTestExcp("2018-2-19", "yyyy-MM-dd")
  169. test "incorrect inputs: month name":
  170. parseTestExcp("2018-Fe", "yyyy-MMM-dd")
  171. test "incorrect inputs: day":
  172. parseTestExcp("2018-02-1", "yyyy-MM-dd")
  173. test "incorrect inputs: day of week":
  174. parseTestExcp("2018-Feb-Mo", "yyyy-MMM-ddd")
  175. test "incorrect inputs: hour":
  176. parseTestExcp("2018-02-19 1:30", "yyyy-MM-dd hh:mm")
  177. test "incorrect inputs: minute":
  178. parseTestExcp("2018-02-19 16:3", "yyyy-MM-dd hh:mm")
  179. test "incorrect inputs: second":
  180. parseTestExcp("2018-02-19 16:30:0", "yyyy-MM-dd hh:mm:ss")
  181. test "incorrect inputs: timezone (z)":
  182. parseTestExcp("2018-02-19 16:30:00 ", "yyyy-MM-dd hh:mm:ss z")
  183. test "incorrect inputs: timezone (zz) 1":
  184. parseTestExcp("2018-02-19 16:30:00 ", "yyyy-MM-dd hh:mm:ss zz")
  185. test "incorrect inputs: timezone (zz) 2":
  186. parseTestExcp("2018-02-19 16:30:00 +1", "yyyy-MM-dd hh:mm:ss zz")
  187. test "incorrect inputs: timezone (zzz) 1":
  188. parseTestExcp("2018-02-19 16:30:00 ", "yyyy-MM-dd hh:mm:ss zzz")
  189. test "incorrect inputs: timezone (zzz) 2":
  190. parseTestExcp("2018-02-19 16:30:00 +01:", "yyyy-MM-dd hh:mm:ss zzz")
  191. test "incorrect inputs: timezone (zzz) 3":
  192. parseTestExcp("2018-02-19 16:30:00 +01:0", "yyyy-MM-dd hh:mm:ss zzz")
  193. test "incorrect inputs: year (yyyy/uuuu)":
  194. parseTestExcp("-0001", "yyyy")
  195. parseTestExcp("-0001", "YYYY")
  196. parseTestExcp("1", "yyyy")
  197. parseTestExcp("12345", "yyyy")
  198. parseTestExcp("1", "uuuu")
  199. parseTestExcp("12345", "uuuu")
  200. parseTestExcp("-1 BC", "UUUU g")
  201. test "incorrect inputs: invalid sign":
  202. parseTestExcp("+1", "YYYY")
  203. parseTestExcp("+1", "dd")
  204. parseTestExcp("+1", "MM")
  205. parseTestExcp("+1", "hh")
  206. parseTestExcp("+1", "mm")
  207. parseTestExcp("+1", "ss")
  208. test "_ as a seperator":
  209. discard parse("2000_01_01", "YYYY'_'MM'_'dd")
  210. test "dynamic timezone":
  211. let tz = staticTz(seconds = -9000)
  212. let dt = initDateTime(1, mJan, 2000, 12, 00, 00, tz)
  213. check dt.utcOffset == -9000
  214. check dt.isDst == false
  215. check $dt == "2000-01-01T12:00:00+02:30"
  216. check $dt.utc == "2000-01-01T09:30:00Z"
  217. check $dt.utc.inZone(tz) == $dt
  218. test "isLeapYear":
  219. check isLeapYear(2016)
  220. check (not isLeapYear(2015))
  221. check isLeapYear(2000)
  222. check (not isLeapYear(1900))
  223. test "TimeInterval":
  224. let t = fromUnix(876124714).utc # Mon 6 Oct 08:58:34 BST 1997
  225. # Interval tests
  226. let t2 = t - 2.years
  227. check t2.year == 1995
  228. let t3 = (t - 7.years - 34.minutes - 24.seconds)
  229. check t3.year == 1990
  230. check t3.minute == 24
  231. check t3.second == 10
  232. check (t + 1.hours).toTime.toUnix == t.toTime.toUnix + 60 * 60
  233. check (t - 1.hours).toTime.toUnix == t.toTime.toUnix - 60 * 60
  234. test "TimeInterval - months":
  235. var dt = initDateTime(1, mFeb, 2017, 00, 00, 00, utc())
  236. check $(dt - initTimeInterval(months = 1)) == "2017-01-01T00:00:00Z"
  237. dt = initDateTime(15, mMar, 2017, 00, 00, 00, utc())
  238. check $(dt - initTimeInterval(months = 1)) == "2017-02-15T00:00:00Z"
  239. dt = initDateTime(31, mMar, 2017, 00, 00, 00, utc())
  240. # This happens due to monthday overflow. It's consistent with Phobos.
  241. check $(dt - initTimeInterval(months = 1)) == "2017-03-03T00:00:00Z"
  242. test "duration":
  243. let d = initDuration
  244. check d(hours = 48) + d(days = 5) == d(weeks = 1)
  245. let dt = initDateTime(01, mFeb, 2000, 00, 00, 00, 0, utc()) + d(milliseconds = 1)
  246. check dt.nanosecond == convert(Milliseconds, Nanoseconds, 1)
  247. check d(seconds = 1, milliseconds = 500) * 2 == d(seconds = 3)
  248. check d(seconds = 3) div 2 == d(seconds = 1, milliseconds = 500)
  249. check d(milliseconds = 1001).seconds == 1
  250. check d(seconds = 1, milliseconds = 500) - d(milliseconds = 1250) ==
  251. d(milliseconds = 250)
  252. check d(seconds = 1, milliseconds = 1) < d(seconds = 1, milliseconds = 2)
  253. check d(seconds = 1) <= d(seconds = 1)
  254. check d(seconds = 0) - d(milliseconds = 1500) == d(milliseconds = -1500)
  255. check d(milliseconds = -1500) == d(seconds = -1, milliseconds = -500)
  256. check d(seconds = -1, milliseconds = 500) == d(milliseconds = -500)
  257. check initDuration(seconds = 1, nanoseconds = 2) <=
  258. initDuration(seconds = 1, nanoseconds = 3)
  259. check (initDuration(seconds = 1, nanoseconds = 3) <=
  260. initDuration(seconds = 1, nanoseconds = 1)).not
  261. test "large/small dates":
  262. discard initDateTime(1, mJan, -35_000, 12, 00, 00, utc())
  263. # with local tz
  264. discard initDateTime(1, mJan, -35_000, 12, 00, 00)
  265. discard initDateTime(1, mJan, 35_000, 12, 00, 00)
  266. # with duration/timeinterval
  267. let dt = initDateTime(1, mJan, -35_000, 12, 00, 00, utc()) +
  268. initDuration(seconds = 1)
  269. check dt.second == 1
  270. let dt2 = dt + 35_001.years
  271. check $dt2 == "0001-01-01T12:00:01Z"
  272. test "compare datetimes":
  273. var dt1 = now()
  274. var dt2 = dt1
  275. check dt1 == dt2
  276. check dt1 <= dt2
  277. dt2 = dt2 + 1.seconds
  278. check dt1 < dt2
  279. test "adding/subtracting TimeInterval":
  280. # add/subtract TimeIntervals and Time/TimeInfo
  281. let now = getTime().utc
  282. check now + convert(Seconds, Nanoseconds, 1).nanoseconds == now + 1.seconds
  283. check now + 1.weeks == now + 7.days
  284. check now - 1.seconds == now - 3.seconds + 2.seconds
  285. check now + 65.seconds == now + 1.minutes + 5.seconds
  286. check now + 60.minutes == now + 1.hours
  287. check now + 24.hours == now + 1.days
  288. check now + 13.months == now + 1.years + 1.months
  289. check toUnix(fromUnix(0) + 2.seconds) == 2
  290. check toUnix(fromUnix(0) - 2.seconds) == -2
  291. var ti1 = now + 1.years
  292. ti1 = ti1 - 1.years
  293. check ti1 == now
  294. ti1 = ti1 + 1.days
  295. check ti1 == now + 1.days
  296. # Bug with adding a day to a Time
  297. let day = 24.hours
  298. let tomorrow = now + day
  299. check tomorrow - now == initDuration(days = 1)
  300. # Disabled for JS because it fails due to precision errors
  301. # (The JS target uses float64 for int64).
  302. when not defined(js):
  303. test "fromWinTime/toWinTime":
  304. check 0.fromUnix.toWinTime.fromWinTime.toUnix == 0
  305. check (-1).fromWinTime.nanosecond == convert(Seconds, Nanoseconds, 1) - 100
  306. check (-1).fromWinTime.toWinTime == -1
  307. # One nanosecond is discarded due to differences in time resolution
  308. check initTime(0, 101).toWinTime.fromWinTime.nanosecond == 100
  309. check initTime(0, 101).toWinTime.fromWinTime.nanosecond == 100
  310. test "issue 7620":
  311. let layout = "M/d/yyyy' 'h:mm:ss' 'tt' 'z"
  312. let t7620_am = parse("4/15/2017 12:01:02 AM +0", layout, utc())
  313. check t7620_am.format(layout) == "4/15/2017 12:01:02 AM Z"
  314. let t7620_pm = parse("4/15/2017 12:01:02 PM +0", layout, utc())
  315. check t7620_pm.format(layout) == "4/15/2017 12:01:02 PM Z"
  316. test "format":
  317. var dt = initDateTime(1, mJan, -0001,
  318. 17, 01, 02, 123_456_789,
  319. staticTz(hours = 1, minutes = 2, seconds = 3))
  320. check dt.format("d") == "1"
  321. check dt.format("dd") == "01"
  322. check dt.format("ddd") == "Fri"
  323. check dt.format("dddd") == "Friday"
  324. check dt.format("h") == "5"
  325. check dt.format("hh") == "05"
  326. check dt.format("H") == "17"
  327. check dt.format("HH") == "17"
  328. check dt.format("m") == "1"
  329. check dt.format("mm") == "01"
  330. check dt.format("M") == "1"
  331. check dt.format("MM") == "01"
  332. check dt.format("MMM") == "Jan"
  333. check dt.format("MMMM") == "January"
  334. check dt.format("s") == "2"
  335. check dt.format("ss") == "02"
  336. check dt.format("t") == "P"
  337. check dt.format("tt") == "PM"
  338. check dt.format("yy") == "02"
  339. check dt.format("yyyy") == "0002"
  340. check dt.format("YYYY") == "2"
  341. check dt.format("uuuu") == "-0001"
  342. check dt.format("UUUU") == "-1"
  343. check dt.format("z") == "-1"
  344. check dt.format("zz") == "-01"
  345. check dt.format("zzz") == "-01:02"
  346. check dt.format("zzzz") == "-01:02:03"
  347. check dt.format("g") == "BC"
  348. check dt.format("fff") == "123"
  349. check dt.format("ffffff") == "123456"
  350. check dt.format("fffffffff") == "123456789"
  351. dt.nanosecond = 1
  352. check dt.format("fff") == "000"
  353. check dt.format("ffffff") == "000000"
  354. check dt.format("fffffffff") == "000000001"
  355. dt.year = 12345
  356. check dt.format("yyyy") == "+12345"
  357. check dt.format("uuuu") == "+12345"
  358. dt.year = -12345
  359. check dt.format("yyyy") == "+12346"
  360. check dt.format("uuuu") == "-12345"
  361. expect ValueError:
  362. discard initTimeFormat("'")
  363. expect ValueError:
  364. discard initTimeFormat("'foo")
  365. expect ValueError:
  366. discard initTimeFormat("foo'")
  367. for tz in [
  368. (staticTz(seconds = 0), "+0", "+00", "+00:00"), # UTC
  369. (staticTz(seconds = -3600), "+1", "+01", "+01:00"), # CET
  370. (staticTz(seconds = -39600), "+11", "+11", "+11:00"), # two digits
  371. (staticTz(seconds = -1800), "+0", "+00", "+00:30"), # half an hour
  372. (staticTz(seconds = 7200), "-2", "-02", "-02:00"), # positive
  373. (staticTz(seconds = 38700), "-10", "-10", "-10:45")]: # positive with three quaters hour
  374. let dt = initDateTime(1, mJan, 2000, 00, 00, 00, tz[0])
  375. doAssert dt.format("z") == tz[1]
  376. doAssert dt.format("zz") == tz[2]
  377. doAssert dt.format("zzz") == tz[3]
  378. test "parse":
  379. check $parse("20180101", "yyyyMMdd", utc()) == "2018-01-01T00:00:00Z"
  380. parseTestExcp("+120180101", "yyyyMMdd")
  381. check parse("1", "YYYY", utc()).year == 1
  382. check parse("1 BC", "YYYY g", utc()).year == 0
  383. check parse("0001 BC", "yyyy g", utc()).year == 0
  384. check parse("+12345 BC", "yyyy g", utc()).year == -12344
  385. check parse("1 AD", "YYYY g", utc()).year == 1
  386. check parse("0001 AD", "yyyy g", utc()).year == 1
  387. check parse("+12345 AD", "yyyy g", utc()).year == 12345
  388. check parse("-1", "UUUU", utc()).year == -1
  389. check parse("-0001", "uuuu", utc()).year == -1
  390. discard parse("foobar", "'foobar'")
  391. discard parse("foo'bar", "'foo''''bar'")
  392. discard parse("'", "''")
  393. parseTestExcp("2000 A", "yyyy g")
  394. test "countLeapYears":
  395. # 1920, 2004 and 2020 are leap years, and should be counted starting at the following year
  396. check countLeapYears(1920) + 1 == countLeapYears(1921)
  397. check countLeapYears(2004) + 1 == countLeapYears(2005)
  398. check countLeapYears(2020) + 1 == countLeapYears(2021)
  399. test "timezoneConversion":
  400. var l = now()
  401. let u = l.utc
  402. l = u.local
  403. check l.timezone == local()
  404. check u.timezone == utc()
  405. test "getDayOfWeek":
  406. check getDayOfWeek(01, mJan, 0000) == dSat
  407. check getDayOfWeek(01, mJan, -0023) == dSat
  408. check getDayOfWeek(21, mSep, 1900) == dFri
  409. check getDayOfWeek(01, mJan, 1970) == dThu
  410. check getDayOfWeek(21, mSep, 1970) == dMon
  411. check getDayOfWeek(01, mJan, 2000) == dSat
  412. check getDayOfWeek(01, mJan, 2021) == dFri