tcodec

package
v2.0.0-beta9 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2019 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	InvalidISOWeekString   = errors.New("Invalid ISOWeek string")
	InvalidWeekNumberError = errors.New("Invalid week number")
)
View Source
var (
	ErrNilDecoder = errors.New("Nil time decoder")
)
View Source
var ISOWeekCodec = NewTimeCodec(func(t time.Time) string {
	y, d := t.ISOWeek()
	return fmt.Sprintf("%d-%02d", y, d)

}, func(value string) (time.Time, error) {
	match := isoweekRx.FindStringSubmatch(value)
	if match == nil {
		return time.Time{}, InvalidISOWeekString
	}
	year, _ := strconv.Atoi(string(match[1]))
	week, _ := strconv.Atoi(string(match[2]))
	if !(0 < week && week <= 53) {
		return time.Time{}, InvalidWeekNumberError
	}
	t := time.Date(year, 1, 0, 0, 0, 0, 0, time.UTC)
	for t.Weekday() > time.Sunday {
		t = t.Add(-24 * time.Hour)
	}
	t = t.Add(time.Duration(week+1) * 7 * 24 * time.Hour)
	return t, nil
})
View Source
var MillisTimeCodec = UnixMillisTimeCodec(0)

Functions

func UnixMillis

func UnixMillis(tm time.Time) int64

Types

type LayoutCodec

type LayoutCodec string
const (
	ANSIC         LayoutCodec = time.ANSIC
	UnixDate      LayoutCodec = time.UnixDate
	RubyDate      LayoutCodec = time.RubyDate
	RFC822        LayoutCodec = time.RFC822
	RFC822Z       LayoutCodec = time.RFC822Z
	RFC850        LayoutCodec = time.RFC850
	RFC1123       LayoutCodec = time.RFC1123
	RFC1123Z      LayoutCodec = time.RFC1123Z
	RFC3339       LayoutCodec = time.RFC3339
	RFC3339Date   LayoutCodec = "2006-01-02"
	RFC3339Millis LayoutCodec = "2006-01-02T15:04:05.999Z07:00"
	RFC3339Nano   LayoutCodec = time.RFC3339Nano
)

func (LayoutCodec) MarshalTime

func (layout LayoutCodec) MarshalTime(t time.Time) string

func (LayoutCodec) UnmarshalTime

func (layout LayoutCodec) UnmarshalTime(value string) (time.Time, error)

type TimeCodec

type TimeCodec interface {
	TimeEncoder
	TimeDecoder
}

func NewTimeCodec

func NewTimeCodec(enc TimeEncoderFunc, dec TimeDecoderFunc) TimeCodec

func UnixMillisTimeCodec

func UnixMillisTimeCodec(step time.Duration) TimeCodec

func UnixTimeCodec

func UnixTimeCodec(step time.Duration) TimeCodec

type TimeDecoder

type TimeDecoder interface {
	UnmarshalTime(string) (time.Time, error)
}

type TimeDecoderFunc

type TimeDecoderFunc func(string) (time.Time, error)

func (TimeDecoderFunc) UnmarshalTime

func (dec TimeDecoderFunc) UnmarshalTime(s string) (time.Time, error)

type TimeDecoders

type TimeDecoders []TimeDecoder

func (TimeDecoders) UnmarshalTime

func (tds TimeDecoders) UnmarshalTime(s string) (t time.Time, err error)

type TimeEncoder

type TimeEncoder interface {
	MarshalTime(time.Time) string
}

type TimeEncoderFunc

type TimeEncoderFunc func(time.Time) string

func (TimeEncoderFunc) MarshalTime

func (enc TimeEncoderFunc) MarshalTime(t time.Time) string

Jump to

Keyboard shortcuts

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