shithub: mc

Download patch

ref: 1f50f96cb6fc364084a4c1f8c39ea100023a702b
parent: ff75912fc34ea245f41e4ecb0f9bf25f54d24a19
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Aug 29 21:43:00 EDT 2014

Fix date parsing.

--- a/lib/date/date.myr
+++ b/lib/date/date.myr
@@ -21,7 +21,7 @@
 	const dursub	: (d : instant, dt : duration	-> instant)
 ;;
 
-const Unix2Julian	= 719468
+const UnixJulianDiff	= 719468
 const Days400y	= 365*400 + 4*25 - 3
 const Days4y	= 365*4 + 1
 
@@ -81,7 +81,7 @@
 
 	Lots of magic. Yer a wizard, 'arry.
 	*/
-	j = e + Unix2Julian
+	j = e + UnixJulianDiff
 	y = (4 * j - 1) / Days400y
 	j = 4 * j - 1 - Days400y * y
 	d = j / 4
--- a/lib/date/parse.myr
+++ b/lib/date/parse.myr
@@ -12,6 +12,7 @@
 ;;
 
 const Default = "%Y-%m-%d"
+const UnixJulianDiff	= 719468
 
 const parse	= {s;		-> parsefmtz(Default, s, "")}
 const parsez	= {s, tz;	-> parsefmtz(Default, s, tz)}
@@ -28,7 +29,7 @@
 	-> `std.Some d
 }
 
-generic intval = {s : byte[:], min : @a::(numeric,integral), max : @a::(numeric,integral), err : bool#-> (@a::(numeric,integral), byte[:])
+generic intval = {dst : @a::(numeric,integral)#, s : byte[:], min : @a::(numeric,integral), max : @a::(numeric,integral), err : bool# -> byte[:]
 	var i
 	var c
 	var num
@@ -38,9 +39,10 @@
 		(c, s) = std.striter(s)
 		if !std.isdigit(c)
 			err# = true
-			-> (0, s)
+			-> s
 		;;
 	;;
+
 	for i = min ; i < max; i++
 		(c, s) = std.striter(s)
 		if !std.isdigit(c)
@@ -47,12 +49,15 @@
 			break
 		;;
 	;;
+
 	num = num[:i]
 	match std.intparse(num)
-	| `std.Some v:	-> (v, s)
+	| `std.Some v:
+		dst# = v
+		-> s
 	| `std.None:
 		err# = true
-		-> (0, s)
+		-> s
 	;;
 }
 
