summaryrefslogtreecommitdiff
path: root/lib/date/test/parse.myr
blob: 6bc734723679be0fa1faafb4380bfbd58cc3e110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std
use date

const main = {
	var buf : byte[1024]

	/* should be unix epoch */
	match date.parsefmt("%Y-%m-%d %H:%M:%S %z", "1969-12-31 16:00:00 -0800")
	| `std.Ok d:
		std.assert(d.actual == 0, "got wrong date")
		eq(std.bfmt(buf[:], "{D}", d), "1969-12-31 16:00:00 -0800")
	| `std.Err m:
		std.fatal("failed to parse date: {}\n", m)
	;;

	match date.parsefmt("%Y-%m-%d", "1969 12 31")
	| `std.Ok d:
		std.fatal("should have errored")
	| `std.Err m:
		eq(std.bfmt(buf[:], "{}", m), "expected separator '-', found ' '")
	;;

	/* parse into utc */
	match date.parsefmtz("%Y-%m-%d %H:%M:%S %z", "1969-12-31 16:00:00 -0800", "")
	| `std.Ok d:
		std.assert(d.actual == 0, "got wrong date")
		eq(std.bfmt(buf[:], "{D}", d), "1970-1-01 00:00:00 +0000")
	| `std.Err m:
		std.fatal("failed to parse date: {}\n", m)
	;;

	/*Fri 29 Aug 2014 07:47:43 PM UTC*/
	match date.parsefmt("%Y-%m-%d %z", "1932-10-23 +0500")
	| `std.Ok d:
		std.assert(d.actual ==  -1173675600 * 1_000_000, "wrong timestamp")
		eq(std.bfmt(buf[:], "{D}", d), "1932-10-23 00:00:00 +0500")
	| `std.Err m:
		std.fatal("Failed to parse date: {}", m)
	;;
}

const eq = {actual, expected
	if !std.sleq(expected, actual)
		std.fatal("expected date {}, got {}\n", expected, actual)
	;;
}