Skip to content

Commit

Permalink
Add full support for iso8601 parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo <[email protected]>
  • Loading branch information
lorenzodonini committed Oct 23, 2023
1 parent 85c930b commit 1eec6cd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 41 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/gorilla/websocket v1.4.1
github.com/kr/pretty v0.1.0 // indirect
github.com/leodido/go-urn v1.1.0 // indirect
github.com/relvacode/iso8601 v1.3.0 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.8.0
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8=
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/relvacode/iso8601 v1.3.0 h1:HguUjsGpIMh/zsTczGN3DVJFxTU/GX+MMmzcKoMO7ko=
github.com/relvacode/iso8601 v1.3.0/go.mod h1:FlNp+jz+TXpyRqgmM7tnzHHzBnz776kmAH2h3sZCn0I=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
46 changes: 26 additions & 20 deletions ocpp1.6/types/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package types

import (
"encoding/json"
"strings"
"errors"
"time"
)

// ISO8601 time format, assuming Zulu timestamp.
const ISO8601 = "2006-01-02T15:04:05Z"
"github.com/relvacode/iso8601"
)

// DateTimeFormat to be used for all OCPP messages.
// DateTimeFormat to be used when serializing all OCPP messages.
//
// The default dateTime format is RFC3339.
// Change this if another format is desired.
Expand All @@ -25,24 +24,31 @@ func NewDateTime(time time.Time) *DateTime {
return &DateTime{Time: time}
}

func null(b []byte) bool {
if len(b) != 4 {
return false
}
if b[0] != 'n' && b[1] != 'u' && b[2] != 'l' && b[3] != 'l' {
return false
}
return true
}

func (dt *DateTime) UnmarshalJSON(input []byte) error {
strInput := string(input)
strInput = strings.Trim(strInput, `"`)
if DateTimeFormat == "" {
var defaultTime time.Time
err := json.Unmarshal(input, &defaultTime)
if err != nil {
return err
}
dt.Time = defaultTime.Local()
// Do not parse null timestamps
if null(input) {
return nil
}
// Assert that timestamp is a string
if len(input) > 0 && input[0] == '"' && input[len(input)-1] == '"' {
input = input[1 : len(input)-1]
} else {
newTime, err := time.Parse(DateTimeFormat, strInput)
if err != nil {
return err
}
dt.Time = newTime.Local()
return errors.New("timestamp not enclosed in double quotes")
}
return nil
// Parse ISO8601
var err error
dt.Time, err = iso8601.Parse(input)
return err
}

func (dt *DateTime) MarshalJSON() ([]byte, error) {
Expand Down
46 changes: 25 additions & 21 deletions ocpp2.0.1/types/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package types

import (
"encoding/json"
"strings"
"errors"
"time"

"github.com/relvacode/iso8601"
)

// DateTimeFormat to be used for all OCPP messages.
// DateTimeFormat to be used when serializing all OCPP messages.
//
// The default dateTime format is RFC3339.
// Change this if another format is desired.
Expand All @@ -22,36 +24,38 @@ func NewDateTime(time time.Time) *DateTime {
return &DateTime{Time: time}
}

// Creates a new DateTime struct, embedding a struct generated using time.Now().
func Now() *DateTime {
return &DateTime{Time: time.Now()}
func null(b []byte) bool {
if len(b) != 4 {
return false
}
if b[0] != 'n' && b[1] != 'u' && b[2] != 'l' && b[3] != 'l' {
return false
}
return true
}

func (dt *DateTime) UnmarshalJSON(input []byte) error {
strInput := string(input)
strInput = strings.Trim(strInput, `"`)
if DateTimeFormat == "" {
var defaultTime time.Time
err := json.Unmarshal(input, &defaultTime)
if err != nil {
return err
}
dt.Time = defaultTime.Local()
// Do not parse null timestamps
if null(input) {
return nil
}
// Assert that timestamp is a string
if len(input) > 0 && input[0] == '"' && input[len(input)-1] == '"' {
input = input[1 : len(input)-1]
} else {
newTime, err := time.Parse(DateTimeFormat, strInput)
if err != nil {
return err
}
dt.Time = newTime.Local()
return errors.New("timestamp not enclosed in double quotes")
}
return nil
// Parse ISO8601
var err error
dt.Time, err = iso8601.Parse(input)
return err
}

func (dt *DateTime) MarshalJSON() ([]byte, error) {
if DateTimeFormat == "" {
return json.Marshal(dt.Time)
}
timeStr := FormatTimestamp(dt.Time)
timeStr := dt.FormatTimestamp()
return json.Marshal(timeStr)
}

Expand Down

0 comments on commit 1eec6cd

Please sign in to comment.