@@ -59,7 +64,6 @@
 
 const filldate = {d, f, s, tz, err -> byte[:]
 	var fc, sc
-	var d
 
 	while f.len != 0
 		(fc, f) = std.striter(f)
@@ -67,30 +71,30 @@
 			(fc, f) = std.striter(f)
 			match fc
 			/* named things */
-			| 'a':	(d.day, s) = indexof(s, _names.abbrevday, err)
-			| 'A':	(d.day, s) = indexof(s, _names.fullday, err)
-			| 'b':	(d.day, s) = indexof(s, _names.abbrevmon, err)
-			| 'B':	(d.day, s) = indexof(s, _names.fullmon, err)
+			| 'a':	s = indexof(&d.day, s, _names.abbrevday, err)
+			| 'A':	s = indexof(&d.day, s, _names.fullday, err)
+			| 'b':	s = indexof(&d.mon, s, _names.abbrevmon, err)
+			| 'B':	s = indexof(&d.mon, s, _names.fullmon, err)
 			| 'c':	s = filldate(d, "%Y-%m-%d", s, tz, err)
 			| 'C':	
-				(d.year, s) = intval(s, 2, 2, err)
+				s = intval(&d.year, s, 2, 2, err)
 				d.year += 1900
-			| 'd':	(d.day, s) = intval(s, 2, 2, err)
+			| 'd':	s = intval(&d.day, s, 2, 2, err)
 			| 'D':	s = filldate(d, "%m/%d/%y", s, tz, err)
-			| 'e':	(d.day, s) = intval(s, 1, 2, err)
+			| 'e':	s = intval(&d.day, s, 1, 2, err)
 			| 'F':	s = filldate(d, "%y-%m-%d", s, tz, err)
 			/*
 			| 'G':	o += std.bfmt(buf[o:], ...?
 			| 'g':
 			*/
-			| 'h':	(d.day, s) = indexof(s, _names.abbrevmon, err)
-			| 'H':	(d.h, s) = intval(s, 2, 2, err)
-			| 'I':	(d.h, s) = intval(s, 2, 2, err)
+			| 'h':	s = indexof(&d.day, s, _names.abbrevmon, err)
+			| 'H':	s = intval(&d.h, s, 2, 2, err)
+			| 'I':	s = intval(&d.h, s, 2, 2, err)
 			| 'j':	std.fatal(1, "year day... unimplemented.")
-			| 'k':	(d.h, s) = intval(s, 1, 2, err)
-			| 'l':	(d.h, s) = intval(s, 1, 2, err)
-			| 'm':	(d.mon, s) = intval(s, 1, 2, err)
-			| 'M':	(d.m, s) = intval(s, 1, 2, err)
+			| 'k':	s = intval(&d.h, s, 1, 2, err)
+			| 'l':	s = intval(&d.h, s, 1, 2, err)
+			| 'm':	s = intval(&d.mon, s, 1, 2, err)
+			| 'M':	s = intval(&d.m, s, 1, 2, err)
 			| 'n':	s = matchstr(s, "\n", err)
 			| 'O':	std.fatal(1, "unsupported %O")
 			| 'p':	s = matchampm(d, s, err)
@@ -97,18 +101,19 @@
 			| 'P':	s = matchampm(d, s, err)
 			| 'r':	s = filldate(d, "%H:%M:%S %P", s, tz, err) 
 			| 'R':	s = filldate(d, "%H:%M %P", s, tz, err)
-			| 's':	(d.actual, s) = intval(s, 1, 64, err)
-			| 'S':	(d.s, s) = intval(s, 1, 2, err)
+			| 's':	s = intval(&d.actual, s, 1, 64, err)
+			| 'S':	s = intval(&d.s, s, 1, 2, err)
 			| 't':	s = eatspace(s)
-			| 'u':	(d.wday, s) = intval(s, 1, 1, err)
+			| 'u':	s = intval(&d.wday, s, 1, 1, err)
 			| 'U':	std.fatal(1, "week number... unimplemented.")
 				/*
 			| 'x':	o += bftime(buf[o:], Datefmt, d)
 			| 'X':	o += bftime(buf[o:], Timefmt, d)
 				*/
-			| 'y':	(d.year, s) = intval(s, 1, 2, err)
+			| 'y':	s = intval(&d.year, s, 1, 2, err)
 				d.year += 1900
-			| 'Y':	(d.year, s) = intval(s, 1, 4, err)
+			| 'Y':	
+				s = intval(&d.year, s, 1, 4, err)
 				/*
 			| 'z':	o += timezone(buf[o:], d.tzoff)
 			| 'Z':	o += std.bfmt(buf[o:], "%s", d.tzname)
@@ -128,6 +133,7 @@
 			-> s
 		;;
 	;;
+	d.actual = time(d)
 	-> s
 }
 
@@ -140,15 +146,17 @@
 	-> s
 }
 
-const indexof = {s, set, err
+const indexof = {dst, s, set, err
 	var i
 	for i = 0; i < set.len; i++
 		if s.len >= set[i].len && std.streq(s, set[i])
-			-> (i, s[set[i].len:])
+			dst# = i
+			-> s
 		;;
 	;;
 	err# = true
-	-> (0, s)
+	dst# = 0
+	-> s
 }
 
 const matchstr = {s, str, err
@@ -175,7 +183,6 @@
 	;;
 }
 
-/*
 const time = {date
 	var t
 	var c, y, ya, m, u
@@ -204,4 +211,3 @@
 	t += date.us castto(std.time)
 	-> t
 }
-*/