ttimes.nim 25 KB

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