shithub: mc

ref: 0f956d6bb55e0455326f4627024c1fa8c8adbff8
dir: /lib/date/test/parse.myr/

View raw version
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-01-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)
	;;

	/*Names*/
	match date.parsefmt("%a, %d %b %Y %H:%M:%S %z", "Thu, 21 Sep 2017 13:55:05 -0400")
	| `std.Ok d:
		std.assert(d.actual == 1506016505 * 1_000_000, "wrong timestamp")
		eq(std.bfmt(buf[:], "{D}", d), "2017-09-21 13:55:05 -0400")
	| `std.Err m:
		std.fatal("Failed to parse date: {}", m)
	;;
}

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