shithub: mc

ref: 5362f2f03b9ca372fa7900861d07a9858b9dccb0
dir: /lib/date/test/fmt.myr/

View raw version
use std
use date

const main = {
	var buf : byte[1024]
	var d, s

	/* epoch */
	d = date.mkinstant(0, "")
	eq("1970-01-01 00:00:00 +0000", std.bfmt(buf[:], "{D}", d))
	d = date.mkinstant(24*3600*1_000_000, "")
	eq("1970-01-02 00:00:00 +0000", std.bfmt(buf[:], "{D}", d))

	/* epoch + 12 hours */
	d = date.mkinstant(12*3600*1_000_000, "")
	eq("1970-01-01 12:00:00 +0000", std.bfmt(buf[:], "{D}", d))

	/* epoch - 6 hours */
	d = date.mkinstant(-6*3600*1_000_000, "")
	eq("1969-12-31 18:00:00 +0000", std.bfmt(buf[:], "{D}", d))

	/* epoch - 12 hours */
	d = date.mkinstant(-12*3600*1_000_000, "")
	eq("1969-12-31 12:00:00 +0000", std.bfmt(buf[:], "{D}", d))

	/* more or less random: Fri 29 Aug 2014 07:47:43 PM UTC*/
	d = date.mkinstant(1_409_341_663*1_000_000, "")
	eq("2014-08-29 19:47:43 +0000", std.bfmt(buf[:], "{D}", d))

	/* large negative time stamp */
	d = date.mkinstant(-50000000000*1_000_000, "")
	eq("385-07-25 07:06:40 +0000", std.bfmt(buf[:], "{D}", d))

	/* date : the bc */
	d = date.mkinstant(-70000000000*1_000_000, "")
	eq("-249-11-19 19:33:20 +0000", std.bfmt(buf[:], "{D}", d))

	/* test addition and subtraction of dates */
	d = date.mkinstant(-1, "")
	d = date.addperiod(d, `date.Hour 1)
	eq("1970-01-01 00:59:59 +0000", std.bfmt(buf[:], "{D}", d))

	d = date.mkinstant(0, "")
	d = date.addperiod(d, `date.Hour 24)
	eq("1970-01-02 00:00:00 +0000", std.bfmt(buf[:], "{D}", d))

	d = date.mkinstant(0, "")
	d = date.addperiod(d, `date.Day 1)
	eq("1970-01-02 00:00:00 +0000", std.bfmt(buf[:], "{D}", d))

	d = date.subperiod(d, `date.Day 1)
	eq("1970-01-01 00:00:00 +0000", std.bfmt(buf[:], "{D}", d))

	d = date.subperiod(d, `date.Year 1)
	eq("1969-01-01 00:00:00 +0000", std.bfmt(buf[:], "{D}", d))

	d = date.addperiod(d, `date.Day 365)
	eq("1970-01-01 00:00:00 +0000", std.bfmt(buf[:], "{D}", d))

	d = date.addperiod(d, `date.Year 2)
	eq("1972-01-01 00:00:00 +0000", std.bfmt(buf[:], "{D}", d))
	d = date.addperiod(d, `date.Day 365)
	eq("1972-12-31 00:00:00 +0000", std.bfmt(buf[:], "{D}", d))

	/* check days */
	d = date.mkinstant(0, "UTC")
	eq("Thu",	std.bfmt(buf[:], "{f=%a}", d))
	eq("Thursday",	std.bfmt(buf[:], "{f=%A}", d))

	d = date.mkinstant(1409396041 * 1_000_000, "UTC")
	eq("Sat",	std.bfmt(buf[:], "{f=%a}", d))
	eq("Saturday", 	std.bfmt(buf[:], "{f=%A}", d))

	d = date.mkinstant(1509396041 * 1_000_000, "UTC")
	eq("Mon",	std.bfmt(buf[:], "{f=%a}", d))
	eq("Monday", 	std.bfmt(buf[:], "{f=%A}", d))


	d = date.mkinstant(12*3601*1_000_000, "")
	for var i = 0; i < 50; i++
		d = date.addperiod(d, `date.Day 1)
		d = date.addperiod(d, `date.Second 1)
		s = std.fmt("1970-{p=0,w=2}-{p=0,w=2} 12:{p=0,w=2}:{p=0,w=2} +0000", \
			(i+1)/31 + 1, (i+1)%31+1, (i+13)/60, (i+13)%60)
		eq(s, std.bfmt(buf[:], "{D}", d))
		std.slfree(s)
	;;
}

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