ttimes.nim 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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 transforming 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. template usingTimezone(tz: string, body: untyped) =
  105. when defined(linux) or defined(macosx):
  106. let oldZone = getEnv("TZ")
  107. putEnv("TZ", tz)
  108. body
  109. putEnv("TZ", oldZone)
  110. suite "ttimes":
  111. # Generate tests for multiple timezone files where available
  112. # Set the TZ env var for each test
  113. when defined(linux) or defined(macosx):
  114. let tz_dir = getEnv("TZDIR", "/usr/share/zoneinfo")
  115. const f = "yyyy-MM-dd HH:mm zzz"
  116. var tz_cnt = 0
  117. for timezone in walkFiles(tz_dir & "/**/*"):
  118. if symlinkExists(timezone) or timezone.endsWith(".tab") or
  119. timezone.endsWith(".list"):
  120. continue
  121. usingTimezone(timezone):
  122. test "test for " & timezone:
  123. tz_cnt.inc
  124. runTimezoneTests()
  125. test "enough timezone files tested":
  126. check tz_cnt > 10
  127. else:
  128. # not on Linux or macosx: run in the local timezone only
  129. test "parseTest":
  130. runTimezoneTests()
  131. test "dst handling":
  132. usingTimezone("Europe/Stockholm"):
  133. # In case of an impossible time, the time is moved to after the
  134. # impossible time period
  135. check initDateTime(26, mMar, 2017, 02, 30, 00).format(f) ==
  136. "2017-03-26 03:30 +02:00"
  137. # In case of an ambiguous time, the earlier time is chosen
  138. check initDateTime(29, mOct, 2017, 02, 00, 00).format(f) ==
  139. "2017-10-29 02:00 +02:00"
  140. # These are just dates on either side of the dst switch
  141. check initDateTime(29, mOct, 2017, 01, 00, 00).format(f) ==
  142. "2017-10-29 01:00 +02:00"
  143. check initDateTime(29, mOct, 2017, 01, 00, 00).isDst
  144. check initDateTime(29, mOct, 2017, 03, 01, 00).format(f) ==
  145. "2017-10-29 03:01 +01:00"
  146. check (not initDateTime(29, mOct, 2017, 03, 01, 00).isDst)
  147. check initDateTime(21, mOct, 2017, 01, 00, 00).format(f) ==
  148. "2017-10-21 01:00 +02:00"
  149. test "issue #6520":
  150. usingTimezone("Europe/Stockholm"):
  151. var local = fromUnix(1469275200).local
  152. var utc = fromUnix(1469275200).utc
  153. let claimedOffset = initDuration(seconds = local.utcOffset)
  154. local.utcOffset = 0
  155. check claimedOffset == utc.toTime - local.toTime
  156. test "issue #5704":
  157. usingTimezone("Asia/Seoul"):
  158. let diff = parse("19700101-000000", "yyyyMMdd-hhmmss").toTime -
  159. parse("19000101-000000", "yyyyMMdd-hhmmss").toTime
  160. check diff == initDuration(seconds = 2208986872)
  161. test "issue #6465":
  162. usingTimezone("Europe/Stockholm"):
  163. let dt = parse("2017-03-25 12:00", "yyyy-MM-dd hh:mm")
  164. check $(dt + initTimeInterval(days = 1)) == "2017-03-26T12:00:00+02:00"
  165. check $(dt + initDuration(days = 1)) == "2017-03-26T13:00:00+02:00"
  166. test "adding/subtracting time across dst":
  167. usingTimezone("Europe/Stockholm"):
  168. let dt1 = initDateTime(26, mMar, 2017, 03, 00, 00)
  169. check $(dt1 - 1.seconds) == "2017-03-26T01:59:59+01:00"
  170. var dt2 = initDateTime(29, mOct, 2017, 02, 59, 59)
  171. check $(dt2 + 1.seconds) == "2017-10-29T02:00:00+01:00"
  172. test "datetime before epoch":
  173. check $fromUnix(-2147483648).utc == "1901-12-13T20:45:52Z"
  174. test "incorrect inputs: empty string":
  175. parseTestExcp("", "yyyy-MM-dd")
  176. test "incorrect inputs: year":
  177. parseTestExcp("20-02-19", "yyyy-MM-dd")
  178. test "incorrect inputs: month number":
  179. parseTestExcp("2018-2-19", "yyyy-MM-dd")
  180. test "incorrect inputs: month name":
  181. parseTestExcp("2018-Fe", "yyyy-MMM-dd")
  182. test "incorrect inputs: day":
  183. parseTestExcp("2018-02-1", "yyyy-MM-dd")
  184. test "incorrect inputs: day of week":
  185. parseTestExcp("2018-Feb-Mo", "yyyy-MMM-ddd")
  186. test "incorrect inputs: hour":
  187. parseTestExcp("2018-02-19 1:30", "yyyy-MM-dd hh:mm")
  188. test "incorrect inputs: minute":
  189. parseTestExcp("2018-02-19 16:3", "yyyy-MM-dd hh:mm")
  190. test "incorrect inputs: second":
  191. parseTestExcp("2018-02-19 16:30:0", "yyyy-MM-dd hh:mm:ss")
  192. test "incorrect inputs: timezone (z)":
  193. parseTestExcp("2018-02-19 16:30:00 ", "yyyy-MM-dd hh:mm:ss z")
  194. test "incorrect inputs: timezone (zz) 1":
  195. parseTestExcp("2018-02-19 16:30:00 ", "yyyy-MM-dd hh:mm:ss zz")
  196. test "incorrect inputs: timezone (zz) 2":
  197. parseTestExcp("2018-02-19 16:30:00 +1", "yyyy-MM-dd hh:mm:ss zz")
  198. test "incorrect inputs: timezone (zzz) 1":
  199. parseTestExcp("2018-02-19 16:30:00 ", "yyyy-MM-dd hh:mm:ss zzz")
  200. test "incorrect inputs: timezone (zzz) 2":
  201. parseTestExcp("2018-02-19 16:30:00 +01:", "yyyy-MM-dd hh:mm:ss zzz")
  202. test "incorrect inputs: timezone (zzz) 3":
  203. parseTestExcp("2018-02-19 16:30:00 +01:0", "yyyy-MM-dd hh:mm:ss zzz")
  204. test "incorrect inputs: year (yyyy/uuuu)":
  205. parseTestExcp("-0001", "yyyy")
  206. parseTestExcp("-0001", "YYYY")
  207. parseTestExcp("1", "yyyy")
  208. parseTestExcp("12345", "yyyy")
  209. parseTestExcp("1", "uuuu")
  210. parseTestExcp("12345", "uuuu")
  211. parseTestExcp("-1 BC", "UUUU g")
  212. test "incorrect inputs: invalid sign":
  213. parseTestExcp("+1", "YYYY")
  214. parseTestExcp("+1", "dd")
  215. parseTestExcp("+1", "MM")
  216. parseTestExcp("+1", "hh")
  217. parseTestExcp("+1", "mm")
  218. parseTestExcp("+1", "ss")
  219. test "_ as a separator":
  220. discard parse("2000_01_01", "YYYY'_'MM'_'dd")
  221. test "dynamic timezone":
  222. let tz = staticTz(seconds = -9000)
  223. let dt = initDateTime(1, mJan, 2000, 12, 00, 00, tz)
  224. check dt.utcOffset == -9000
  225. check dt.isDst == false
  226. check $dt == "2000-01-01T12:00:00+02:30"
  227. check $dt.utc == "2000-01-01T09:30:00Z"
  228. check $dt.utc.inZone(tz) == $dt
  229. test "isLeapYear":
  230. check isLeapYear(2016)
  231. check (not isLeapYear(2015))
  232. check isLeapYear(2000)
  233. check (not isLeapYear(1900))
  234. test "TimeInterval":
  235. let t = fromUnix(876124714).utc # Mon 6 Oct 08:58:34 BST 1997
  236. # Interval tests
  237. let t2 = t - 2.years
  238. check t2.year == 1995
  239. let t3 = (t - 7.years - 34.minutes - 24.seconds)
  240. check t3.year == 1990
  241. check t3.minute == 24
  242. check t3.second == 10
  243. check (t + 1.hours).toTime.toUnix == t.toTime.toUnix + 60 * 60
  244. check (t - 1.hours).toTime.toUnix == t.toTime.toUnix - 60 * 60
  245. test "TimeInterval - months":
  246. var dt = initDateTime(1, mFeb, 2017, 00, 00, 00, utc())
  247. check $(dt - initTimeInterval(months = 1)) == "2017-01-01T00:00:00Z"
  248. dt = initDateTime(15, mMar, 2017, 00, 00, 00, utc())
  249. check $(dt - initTimeInterval(months = 1)) == "2017-02-15T00:00:00Z"
  250. dt = initDateTime(31, mMar, 2017, 00, 00, 00, utc())
  251. # This happens due to monthday overflow. It's consistent with Phobos.
  252. check $(dt - initTimeInterval(months = 1)) == "2017-03-03T00:00:00Z"
  253. test "duration":
  254. let d = initDuration
  255. check d(hours = 48) + d(days = 5) == d(weeks = 1)
  256. let dt = initDateTime(01, mFeb, 2000, 00, 00, 00, 0, utc()) + d(milliseconds = 1)
  257. check dt.nanosecond == convert(Milliseconds, Nanoseconds, 1)
  258. check d(seconds = 1, milliseconds = 500) * 2 == d(seconds = 3)
  259. check d(seconds = 3) div 2 == d(seconds = 1, milliseconds = 500)
  260. check d(milliseconds = 1001).inSeconds == 1
  261. check d(seconds = 1, milliseconds = 500) - d(milliseconds = 1250) ==
  262. d(milliseconds = 250)
  263. check d(seconds = 1, milliseconds = 1) < d(seconds = 1, milliseconds = 2)
  264. check d(seconds = 1) <= d(seconds = 1)
  265. check d(seconds = 0) - d(milliseconds = 1500) == d(milliseconds = -1500)
  266. check d(milliseconds = -1500) == d(seconds = -1, milliseconds = -500)
  267. check d(seconds = -1, milliseconds = 500) == d(milliseconds = -500)
  268. check initDuration(seconds = 1, nanoseconds = 2) <=
  269. initDuration(seconds = 1, nanoseconds = 3)
  270. check (initDuration(seconds = 1, nanoseconds = 3) <=
  271. initDuration(seconds = 1, nanoseconds = 1)).not
  272. test "large/small dates":
  273. discard initDateTime(1, mJan, -35_000, 12, 00, 00, utc())
  274. # with local tz
  275. discard initDateTime(1, mJan, -35_000, 12, 00, 00)
  276. discard initDateTime(1, mJan, 35_000, 12, 00, 00)
  277. # with duration/timeinterval
  278. let dt = initDateTime(1, mJan, -35_000, 12, 00, 00, utc()) +
  279. initDuration(seconds = 1)
  280. check dt.second == 1
  281. let dt2 = dt + 35_001.years
  282. check $dt2 == "0001-01-01T12:00:01Z"
  283. test "compare datetimes":
  284. var dt1 = now()
  285. var dt2 = dt1
  286. check dt1 == dt2
  287. check dt1 <= dt2
  288. dt2 = dt2 + 1.seconds
  289. check dt1 < dt2
  290. test "adding/subtracting TimeInterval":
  291. # add/subtract TimeIntervals and Time/TimeInfo
  292. let now = getTime().utc
  293. let isSpecial = now.isLeapDay
  294. check now + convert(Seconds, Nanoseconds, 1).nanoseconds == now + 1.seconds
  295. check now + 1.weeks == now + 7.days
  296. check now - 1.seconds == now - 3.seconds + 2.seconds
  297. check now + 65.seconds == now + 1.minutes + 5.seconds
  298. check now + 60.minutes == now + 1.hours
  299. check now + 24.hours == now + 1.days
  300. if not isSpecial:
  301. check now + 13.months == now + 1.years + 1.months
  302. check toUnix(fromUnix(0) + 2.seconds) == 2
  303. check toUnix(fromUnix(0) - 2.seconds) == -2
  304. var ti1 = now + 1.years
  305. ti1 = ti1 - 1.years
  306. if not isSpecial:
  307. check ti1 == now
  308. ti1 = ti1 + 1.days
  309. if not isSpecial:
  310. check ti1 == now + 1.days
  311. # Bug with adding a day to a Time
  312. let day = 24.hours
  313. let tomorrow = now + day
  314. check tomorrow - now == initDuration(days = 1)
  315. # Disabled for JS because it fails due to precision errors
  316. # (The JS target uses float64 for int64).
  317. when not defined(js):
  318. test "fromWinTime/toWinTime":
  319. check 0.fromUnix.toWinTime.fromWinTime.toUnix == 0
  320. check (-1).fromWinTime.nanosecond == convert(Seconds, Nanoseconds, 1) - 100
  321. check (-1).fromWinTime.toWinTime == -1
  322. # One nanosecond is discarded due to differences in time resolution
  323. check initTime(0, 101).toWinTime.fromWinTime.nanosecond == 100
  324. check initTime(0, 101).toWinTime.fromWinTime.nanosecond == 100
  325. test "issue 7620":
  326. let layout = "M/d/yyyy' 'h:mm:ss' 'tt' 'z"
  327. let t7620_am = parse("4/15/2017 12:01:02 AM +0", layout, utc())
  328. check t7620_am.format(layout) == "4/15/2017 12:01:02 AM Z"
  329. let t7620_pm = parse("4/15/2017 12:01:02 PM +0", layout, utc())
  330. check t7620_pm.format(layout) == "4/15/2017 12:01:02 PM Z"
  331. test "format":
  332. var dt = initDateTime(1, mJan, -0001,
  333. 17, 01, 02, 123_456_789,
  334. staticTz(hours = 1, minutes = 2, seconds = 3))
  335. check dt.format("d") == "1"
  336. check dt.format("dd") == "01"
  337. check dt.format("ddd") == "Fri"
  338. check dt.format("dddd") == "Friday"
  339. check dt.format("h") == "5"
  340. check dt.format("hh") == "05"
  341. check dt.format("H") == "17"
  342. check dt.format("HH") == "17"
  343. check dt.format("m") == "1"
  344. check dt.format("mm") == "01"
  345. check dt.format("M") == "1"
  346. check dt.format("MM") == "01"
  347. check dt.format("MMM") == "Jan"
  348. check dt.format("MMMM") == "January"
  349. check dt.format("s") == "2"
  350. check dt.format("ss") == "02"
  351. check dt.format("t") == "P"
  352. check dt.format("tt") == "PM"
  353. check dt.format("yy") == "02"
  354. check dt.format("yyyy") == "0002"
  355. check dt.format("YYYY") == "2"
  356. check dt.format("uuuu") == "-0001"
  357. check dt.format("UUUU") == "-1"
  358. check dt.format("z") == "-1"
  359. check dt.format("zz") == "-01"
  360. check dt.format("zzz") == "-01:02"
  361. check dt.format("zzzz") == "-01:02:03"
  362. check dt.format("g") == "BC"
  363. check dt.format("fff") == "123"
  364. check dt.format("ffffff") == "123456"
  365. check dt.format("fffffffff") == "123456789"
  366. dt.nanosecond = 1
  367. check dt.format("fff") == "000"
  368. check dt.format("ffffff") == "000000"
  369. check dt.format("fffffffff") == "000000001"
  370. dt.year = 12345
  371. check dt.format("yyyy") == "+12345"
  372. check dt.format("uuuu") == "+12345"
  373. dt.year = -12345
  374. check dt.format("yyyy") == "+12346"
  375. check dt.format("uuuu") == "-12345"
  376. expect ValueError:
  377. discard initTimeFormat("'")
  378. expect ValueError:
  379. discard initTimeFormat("'foo")
  380. expect ValueError:
  381. discard initTimeFormat("foo'")
  382. for tz in [
  383. (staticTz(seconds = 0), "+0", "+00", "+00:00"), # UTC
  384. (staticTz(seconds = -3600), "+1", "+01", "+01:00"), # CET
  385. (staticTz(seconds = -39600), "+11", "+11", "+11:00"), # two digits
  386. (staticTz(seconds = -1800), "+0", "+00", "+00:30"), # half an hour
  387. (staticTz(seconds = 7200), "-2", "-02", "-02:00"), # positive
  388. (staticTz(seconds = 38700), "-10", "-10", "-10:45")]: # positive with three quaters hour
  389. let dt = initDateTime(1, mJan, 2000, 00, 00, 00, tz[0])
  390. doAssert dt.format("z") == tz[1]
  391. doAssert dt.format("zz") == tz[2]
  392. doAssert dt.format("zzz") == tz[3]
  393. test "format locale":
  394. let loc = DateTimeLocale(
  395. MMM: ["Fir","Sec","Thi","Fou","Fif","Six","Sev","Eig","Nin","Ten","Ele","Twe"],
  396. MMMM: ["Firsty", "Secondy", "Thirdy", "Fourthy", "Fifthy", "Sixthy", "Seventhy", "Eighthy", "Ninthy", "Tenthy", "Eleventhy", "Twelfthy"],
  397. ddd: ["Red", "Ora.", "Yel.", "Gre.", "Blu.", "Vio.", "Whi."],
  398. dddd: ["Red", "Orange", "Yellow", "Green", "Blue", "Violet", "White"],
  399. )
  400. var dt = initDateTime(5, mJan, 2010, 17, 01, 02, utc())
  401. check dt.format("d", loc) == "5"
  402. check dt.format("dd", loc) == "05"
  403. check dt.format("ddd", loc) == "Ora."
  404. check dt.format("dddd", loc) == "Orange"
  405. check dt.format("M", loc) == "1"
  406. check dt.format("MM", loc) == "01"
  407. check dt.format("MMM", loc) == "Fir"
  408. check dt.format("MMMM", loc) == "Firsty"
  409. test "parse":
  410. check $parse("20180101", "yyyyMMdd", utc()) == "2018-01-01T00:00:00Z"
  411. parseTestExcp("+120180101", "yyyyMMdd")
  412. check parse("1", "YYYY", utc()).year == 1
  413. check parse("1 BC", "YYYY g", utc()).year == 0
  414. check parse("0001 BC", "yyyy g", utc()).year == 0
  415. check parse("+12345 BC", "yyyy g", utc()).year == -12344
  416. check parse("1 AD", "YYYY g", utc()).year == 1
  417. check parse("0001 AD", "yyyy g", utc()).year == 1
  418. check parse("+12345 AD", "yyyy g", utc()).year == 12345
  419. check parse("-1", "UUUU", utc()).year == -1
  420. check parse("-0001", "uuuu", utc()).year == -1
  421. discard parse("foobar", "'foobar'")
  422. discard parse("foo'bar", "'foo''''bar'")
  423. discard parse("'", "''")
  424. parseTestExcp("2000 A", "yyyy g")
  425. test "parse locale":
  426. let loc = DateTimeLocale(
  427. MMM: ["Fir","Sec","Thi","Fou","Fif","Six","Sev","Eig","Nin","Ten","Ele","Twe"],
  428. MMMM: ["Firsty", "Secondy", "Thirdy", "Fourthy", "Fifthy", "Sixthy", "Seventhy", "Eighthy", "Ninthy", "Tenthy", "Eleventhy", "Twelfthy"],
  429. ddd: ["Red", "Ora.", "Yel.", "Gre.", "Blu.", "Vio.", "Whi."],
  430. dddd: ["Red", "Orange", "Yellow", "Green", "Blue", "Violet", "White"],
  431. )
  432. check $parse("02 Fir 2019", "dd MMM yyyy", utc(), loc) == "2019-01-02T00:00:00Z"
  433. check $parse("Fourthy 6, 2017", "MMMM d, yyyy", utc(), loc) == "2017-04-06T00:00:00Z"
  434. test "timezoneConversion":
  435. var l = now()
  436. let u = l.utc
  437. l = u.local
  438. check l.timezone == local()
  439. check u.timezone == utc()
  440. test "getDayOfWeek":
  441. check getDayOfWeek(01, mJan, 0000) == dSat
  442. check getDayOfWeek(01, mJan, -0023) == dSat
  443. check getDayOfWeek(21, mSep, 1900) == dFri
  444. check getDayOfWeek(01, mJan, 1970) == dThu
  445. check getDayOfWeek(21, mSep, 1970) == dMon
  446. check getDayOfWeek(01, mJan, 2000) == dSat
  447. check getDayOfWeek(01, mJan, 2021) == dFri
  448. test "between - simple":
  449. let x = initDateTime(10, mJan, 2018, 13, 00, 00)
  450. let y = initDateTime(11, mJan, 2018, 12, 00, 00)
  451. doAssert x + between(x, y) == y
  452. test "between - dst start":
  453. usingTimezone("Europe/Stockholm"):
  454. let x = initDateTime(25, mMar, 2018, 00, 00, 00)
  455. let y = initDateTime(25, mMar, 2018, 04, 00, 00)
  456. doAssert x + between(x, y) == y
  457. test "between - empty interval":
  458. let x = now()
  459. let y = x
  460. doAssert x + between(x, y) == y
  461. test "between - dst end":
  462. usingTimezone("Europe/Stockholm"):
  463. let x = initDateTime(27, mOct, 2018, 02, 00, 00)
  464. let y = initDateTime(28, mOct, 2018, 01, 00, 00)
  465. doAssert x + between(x, y) == y
  466. test "between - long day":
  467. usingTimezone("Europe/Stockholm"):
  468. # This day is 25 hours long in Europe/Stockholm
  469. let x = initDateTime(28, mOct, 2018, 00, 30, 00)
  470. let y = initDateTime(29, mOct, 2018, 00, 00, 00)
  471. doAssert between(x, y) == 24.hours + 30.minutes
  472. doAssert x + between(x, y) == y
  473. test "between - offset change edge case":
  474. # This test case is important because in this case
  475. # `x + between(x.utc, y.utc) == y` is not true, which is very rare.
  476. usingTimezone("America/Belem"):
  477. let x = initDateTime(24, mOct, 1987, 00, 00, 00)
  478. let y = initDateTime(26, mOct, 1987, 23, 00, 00)
  479. doAssert x + between(x, y) == y
  480. doAssert y + between(y, x) == x
  481. test "between - all units":
  482. let x = initDateTime(1, mJan, 2000, 00, 00, 00, utc())
  483. let ti = initTimeInterval(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
  484. let y = x + ti
  485. doAssert between(x, y) == ti
  486. doAssert between(y, x) == -ti
  487. test "between - monthday overflow":
  488. let x = initDateTime(31, mJan, 2001, 00, 00, 00, utc())
  489. let y = initDateTime(1, mMar, 2001, 00, 00, 00, utc())
  490. doAssert x + between(x, y) == y
  491. test "between - misc":
  492. block:
  493. let x = initDateTime(31, mDec, 2000, 12, 00, 00, utc())
  494. let y = initDateTime(01, mJan, 2001, 00, 00, 00, utc())
  495. doAssert between(x, y) == 12.hours
  496. block:
  497. let x = initDateTime(31, mDec, 2000, 12, 00, 00, utc())
  498. let y = initDateTime(02, mJan, 2001, 00, 00, 00, utc())
  499. doAssert between(x, y) == 1.days + 12.hours
  500. block:
  501. let x = initDateTime(31, mDec, 1995, 00, 00, 00, utc())
  502. let y = initDateTime(01, mFeb, 2000, 00, 00, 00, utc())
  503. doAssert x + between(x, y) == y
  504. block:
  505. let x = initDateTime(01, mDec, 1995, 00, 00, 00, utc())
  506. let y = initDateTime(31, mJan, 2000, 00, 00, 00, utc())
  507. doAssert x + between(x, y) == y
  508. block:
  509. let x = initDateTime(31, mJan, 2000, 00, 00, 00, utc())
  510. let y = initDateTime(01, mFeb, 2000, 00, 00, 00, utc())
  511. doAssert x + between(x, y) == y
  512. block:
  513. let x = initDateTime(01, mJan, 1995, 12, 00, 00, utc())
  514. let y = initDateTime(01, mFeb, 1995, 00, 00, 00, utc())
  515. doAssert between(x, y) == 4.weeks + 2.days + 12.hours
  516. block:
  517. let x = initDateTime(31, mJan, 1995, 00, 00, 00, utc())
  518. let y = initDateTime(10, mFeb, 1995, 00, 00, 00, utc())
  519. doAssert x + between(x, y) == y
  520. block:
  521. let x = initDateTime(31, mJan, 1995, 00, 00, 00, utc())
  522. let y = initDateTime(10, mMar, 1995, 00, 00, 00, utc())
  523. doAssert x + between(x, y) == y
  524. doAssert between(x, y) == 1.months + 1.weeks
  525. test "default DateTime": # https://github.com/nim-lang/RFCs/issues/211
  526. var num = 0
  527. for ai in Month: num.inc
  528. check num == 12
  529. var a: DateTime
  530. check a == DateTime.default
  531. check not a.isInitialized
  532. check $a == "Uninitialized DateTime"
  533. expect(AssertionDefect): discard getDayOfWeek(a.monthday, a.month, a.year)
  534. expect(AssertionDefect): discard a.toTime
  535. expect(AssertionDefect): discard a.utc()
  536. expect(AssertionDefect): discard a.local()
  537. expect(AssertionDefect): discard a.inZone(utc())
  538. expect(AssertionDefect): discard a + initDuration(seconds = 1)
  539. expect(AssertionDefect): discard a + initTimeInterval(seconds = 1)
  540. expect(AssertionDefect): discard a.isLeapDay
  541. expect(AssertionDefect): discard a < a
  542. expect(AssertionDefect): discard a <= a
  543. expect(AssertionDefect): discard getDateStr(a)
  544. expect(AssertionDefect): discard getClockStr(a)
  545. expect(AssertionDefect): discard a.format "yyyy"
  546. expect(AssertionDefect): discard a.format initTimeFormat("yyyy")
  547. expect(AssertionDefect): discard between(a, a)
  548. test "inX procs":
  549. doAssert initDuration(seconds = 1).inSeconds == 1
  550. doAssert initDuration(seconds = -1).inSeconds == -1
  551. doAssert initDuration(seconds = -1, nanoseconds = 1).inSeconds == 0
  552. doAssert initDuration(nanoseconds = -1).inSeconds == 0
  553. doAssert initDuration(milliseconds = 500).inMilliseconds == 500
  554. doAssert initDuration(milliseconds = -500).inMilliseconds == -500
  555. doAssert initDuration(nanoseconds = -999999999).inMilliseconds == -999