Skip to content

Commit

Permalink
Add date and datetime string parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
shellyln committed Feb 11, 2022
1 parent 65db14e commit 7a537da
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions extra/classes/classes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ const (
IntegerNumberStr = ":extra:IntegerNumberStr"
FloatNumberStr = ":extra:FloatNumberStr"
NumericStr = ":extra:NumericStr"
DateStr = ":extra:DateStr"
DateTimeStr = ":extra:DateTimeStr"
)
94 changes: 94 additions & 0 deletions extra/extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}

0 comments on commit 7a537da

Please sign in to comment.