1import time
2
3fn test_parse() {
4	s := '2018-01-27 12:48:34'
5	t := time.parse(s) or {
6		assert false
7		return
8	}
9	assert t.year == 2018 && t.month == 1 && t.day == 27 && t.hour == 12 && t.minute == 48 && t.second == 34
10	assert t.unix == 1517057314
11}
12
13fn test_parse_invalid() {
14	s := 'Invalid time string'
15	time.parse(s) or {
16		assert true
17		return
18	}
19	assert false
20}
21
22fn test_parse_rfc2822() {
23	s1 := 'Thu, 12 Dec 2019 06:07:45 GMT'
24	t1 := time.parse_rfc2822(s1) or {
25		assert false
26		return
27	}
28	assert t1.year == 2019 && t1.month == 12 && t1.day == 12 && t1.hour == 6 && t1.minute == 7 && t1.second == 45
29	assert t1.unix == 1576130865
30	s2 := 'Thu 12 Dec 2019 06:07:45 +0800'
31	t2 := time.parse_rfc2822(s2) or {
32		assert false
33		return
34	}
35	assert t2.year == 2019 && t2.month == 12 && t2.day == 12 && t2.hour == 6 && t2.minute == 7 && t2.second == 45
36	assert t2.unix == 1576130865
37}
38
39fn test_parse_rfc2822_invalid() {
40	s3 := 'Thu 12 Foo 2019 06:07:45 +0800'
41	time.parse_rfc2822(s3) or {
42		assert true
43		return
44	}
45	assert false
46}
47
48fn test_iso8601_parse_utc_diff() {
49	format_utc 	:= '2020-06-05T15:38:06.015959+00:00'
50	format_cest := '2020-06-05T15:38:06.015959+02:00'
51
52	t_utc 	:= time.parse_iso8601(format_utc) or {panic(err)}
53	t_cest 	:= time.parse_iso8601(format_cest) or {panic(err)}
54
55	assert t_utc.year  == 2020
56	assert t_cest.year == 2020
57	assert t_utc.month == 6
58	assert t_cest.month == 6
59	assert t_utc.day == 5
60	assert t_cest.day == 5
61	// if it was formatted in utc it should be
62	// two hours before if it was formatted in
63	// cest time
64	assert t_utc.hour == (t_cest.hour + 2)
65	assert t_utc.minute == 38
66	assert t_cest.minute == 38
67	assert t_utc.second == 6
68	assert t_cest.second == 6
69	assert t_utc.microsecond == 15959
70	assert t_cest.microsecond == 15959
71}