commit a1a97fcddea3141a42d8281e9968fc5c05ed4d51
parent 5fde468d3c041437ef017e083890e5695b3d0ac0
Author: Armin Preiml <apreiml@strohwolke.at>
Date: Fri, 12 May 2023 21:43:30 +0200
datetime: implement %F and %T specifiers in parse
Signed-off-by: Armin Preiml <apreiml@strohwolke.at>
Diffstat:
1 file changed, 30 insertions(+), 0 deletions(-)
diff --git a/datetime/parse.ha b/datetime/parse.ha
@@ -84,6 +84,12 @@ fn parse_specifier(
scan_for(iter, MONTHS...)? + 1;
case 'd' => v.day =
scan_int(iter, 2, false)?;
+ case 'F' =>
+ v.year = scan_int(iter, 4, false)?;
+ eat_rune(iter, '-')?;
+ v.month = scan_int(iter, 2, false)?;
+ eat_rune(iter, '-')?;
+ v.day = scan_int(iter, 2, false)?;
case 'H' => v.hour =
scan_int(iter, 2, false)?;
case 'I' => v.halfhour =
@@ -102,6 +108,12 @@ fn parse_specifier(
scan_for(iter, "AM", "PM", "am", "pm")? % 2 == 1;
case 'S' => v.second =
scan_int(iter, 2, false)?;
+ case 'T' =>
+ v.hour = scan_int(iter, 2, false)?;
+ eat_rune(iter, ':')?;
+ v.minute = scan_int(iter, 2, false)?;
+ eat_rune(iter, ':')?;
+ v.second = scan_int(iter, 2, false)?;
case 'u' => v.weekday =
scan_int(iter, 1, false)? - 1;
case 'U' => v.week =
@@ -290,6 +302,15 @@ fn scan_str(iter: *strings::iterator) (str | failure) = {
assert(v.day as int == 27 , "%d: incorrect");
let v = newvirtual();
+ assert(parse(&v, "%F", "2012-10-01") is void , "%d: parsefail");
+ assert(v.year is int , "%d: void");
+ assert(v.year as int == 2012 , "%d: incorrect");
+ assert(v.month is int , "%d: void");
+ assert(v.month as int == 10 , "%d: incorrect");
+ assert(v.day is int , "%d: void");
+ assert(v.day as int == 1 , "%d: incorrect");
+
+ let v = newvirtual();
assert(parse(&v, "%H", "22") is void , "%H: parsefail");
assert(v.hour is int , "%H: void");
assert(v.hour as int == 22 , "%H: incorrect");
@@ -335,6 +356,15 @@ fn scan_str(iter: *strings::iterator) (str | failure) = {
assert(v.second as int == 8 , "%S: incorrect");
let v = newvirtual();
+ assert(parse(&v, "%T", "18:42:05") is void , "%d: parsefail");
+ assert(v.hour is int , "%d: void");
+ assert(v.hour as int == 18 , "%d: incorrect");
+ assert(v.minute is int , "%d: void");
+ assert(v.minute as int == 42 , "%d: incorrect");
+ assert(v.second is int , "%d: void");
+ assert(v.second as int == 5 , "%d: incorrect");
+
+ let v = newvirtual();
assert(parse(&v, "%u", "5") is void , "%u: parsefail");
assert(v.weekday is int , "%u: void");
assert(v.weekday as int == 4 , "%u: incorrect");