iso8601

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

README

Package iso8601

import "github.com/dacut/go-iso8601"

Proper ISO 8601 (not RFC 3339) time parsing and formatting for Golang.

Using Go's time.RFC3339 format string to parse timestamps will oftain fail on valid ISO 8601 (and certain non-Go-produced RFC 3339) timestamps, e.g.:

  • 20171031T235959Z -- compact ISO 8601 representation
  • 2017-10-31 23:59:59Z -- RFC 3339 alternate separator
  • 2017-10-31t23:59:59Z -- ISO 8601 with lowercase 't' separator
  • 2017-10-31T23:59:59.123Z -- RFC 3339 with milliseconds

This package handles these formats. It properly rejects certain invalid formats that might be accepted by naive regular expressions:

  • 2017-1031T235959Z -- separators must be all or none for a date
  • 20171031T23:5959Z -- same with time

Index

ParseISO8601Timestamp converts an ISO 8601 timestamp into a time.Time result. Compared to time.Parse(time.RFC3339) and time.Parse(time.RFC3339Nano), this accepts the full range of ISO 8601 formats and the RFC 3339 variants.

func ParseISO8601Timestamp(s string) (time.Time, error)

Documentation

Index

Constants

View Source
const ISO8601CompactFormat = "20060102T150405Z"

ISO8601CompactFormat is a layout for time.Format that prints a time value in the most compact ISO8601 format available. This assumes the time value is in UTC, and returns the time zone as 'Z'.

Variables

This section is empty.

Functions

func ParseISO8601Timestamp

func ParseISO8601Timestamp(s string) (time.Time, error)

ParseISO8601Timestamp converts an ISO 8601 timestamp into a time.Time result. Compared to time.Parse(time.RFC3339) and time.Parse(time.RFC3339Nano), this accepts the full range of ISO 8601 formats and the RFC 3339 variants.

Types

type Time

type Time struct {
	time.Time
}

Time extends time.Time to handle valid ISO 8601 (but invalid RFC 3336) formatted timestamps in UnmarshalText.

func Date

func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time

Date returns the Time corresponding to

yyyy-mm-dd hh:mm:ss + nsec nanoseconds

in the appropriate zone for that time in the given location.

The month, day, hour, min, sec, and nsec values may be outside their usual ranges and will be normalized during the conversion. For example, October 32 converts to November 1.

A daylight savings time transition skips or repeats times. For example, in the United States, March 13, 2011 2:15am never occurred, while November 6, 2011 1:15am occurred twice. In such cases, the choice of time zone, and therefore the time, is not well-defined. Date returns a time that is correct in one of the two zones involved in the transition, but it does not guarantee which.

Date panics if loc is nil.

func Now

func Now() Time

Now returns the current local time.

func Unix

func Unix(sec int64, nsec int64) Time

Unix returns the local Time corresponding to the given Unix time, sec seconds and nsec nanoseconds since January 1, 1970 UTC. It is valid to pass nsec outside the range [0, 999999999]. Not all sec values have a corresponding time value. One such value is 1<<63-1 (the largest int64 value).

func (Time) Add

func (t Time) Add(d time.Duration) Time

Add returns the time t+d.

func (Time) AddDate

func (t Time) AddDate(years int, months int, days int) Time

AddDate returns the time corresponding to adding the given number of years, months, and days to t. For example, AddDate(-1, 2, 3) applied to January 1, 2011 returns March 4, 2010.

AddDate normalizes its result in the same way that Date does, so, for example, adding one month to October 31 yields December 1, the normalized form for November 31.

func (Time) In

func (t Time) In(loc *time.Location) Time

In returns a copy of t representing the same time instant, but with the copy's location information set to loc for display purposes.

In panics if loc is nil.

func (Time) Local

func (t Time) Local() Time

Local returns t with the location set to local time.

func (Time) Round

func (t Time) Round(d time.Duration) Time

Round returns the result of rounding t to the nearest multiple of d (since the zero time). The rounding behavior for halfway values is to round up. If d <= 0, Round returns t stripped of any monotonic clock reading but otherwise unchanged.

Round operates on the time as an absolute duration since the zero time; it does not operate on the presentation form of the time. Thus, Round(Hour) may return a time with a non-zero minute, depending on the time's Location.

func (Time) String

func (t Time) String() string

String returns the time formatted using the RFC3339Nano string:

"2006-01-02T15:04:05.999999999Z07:00"

func (Time) Truncate

func (t Time) Truncate(d time.Duration) Time

Truncate returns the result of rounding t down to a multiple of d (since the zero time). If d <= 0, Truncate returns t stripped of any monotonic clock reading but otherwise unchanged.

Truncate operates on the time as an absolute duration since the zero time; it does not operate on the presentation form of the time. Thus, Truncate(Hour) may return a time with a non-zero minute, depending on the time's Location.

func (Time) UTC

func (t Time) UTC() Time

UTC returns t with the location set to UTC.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. The time is expected to be a quoted string in any ISO 8601 format.

func (*Time) UnmarshalText

func (t *Time) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. The time is expected to be in ISO 8601 format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL