timehelper

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2023 License: MIT Imports: 3 Imported by: 3

README

timehelper

Go Reference

Helpers for Go's time package.

Documentation

Overview

Package timehelper contains time package helpers.

Index

Constants

View Source
const (
	Tick       time.Duration = 100 * time.Nanosecond
	Day        time.Duration = 24 * time.Hour
	Week       time.Duration = 7 * Day
	CommonYear time.Duration = 365 * Day
	LeapYear   time.Duration = 366 * Day
)

Durations missing in time package.

Variables

This section is empty.

Functions

func AddMonth

func AddMonth(t time.Time, mons int) time.Time

AddMonth adds 'mons' months to 't'. If the resulting day is beyond the number of days in the month, the last day of the month is returned.

func DayDiff

func DayDiff(t1, t2 time.Time) int

DayDiff returns number of whole days containing in difference (t2 - t1). If 't2' is before 't1', the result (if not zero) is negative.

func Days

func Days(d time.Duration) float64

Days returns the duration as a floating point number of Day s.

func DaysInMonth

func DaysInMonth(year int, month time.Month) int

DaysInMonth returns number of days in the 'month' of the 'year'. If 'month' is invalid -1 is returned. Use ValidMonth if need month validation.

func DurationHMS

func DurationHMS(hour, min, sec int) time.Duration

DurationHMS returns time.Duration corresponding to 'hour' hours + 'min' minutes + 'sec' seconds.

func FirstWeekdayInYear

func FirstWeekdayInYear(year int, weekday time.Weekday) time.Time

FirstWeekdayInYear returns the first occurrence of the 'weekday' in the 'year'. The returned value has zero clock and time.UTC time zone.

func IsLeapYear

func IsLeapYear(year int) bool

IsLeapYear reports whether the 'year' is leap.

func LastWeekdayInYear

func LastWeekdayInYear(year int, weekday time.Weekday) time.Time

LastWeekdayInYear returns the last occurrence of the 'weekday' in the 'year'. The returned value has zero clock and time.UTC time zone.

func MonthDiff

func MonthDiff(t1, t2 time.Time) int

MonthDiff returns number of whole months containing in difference (t2 - t1). If 't2' is before 't1', the result (if not zero) is negative.

func MulDuration

func MulDuration(mul float64, d time.Duration) time.Duration

MulDuration returns time.Duration that represents a specified multiple of 'd'.

func NextClosestWeekday

func NextClosestWeekday(t time.Time, weekday time.Weekday) time.Time

NextClosestWeekday returns date of the 'weekday' closest to 't' and not before 't'.

func NextWeekday

func NextWeekday(weekday time.Weekday) time.Weekday

NextWeekday returns time.Weekday next after 'weekday'.

func PrevClosestWeekday

func PrevClosestWeekday(t time.Time, weekday time.Weekday) time.Time

PrevClosestWeekday returns date of the 'weekday' closest to 't' and not after 't'.

func PrevWeekday

func PrevWeekday(weekday time.Weekday) time.Weekday

PrevWeekday returns time.Weekday previous to 'weekday'.

func TimeYMD added in v0.3.0

func TimeYMD(year int, month time.Month, day int) time.Time

TimeYMD returns time.Time corresponding to 'year', 'month' and 'day'. The returned value has zero clock and time.UTC time zone.

func ValidHMS added in v0.3.0

func ValidHMS(hour, min, sec int) error

ValidHMS reports whether 'hour', 'min', 'sec' constitute a valid clock time.

func ValidMonth

func ValidMonth(month time.Month) error

ValidMonth reports whether 'month' represents a valid month.

func ValidWeek added in v0.3.0

func ValidWeek(year, week int) error

ValidWeek reports whether 'week' is valid for 'year'.

func ValidWeekday

func ValidWeekday(weekday time.Weekday) error

ValidWeekday reports whether 'weekday' is valid.

func ValidYMD added in v0.3.0

func ValidYMD(year int, month time.Month, day int) error

ValidYMD reports whether 'year', 'month', 'day' constitute a valid date.

func WeekDiff

func WeekDiff(t1, t2 time.Time) int

WeekDiff returns number of whole weeks containing in difference (t2 - t1). If 't2' is before 't1', the result (if not zero) is negative.

func WeekdaysInYear

func WeekdaysInYear(year int, weekday time.Weekday) int

WeekdaysInYear returns number of 'weekdays' in the 'year'.

func Weeks

func Weeks(d time.Duration) float64

Weeks returns the duration as a floating point number of Week s.

func YearDiff

func YearDiff(t1, t2 time.Time) int

YearDiff returns number of whole years containing in difference (t2 - t1). If 't2' is before 't1', the result (if not zero) is negative.

Types

type Clock added in v0.3.0

type Clock struct {
	Hour   int
	Minute int
	Second int
}

