summaryrefslogtreecommitdiff
path: root/lib/date
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2014-08-30 02:25:31 -0400
committerOri Bernstein <ori@eigenstate.org>2014-08-30 02:25:31 -0400
commite4505dbcbb4d305d789d41909a1f606144eb5652 (patch)
tree377d622a8ef5f1ee7695c2c376290386884e5488 /lib/date
parent1f50f96cb6fc364084a4c1f8c39ea100023a702b (diff)
downloadmc-e4505dbcbb4d305d789d41909a1f606144eb5652.tar.gz
Add working date parsing. Mostly.
Lots of testing yet to do.
Diffstat (limited to 'lib/date')
-rw-r--r--lib/date/fmt.myr7
-rw-r--r--lib/date/parse.myr28
-rw-r--r--lib/date/test/data/parse-test-expected1
-rw-r--r--lib/date/test/parse-test.myr14
-rw-r--r--lib/date/test/tests3
5 files changed, 43 insertions, 10 deletions
diff --git a/lib/date/fmt.myr b/lib/date/fmt.myr
index 3ba50c1..f92b4ee 100644
--- a/lib/date/fmt.myr
+++ b/lib/date/fmt.myr
@@ -106,7 +106,8 @@ const timezone = {buf, off
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)
}
diff --git a/lib/date/parse.myr b/lib/date/parse.myr
index 06414ae..82429a5 100644
--- a/lib/date/parse.myr
+++ b/lib/date/parse.myr
@@ -5,17 +5,12 @@ use "names.use"
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 @@ const filldate = {d, f, s, tz, err -> byte[:]
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)
@@ -159,6 +154,27 @@ const indexof = {dst, s, set, err
-> 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)
err# = true
diff --git a/lib/date/test/data/parse-test-expected b/lib/date/test/data/parse-test-expected
new file mode 100644
index 0000000..99dd5bf
--- /dev/null
+++ b/lib/date/test/data/parse-test-expected
@@ -0,0 +1 @@
+1932-10-23 00:0:0 +0500
diff --git a/lib/date/test/parse-test.myr b/lib/date/test/parse-test.myr
new file mode 100644
index 0000000..8bbbb56
--- /dev/null
+++ b/lib/date/test/parse-test.myr
@@ -0,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])
+ ;;
+}
diff --git a/lib/date/test/tests b/lib/date/test/tests
index a1b3ddf..13d4f63 100644
--- a/lib/date/test/tests
+++ b/lib/date/test/tests
@@ -20,4 +20,5 @@ mkdir -p tmpout
# result: Result value
# What we compare with. This should be self-
# evident.
-B ftime-test C
+B ftime-test C
+B parse-test C