https://github.com/btubbs/datetime
A Go (golang) library for parsing most ISO8601 timestamps
https://github.com/btubbs/datetime
datetime golang parser time
Last synced: 10 days ago
JSON representation
A Go (golang) library for parsing most ISO8601 timestamps
- Host: GitHub
- URL: https://github.com/btubbs/datetime
- Owner: btubbs
- License: mit
- Created: 2018-05-22T20:25:23.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-06-01T17:56:11.000Z (over 4 years ago)
- Last Synced: 2025-09-18T16:03:15.095Z (4 months ago)
- Topics: datetime, golang, parser, time
- Language: Go
- Size: 27.3 KB
- Stars: 24
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# THIS REPO HAS MOVED
See the new home at https://github.com/nav-inc/datetime
# datetime [](https://travis-ci.org/nav-inc/datetime)
`datetime` provides a Parse function for turning commonly-used
[ISO 8601](http://www.loc.gov/standards/datetime/iso-tc154-wg5_n0038_iso_wd_8601-1_2016-02-16.pdf) 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:
```go
package main
import (
"fmt"
"time"
"github.com/nav-inc/datetime"
)
func main() {
// just a year, defaulting to the time.UTC timezone
fmt.Println(datetime.Parse("2007", time.UTC)) // 2007-01-01 00:00:00 +0000 UTC
// a year and a month, this time defaulting to time.Local timezone
fmt.Println(datetime.Parse("2007-11", time.Local)) // 2007-11-01 00:00:00 -0600 MDT
// a full date
fmt.Println(datetime.Parse("2007-11-22", time.UTC)) // 2007-11-22 00:00:00 +0000 UTC
// adding time
fmt.Println(datetime.Parse("2007-11-22T12:30:22", time.UTC)) // 2007-11-22 12:30:22 -0700 MST
// fractions of a second
fmt.Println(datetime.Parse("2007-11-22T12:30:22.321", time.UTC)) // 2007-11-22 12:30:22.321 -0700 MST
// omitting dashes and colons, as ISO 8601 allows
fmt.Println(datetime.Parse("20071122T123022", time.UTC)) // 2007-11-22 12:30:22 -0700 MST
// a timezone offset inside the input will override the default provided to datetime.Parse
fmt.Println(datetime.Parse("2007-11-22T12:30:22+0800", time.Local)) // 2007-11-22 12:30:22 +0800 +0800
// adding separators to the offset too
fmt.Println(datetime.Parse("2007-11-22T12:30:22+08:00", time.UTC)) // 2007-11-22 12:30:22 +0800 +08:00
// using a shorthand for UTC
fmt.Println(datetime.Parse("2007-11-22T12:30:22Z", time.Local)) // 2007-11-22 12:30:22 +0000 UTC
}
```
`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.