Skip to content

Semior001/timetype

Repository files navigation

timetype Go Coverage Status go report card PkgGoDev

Package adds some time types for easier work, serialize and deserialize them and some helper functions. Types satisfy the fmt.GoStringer and fmt.Stringer interfaces for easier debugging and sql.Scanner and sql.Valuer to allow to use this types with the SQL drivers.

timetype.Clock

The type implements sql.Scanner and json.Unmarshaler and tries to read the time value in two formats: ISO8601 for times without date and ISO8601 with micro precision without date.

// Clock is a wrapper for time.time to allow parsing datetime stamp with time only in
// ISO 8601 format, like "15:04:05"
type Clock time.Time
// NewClock returns the Clock in the given location with given hours, minutes and secs
func NewClock(h, m, s int, loc *time.Location) Clock
// NewUTCClock returns new clock with given hours, minutes and seconds in the UTC location
func NewUTCClock(h, m, s int) Clock 

timetype.Duration

// Duration is a wrapper of time.Duration, that allows to marshal and unmarshal time in RFC3339 format
type Duration time.Duration

Helpers

// ParseWeekday parses a weekday from a string and, if it's
// can't be parsed, returns
func ParseWeekday(s string) (time.Weekday, error)

Errors

// Parsing errors
var (
    ErrInvalidClock    = errors.New("timetype: invalid clock")
    ErrInvalidDuration = errors.New("timetype: invalid duration")
    ErrInvalidWeekday  = errors.New("timetype: invalid weekday")
    ErrUnknownFormat   = errors.New("timetype: unknown format")
)

Time formats

// Templates to parse clocks
const (
	ISO8601Clock      = "15:04:05"
	ISO8601ClockMicro = "15:04:05.000000"
)

About

specific time types

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages