Documentation
¶
Overview ¶
Package timehelper contains time package helpers.
Index ¶
- Constants
- func AddMonth(t time.Time, mons int) time.Time
- func BuildTime(buildDateTimeStr string) (time.Time, error)
- func DayDiff(t1, t2 time.Time) int
- func Days(d time.Duration) float64
- func DaysInMonth(year int, month time.Month) int
- func DurationHMS(hour, min, sec int) time.Duration
- func FirstWeekdayInYear(year int, weekday time.Weekday) time.Time
- func IsLeapYear(year int) bool
- func LastWeekdayInYear(year int, weekday time.Weekday) time.Time
- func MonthDiff(t1, t2 time.Time) int
- func MulDuration(mul float64, d time.Duration) time.Duration
- func NextClosestWeekday(t time.Time, weekday time.Weekday) time.Time
- func NextWeekday(weekday time.Weekday) time.Weekday
- func PrevClosestWeekday(t time.Time, weekday time.Weekday) time.Time
- func PrevWeekday(weekday time.Weekday) time.Weekday
- func TimeYMD(year int, month time.Month, day int) time.Time
- func ValidHMS(hour, min, sec int) error
- func ValidMonth(month time.Month) error
- func ValidWeek(year, week int) error
- func ValidWeekday(weekday time.Weekday) error
- func ValidYMD(year int, month time.Month, day int) error
- func WeekDiff(t1, t2 time.Time) int
- func WeekdaysInYear(year int, weekday time.Weekday) int
- func Weeks(d time.Duration) float64
- func YearDiff(t1, t2 time.Time) int
- type Clock
- type Date
- type TimeString
- type UnixMsec
- type UnixSec
Constants ¶
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 ¶
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 BuildTime ¶ added in v0.11.0
BuildTime parses 'buildDateTimeStr' as local time set by -ldflags during build process.
First, declare 'var buildDateTimeStr string' in the 'main' package. Then, add '-ldflags' build flag to 'go build' command. Under Windows: -ldflags "-X 'main.buildDateTimeStr=%DATE%T%TIME%'". Under Linux: -ldflags "-X 'main.buildDateTimeStr=$(date +%Y-%m-%dT%H:%M:%S)'".
func DayDiff ¶
DayDiff returns number of whole days containing in difference (t2 - t1). If 't2' is before 't1', the result (if not zero) is negative.
func DaysInMonth ¶
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 ¶
DurationHMS returns time.Duration corresponding to 'hour' hours + 'min' minutes + 'sec' seconds.
func FirstWeekdayInYear ¶
FirstWeekdayInYear returns the first occurrence of the 'weekday' in the 'year'. The returned value has zero clock and time.UTC time zone.
func LastWeekdayInYear ¶
LastWeekdayInYear returns the last occurrence of the 'weekday' in the 'year'. The returned value has zero clock and time.UTC time zone.
func MonthDiff ¶
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 ¶
MulDuration returns time.Duration that represents a specified multiple of 'd'.
func NextClosestWeekday ¶
NextClosestWeekday returns date of the 'weekday' closest to 't' and not before 't'.
func NextWeekday ¶
NextWeekday returns time.Weekday next after 'weekday'.
func PrevClosestWeekday ¶
PrevClosestWeekday returns date of the 'weekday' closest to 't' and not after 't'.
func PrevWeekday ¶
PrevWeekday returns time.Weekday previous to 'weekday'.
func TimeYMD ¶ added in v0.3.0
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
ValidHMS reports whether 'hour', 'min', 'sec' constitute a valid clock time.
func ValidMonth ¶
ValidMonth reports whether 'month' represents a valid month.
func ValidWeekday ¶
ValidWeekday reports whether 'weekday' is valid.
func ValidYMD ¶ added in v0.3.0
ValidYMD reports whether 'year', 'month', 'day' constitute a valid date.
func WeekDiff ¶
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 ¶
WeekdaysInYear returns number of 'weekdays' in the 'year'.
Types ¶
type Clock ¶ added in v0.3.0
Clock combines hour, minute and second.
func ClockFromTime ¶ added in v0.5.0
ClockFromTime extracts Clock part from 't'.
func (Clock) Duration ¶ added in v0.3.0
Duration returns time.Duration corresponding to c.Hour hours + c.Minute minutes + c.Second seconds.
type Date ¶ added in v0.3.0
Date combines year, month and day.
func CatholicEaster ¶
CatholicEaster returns Gregorian date for catholic Easter for the 'year'.
func DateFromTime ¶ added in v0.3.0
DateFromTime extracts Date part from 't'.
func GregorianToJulian ¶ added in v0.4.0
func JulianToGregorian ¶
func OrthodoxEaster ¶
OrthodoxEaster returns Gregorian date for orthodox Easter for the 'year'.
func (Date) Time ¶ added in v0.3.0
Time returns time.Time corresponding to 'd'. The returned value has zero clock and time.UTC time zone.
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'.
type UnixMsec ¶ added in v0.7.0
UnixMsec is a helper type to marshal/unmarshal time.Time as a Unix time in milliseconds.
func (UnixMsec) MarshalJSON ¶ added in v0.9.0
MarshalJSON implements the encoding/json.Marshaler interface.
func (UnixMsec) MarshalText ¶ added in v0.7.0
MarshalText implements the encoding.TextMarshaler interface.
func (*UnixMsec) ParseMSecs ¶ added in v0.7.0
ParseMSecs parses milliseconds to UnixMsec.
func (UnixMsec) String ¶ added in v0.7.0
String returns decimal string representation of 't'. String implements the fmt.Stringer interface.
func (*UnixMsec) UnmarshalJSON ¶ added in v0.8.0
UnmarshalJSON implements the encoding/json.Unmarshaler interface.
func (*UnixMsec) UnmarshalText ¶ added in v0.7.0
UnmarshalText implements the encoding.TextUnmarshaler interface. Time zone of the unmarshaled value is time.UTC.
type UnixSec ¶ added in v0.7.0
UnixSec is a helper type to marshal/unmarshal time.Time as a Unix time in seconds.
func (UnixSec) MarshalJSON ¶ added in v0.9.0
MarshalJSON implements the encoding/json.Marshaler interface.
func (UnixSec) MarshalText ¶ added in v0.7.0
MarshalText implements the encoding.TextMarshaler interface.
func (UnixSec) String ¶ added in v0.7.0
String returns decimal string representation of 't'. String implements the fmt.Stringer interface.
func (*UnixSec) UnmarshalJSON ¶ added in v0.8.0
UnmarshalJSON implements the encoding/json.Unmarshaler interface.
func (*UnixSec) UnmarshalText ¶ added in v0.7.0
UnmarshalText implements the encoding.TextUnmarshaler interface. Time zone of the unmarshaled value is time.UTC.