Documentation
¶
Overview ¶
Package tyme provides wrapper types around time.Time, with sensible JSON/YAML marshaling/unmarshaling support.
Unmarshaling supports a wide variety of formats, automatically doing a best effort to understand the given input, thanks to using the github.com/araddon/dateparse package.
Marshaling always produces a string in RFC 3339 format, by simply formatting the Time with the time.RFC3339Nano layout.
Index ¶
Examples ¶
Constants ¶
const RFC3339NanoJSON = `"` + time.RFC3339Nano + `"`
RFC3339NanoJSON is the time.RFC3339Nano format with double quotes around it.
Variables ¶
var ( // RetryAmbiguousDateWithSwap is option available in dateparse. This var // controls if Time's unmarshalers enables it or not. RetryAmbiguousDateWithSwap = false // PreferMonthFirst is option available in dateparse. This var // controls if Time's unmarshalers enables it or not. PreferMonthFirst = false )
Functions ¶
This section is empty.
Types ¶
type Time ¶
Time is a wrapper around time.Time that implements JSON and YAML marshaler and unmarshaler interfaces.
It marshals to a string in RFC 3339 format, with sub-second precision added if present.
It unmarshals from a wide range of string date and time formats, by using the dateparse package.
func Parse ¶
Parse is a helper function to parse a wide range of string date and time formats using dateparse.ParseAny.
func (Time) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface, and formats the time as a JSON string in RFC 3339 format, with sub-second precision added if present.
Example ¶
type Order struct {
Date tyme.Time `json:"date"`
}
t := time.Date(2006, 1, 2, 15, 4, 5, 999000000, time.UTC)
order := Order{Date: tyme.Time(t)}
b, _ := json.Marshal(order)
fmt.Println(string(b))
Output: {"date":"2006-01-02T15:04:05.999Z"}
func (Time) MarshalYAML ¶
MarshalYAML implements the yaml.Marshaler interface, and formats the time as a YAML timestamp type in RFC 3339 string format, with sub-second precision added if present.
Example ¶
type Order struct {
Date tyme.Time `yaml:"date"`
}
t := time.Date(2006, 1, 2, 15, 4, 5, 999000000, time.UTC)
order := Order{Date: tyme.Time(t)}
b, _ := yaml.Marshal(order)
fmt.Println(string(b))
Output: date: 2006-01-02T15:04:05.999Z
func (*Time) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface, and parses a wide range of string date and time formats, by using the dateparse package.
type TimeRFC3339 ¶
TimeRFC3339 is a wrapper around time.Time that implements JSON/YAML marshaling/unmarshaling interfaces. As opposed to Time, only RFC 3339 formatted input is accepted when unmarshaling.
It marshals to a string in RFC 3339 format, with sub-second precision added if present.
It will only unmarshal from a string in RFC 3339 format. Any other format will cause an unmarshaling error.
func (TimeRFC3339) MarshalJSON ¶
func (t TimeRFC3339) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface, and formats the time as a JSON string in RFC 3339 format, with sub-second precision added if present.
func (TimeRFC3339) MarshalYAML ¶
func (t TimeRFC3339) MarshalYAML() (interface{}, error)
MarshalYAML implements the yaml.Marshaler interface, and formats the time as a YAML timestamp type in RFC 3339 string format, with sub-second precision added if present.
func (*TimeRFC3339) UnmarshalJSON ¶
func (t *TimeRFC3339) UnmarshalJSON(b []byte) error
UnmarshalJSON implements the json.Unmarshaler interface, and parses a JSON string in RFC 3339 format, with sub-second precision added if present.
func (*TimeRFC3339) UnmarshalYAML ¶
func (t *TimeRFC3339) UnmarshalYAML(node *yaml.Node) error
UnmarshalYAML implements the yaml.Unmarshaler interface, and parses a YAML timestamp or string formatted according to RFC 3339, with sub-second precision added if present.