timetype

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2020 License: MIT Imports: 5 Imported by: 1

README

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"
)

Documentation

Index

Constants

View Source
const (
	ISO8601Clock      = "15:04:05"
	ISO8601ClockMicro = "15:04:05.000000"
)

Templates to parse clocks

Variables

View Source
var (
	ErrInvalidClock    = errors.New("timetype: invalid clock")
	ErrInvalidDuration = errors.New("timetype: invalid duration")
)

Parsing errors

View Source
var ErrInvalidWeekday = errors.New("timetype: invalid weekday")

ErrInvalidWeekday if the weekday string cannot be parsed into time.Weekday

Functions

func ParseWeekday added in v0.1.0

func ParseWeekday(s string) (time.Weekday, error)

ParseWeekday parses a weekday from a string and, if it's can't be parsed, returns

func TryParseTime added in v0.1.2

func TryParseTime(val string, formats ...string) (time.Time, error)

TryParseTime tries to parse the value as a time.Time in several formats, it doesn't

Types

type Clock

type Clock time.Time

Clock is a wrapper for time.time to allow parsing datetime stamp with time only in ISO 8601 format, like "15:04:05"

func NewClock added in v0.1.0

func NewClock(h, m, s, ns int, loc *time.Location) Clock

NewClock returns the Clock in the given location with given hours, minutes and secs

func NewUTCClock added in v0.1.0

func NewUTCClock(h, m, s, ns int) Clock

NewUTCClock returns new clock with given hours, minutes and seconds in the UTC location

func (Clock) GoString added in v0.1.0

func (h Clock) GoString() string

GoString implements fmt.GoStringer to use Clock in %#v formats

func (Clock) MarshalJSON

func (h Clock) MarshalJSON() ([]byte, error)

MarshalJSON marshals time into time

func (*Clock) Scan added in v0.1.0

func (h *Clock) Scan(src interface{}) (err error)

Scan the given SQL value as Clock

func (Clock) String added in v0.1.0

func (h Clock) String() string

String implements fmt.Stringer to print and log Clock properly

func (*Clock) UnmarshalJSON

func (h *Clock) UnmarshalJSON(b []byte) error

UnmarshalJSON converts time to ISO 8601 representation

func (Clock) Value added in v0.1.0

func (h Clock) Value() (driver.Value, error)

Value returns the SQL value of the given Clock

type Duration

type Duration time.Duration

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

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON simply marshals duration into nanoseconds

func (*Duration) Scan added in v0.1.0

func (d *Duration) Scan(src interface{}) (err error)

Scan the given SQL value as Duration

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) error

UnmarshalJSON converts time duration from RFC3339 format into time.Duration

func (Duration) Value added in v0.1.0

func (d Duration) Value() (driver.Value, error)

Value returns the SQL value of the given Duration

type UnknownFormatError added in v0.1.2

type UnknownFormatError struct {
	Errors  []error
	Layouts []string
	Val     string
}

UnknownFormatError composes all errors got from all attempts to parse in different layouts

func (*UnknownFormatError) Error added in v0.1.2

func (e *UnknownFormatError) Error() string

Error returns the string representation of a UnknownFormatError.

Jump to

Keyboard shortcuts

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