timez

package
v0.48.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package timez contains time functionality.

Index

Constants

View Source
const (
	// DefaultDatetime is the name of the default datetime layout.
	// Its value is NOT the actual layout value itself.
	// Use FormatFunc(DefaultDatetime) to get a format function.
	DefaultDatetime = rfc3339Z

	// DefaultDate is the name of the default date layout.
	// Its value is NOT the actual layout value itself.
	// Use FormatFunc(DefaultDate) to get a format function.
	DefaultDate = dateOnly

	// DefaultTime is the name of the default time layout.
	// Its value is NOT the actual layout value itself.
	// Use FormatFunc(DefaultTime) to get a format function.
	DefaultTime = timeOnly
)
View Source
const (
	// ISO8601 is (our definition of) the ISO8601 timestamp with millisecond
	// precision.
	ISO8601 = "2006-01-02T15:04:05.000Z07:00"

	// ISO8601Z is the same as ISO8601, but in zulu time.
	ISO8601Z = "2006-01-02T15:04:05.000Z"

	// RFC3339Variant is a variant using "-0700" suffix.
	RFC3339Variant = "2006-01-02T15:04:05-0700"

	// RFC3339Z is an RFC3339 format, in zulu time.
	RFC3339Z = "2006-01-02T15:04:05Z"

	// RFC3339NanoZ is time.RFC3339Nano in zulu time.
	RFC3339NanoZ = "2006-01-02T15:04:05.999999999Z"

	// ExcelLongDate is the "long date" used by Excel.
	ExcelLongDate = "Monday, January 2, 2006"

	// ExcelDatetimeMDYNoSeconds is a datetime format used by Excel.
	// The date part is MM/D/YY.
	ExcelDatetimeMDYNoSeconds = "01/2/06 15:04"

	// ExcelDatetimeMDYSeconds is similar to ExcelDatetimeMDYNoSeconds,
	// but includes a seconds component in the time.
	ExcelDatetimeMDYSeconds = "01/2/06 15:04:05"

	// DateHourMinuteSecond has date followed by time, including seconds.
	DateHourMinuteSecond = "2006-01-02 15:04:05"

	// DateHourMinute has date followed by time, not including seconds.
	DateHourMinute = "2006-01-02 15:04"
)

Variables

View Source
var TimestampLayouts = map[string]string{
	"RFC3339":                   time.RFC3339,
	"RFC3339Z":                  RFC3339Z,
	"ISO8601":                   ISO8601,
	"ISO8601Z":                  ISO8601Z,
	"RFC3339Nano":               time.RFC3339Nano,
	"RFC3339NanoZ":              RFC3339NanoZ,
	"ANSIC":                     time.ANSIC,
	"UnixDate":                  time.UnixDate,
	"RubyDate":                  time.RubyDate,
	"RFC8222":                   time.RFC822,
	"RFC8222Z":                  time.RFC822Z,
	"RFC850":                    time.RFC850,
	"RFC1123":                   time.RFC1123,
	"RFC1123Z":                  time.RFC1123Z,
	"Stamp":                     time.Stamp,
	"StampMilli":                time.StampMilli,
	"StampMicro":                time.StampMicro,
	"StampNano":                 time.StampNano,
	"DateHourMinuteSecond":      DateHourMinuteSecond,
	"DateHourMinute":            DateHourMinute,
	"ExcelDatetimeMDYSeconds":   ExcelDatetimeMDYSeconds,
	"ExcelDatetimeMDYNoSeconds": ExcelDatetimeMDYNoSeconds,
}

TimestampLayouts is a map of timestamp layout names to layout string.

Functions

func DateUTC

func DateUTC(t time.Time) string

DateUTC returns a date representation (2020-10-31) of t in UTC.

func FormatFunc

func FormatFunc(layout string) func(time.Time) string

FormatFunc returns a time format function. If layout is a named layout (per NamedLayouts, ignoring case), a func for the named layout is returned. Otherwise layout is treated as a strftime layout (NOT as a stdlib time layout).

func MustParse added in v0.47.0

func MustParse(layout, value string) time.Time

MustParse is like time.Parse, but panics on error.

func NamedLayouts

func NamedLayouts() []string

NamedLayouts returns a new slice containing the layout names supported by FormatFunc.

func ParseDateOrTimestampUTC

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

ParseDateOrTimestampUTC attempts to parse s as either a date (see ParseDateUTC), or timestamp (see ParseTimestampUTC). The returned time is in UTC.

func ParseDateUTC

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

ParseDateUTC accepts a date string s, returning the UTC midnight time of that date. Arg s must in format "2006-01-02".

func ParseLocalDate

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

ParseLocalDate accepts a date string s, returning the local midnight time of that date. Arg s must in format "2006-01-02".

func ParseTimestampUTC

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

ParseTimestampUTC is the counterpart of TimestampUTC. It attempts to parse s first in ISO8601, then time.RFC3339 format, falling back to the subtly different variants.

func TimestampToDate

func TimestampToDate(s string) string

TimestampToDate takes a ISO8601, ISO8601_X or RFC3339 timestamp, and returns just the date component. On error, the empty string is returned.

func TimestampToRFC3339

func TimestampToRFC3339(s string) string

TimestampToRFC3339 takes a ISO8601, ISO8601_X or RFC3339 timestamp, and returns RFC3339. That is, the milliseconds are dropped. On error, the empty string is returned.

func TimestampUTC

func TimestampUTC(t time.Time) string

TimestampUTC returns the ISO8601 representation of t in UTC.

Types

This section is empty.

Jump to

Keyboard shortcuts

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