ref: b84692c0fbbd771deab1e8fb75a7f83585c24253
parent: 01be94abcf0a010b24bc7f7b177bf40c5c45b7f1
author: Ori Bernstein <[email protected]>
date: Sat Nov 7 09:49:33 EST 2015
Pass the time zone offset through everywhere. We don't always have a zone name, eg, when parsing from a string.
--- a/lib/date/date.myr
+++ b/lib/date/date.myr
@@ -34,7 +34,7 @@
const Mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
const utcnow = {
- -> mkinstant(std.now(), "")
+ -> mkinstantoff(std.now(), "", 0)
}
const now = {tz : byte[:]
@@ -53,13 +53,27 @@
}
const mkdatetime = {year, mon, day, h, m, s, tz
- -> mkinstant(recalc(&[
- .year=year, .mon=mon, .day=day,
- .h=h, .m=m, .s=s
- ]), tz)
+ var tm
+
+ tm = recalc(&[.year=year, .mon=mon, .day=day,
+ .h=h, .m=m, .s=s,])
+
+ match _zoneinfo.findtzoff(tz, tm)
+ | `std.Some off:
+ -> mkinstantoff(tm, tz, off)
+ | `std.None:
+ std.fatal("invalid timezone {}\n", tz)
+ ;;
}
-const mkinstant = {tm, tz
+const mkinstant = {tm, tz
+ match _zoneinfo.findtzoff(tz, tm)
+ | `std.Some o: -> mkinstantoff(tm, tz, o)
+ | `std.None: std.fatal("no zone named {}\n", tz)
+ ;;
+}
+
+const mkinstantoff = {tm, tz, tzoff
var j, y, m, d
var t, e
var inst
@@ -67,12 +81,9 @@
inst.actual = tm
/* time zones */
std.assert(tz.len <= inst._tzbuf.len, "time zone name too long\n")
- match _zoneinfo.findtzoff(tz, tm)
- | `std.Some o: inst.tzoff = o
- | `std.None: std.fatal("no zone named {}\n", tz)
- ;;
std.slcp(inst._tzbuf[:tz.len], tz)
inst.tzname = inst._tzbuf[:tz.len]
+ inst.tzoff = tzoff
tm += inst.tzoff castto(std.time)
/* break up time */
@@ -162,11 +173,11 @@
}
const add = {d, dt
- -> mkinstant(d.actual + (dt castto(std.time)), d.tzname)
+ -> mkinstantoff(d.actual + (dt castto(std.time)), d.tzname, d.tzoff)
}
const sub = {d, dt
- -> mkinstant(d.actual - (dt castto(std.time)), d.tzname)
+ -> mkinstantoff(d.actual - (dt castto(std.time)), d.tzname, d.tzoff)
}
const addperiod = {inst, p
@@ -178,7 +189,7 @@
| `Minute m: inst.m += m
| `Second s: inst.s += s
;;
- -> mkinstant(recalc(&inst), inst.tzname)
+ -> mkinstantoff(recalc(&inst), inst.tzname, inst.tzoff)
}
const subperiod = {inst, p
@@ -190,7 +201,7 @@
| `Minute m: inst.m -= m
| `Second s: inst.s -= s
;;
- -> mkinstant(recalc(&inst), inst.tzname)
+ -> mkinstantoff(recalc(&inst), inst.tzname, inst.tzoff)
}
const duration = {a, b
--- a/lib/date/zoneinfo+posixy.myr
+++ b/lib/date/zoneinfo+posixy.myr
@@ -84,8 +84,8 @@
-> std.zalloc()
;;
- if !std.sleq(p[:4], "TZif\0")
- std.put("%s is not a zone info file\n", file)
+ if !std.sleq(p[:4], "TZif")
+ std.put("{} is not a zone info file\n", file)
-> std.zalloc()
;;