Clock combines hour, minute and second.

func ClockFromTime added in v0.5.0

func ClockFromTime(t time.Time) Clock

ClockFromTime extracts Clock part from 't'.

func (Clock) Duration added in v0.3.0

func (c Clock) Duration() time.Duration

Duration returns time.Duration corresponding to c.Hour hours + c.Minute minutes + c.Second seconds.

func (Clock) TimeDate added in v0.3.0

func (c Clock) TimeDate(d Date) time.Time

TimeDate returns time.Time corresponding to 'd' + 'c'. The returned value has zero nanoseconds and time.UTC time zone.

func (Clock) Valid added in v0.3.0

func (c Clock) Valid() error

Valid reports whether 'c' represents a valid clock time.

type Date added in v0.3.0

type Date struct {
	Year  int
	Month time.Month
	Day   int
}

Date combines year, month and day.

func CatholicEaster

func CatholicEaster(year int) Date

CatholicEaster returns Gregorian date for catholic Easter for the 'year'.

func DateFromTime added in v0.3.0

func DateFromTime(t time.Time) Date

DateFromTime extracts Date part from 't'.

func GregorianToJulian added in v0.4.0

func GregorianToJulian(gregorian Date) (Date, error)

GregorianToJulian converts Gregorian date to Julian date.

func JulianToGregorian

func JulianToGregorian(julian Date) (Date, error)

JulianToGregorian converts Julian date to Gregorian date.

func OrthodoxEaster

func OrthodoxEaster(year int) Date

OrthodoxEaster returns Gregorian date for orthodox Easter for the 'year'.

func (Date) Time added in v0.3.0

func (d Date) Time() time.Time

Time returns time.Time corresponding to 'd'. The returned value has zero clock and time.UTC time zone.

func (Date) TimeClock added in v0.3.0

func (d Date) TimeClock(c Clock) time.Time

TimeClock returns time.Time corresponding to 'd' + 'c'. The returned value has zero nanoseconds and time.UTC time zone.

func (Date) Valid added in v0.3.0

func (d Date) Valid() error

Valid reports whether 'd' represents a valid date.

type TimeString

type TimeString string

TimeString is a helper type to store time.Time as a string. TimeString may be used instead of time.Time for encoding/decoding time.Time as JSON, since time.Time does not respect encoding/json 'omitempty' option.

func NewTimeString

func NewTimeString(t time.Time) (TimeString, error)

NewTimeString creates TimeString from 't'.

func (TimeString) Time

func (ts TimeString) Time() (time.Time, error)

Time converts 'ts' to time.Time.

type UnixMsec added in v0.7.0

type UnixMsec time.Time

UnixMsec is a helper type to marshal/unmarshal time.Time as a Unix time in milliseconds.

func (UnixMsec) MarshalJSON added in v0.9.0

func (t UnixMsec) MarshalJSON() ([]byte, error)

MarshalJSON implements the encoding/json.Marshaler interface.

func (UnixMsec) MarshalText added in v0.7.0

func (t UnixMsec) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*UnixMsec) ParseMSecs added in v0.7.0

func (t *UnixMsec) ParseMSecs(msec int64)

ParseMSecs parses milliseconds to UnixMsec.

func (UnixMsec) String added in v0.7.0

func (t UnixMsec) String() string

String returns decimal string representation of 't'. String implements the fmt.Stringer interface.

func (*UnixMsec) UnmarshalJSON added in v0.8.0

func (t *UnixMsec) UnmarshalJSON(bb []byte) error

UnmarshalJSON implements the encoding/json.Unmarshaler interface.

func (*UnixMsec) UnmarshalText added in v0.7.0

func (t *UnixMsec) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. Time zone of the unmarshaled value is time.UTC.

type UnixSec added in v0.7.0

type UnixSec time.Time

UnixSec is a helper type to marshal/unmarshal time.Time as a Unix time in seconds.

func (UnixSec) MarshalJSON added in v0.9.0

func (t UnixSec) MarshalJSON() ([]byte, error)

MarshalJSON implements the encoding/json.Marshaler interface.

func (UnixSec) MarshalText added in v0.7.0

func (t UnixSec) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*UnixSec) ParseSec added in v0.7.0

func (t *UnixSec) ParseSec(sec int64)

ParseSec parses seconds to UnixSec.

func (UnixSec) String added in v0.7.0

func (t UnixSec) String() string

String returns decimal string representation of 't'. String implements the fmt.Stringer interface.

func (*UnixSec) UnmarshalJSON added in v0.8.0

func (t *UnixSec) UnmarshalJSON(bb []byte) error

UnmarshalJSON implements the encoding/json.Unmarshaler interface.

func (*UnixSec) UnmarshalText added in v0.7.0

func (t *UnixSec) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. Time zone of the unmarshaled value is time.UTC.

Jump to

Keyboard shortcuts

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