shithub: mc

Download patch

ref: cb7369fdcdfba4090241c5415d97e627a10189d3
parent: e4505dbcbb4d305d789d41909a1f606144eb5652
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Aug 31 08:32:07 EDT 2014

Just use constants for well-known formats.

--- a/lib/date/fmt.myr
+++ b/lib/date/fmt.myr
@@ -4,42 +4,25 @@
 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 fmt	: (f : byte[:], d : instant	-> byte[:])
+	const bfmt	: (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 fmt = {d, time
-	if time
-		-> ftime(Datetimefmt, d)
-	else
-		-> ftime(Datefmt, d)
-	;;
-}
-
-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 @@
 			| '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 @@
 			| '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)
--- a/lib/date/test/ftime-test.myr
+++ b/lib/date/test/ftime-test.myr
@@ -8,6 +8,6 @@
 
 	/*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])
 }
--- a/lib/date/test/parse-test.myr
+++ b/lib/date/test/parse-test.myr
@@ -8,7 +8,7 @@
 	/*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])
 	;;
 }