tc

package module
v0.0.0-...-5a25af6 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2017 License: MIT Imports: 5 Imported by: 0

README

go-timecodec

A helper TimeCodec type for Go

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 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 = NewTimeCodec(func(t time.Time) string {
	return strconv.FormatInt(UnixMillis(t), 10)
}, func(value string) (time.Time, error) {
	ms, err := strconv.ParseInt(value, 10, 64)
	if err != nil {
		return time.Time{}, err
	}
	t := time.Unix(0, int64(time.Duration(ms)*time.Millisecond))

	return t, err
})

Functions

func Round

func Round(tm time.Time, unit time.Duration) time.Time

func UnixMillis

func UnixMillis(tm time.Time) int64

Types

type LayoutCodec

type LayoutCodec string

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)

type TimeEncoder

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

type TimeEncoderFunc

type TimeEncoderFunc func(time.Time) string

Jump to

Keyboard shortcuts

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