datetime 
datetime
provides a Parse function for turning commonly-used
ISO 8601 date/time formats into
Golang time.Time variables. datetime.Parse
takes two arguments:
- the string you want to parse
- the timezone location to be used if there's not one specified inside the string
Unlike Go's built-in RFC-3339 time format, this package automatically supports ISO 8601 date and
time stamps with varying levels of granularity. Examples:
package main
import (
"fmt"
"time"
"github.com/nav-inc/datetime"
)
func main() {
fmt.Println(datetime.Parse("2007", time.UTC))
fmt.Println(datetime.Parse("2007-11", time.Local))
fmt.Println(datetime.Parse("2007-11-22", time.UTC))
fmt.Println(datetime.Parse("2007-11-22T12:30:22", time.UTC))
fmt.Println(datetime.Parse("2007-11-22T12:30:22.321", time.UTC))
fmt.Println(datetime.Parse("20071122T123022", time.UTC))
fmt.Println(datetime.Parse("2007-11-22T12:30:22+0800", time.Local))
fmt.Println(datetime.Parse("2007-11-22T12:30:22+08:00", time.UTC))
fmt.Println(datetime.Parse("2007-11-22T12:30:22Z", time.Local))
}
DefaultUTC
and DefaultLocal
types are also provided. Used as struct fields, their Scan, Value,
and UnmarshalJSON methods support easy parsing of ISO 8601 timestamps from external systems.