ref: e4505dbcbb4d305d789d41909a1f606144eb5652
parent: 1f50f96cb6fc364084a4c1f8c39ea100023a702b
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Aug 29 22:25:31 EDT 2014
Add working date parsing. Mostly. Lots of testing yet to do.
--- a/lib/date/fmt.myr
+++ b/lib/date/fmt.myr
@@ -106,7 +106,8 @@
off = -off
sep = "-"
;;
- h = off % 3600
- m = off / 3600
- -> std.bfmt(buf, "%s%02i%02i", sep, h, m)
+ off /= 1_000_000
+ h = off / 3600
+ m = off % 3600
+ -> std.bfmt(buf, "%s%02l%02l", sep, h, m)
}
--- a/lib/date/parse.myr
+++ b/lib/date/parse.myr
@@ -5,17 +5,12 @@
pkg date =
/* date i/o */
- const parse : (s : byte[:] -> std.option(instant))
- const parsez : (s : byte[:], tz : byte[:] -> std.option(instant))
const parsefmt : (f : byte[:], s: byte[:] -> std.option(instant))
const parsefmtz : (f : byte[:], s: byte[:], tz : byte[:] -> std.option(instant))
;;
-const Default = "%Y-%m-%d"
const UnixJulianDiff = 719468
-const parse = {s; -> parsefmtz(Default, s, "")}
-const parsez = {s, tz; -> parsefmtz(Default, s, tz)}
const parsefmt = {f, s; -> parsefmtz(f, s, "")}
const parsefmtz = {f, s, tz
var d
@@ -114,8 +109,8 @@
d.year += 1900
| 'Y':
s = intval(&d.year, s, 1, 4, err)
+ | 'z': s = timezone(&d.tzoff, s, err)
/*
- | 'z': o += timezone(buf[o:], d.tzoff)
| 'Z': o += std.bfmt(buf[o:], "%s", d.tzname)
*/
| '%': s = matchstr(s, "%", err)
@@ -158,6 +153,27 @@
dst# = 0
-> s
}
+
+const timezone = {dst, s, err
+ var isneg
+ var tzoff
+
+ if s.len < 1
+ err# = true
+ -> ""
+ ;;
+ if std.sleq(s[:1], "-")
+ isneg = true
+ elif std.sleq(s[:1], "+")
+ isneg = false
+ else
+ err# = true
+ -> ""
+ ;;
+ s = intval(&tzoff, s[1:], 2, 4, err)
+ dst# = (tzoff / 100) * 3600 * 1_000_000 + (tzoff % 100) * 60 * 1_000_000
+}
+
const matchstr = {s, str, err
if s.len <= str.len || !std.sleq(s[:str.len], str)
--- /dev/null
+++ b/lib/date/test/data/parse-test-expected
@@ -1,0 +1,1 @@
+1932-10-23 00:0:0 +0500
--- /dev/null
+++ b/lib/date/test/parse-test.myr
@@ -1,0 +1,14 @@
+use std
+use date
+
+const main = {
+ var buf : byte[1024]
+ var n
+
+ /*Fri 29 Aug 2014 07:47:43 PM UTC*/
+ match date.parsefmt("%Y-%m-%d %z", "1932-10-23 +0500")
+ | `std.Some d:
+ n = date.bfmt(buf[:], d, true)
+ std.put("%s\n", buf[:n])
+ ;;
+}
--- a/lib/date/test/tests
+++ b/lib/date/test/tests
@@ -20,4 +20,5 @@
# result: Result value
# What we compare with. This should be self-
# evident.
-B ftime-test C
+B ftime-test C
+B parse-test C