ttimes.nim 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. discard """
  2. targets: "c js"
  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.isDst = false
  12. result.utcOffset = offset
  13. result.time = adjTime + initDuration(seconds = offset)
  14. proc zonedTimeFromTime(time: Time): ZonedTime =
  15. result.isDst = false
  16. result.utcOffset = offset
  17. result.time = time
  18. newTimezone("", zonedTimeFromTime, zonedTimeFromAdjTime)
  19. template parseTest(s, f, sExpected: string, ydExpected: int) =
  20. let
  21. parsed = s.parse(f, utc())
  22. parsedStr = $parsed
  23. check parsedStr == sExpected
  24. check parsed.yearday == ydExpected
  25. template parseTestExcp(s, f: string) =
  26. expect ValueError:
  27. let parsed = s.parse(f)
  28. template parseTestTimeOnly(s, f, sExpected: string) =
  29. check sExpected in $s.parse(f, utc())
  30. # because setting a specific timezone for testing is platform-specific, we use
  31. # explicit timezone offsets in all tests.
  32. template runTimezoneTests() =
  33. parseTest("Tuesday at 09:04am on Dec 15, 2015 +0",
  34. "dddd 'at' hh:mmtt 'on' MMM d, yyyy z", "2015-12-15T09:04:00Z", 348)
  35. # ANSIC = "Mon Jan _2 15:04:05 2006"
  36. parseTest("Thu Jan 12 15:04:05 2006 +0", "ddd MMM dd HH:mm:ss yyyy z",
  37. "2006-01-12T15:04:05Z", 11)
  38. # UnixDate = "Mon Jan _2 15:04:05 MST 2006"
  39. parseTest("Thu Jan 12 15:04:05 2006 +0", "ddd MMM dd HH:mm:ss yyyy z",
  40. "2006-01-12T15:04:05Z", 11)
  41. # RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
  42. parseTest("Mon Feb 29 15:04:05 -07:00 2016 +0", "ddd MMM dd HH:mm:ss zzz yyyy z",
  43. "2016-02-29T15:04:05Z", 59) # leap day
  44. # RFC822 = "02 Jan 06 15:04 MST"
  45. parseTest("12 Jan 16 15:04 +0", "dd MMM yy HH:mm z",
  46. "2016-01-12T15:04:00Z", 11)
  47. # RFC822Z = "02 Jan 06 15:04 -0700" # RFC822 with numeric zone
  48. parseTest("01 Mar 16 15:04 -07:00", "dd MMM yy HH:mm zzz",
  49. "2016-03-01T22:04:00Z", 60) # day after february in leap year
  50. # RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
  51. parseTest("Monday, 12-Jan-06 15:04:05 +0", "dddd, dd-MMM-yy HH:mm:ss z",
  52. "2006-01-12T15:04:05Z", 11)
  53. # RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
  54. parseTest("Sun, 01 Mar 2015 15:04:05 +0", "ddd, dd MMM yyyy HH:mm:ss z",
  55. "2015-03-01T15:04:05Z", 59) # day after february in non-leap year
  56. # RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" # RFC1123 with numeric zone
  57. parseTest("Thu, 12 Jan 2006 15:04:05 -07:00", "ddd, dd MMM yyyy HH:mm:ss zzz",
  58. "2006-01-12T22:04:05Z", 11)
  59. # RFC3339 = "2006-01-02T15:04:05Z07:00"
  60. parseTest("2006-01-12T15:04:05Z-07:00", "yyyy-MM-dd'T'HH:mm:ss'Z'zzz",
  61. "2006-01-12T22:04:05Z", 11)
  62. # RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
  63. parseTest("2006-01-12T15:04:05.999999999Z-07:00",
  64. "yyyy-MM-dd'T'HH:mm:ss'.999999999Z'zzz", "2006-01-12T22:04:05Z", 11)
  65. for tzFormat in ["z", "zz", "zzz"]:
  66. # formatting timezone as 'Z' for UTC
  67. parseTest("2001-01-12T22:04:05Z", "yyyy-MM-dd'T'HH:mm:ss" & tzFormat,
  68. "2001-01-12T22:04:05Z", 11)
  69. # timezone offset formats
  70. parseTest("2001-01-12T15:04:05 +7", "yyyy-MM-dd'T'HH:mm:ss z",
  71. "2001-01-12T08:04:05Z", 11)
  72. parseTest("2001-01-12T15:04:05 +07", "yyyy-MM-dd'T'HH:mm:ss zz",
  73. "2001-01-12T08:04:05Z", 11)
  74. parseTest("2001-01-12T15:04:05 +07:00", "yyyy-MM-dd'T'HH:mm:ss zzz",
  75. "2001-01-12T08:04:05Z", 11)
  76. parseTest("2001-01-12T15:04:05 +07:30:59", "yyyy-MM-dd'T'HH:mm:ss zzzz",
  77. "2001-01-12T07:33:06Z", 11)
  78. parseTest("2001-01-12T15:04:05 +0700", "yyyy-MM-dd'T'HH:mm:ss ZZZ",
  79. "2001-01-12T08:04:05Z", 11)
  80. parseTest("2001-01-12T15:04:05 +073059", "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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # datetime before epoch
  173. check $fromUnix(-2147483648).utc == "1901-12-13T20:45:52Z"
  174. block: # incorrect inputs: empty string
  175. parseTestExcp("", "yyyy-MM-dd")
  176. block: # incorrect inputs: year
  177. parseTestExcp("20-02-19", "yyyy-MM-dd")
  178. block: # incorrect inputs: month number
  179. parseTestExcp("2018-2-19", "yyyy-MM-dd")
  180. block: # incorrect inputs: month name
  181. parseTestExcp("2018-Fe", "yyyy-MMM-dd")
  182. block: # incorrect inputs: day
  183. parseTestExcp("2018-02-1", "yyyy-MM-dd")
  184. block: # incorrect inputs: day of week
  185. parseTestExcp("2018-Feb-Mo", "yyyy-MMM-ddd")
  186. block: # incorrect inputs: hour
  187. parseTestExcp("2018-02-19 1:30", "yyyy-MM-dd hh:mm")
  188. block: # incorrect inputs: minute
  189. parseTestExcp("2018-02-19 16:3", "yyyy-MM-dd hh:mm")
  190. block: # incorrect inputs: second
  191. parseTestExcp("2018-02-19 16:30:0", "yyyy-MM-dd hh:mm:ss")
  192. block: # incorrect inputs: timezone (z)
  193. parseTestExcp("2018-02-19 16:30:00 ", "yyyy-MM-dd hh:mm:ss z")
  194. block: # incorrect inputs: timezone (zz) 1
  195. parseTestExcp("2018-02-19 16:30:00 ", "yyyy-MM-dd hh:mm:ss zz")
  196. block: # incorrect inputs: timezone (zz) 2
  197. parseTestExcp("2018-02-19 16:30:00 +1", "yyyy-MM-dd hh:mm:ss zz")
  198. block: # incorrect inputs: timezone (zzz) 1
  199. parseTestExcp("2018-02-19 16:30:00 ", "yyyy-MM-dd hh:mm:ss zzz")
  200. block: # incorrect inputs: timezone (zzz) 2
  201. parseTestExcp("2018-02-19 16:30:00 +01:", "yyyy-MM-dd hh:mm:ss zzz")
  202. block: # incorrect inputs: timezone (zzz) 3
  203. parseTestExcp("2018-02-19 16:30:00 +01:0", "yyyy-MM-dd hh:mm:ss zzz")
  204. block: # 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. block: # 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. block: # _ as a separator
  220. discard parse("2000_01_01", "YYYY'_'MM'_'dd")
  221. block: # 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. block: # isLeapYear
  230. check isLeapYear(2016)
  231. check (not isLeapYear(2015))
  232. check isLeapYear(2000)
  233. check (not isLeapYear(1900))
  234. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # between - empty interval
  458. let x = now()
  459. let y = x
  460. doAssert x + between(x, y) == y
  461. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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. block: # 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
  556. block: # getIsoWeekAndYear
  557. doAssert getIsoWeekAndYear(initDateTime(04, mNov, 2019, 00, 00, 00)) == (isoweek: 45.IsoWeekRange, isoyear: 2019.IsoYear)
  558. doAssert initDateTime(dMon, 45, 2019.IsoYear, 00, 00, 00) == initDateTime(04, mNov, 2019, 00, 00, 00)
  559. doAssert getIsoWeekAndYear(initDateTime(28, mDec, 2019, 00, 00, 00)) == (isoweek: 52.IsoWeekRange, isoyear: 2019.IsoYear)
  560. doAssert initDateTime(dSat, 52, 2019.IsoYear, 00, 00, 00) == initDateTime(28, mDec, 2019, 00, 00, 00)
  561. doAssert getIsoWeekAndYear(initDateTime(29, mDec, 2019, 00, 00, 00)) == (isoweek: 52.IsoWeekRange, isoyear: 2019.IsoYear)
  562. doAssert initDateTime(dSun, 52, 2019.IsoYear, 00, 00, 00) == initDateTime(29, mDec, 2019, 00, 00, 00)
  563. doAssert getIsoWeekAndYear(initDateTime(30, mDec, 2019, 00, 00, 00)) == (isoweek: 01.IsoWeekRange, isoyear: 2020.IsoYear)
  564. doAssert initDateTime(dMon, 01, 2020.IsoYear, 00, 00, 00) == initDateTime(30, mDec, 2019, 00, 00, 00)
  565. doAssert getIsoWeekAndYear(initDateTime(31, mDec, 2019, 00, 00, 00)) == (isoweek: 01.IsoWeekRange, isoyear: 2020.IsoYear)
  566. doAssert initDateTime(dTue, 01, 2020.IsoYear, 00, 00, 00) == initDateTime(31, mDec, 2019, 00, 00, 00)
  567. doAssert getIsoWeekAndYear(initDateTime(01, mJan, 2020, 00, 00, 00)) == (isoweek: 01.IsoWeekRange, isoyear: 2020.IsoYear)
  568. doAssert initDateTime(dWed, 01, 2020.IsoYear, 00, 00, 00) == initDateTime(01, mJan, 2020, 00, 00, 00)
  569. doAssert getIsoWeekAndYear(initDateTime(02, mJan, 2020, 00, 00, 00)) == (isoweek: 01.IsoWeekRange, isoyear: 2020.IsoYear)
  570. doAssert initDateTime(dThu, 01, 2020.IsoYear, 00, 00, 00) == initDateTime(02, mJan, 2020, 00, 00, 00)
  571. doAssert getIsoWeekAndYear(initDateTime(05, mApr, 2020, 00, 00, 00)) == (isoweek: 14.IsoWeekRange, isoyear: 2020.IsoYear)
  572. doAssert initDateTime(dSun, 14, 2020.IsoYear, 00, 00, 00) == initDateTime(05, mApr, 2020, 00, 00, 00)
  573. doAssert getIsoWeekAndYear(initDateTime(06, mApr, 2020, 00, 00, 00)) == (isoweek: 15.IsoWeekRange, isoyear: 2020.IsoYear)
  574. doAssert initDateTime(dMon, 15, 2020.IsoYear, 00, 00, 00) == initDateTime(06, mApr, 2020, 00, 00, 00)
  575. doAssert getIsoWeekAndYear(initDateTime(10, mApr, 2020, 00, 00, 00)) == (isoweek: 15.IsoWeekRange, isoyear: 2020.IsoYear)
  576. doAssert initDateTime(dFri, 15, 2020.IsoYear, 00, 00, 00) == initDateTime(10, mApr, 2020, 00, 00, 00)
  577. doAssert getIsoWeekAndYear(initDateTime(12, mApr, 2020, 00, 00, 00)) == (isoweek: 15.IsoWeekRange, isoyear: 2020.IsoYear)
  578. doAssert initDateTime(dSun, 15, 2020.IsoYear, 00, 00, 00) == initDateTime(12, mApr, 2020, 00, 00, 00)
  579. doAssert getIsoWeekAndYear(initDateTime(13, mApr, 2020, 00, 00, 00)) == (isoweek: 16.IsoWeekRange, isoyear: 2020.IsoYear)
  580. doAssert initDateTime(dMon, 16, 2020.IsoYear, 00, 00, 00) == initDateTime(13, mApr, 2020, 00, 00, 00)
  581. doAssert getIsoWeekAndYear(initDateTime(15, mApr, 2020, 00, 00, 00)) == (isoweek: 16.IsoWeekRange, isoyear: 2020.IsoYear)
  582. doAssert initDateTime(dThu, 16, 2020.IsoYear, 00, 00, 00) == initDateTime(16, mApr, 2020, 00, 00, 00)
  583. doAssert getIsoWeekAndYear(initDateTime(17, mJul, 2020, 00, 00, 00)) == (isoweek: 29.IsoWeekRange, isoyear: 2020.IsoYear)
  584. doAssert initDateTime(dFri, 29, 2020.IsoYear, 00, 00, 00) == initDateTime(17, mJul, 2020, 00, 00, 00)
  585. doAssert getIsoWeekAndYear(initDateTime(19, mJul, 2020, 00, 00, 00)) == (isoweek: 29.IsoWeekRange, isoyear: 2020.IsoYear)
  586. doAssert initDateTime(dSun, 29, 2020.IsoYear, 00, 00, 00) == initDateTime(19, mJul, 2020, 00, 00, 00)
  587. doAssert getIsoWeekAndYear(initDateTime(20, mJul, 2020, 00, 00, 00)) == (isoweek: 30.IsoWeekRange, isoyear: 2020.IsoYear)
  588. doAssert initDateTime(dMon, 30, 2020.IsoYear, 00, 00, 00) == initDateTime(20, mJul, 2020, 00, 00, 00)
  589. doAssert getIsoWeekAndYear(initDateTime(23, mJul, 2020, 00, 00, 00)) == (isoweek: 30.IsoWeekRange, isoyear: 2020.IsoYear)
  590. doAssert initDateTime(dThu, 30, 2020.IsoYear, 00, 00, 00) == initDateTime(23, mJul, 2020, 00, 00, 00)
  591. doAssert getIsoWeekAndYear(initDateTime(31, mDec, 2020, 00, 00, 00)) == (isoweek: 53.IsoWeekRange, isoyear: 2020.IsoYear)
  592. doAssert initDateTime(dThu, 53, 2020.IsoYear, 00, 00, 00) == initDateTime(31, mDec, 2020, 00, 00, 00)
  593. doAssert getIsoWeekAndYear(initDateTime(01, mJan, 2021, 00, 00, 00)) == (isoweek: 53.IsoWeekRange, isoyear: 2020.IsoYear)
  594. doAssert initDateTime(dFri, 53, 2020.IsoYear, 00, 00, 00) == initDateTime(01, mJan, 2021, 00, 00, 00)
  595. doAssert getIsoWeekAndYear(initDateTime(02, mJan, 2021, 00, 00, 00)) == (isoweek: 53.IsoWeekRange, isoyear: 2020.IsoYear)
  596. doAssert initDateTime(dSat, 53, 2020.IsoYear, 00, 00, 00) == initDateTime(02, mJan, 2021, 00, 00, 00)
  597. doAssert getIsoWeekAndYear(initDateTime(03, mJan, 2021, 00, 00, 00)) == (isoweek: 53.IsoWeekRange, isoyear: 2020.IsoYear)
  598. doAssert initDateTime(dSun, 53, 2020.IsoYear, 00, 00, 00) == initDateTime(03, mJan, 2021, 00, 00, 00)
  599. doAssert getIsoWeekAndYear(initDateTime(04, mJan, 2021, 00, 00, 00)) == (isoweek: 01.IsoWeekRange, isoyear: 2021.IsoYear)
  600. doAssert initDateTime(dMon, 01, 2021.IsoYear, 00, 00, 00) == initDateTime(04, mJan, 2021, 00, 00, 00)
  601. doAssert getIsoWeekAndYear(initDateTime(01, mFeb, 2021, 00, 00, 00)) == (isoweek: 05.IsoWeekRange, isoyear: 2021.IsoYear)
  602. doAssert initDateTime(dMon, 05, 2021.IsoYear, 00, 00, 00) == initDateTime(01, mFeb, 2021, 00, 00, 00)
  603. doAssert getIsoWeekAndYear(initDateTime(01, mFeb, 2021, 01, 02, 03, 400_000_000, staticTz(hours=1))) == (isoweek: 05.IsoWeekRange, isoyear: 2021.IsoYear)
  604. 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))
  605. doAssert getIsoWeekAndYear(initDateTime(01, mApr, +0001, 00, 00, 00)) == (isoweek: 13.IsoWeekRange, isoyear: 0001.IsoYear)
  606. doAssert initDateTime(dSun, 13, 0001.IsoYear, 00, 00, 00) == initDateTime(01, mApr, 0001, 00, 00, 00)
  607. doAssert getIsoWeekAndYear(initDateTime(01, mApr, +0000, 00, 00, 00)) == (isoweek: 13.IsoWeekRange, isoyear: 0000.IsoYear)
  608. doAssert initDateTime(dSat, 13, 0000.IsoYear, 00, 00, 00) == initDateTime(01, mApr, 0000, 00, 00, 00)
  609. doAssert getIsoWeekAndYear(initDateTime(01, mApr, -0001, 00, 00, 00)) == (isoweek: 13.IsoWeekRange, isoyear: (-0001).IsoYear)
  610. doAssert initDateTime(dThu, 13, (-0001).IsoYear, 00, 00, 00) == initDateTime(01, mApr, -0001, 00, 00, 00)
  611. doAssert getIsoWeekAndYear(initDateTime(01, mApr, -0002, 00, 00, 00)) == (isoweek: 14.IsoWeekRange, isoyear: (-0002).IsoYear)
  612. doAssert initDateTime(dWed, 14, (-0002).IsoYear, 00, 00, 00) == initDateTime(01, mApr, -0002, 00, 00, 00)
  613. doAssert getIsoWeekAndYear(initDateTime(01, mApr, -0753, 00, 00, 00)) == (isoweek: 14.IsoWeekRange, isoyear: (-0753).IsoYear)
  614. doAssert initDateTime(dMon, 14, (-0753).IsoYear, 00, 00, 00) == initDateTime(01, mApr, -0753, 00, 00, 00)
  615. block: # getWeeksInIsoYear
  616. doAssert getWeeksInIsoYear((-0014).IsoYear) == 52
  617. doAssert getWeeksInIsoYear((-0013).IsoYear) == 53
  618. doAssert getWeeksInIsoYear((-0012).IsoYear) == 52
  619. doAssert getWeeksInIsoYear((-0009).IsoYear) == 52
  620. doAssert getWeeksInIsoYear((-0008).IsoYear) == 53
  621. doAssert getWeeksInIsoYear((-0007).IsoYear) == 52
  622. doAssert getWeeksInIsoYear((-0003).IsoYear) == 52
  623. doAssert getWeeksInIsoYear((-0002).IsoYear) == 53
  624. doAssert getWeeksInIsoYear((-0001).IsoYear) == 52
  625. doAssert getWeeksInIsoYear(0003.IsoYear) == 52
  626. doAssert getWeeksInIsoYear(0004.IsoYear) == 53
  627. doAssert getWeeksInIsoYear(0005.IsoYear) == 52
  628. doAssert getWeeksInIsoYear(1653.IsoYear) == 52
  629. doAssert getWeeksInIsoYear(1654.IsoYear) == 53
  630. doAssert getWeeksInIsoYear(1655.IsoYear) == 52
  631. doAssert getWeeksInIsoYear(1997.IsoYear) == 52
  632. doAssert getWeeksInIsoYear(1998.IsoYear) == 53
  633. doAssert getWeeksInIsoYear(1999.IsoYear) == 52
  634. doAssert getWeeksInIsoYear(2008.IsoYear) == 52
  635. doAssert getWeeksInIsoYear(2009.IsoYear) == 53
  636. doAssert getWeeksInIsoYear(2010.IsoYear) == 52
  637. doAssert getWeeksInIsoYear(2014.IsoYear) == 52
  638. doAssert getWeeksInIsoYear(2015.IsoYear) == 53
  639. doAssert getWeeksInIsoYear(2016.IsoYear) == 52