From 7a537da9f4ded461ebadfdb71950e255cc8d139c Mon Sep 17 00:00:00 2001 From: shellyln Date: Fri, 11 Feb 2022 23:21:19 +0900 Subject: [PATCH] Add date and datetime string parser. --- extra/classes/classes.go | 2 + extra/extra.go | 94 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/extra/classes/classes.go b/extra/classes/classes.go index 23f88bb..b944568 100644 --- a/extra/classes/classes.go +++ b/extra/classes/classes.go @@ -7,4 +7,6 @@ const ( IntegerNumberStr = ":extra:IntegerNumberStr" FloatNumberStr = ":extra:FloatNumberStr" NumericStr = ":extra:NumericStr" + DateStr = ":extra:DateStr" + DateTimeStr = ":extra:DateTimeStr" ) diff --git a/extra/extra.go b/extra/extra.go index 07fc99a..ec80822 100644 --- a/extra/extra.go +++ b/extra/extra.go @@ -174,3 +174,97 @@ func NumericStr() ParserFn { ChangeClassName(clsz.NumericStr), ) } + +// Parse the ISO 8601 date string. (yyyy-MM-dd) +func DateStr() ParserFn { + return Trans( + FlatGroup( + ZeroOrOnce(Seq("-")), + Repeat(Times{Min: 4, Max: -1}, Number()), + Seq("-"), + CharRange(RuneRange{Start: '0', End: '1'}), + CharRange(RuneRange{Start: '0', End: '9'}), + Seq("-"), + CharRange(RuneRange{Start: '0', End: '3'}), + CharRange(RuneRange{Start: '0', End: '9'}), + ), + Concat, + ChangeClassName(clsz.DateStr), + ) +} + +// Parse the ISO 8601 datetime string. +// (yyyy-MM-ddThh:mmZ , ... , yyyy-MM-ddThh:mm:ss.fffffffffZ) +// (yyyy-MM-ddThh:mm+00:00 , ... , yyyy-MM-ddThh:mm:ss.fffffffff+00:00) +func DateTimeStr() ParserFn { + return Trans( + FlatGroup( + ZeroOrOnce(Seq("-")), + Repeat(Times{Min: 4, Max: -1}, Number()), + Seq("-"), + CharRange(RuneRange{Start: '0', End: '1'}), + CharRange(RuneRange{Start: '0', End: '9'}), + Seq("-"), + CharRange(RuneRange{Start: '0', End: '3'}), + CharRange(RuneRange{Start: '0', End: '9'}), + Seq("T"), + CharRange(RuneRange{Start: '0', End: '2'}), + CharRange(RuneRange{Start: '0', End: '9'}), + Seq(":"), + CharRange(RuneRange{Start: '0', End: '5'}), + CharRange(RuneRange{Start: '0', End: '9'}), + First( + FlatGroup( + Seq(":"), + CharRange(RuneRange{Start: '0', End: '6'}), + CharRange(RuneRange{Start: '0', End: '9'}), + First( + FlatGroup( + Seq("."), + Trans( + Repeat(Times{Min: 1, Max: 9}, // 3: milli, 6: micro, 9: nano + CharRange(RuneRange{Start: '0', End: '9'}), + ), + Concat, + func(ctx ParserContext, asts AstSlice) (AstSlice, error) { + return AstSlice{{ + Type: AstType_String, + Value: (asts[len(asts)-1].Value.(string) + "000000000")[0:9], + }}, nil + }, + ), + ), + Zero(Ast{ + Type: AstType_String, + Value: ".000000000", + }), + ), + ), + Zero(Ast{ + Type: AstType_String, + Value: ":00.000000000", + }), + ), + First( + FlatGroup( + erase(Seq("Z")), + Zero(Ast{ + Type: AstType_String, + Value: "+00:00", + }), + ), + FlatGroup( + CharClass("+", "-"), + Repeat(Times{Min: 2, Max: 2}, + CharRange(RuneRange{Start: '0', End: '9'}), + ), + Seq(":"), + CharRange(RuneRange{Start: '0', End: '5'}), + CharRange(RuneRange{Start: '0', End: '9'}), + ), + ), + ), + Concat, + ChangeClassName(clsz.DateTimeStr), + ) +}