Added tests and fixed timestamp handling
All checks were successful
/ publish (push) Successful in 47s
All checks were successful
/ publish (push) Successful in 47s
This commit is contained in:
parent
5c89c44ff7
commit
e74906e0c4
4 changed files with 164 additions and 34 deletions
|
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -10,51 +9,66 @@ type TimeComponent struct {
|
|||
TS *string `parser:"@Time"`
|
||||
}
|
||||
|
||||
func (t TimeComponent) Time() (time.Time, error) {
|
||||
if strings.HasSuffix(*t.TS, "Z") {
|
||||
return time.ParseInLocation("15:04:05Z", *t.TS, time.UTC)
|
||||
}
|
||||
return time.ParseInLocation("15:04:05", *t.TS, time.Local)
|
||||
func (t TimeComponent) Time(loc *time.Location) (time.Time, error) {
|
||||
return time.ParseInLocation("15:04:05", *t.TS, loc)
|
||||
}
|
||||
|
||||
func (t TimeComponent) RequiresRefreshing() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type TimeInZoneComponent struct {
|
||||
TS *string `parser:"@Time"`
|
||||
InUTC bool `parser:"@Z?"`
|
||||
}
|
||||
|
||||
func (t TimeInZoneComponent) Time() (time.Time, error) {
|
||||
loc := time.Local
|
||||
if t.InUTC {
|
||||
loc = time.UTC
|
||||
}
|
||||
|
||||
return time.ParseInLocation("15:04:05", *t.TS, loc)
|
||||
}
|
||||
|
||||
func (t TimeInZoneComponent) RequiresRefreshing() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type DateWithTimeComponents struct {
|
||||
Date *string `parser:"@Date"`
|
||||
MaybeTime *TimeComponent `parser:" @@?"`
|
||||
MaybeTime *TimeComponent `parser:"(T? @@)?"`
|
||||
InUTC bool `parser:"(@Z)?"`
|
||||
}
|
||||
|
||||
func (d DateWithTimeComponents) Time() (time.Time, error) {
|
||||
t, err := time.Parse("2006-01-02", *d.Date)
|
||||
loc := time.Local
|
||||
if d.InUTC {
|
||||
loc = time.UTC
|
||||
}
|
||||
|
||||
t, err := time.ParseInLocation("2006-01-02", *d.Date, loc)
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
if d.MaybeTime != nil {
|
||||
tt, err := d.MaybeTime.Time()
|
||||
tt, err := d.MaybeTime.Time(loc)
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
t = t.Add(tt.Sub(tt.Truncate(24 * time.Hour)))
|
||||
t = time.Date(t.Year(), t.Month(), t.Day(), tt.Hour(), tt.Minute(), tt.Second(), 0, loc)
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
type Timestamp struct {
|
||||
Full *string `parser:"@Timestamp"`
|
||||
DT *DateWithTimeComponents `parser:"| @@"`
|
||||
TimeToday *TimeComponent `parser:" | @@"`
|
||||
DT *DateWithTimeComponents `parser:"@@"`
|
||||
TimeToday *TimeInZoneComponent `parser:" | @@"`
|
||||
Int *int `parser:" | @Int"`
|
||||
}
|
||||
|
||||
func (t Timestamp) Time() (time.Time, error) {
|
||||
switch {
|
||||
case t.Full != nil:
|
||||
if strings.HasSuffix(*t.Full, "Z") {
|
||||
return time.ParseInLocation("2006-01-02T15:04:05Z", *t.Full, time.UTC)
|
||||
}
|
||||
return time.ParseInLocation("2006-01-02T15:04:05", *t.Full, time.Local)
|
||||
case t.DT != nil:
|
||||
return t.DT.Time()
|
||||
case t.TimeToday != nil:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue