Documentation
¶
Overview ¶
Package isoparse parses ISO 8601 date, time, and datetime strings into time.Time values without requiring a format string.
The three entry points are ParseDatetime, ParseDate, and ParseTime. Parse failures return a *ParseError; inspect via errors.As.
Inputs that omit a UTC offset are returned in time.Local; inputs with a recognizable offset use time.FixedZone (no IANA name is inferred, since an offset like -05:00 is DST-ambiguous). See SetLoc for re-attaching a different *time.Location without shifting wall-clock components.
Most of the parsing logic is ported from the isoparser module (by Paul Ganssle) in Python's dateutil library; semantics are adapted to Go's time package. See the project README for supported formats and deviations from the ISO 8601:2004 standard.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseDate ¶
ParseDate parses an ISO 8601 date string (no time component) into a time.Time with time.Local as its location.
Example ¶
package main
import (
"fmt"
"github.com/bsolomon1124/isoparse"
)
func main() {
t, _ := isoparse.ParseDate("2024-03-15")
fmt.Println(t.Format("2006-01-02"))
}
Output: 2024-03-15
Example (Ordinal) ¶
package main
import (
"fmt"
"github.com/bsolomon1124/isoparse"
)
func main() {
// Ordinal date: the 75th day of 2024.
t, _ := isoparse.ParseDate("2024-075")
fmt.Println(t.Format("2006-01-02"))
}
Output: 2024-03-15
Example (WeekDate) ¶
package main
import (
"fmt"
"github.com/bsolomon1124/isoparse"
)
func main() {
// ISO week date: week 11, day 5 of 2024.
t, _ := isoparse.ParseDate("2024-W11-5")
fmt.Println(t.Format("2006-01-02"))
}
Output: 2024-03-15
func ParseDatetime ¶
ParseDatetime parses an ISO 8601 datetime (combined date and time). The date/time separator may be any non-numeric ASCII character, not strictly "T".
ParseDatetime also accepts date-only input, but ParseDate is faster when the caller knows no time portion is present.
Behaves like time.Parse but without a layout argument: on error, the returned time.Time is the zero value.
Example ¶
package main
import (
"fmt"
"time"
"github.com/bsolomon1124/isoparse"
)
func main() {
t, err := isoparse.ParseDatetime("2024-03-15T14:30:45Z")
if err != nil {
panic(err)
}
fmt.Println(t.Format(time.RFC3339Nano))
}
Output: 2024-03-15T14:30:45Z
Example (Basic) ¶
package main
import (
"fmt"
"time"
"github.com/bsolomon1124/isoparse"
)
func main() {
// Compact (no-separator) representation.
t, _ := isoparse.ParseDatetime("20240315T143045Z")
fmt.Println(t.Format(time.RFC3339))
}
Output: 2024-03-15T14:30:45Z
Example (Offset) ¶
package main
import (
"fmt"
"time"
"github.com/bsolomon1124/isoparse"
)
func main() {
// Non-zero offset → time.FixedZone.
t, _ := isoparse.ParseDatetime("2024-03-15T10:00:00-05:00")
fmt.Println(t.Format(time.RFC3339))
}
Output: 2024-03-15T10:00:00-05:00
func ParseTime ¶
ParseTime parses an ISO 8601 time string (no date component) and returns the component ints [hour, minute, second, nanosecond] along with the location. Accepted forms include HH, HH:MM, HHMM, HH:MM:SS, HHMMSS, and any of the above with a fractional seconds suffix and/or timezone suffix.
Example ¶
package main
import (
"fmt"
"github.com/bsolomon1124/isoparse"
)
func main() {
components, _, _ := isoparse.ParseTime("14:30:45.5Z")
fmt.Printf("h=%d m=%d s=%d ns=%d\n", components[0], components[1], components[2], components[3])
}
Output: h=14 m=30 s=45 ns=500000000
func SetLoc ¶
SetLoc returns a new time.Time with the same wall-clock components (year, month, day, hour, minute, second, nanosecond) as t, but with loc as its *time.Location.
Unlike time.Time.In, SetLoc does not preserve the instant: it reinterprets the same wall-clock reading in loc. Use this when an input like "2024-03-15T10:00:00" was meant to be in, say, America/New_York rather than the machine's time.Local.
Example ¶
package main
import (
"fmt"
"time"
"github.com/bsolomon1124/isoparse"
)
func main() {
// Parsing "2024-03-15T10:00:00" yields a Time in time.Local. If the input
// was actually meant to be in a specific zone, SetLoc re-attaches that
// zone without shifting the wall-clock components (unlike Time.In).
t, _ := isoparse.ParseDatetime("2024-03-15T10:00:00")
ny, _ := time.LoadLocation("America/New_York")
t = isoparse.SetLoc(t, ny)
fmt.Println(t.Format(time.RFC3339))
}
Output: 2024-03-15T10:00:00-04:00
Types ¶
type ParseError ¶
type ParseError struct {
Datetime string // the offending input
Message string // optional; a specific reason
}
ParseError describes a failure to parse a date, time, or datetime string. It is the sole error type returned by the Parse* functions in this package; use errors.As to inspect.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
Error implements the [error] interface.