summaryrefslogtreecommitdiff
path: root/lib/date
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-09-14 22:50:05 -0700
committerOri Bernstein <ori@eigenstate.org>2015-09-22 09:43:57 -0700
commit19e038b0670828810453c284589469c6520aa7d9 (patch)
tree7e080849a18342671e86a806af18df733cc8c759 /lib/date
parent06e5f064ec985d4fd9e530e5a94cf4b1721d64f1 (diff)
downloadmc-19e038b0670828810453c284589469c6520aa7d9.tar.gz
Fix dates in the BC
Diffstat (limited to 'lib/date')
-rw-r--r--lib/date/date.myr13
-rw-r--r--lib/date/fmt.myr1
-rw-r--r--lib/date/test/fmt.myr4
3 files changed, 16 insertions, 2 deletions
diff --git a/lib/date/date.myr b/lib/date/date.myr
index ed17133..c5589ce 100644
--- a/lib/date/date.myr
+++ b/lib/date/date.myr
@@ -26,6 +26,7 @@ pkg date =
const Days400y = 365*400 + 4*25 - 3
const Days4y = 365*4 + 1
const DayUsec = (24*60*60*1_000_000)
+const Mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
const utcnow = {
-> mkinstant(std.now(), "")
@@ -70,10 +71,13 @@ const mkinstant = {tm, tz
inst.us = (t % 1_000_000) castto(int)
t /= 1_000_000
inst.s = (t % 60) castto(int)
+ std.assert(inst.s >= 0, "inst.s negative??\n")
t /= 60
inst.m = (t % 60) castto(int)
+ std.assert(inst.m >= 0, "inst.m negative??\n")
t /= 60
inst.h = t castto(int)
+ std.assert(inst.h >= 0, "inst.h negative??\n")
/* weekday */
inst.wday = ((e + 4) % 7) castto(int) /* the world started on Thursday */
@@ -88,9 +92,7 @@ const mkinstant = {tm, tz
Lots of magic. Yer a wizard, 'arry.
*/
j = (tm + 2440588 * DayUsec) / DayUsec
- std.put("j = {}\n", j)
j -= 1721119
- std.assert(j > 0, "date too negative")
y = (4 * j - 1) / Days400y
j = 4 * j - 1 - Days400y * y
@@ -116,6 +118,13 @@ const mkinstant = {tm, tz
if y <= 0
y--
;;
+ /* and if j negative, the day and month are also negative */
+ if m < 0
+ m += 12
+ ;;
+ if d < 0
+ d += Mdays[m - 1]
+ ;;
inst.year = y castto(int)
inst.mon = m castto(int)
diff --git a/lib/date/fmt.myr b/lib/date/fmt.myr
index 3088e00..66454d5 100644
--- a/lib/date/fmt.myr
+++ b/lib/date/fmt.myr
@@ -22,6 +22,7 @@ const Datetimefmt = "%Y-%m-%d %H:%M:%S %z"
const Timefmt = "%h:%m:{} %z"
const Datefmt = "%Y-%m-%d %z"
+/* Always formats in proleptic Gregorian format */
const sbfmt = {sb, ap, opts
var d : instant
var fmt
diff --git a/lib/date/test/fmt.myr b/lib/date/test/fmt.myr
index 1069c86..4160315 100644
--- a/lib/date/test/fmt.myr
+++ b/lib/date/test/fmt.myr
@@ -28,6 +28,10 @@ const main = {
/* large negative time stamp */
d = date.mkinstant(-50000000000*1_000_000, "")
eq("385-7-25 07:06:40 +0000", std.bfmt(buf[:], "{D}", d))
+
+ /* date in the bc */
+ d = date.mkinstant(-70000000000*1_000_000, "")
+ eq("-249-11-19 19:33:20 +0000", std.bfmt(buf[:], "{D}", d))
}
const eq = {expected, actual