#!/usr/bin/env ascript
print_func(func:function) = printf('%s = %s\n', func, func.expr())
tester(test:expr):map:dynamic_scope = printf("%s .. %s\n", test, eval(test)):nomap

lambda {
	tester(`time.datetime(2011, 1, 10, 8, 3, 0, 0).unixtime)
	tester(`time.datetime(2001, 9, 9, 1, 46, 40, 0).unixtime)
	tester(`time.datetime(2009, 2, 13, 23, 31, 30, 0).unixtime)
}()
lambda {
	tester(`time.parse('Sun, 06 Nov 1994 08:49:37 -0900'))
	tester(`time.parse('Sun, 06 Nov 1994 08:49:37 +0900'))
	tester(`time.parse('Sun, 06 Nov 1994 08:49:37 +0845'))
	tester(`time.parse('Sun, 06 Nov 1994 08:49:37 +1245'))
	tester(`time.parse('Sun, 06 Nov 1994 08:49:37 -0930'))
	tester(`time.parse('Sun, 06 Nov 1994 08:49:37 GMT'))
	tester(`time.parse('Sunday, 06-Nov-94 08:49:37 GMT'))
	tester(`time.parse('Sun Nov  6 08:49:37 1994'))
	tester(`time.parse('Sat, 06 Nov 2010 08:49:37 GMT'))
	tester(`time.parse('Saturday, 06-Nov-10 08:49:37 GMT'))
	tester(`time.parse('Sat Nov  6 08:49:37 2010'))
	tester(`time.parse('Wed, 06 Nov 2069 08:49:37 GMT'))
	tester(`time.parse('Wednesday, 06-Nov-69 08:49:37 GMT'))
	tester(`time.parse('Wed Nov  6 08:49:37 2069'))
	tester(`time.parse('Thu, 06 Nov 2070 08:49:37 GMT'))
	tester(`time.parse('Thursday, 06-Nov-70 08:49:37 GMT'))
	tester(`time.parse('Sun Nov  6 08:49:37 2070'))
	tester(`time.parse('Fri Dec 31 23:59:59 9999'))
	tester(`time.parse('2010-11-06T08:49:37+09:00'))
	tester(`time.parse('Tue Aug 31 23:54:46 +0000 2010'))
	try {
		tester(`time.parse(''))
	} except {|e|
		println('expected error occurs: ', e.text)
	}
	try {
		tester(`time.parse('x'))
	} except {|e|
		println('expected error occurs: ', e.text)
	}
	try {
		tester(`time.parse('111'))
	} except {|e|
		println('expected error occurs: ', e.text)
	}
	try {
		tester(`time.parse('Sun Dec 31 24:59:59 9999'))
	} except {|e|
		println('expected error occurs: ', e.text)
	}
	try {
		tester(`time.parse('Sun Dec 31 003:59:59 2010'))
	} except {|e|
		println('expected error occurs: ', e.text)
	}
}()
lambda {
	tdOrg = time.datetime(1900, 1, 1)
	dayOffsets1 = rands(1000, 365 * 200)
	dayOffsets2 = rands(1000, 365 * 200)
	td1 = tdOrg + time.delta(dayOffsets1)
	td2 = tdOrg + time.delta(dayOffsets2)
	println("operation of datetime .. ",
		chooseif(((td1 - td2):*days == dayOffsets1 - dayOffsets2).and(),
														"OK", "**** NG ****"))
	tester(`(3 * time.delta(1, 0, 0)))
	tester(`(time.delta(1, 0, 0) * 3))
	tester(`(time.delta(0, 1, 0) * 3))
	tester(`(time.delta(0, 0, 1) * 3))
	tester(`(time.delta(0, 3600, 0) * 24))
	tester(`(time.delta(0, 3600, 0) * 24 * 4))
	tester(`(time.delta(0, 0, 100000) * 10))
	tester(`(time.delta(0, 3600 * 24 - 1, 1000000)))
	tester(`(time.delta(0, 3600 * 24 - 1, 0) + time.delta(0, 1, 0)))
	tester(`(time.delta(3, 0, 0) + time.delta(5, 0, 0)))
	tester(`(time.delta(3, 0, 0) - time.delta(5, 0, 0)))
	tester(`(time.delta(8, 1000, 1000) + time.delta(5, 300, 300)))
	tester(`(time.delta(8, 1000, 1000) - time.delta(5, 300, 300)))
	tester(`(time.delta(8, 0, 0) - time.delta(5, 300, 300)))
	tester(`(time.delta(5, 300, 300) + time.delta(2, 86099, 999700)))
	tester(`(time.datetime(2000, 3, 21) + time.delta(1)))
	tester(`(time.delta(1) + time.datetime(2000, 3, 21)))
	tester(`(time.datetime(2000, 3, 21) - time.delta(1)))
	tester(`(time.datetime(2000, 3, 21) + (-time.delta(1))))
	tester(`(time.datetime(2000, 3, 21) + time.delta(0, 3600)))
	tester(`(time.datetime(2000, 3, 21) + time.delta(0, -3600)))
	tester(`(time.datetime(2000, 3, 21) - time.delta(0, 3600)))
	tester(`(time.datetime(2000, 3, 21) + (-time.delta(0, 3600))))
	tester(`(time.datetime(2000, 3, 21) + time.delta(0, 0, 1000000)))
	tester(`(time.datetime(2000, 3, 21) - time.delta(0, 0, 1000000)))
	tester(`(time.datetime(2000, 3, 21) + (-time.delta(0, 0, 1000000))))
	tester(`(time.datetime(2000, 3, 21) + time.delta(0, 0, 2000000)))
	tester(`(time.datetime(2000, 3, 21) - time.delta(0, 0, 2000000)))
	tester(`(time.datetime(2000, 3, 21) + (-time.delta(0, 0, 2000000))))
	tester(`(time.datetime(2000, 3, 1) + time.delta(1)))
	tester(`(time.delta(1) + time.datetime(2000, 3, 1)))
	tester(`(time.datetime(2000, 3, 1) - time.delta(1)))
	tester(`(time.datetime(2000, 3, 1) + (-time.delta(1))))
	tester(`(time.datetime(2000, 3, 1) + time.delta(0, 3600)))
	tester(`(time.datetime(2000, 3, 1) + time.delta(0, -3600)))
	tester(`(time.datetime(2000, 3, 1) - time.delta(0, 3600)))
	tester(`(time.datetime(2000, 3, 1) + (-time.delta(0, 3600))))
	tester(`(time.datetime(2000, 3, 1) + time.delta(0, 0, 1000000)))
	tester(`(time.datetime(2000, 3, 1) - time.delta(0, 0, 1000000)))
	tester(`(time.datetime(2000, 3, 1) + (-time.delta(0, 0, 1000000))))
	tester(`(time.datetime(2000, 3, 1) + time.delta(0, 0, 2000000)))
	tester(`(time.datetime(2000, 3, 1) - time.delta(0, 0, 2000000)))
	tester(`(time.datetime(2000, 3, 1) + (-time.delta(0, 0, 2000000))))
	tester(`chooseif(time.isleap(1800..1899), "*", ".").join())
	tester(`chooseif(time.isleap(1900..1999), "*", ".").join())
	tester(`chooseif(time.isleap(2000..2099), "*", ".").join())
	tester(`chooseif(time.isleap(2100..2199), "*", ".").join())
}()
