summaryrefslogtreecommitdiff
path: root/lib/date
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2014-08-31 12:32:07 -0400
committerOri Bernstein <ori@eigenstate.org>2014-08-31 12:32:07 -0400
commitcb7369fdcdfba4090241c5415d97e627a10189d3 (patch)
treee9a751637a9c91b00271831ea6c4a62856747774 /lib/date
parente4505dbcbb4d305d789d41909a1f606144eb5652 (diff)
downloadmc-cb7369fdcdfba4090241c5415d97e627a10189d3.tar.gz
Just use constants for well-known formats.
Diffstat (limited to 'lib/date')
-rw-r--r--lib/date/fmt.myr45
-rw-r--r--lib/date/test/ftime-test.myr2
-rw-r--r--lib/date/test/parse-test.myr2
3 files changed, 16 insertions, 33 deletions
diff --git a/lib/date/fmt.myr b/lib/date/fmt.myr
index f92b4ee..21606d7 100644
--- a/lib/date/fmt.myr
+++ b/lib/date/fmt.myr
@@ -4,42 +4,25 @@ use "types.use"
use "names.use"
pkg date =
- const fmt : (d : instant, time : bool -> byte[:])
- const bfmt : (buf : byte[:], d : instant, time : bool -> std.size)
- const ftime : (f : byte[:], d : instant -> byte[:])
- const bftime : (buf : byte[:], f : byte[:], d : instant -> std.size)
-;;
-
-const Datetimefmt = "%Y-%m-%d %H:%M:%S %z"
-const Timefmt = "%h:%m:%s %z"
-const Datefmt = "%Y-%m-%d %z"
+ const Datetimefmt = "%Y-%m-%d %H:%M:%S %z"
+ const Timefmt = "%h:%m:%s %z"
+ const Datefmt = "%Y-%m-%d %z"
-const fmt = {d, time
- if time
- -> ftime(Datetimefmt, d)
- else
- -> ftime(Datefmt, d)
- ;;
-}
+ const fmt : (f : byte[:], d : instant -> byte[:])
+ const bfmt : (buf : byte[:], f : byte[:], d : instant -> std.size)
+;;
-const bfmt = {buf, d, time
- if time
- -> bftime(buf, Datetimefmt, d)
- else
- -> bftime(buf, Datefmt, d)
- ;;
-}
-const ftime = {f, d
+const fmt = {f, d
var buf
var sz
buf = std.slalloc(2048)
- sz = bftime(buf, f, d)
+ sz = bfmt(buf, f, d)
-> buf[:sz]
}
-const bftime = {buf, f, d
+const bfmt = {buf, f, d
var c
var o
@@ -53,7 +36,7 @@ const bftime = {buf, f, d
| 'A': o += std.bfmt(buf[o:], "%s", _names.fullday[d.day])
| 'b': o += std.bfmt(buf[o:], "%s", _names.abbrevmon[d.mon])
| 'B': o += std.bfmt(buf[o:], "%s", _names.fullmon[d.mon])
- | 'c': o += bftime(buf[o:], "%Y-%m-%d", d)
+ | 'c': o += bfmt(buf[o:], "%Y-%m-%d", d)
| 'C': o += std.bfmt(buf[o:], "%02i", d.year % 100)
| 'd': o += std.bfmt(buf[o:], "%02i", d.day)
| 'D': o += std.bfmt(buf[o:], "%m/%d/%y (wtf america)", d.mon, d.day, d.year)
@@ -75,15 +58,15 @@ const bftime = {buf, f, d
| 'O': o += std.bfmt(buf[o:], "unsupported %O")
| 'p': o += std.bfmt(buf[o:], "%s", ["AM", "PM"][d.h/12])
| 'P': o += std.bfmt(buf[o:], "%s", ["am", "pm"][d.h/12])
- | 'r': o += bftime(buf[o:], "%H:%M:%S %P", d)
- | 'R': o += bftime(buf[o:], "%H:%M %P", d)
+ | 'r': o += bfmt(buf[o:], "%H:%M:%S %P", d)
+ | 'R': o += bfmt(buf[o:], "%H:%M %P", d)
| 's': o += std.bfmt(buf[o:], "%l", d.actual)
| 'S': o += std.bfmt(buf[o:], "%i", d.s)
| 't': o += std.bfmt(buf[o:], "\t")
| 'u': o += std.bfmt(buf[o:], "%i", d.wday)
| 'U': o += std.bfmt(buf[o:], "week number... unimplemented.")
- | 'x': o += bftime(buf[o:], Datefmt, d)
- | 'X': o += bftime(buf[o:], Timefmt, d)
+ | 'x': o += bfmt(buf[o:], Datefmt, d)
+ | 'X': o += bfmt(buf[o:], Timefmt, d)
| 'y': o += std.bfmt(buf[o:], "%i", d.year % 100)
| 'Y': o += std.bfmt(buf[o:], "%i", d.year)
| 'z': o += timezone(buf[o:], d.tzoff)
diff --git a/lib/date/test/ftime-test.myr b/lib/date/test/ftime-test.myr
index 453a977..89792f1 100644
--- a/lib/date/test/ftime-test.myr
+++ b/lib/date/test/ftime-test.myr
@@ -8,6 +8,6 @@ const main = {
/*Fri 29 Aug 2014 07:47:43 PM UTC*/
d = date.mkdate(1_409_341_663*1_000_000, "")
- n = date.bfmt(buf[:], d, true)
+ n = date.bfmt(buf[:], date.Datetimefmt, d)
std.put("%s\n", buf[:n])
}
diff --git a/lib/date/test/parse-test.myr b/lib/date/test/parse-test.myr
index 8bbbb56..adc6b0f 100644
--- a/lib/date/test/parse-test.myr
+++ b/lib/date/test/parse-test.myr
@@ -8,7 +8,7 @@ const main = {
/*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)
+ n = date.bfmt(buf[:], date.Datetimefmt, d)
std.put("%s\n", buf[:n])
;;
}