tyme

package module
v0.0.0-...-016d066 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2022 License: MIT Imports: 4 Imported by: 0

README

tyme

Warning: Work in progress.

Set of small go modules to make marshaling/unmarshaling time and duration values easier.

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

View Source
const RFC3339NanoJSON = `"` + time.RFC3339Nano + `"`

RFC3339NanoJSON is the time.RFC3339Nano format with double quotes around it.

Variables

View Source
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

type Time time.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

func Parse(s string) (Time, error)

Parse is a helper function to parse a wide range of string date and time formats using dateparse.ParseAny.

func (Time) MarshalJSON

func (t Time) 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.

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

func (t Time) 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.

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

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

UnmarshalJSON implements the json.Unmarshaler interface, and parses a wide range of string date and time formats, by using the dateparse package.

func (*Time) UnmarshalYAML

func (t *Time) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements the yaml.Unmarshaler interface, and parses a wide range of date and time formats, by using the dateparse package.

type TimeRFC3339

type TimeRFC3339 time.Time

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.

Directories

Path Synopsis
dur module
ts module

Jump to

Keyboard shortcuts

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