diff options
author | Ori Bernstein <ori@eigenstate.org> | 2015-09-22 21:55:21 -0700 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2015-09-22 21:55:21 -0700 |
commit | c376b622ebbff34ddb414b50a383339a9ef79b32 (patch) | |
tree | 5dfa2714e9ab313ddde3ad863934a36197280641 /lib/date | |
parent | a547a3f019967cd0609eced8cdfa01b550ec0aaf (diff) | |
download | mc-c376b622ebbff34ddb414b50a383339a9ef79b32.tar.gz |
Refactor a bit.
Diffstat (limited to 'lib/date')
-rw-r--r-- | lib/date/date.myr | 51 | ||||
-rw-r--r-- | lib/date/parse.myr | 32 |
2 files changed, 29 insertions, 54 deletions
diff --git a/lib/date/date.myr b/lib/date/date.myr index 095e77f..e5743c9 100644 --- a/lib/date/date.myr +++ b/lib/date/date.myr @@ -14,6 +14,7 @@ pkg date = const localoff : (tm : std.time -> duration) const tzoff : (tzname : byte[:], tm : std.time -> duration) + const tzname : (tzoff : int -> byte[:]) const isleap : (d : instant -> bool) /* date differences */ @@ -23,6 +24,8 @@ pkg date = const subperiod : (d : instant, dt : period -> instant) const duration : (a : instant, b : instant -> duration) + + pkglocal const recalc : (inst : instant# -> std.time) ;; const Days400y = 365*400 + 4*25 - 3 @@ -46,11 +49,11 @@ const tozone = {d, tz } const mkdate = {y, m, d, tz - -> mkinstant(recalc([.year=y, .mon=m, .day=d]), tz) + -> mkinstant(recalc(&[.year=y, .mon=m, .day=d]), tz) } const mkdatetime = {year, mon, day, h, m, s, tz - -> mkinstant(recalc([ + -> mkinstant(recalc(&[ .year=year, .mon=mon, .day=day, .h=h, .m=m, .s=s ]), tz) @@ -171,7 +174,7 @@ const addperiod = {inst, p | `Minute m: inst.m += m | `Second s: inst.s += s ;; - -> mkinstant(recalc(inst), inst.tzname) + -> mkinstant(recalc(&inst), inst.tzname) } const subperiod = {inst, p @@ -183,7 +186,7 @@ const subperiod = {inst, p | `Minute m: inst.m -= m | `Second s: inst.s -= s ;; - -> mkinstant(recalc(inst), inst.tzname) + -> mkinstant(recalc(&inst), inst.tzname) } const duration = {a, b @@ -192,29 +195,29 @@ const duration = {a, b const recalc = {inst var c, ya, j, tm - var year, mon, day - var h, m, s, us - - year = inst.year castto(std.time) - mon = inst.mon castto(std.time) - day = inst.day castto(std.time) - h = inst.h castto(std.time) - m = inst.m castto(std.time) - s = inst.s castto(std.time) - us = inst.us castto(std.time) - - if m > 2 - mon -= 3 + var y, m, d + + + if inst.mon > 2 + m = (inst.mon - 3) castto(std.time) + y = inst.year castto(std.time) else - mon += 9 - year -= 1 + m = (inst.mon + 9) castto(std.time) + y = (inst.year - 1) castto(std.time) ;; - c = year / 100 - ya = year - 100 * c - j = (c*Days400y/4 + Days4y*ya/4 + (153*mon+2)/5 + day) - j -= 719469 + d = inst.day castto(std.time) + + c = y / 100 + ya = y - 100 * c + j = c * Days400y / 4 + \ + Days4y * ya / 4 + \ + (153 * m + 2)/5 + d - \ + 719469 tm = j * DayUsec - tm += (3600*h + 60*m + s)*1_000_000 + us + tm += (inst.h castto(std.time)) * 3600*1_000_000 + tm += (inst.m castto(std.time)) * 60*1_000_000 + tm += (inst.s castto(std.time)) * 1_000_000 + tm += (inst.us castto(std.time)) -> tm } diff --git a/lib/date/parse.myr b/lib/date/parse.myr index dffac5d..0cd5d77 100644 --- a/lib/date/parse.myr +++ b/lib/date/parse.myr @@ -2,6 +2,7 @@ use std use "types.use" use "names.use" +use "date.use" pkg date = /* date i/o */ @@ -130,7 +131,7 @@ const filldate = {d, f, s, tz, err -> byte[:] -> s ;; ;; - d.actual = time(d) + d.actual = recalc(d) -> s } @@ -201,32 +202,3 @@ const matchampm = {d, s, err -> s ;; } - -const time = {inst - var t - var c, y, ya, m, u - - t = 0 - - if inst.mon > 2 - m = (inst.mon - 3) castto(std.time) - else - m = (inst.mon + 9) castto(std.time) - y = (inst.year - 1) castto(std.time) - ;; - - c = y / 100 - ya = y - 100 * c - u = (146097 * c) / 4 + \ - (1461 * ya) / 4 + \ - (153 * m + 2) / 5 + \ - (inst.day castto(std.time)) + \ - UnixJulianDiff - - t += (u * 24*60*60*1_000_000) - t += (inst.h castto(std.time)) * 60*60*1_000_000 - t += (inst.m castto(std.time)) * 60*1_000_000 - t += (inst.s castto(std.time)) * 1_000_000 - t += inst.us castto(std.time) - -> t -} |