1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-08 04:21:57 +00:00

minor improvements on x/jsonx.ISO8601 time parser

note that many new features are coming that I'm designing for the past 2-3 months, this is a commit with just one hotfix
This commit is contained in:
Gerasimos (Makis) Maropoulos
2024-04-08 20:21:21 +03:00
parent ddda4f6998
commit 94cfb5494f
2 changed files with 196 additions and 27 deletions

View File

@@ -6,6 +6,77 @@ import (
"time"
)
func TestParseISO8601(t *testing.T) {
tests := []struct {
name string
input string
want ISO8601
wantErr bool
}{
{
name: "Timestamp with microseconds",
input: "2024-01-02T15:04:05.999999Z",
want: ISO8601(time.Date(2024, 01, 02, 15, 04, 05, 999999*1000, time.UTC)),
wantErr: false,
},
{
name: "Timestamp with timezone but no microseconds",
input: "2024-01-02T15:04:05+07:00",
want: ISO8601(time.Date(2024, 01, 02, 15, 04, 05, 0, time.FixedZone("", 7*3600))),
wantErr: false,
},
{
name: "Timestamp with timezone of UTC with microseconds",
input: "2024-04-08T08:05:04.830140+00:00",
// time.Date function interprets the nanosecond parameter. The time.Date function expects the nanosecond parameter to be the entire nanosecond part of the time, not just the microsecond part.
// When we pass 830140 as the nanosecond argument, Go interprets this as 830140 nanoseconds,
// which is equivalent to 000830140 microseconds (padded with leading zeros to fill the nanosecond precision).
// This is why we see 2024-04-08 08:05:04.00083014 +0000 UTC as the output.
// To correctly represent 830140 microseconds, we need to convert it to nanoseconds by multiplying by 1000 (or set the value to 830140000).
want: ISO8601(time.Date(2024, 04, 8, 8, 05, 04, 830140*1000, time.UTC)),
wantErr: false,
},
{
name: "Timestamp with timezone but no microseconds (2)",
input: "2024-04-08T04:47:10+03:00",
want: ISO8601(time.Date(2024, 04, 8, 4, 47, 10, 0, time.FixedZone("", 3*3600))),
wantErr: false,
},
{
name: "Timestamp with Zulu time",
input: "2024-01-02T15:04:05Z",
want: ISO8601(time.Date(2024, 01, 02, 15, 04, 05, 0, time.UTC)),
wantErr: false,
},
{
name: "Basic ISO8601 layout",
input: "2024-01-02T15:04:05",
want: ISO8601(time.Date(2024, 01, 02, 15, 04, 05, 0, time.UTC)),
wantErr: false,
},
{
name: "Invalid format",
input: "2024-01-02",
want: ISO8601{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseISO8601(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParseISO8601() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !time.Time(got).Equal(time.Time(tt.want)) {
t.Errorf("ParseISO8601() = %v (%s), want %v (%s)", got, got.ToTime().String(), tt.want, tt.want.ToTime().String())
}
})
}
}
func TestISO8601(t *testing.T) {
data := `{"start": "2021-08-20T10:05:01", "end": "2021-12-01T17:05:06", "nothing": null, "empty": ""}`
v := struct {