ref: cc9da08bbc40e8b2b4562df28572b58f7e78dbe0
dir: /lib/date/fmt.myr/
use std
use "types.use"
use "names.use"
pkg date =
const Datetimefmt = "%Y-%m-%d %H:%M:%S %z"
const Timefmt = "%h:%m:{} %z"
const Datefmt = "%Y-%m-%d %z"
const fmt : (f : byte[:], d : instant -> byte[:])
const bfmt : (buf : byte[:], f : byte[:], d : instant -> byte[:])
;;
const fmt = {f, d
var buf
buf = std.slalloc(2048)
-> bfmt(buf, f, d)
}
const bfmt = {buf, f, d
var c, s
var o
o = 0
s = ""
while f.len != 0
(c, f) = std.striter(f)
if c == '%'
(c, f) = std.striter(f)
match c
| 'a': s = std.bfmt(buf[o:], "{}", _names.abbrevday[d.day])
| 'A': s = std.bfmt(buf[o:], "{}", _names.fullday[d.day])
| 'b': s = std.bfmt(buf[o:], "{}", _names.abbrevmon[d.mon])
| 'B': s = std.bfmt(buf[o:], "{}", _names.fullmon[d.mon])
| 'c': s = bfmt(buf[o:], "%Y-%m-%d", d)
| 'C': s = std.bfmt(buf[o:], "{p=0,w=2}", d.year % 100)
| 'd': s = std.bfmt(buf[o:], "{p=0,w=2}", d.day)
| 'D': s = bfmt(buf[o:], "%m/%d/%y (wtf america)", d)
| 'e': s = std.bfmt(buf[o:], "{w=2}", d.day)
| 'F': s = bfmt(buf[o:], "%y-%m-%d", d)
/*
| 'G': s = std.bfmt(buf[o:], ...?
| 'g':
*/
| 'h': s = std.bfmt(buf[o:], "{}", _names.abbrevmon[d.mon])
| 'H': s = std.bfmt(buf[o:], "{p=0,w=2}", d.h)
| 'I': s = std.bfmt(buf[o:], "{p=0,w=2}", d.h % 12)
| 'j': s = std.bfmt(buf[o:], "year day... unimplemented.")
| 'k': s = std.bfmt(buf[o:], "{}", d.h)
| 'l': s = std.bfmt(buf[o:], "{}", d.h % 12)
| 'm': s = std.bfmt(buf[o:], "{}", d.mon)
| 'M': s = std.bfmt(buf[o:], "{}", d.m)
| 'n': s = std.bfmt(buf[o:], "\n")
| 'O': s = std.bfmt(buf[o:], "unsupported %O")
| 'p': s = std.bfmt(buf[o:], "{}", ["AM", "PM"][d.h/12])
| 'P': s = std.bfmt(buf[o:], "{}", ["am", "pm"][d.h/12])
| 'r': s = bfmt(buf[o:], "%H:%M:%S %P", d)
| 'R': s = bfmt(buf[o:], "%H:%M %P", d)
| 's': s = std.bfmt(buf[o:], "{}", d.actual)
| 'S': s = std.bfmt(buf[o:], "{}", d.s)
| 't': s = std.bfmt(buf[o:], "\t")
| 'u': s = std.bfmt(buf[o:], "{}", d.wday)
| 'U': s = std.bfmt(buf[o:], "week number... unimplemented.")
| 'x': s = bfmt(buf[o:], Datefmt, d)
| 'X': s = bfmt(buf[o:], Timefmt, d)
| 'y': s = std.bfmt(buf[o:], "{}", d.year % 100)
| 'Y': s = std.bfmt(buf[o:], "{}", d.year)
| 'z': s = timezone(buf[o:], d.tzoff)
| 'Z': s = std.bfmt(buf[o:], "{}", d.tzname)
| '%': s = std.bfmt(buf[o:], "%")
| _: std.fatal("unknown format character {}\n", c)
;;
else
s = std.bfmt(buf[o:], "{}", c)
;;
o += s.len
;;
-> buf[:o]
}
const timezone = {buf, off
var h, m
var sep
sep = "+"
if off < 0
off = -off
sep = "-"
;;
off /= 1_000_000
h = off / 3600
m = off % 3600
-> std.bfmt(buf, "{}{p=0,w=2}{p=0,w=2}", sep, h, m)
}