forked from: https://godoc.org/github.com/uudashr/iso8601
JSON time serialization support ISO 8601 specification.
time.Time type embedded into iso8601.Time - easier to work with when mostly using for unmarshalling json
go get github.com/kaido42/iso8601
Use the time Layout (same with time.RFC3339Nano)
import "time"
func parseTime(s string) (t time.Time, err error) {
t, err = time.Parse(iso8601.Layout, s)
return
}Use it on struct
import (
"fmt"
"json"
"time"
)
type Event struct {
Name string `json:"name"`
OccuredOn iso8601.Time `json:"occuredOn"`
}
now := time.Now()
event := Event {Name: "Sign In", iso8601.Time(Time:now)}
b, _ := json.Marshal(event)
fmt.Println(string(b)) // show the marshalled structUnmarshal into struct
import (
"time"
)
type Event struct {
Name string `json:"name"`
OccuredOn iso8601.Time `json:"occuredOn"`
}
source := "{\"name\":\"test\",\"occuredOn\":\"2002-10-02T10:00:00-05:00\"}"
var event Event
json.Unmarshal(source, &event)
fmt.Prinln("occured at unixtime: %d", event.OccuredOn.Unix())