shithub: mc

ref: 0b561ccda3352aa132a5809b73de295ff37bc979
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-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)
	;;
}