From 560f10ae4eb2b45bfd74a7a49d7fa3f79f1a23a6 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Tue, 5 Dec 2023 19:57:17 -0800 Subject: [PATCH] toml: parse more datetime formats into timestamps --- basis/toml/toml-tests.factor | 108 +++++++++++++++++++++++++++++++++-- basis/toml/toml.factor | 14 ++--- 2 files changed, 109 insertions(+), 13 deletions(-) diff --git a/basis/toml/toml-tests.factor b/basis/toml/toml-tests.factor index b3423c68d5..dca56efe6d 100644 --- a/basis/toml/toml-tests.factor +++ b/basis/toml/toml-tests.factor @@ -1,4 +1,4 @@ -USING: assocs multiline present toml tools.test ; +USING: assocs calendar multiline present toml tools.test ; ! Example document @@ -14,7 +14,15 @@ USING: assocs multiline present toml tools.test ; "bio" "GitHub Cofounder & CEO\nLikes tater tots and beer." } - { "dob" "1979-05-27T07:32:00Z" } + { "dob" + T{ timestamp + { year 1979 } + { month 5 } + { day 27 } + { hour 7 } + { minute 32 } + } + } } } { @@ -394,19 +402,107 @@ bool2 = false]=] toml> ! Offset Date-Time -! XXX: +{ + H{ + { + "odt4" + T{ timestamp + { year 1979 } + { month 5 } + { day 27 } + { hour 7 } + { minute 32 } + } + } + { + "odt1" + T{ timestamp + { year 1979 } + { month 5 } + { day 27 } + { hour 7 } + { minute 32 } + } + } + { + "odt2" + T{ timestamp + { year 1979 } + { month 5 } + { day 27 } + { minute 32 } + { gmt-offset T{ duration { hour -7 } } } + } + } + { + "odt3" + T{ timestamp + { year 1979 } + { month 5 } + { day 27 } + { minute 32 } + { second 999999/1000000 } + { gmt-offset T{ duration { hour -7 } } } + } + } + } +} [ + [=[ +odt1 = 1979-05-27T07:32:00Z +odt2 = 1979-05-27T00:32:00-07:00 +odt3 = 1979-05-27T00:32:00.999999-07:00 +odt4 = 1979-05-27 07:32:00Z +]=] toml> +] unit-test ! Local Date-Time -! XXX: +{ + H{ + { + "ldt1" + T{ timestamp + { year 1979 } + { month 5 } + { day 27 } + { hour 7 } + { minute 32 } + } + } + { + "ldt2" + T{ timestamp + { year 1979 } + { month 5 } + { day 27 } + { minute 32 } + { second 999999/1000000 } + } + } + } +} [ + [=[ +ldt1 = 1979-05-27T07:32:00 +ldt2 = 1979-05-27T00:32:00.999999 +]=] toml> +] unit-test ! Local Date -! XXX: +{ H{ { "ld1" "1979-05-27" } } } [ + [=[ +ld1 = 1979-05-27 +]=] toml> +] unit-test ! Local Time -! XXX: +{ H{ { "lt2" "00:32:00.999999" } { "lt1" "07:32:00" } } } [ + [=[ +lt1 = 07:32:00 +lt2 = 00:32:00.999999 +]=] toml> +] unit-test ! Array diff --git a/basis/toml/toml.factor b/basis/toml/toml.factor index 4697b81cf4..ac0f6d7771 100644 --- a/basis/toml/toml.factor +++ b/basis/toml/toml.factor @@ -1,9 +1,9 @@ ! Copyright (C) 2019 John Benediktsson ! See https://factorcode.org/license.txt for BSD license -USING: accessors ascii assocs hashtables io.encodings.utf8 -io.files kernel make math.parser peg peg.parsers regexp -sequences splitting strings.parser ; +USING: accessors ascii assocs calendar.parser hashtables +io.encodings.utf8 io.files kernel make math.parser peg +peg.parsers regexp sequences splitting strings.parser ; ! https://github.com/toml-lang/toml/blob/main/toml.abnf @@ -186,12 +186,12 @@ M: table update-toml decdigit 2 exactly-n , ":" token , decdigit 2 exactly-n , - "." token decdigit repeat1 2seq optional , + "." token decdigit repeat1 2seq optional [ concat ] action , ] seq* [ "" concat-as ] action ; : timezone-parser ( -- parser ) "Z" token - "-" token + "+" token "-" token 2choice decdigit 2 exactly-n ":" token decdigit 2 exactly-n 4seq [ "" concat-as ] action 2choice ; @@ -199,10 +199,10 @@ M: table update-toml : datetime-parser ( -- parser ) [ date-parser , - "T" token " " token 2choice , + "T" token "t" token " " token 3choice , time-parser , timezone-parser optional , - ] seq* [ "" concat-as ] action ; + ] seq* [ "" concat-as rfc3339>timestamp ] action ; : separator ( -- parser ) "," token comment optional 2seq ; -- 2.34.1