Skip to content

Commit

Permalink
toml: parse more datetime formats into timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Dec 6, 2023
1 parent 986786c commit 560f10a
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 13 deletions.
108 changes: 102 additions & 6 deletions basis/toml/toml-tests.factor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
USING: assocs multiline present toml tools.test ;
USING: assocs calendar multiline present toml tools.test ;

! Example document

Expand All @@ -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 }
}
}
}
}
{
Expand Down Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions basis/toml/toml.factor
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -186,23 +186,23 @@ 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 ;

: 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 ;
Expand Down

0 comments on commit 560f10a

Please sign in to comment.