ref: b10b3137d2423629ee2641bf178196e243697c20
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:%s %z"
const Datefmt = "%Y-%m-%d %z"
const fmt : (f : byte[:], d : instant -> byte[:])
const bfmt : (buf : byte[:], f : byte[:], d : instant -> std.size)
;;
const fmt = {f, d
var buf
var sz
buf = std.slalloc(2048)
sz = bfmt(buf, f, d)
-> buf[:sz]
}
const bfmt = {buf, f, d
var c
var o
o = 0
while f.len != 0
(c, f) = std.striter(f)
if c == '%'
(c, f) = std.striter(f)
match c
| 'a': o += std.bfmt(buf[o:], "%s", _names.abbrevday[d.day])
| '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 += 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)
| 'e': o += std.bfmt(buf[o:], "%2i", d.day)
| 'F': o += std.bfmt(buf[o:], "%y-%m-%d", d.year, d.mon, d.day)
/*
| 'G': o += std.bfmt(buf[o:], ...?
| 'g':
*/
| 'h': o += std.bfmt(buf[o:], "%s", _names.abbrevmon[d.mon])
| 'H': o += std.bfmt(buf[o:], "%02i", d.h)
| 'I': o += std.bfmt(buf[o:], "%02i", d.h % 12)
| 'j': o += std.bfmt(buf[o:], "year day... unimplemented.")
| 'k': o += std.bfmt(buf[o:], "%i", d.h)
| 'l': o += std.bfmt(buf[o:], "%i", d.h % 12)
| 'm': o += std.bfmt(buf[o:], "%i", d.mon)
| 'M': o += std.bfmt(buf[o:], "%i", d.m)
| 'n': o += std.bfmt(buf[o:], "\n")
| '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 += 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 += 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)
| 'Z': o += std.bfmt(buf[o:], "%s", d.tzname)
| '%': o += std.bfmt(buf[o:], "%%")
| _: std.fatal(1, "unknown format character %c\n", c)
;;
else
o += std.bfmt(buf[o:], "%c", c)
;;
;;
-> 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, "%s%02l%02l", sep, h, m)
}