Documentation
¶
Overview ¶
Package timeutil provides methods for working with time and date
Index ¶
- Constants
- Variables
- func DurationAs(t, unit time.Duration) int
- func DurationToSeconds(d time.Duration) float64
- func EndOfDay(t time.Time) time.Time
- func EndOfHour(t time.Time) time.Time
- func EndOfMonth(t time.Time) time.Time
- func EndOfWeek(t time.Time, firstDay time.Weekday) time.Time
- func EndOfYear(t time.Time) time.Time
- func Format(d time.Time, f string) string
- func FromISOWeek(week, year int, loc *time.Location) time.Time
- func IsWeekend(t time.Time) bool
- func MiniDuration(d time.Duration, separator ...string) string
- func NextDay(t time.Time) time.Time
- func NextMonth(t time.Time) time.Time
- func NextWeek(t time.Time, firstDay time.Weekday) time.Time
- func NextWeekend(t time.Time) time.Time
- func NextWorkday(t time.Time) time.Time
- func NextYear(t time.Time) time.Time
- func ParseDuration(dur string, defMod ...rune) (time.Duration, error)
- func PrettyDuration(d any) string
- func PrettyDurationInDays(d any) string
- func PrettyDurationSimple(d any) string
- func PrevDay(t time.Time) time.Time
- func PrevMonth(t time.Time) time.Time
- func PrevWeek(t time.Time, firstDay time.Weekday) time.Time
- func PrevWeekend(t time.Time) time.Time
- func PrevWorkday(t time.Time) time.Time
- func PrevYear(t time.Time) time.Time
- func SecondsToDuration(d float64) time.Duration
- func ShortDuration(d any, highPrecision ...bool) string
- func Since(t time.Time, unit time.Duration) int
- func StartOfDay(t time.Time) time.Time
- func StartOfHour(t time.Time) time.Time
- func StartOfMonth(t time.Time) time.Time
- func StartOfWeek(t time.Time, firstDay time.Weekday) time.Time
- func StartOfYear(t time.Time) time.Time
- func Until(t time.Time, unit time.Duration) int
- type Date
- type Period
- func (p Period) Contains(t time.Time) bool
- func (p Period) Days() int
- func (p Period) Duration() time.Duration
- func (p Period) DurationAs(unit time.Duration) int
- func (p Period) Hours() int
- func (p Period) Minutes() int
- func (p Period) Seconds() int
- func (p Period) String() string
- func (p Period) Stringf(f string) string
- func (p Period) Weeks() int
- func (p Period) Years() int
Examples ¶
- Date
- Date.Time
- Date.Unix
- DurationAs
- DurationToSeconds
- EndOfDay
- EndOfHour
- EndOfMonth
- EndOfWeek
- EndOfYear
- Format
- FromISOWeek
- IsWeekend
- MiniDuration
- NextDay
- NextMonth
- NextWeek
- NextWeekend
- NextWorkday
- NextYear
- ParseDuration
- Period.Contains
- Period.Days
- Period.Duration
- Period.DurationAs
- Period.Hours
- Period.Minutes
- Period.Seconds
- Period.String
- Period.Stringf
- Period.Weeks
- Period.Years
- PrettyDuration
- PrettyDurationInDays
- PrettyDurationSimple
- PrevDay
- PrevMonth
- PrevWeek
- PrevWeekend
- PrevWorkday
- PrevYear
- SecondsToDuration
- ShortDuration
- Since
- StartOfDay
- StartOfHour
- StartOfMonth
- StartOfWeek
- StartOfYear
- Until
Constants ¶
const ( NS = time.Nanosecond US = time.Microsecond MS = time.Millisecond SECOND = time.Second MINUTE = time.Minute HOUR = time.Hour DAY = 24 * HOUR WEEK = 7 * DAY YEAR = 365 * DAY )
Variables ¶
var StartDate = uint32(1322611200)
StartDate contains start date timestamp
Functions ¶
func DurationAs ¶ added in v13.20.2
DurationAs returns duration in given units
Example ¶
d1 := time.Date(2021, 1, 1, 12, 30, 15, 0, time.Local) d2 := time.Date(2024, 6, 15, 14, 15, 45, 0, time.Local) d := d2.Sub(d1) fmt.Printf("Days: %d\n", DurationAs(d, DAY))
Output: Days: 1261
func DurationToSeconds ¶
DurationToSeconds converts time.Duration to float64
Example ¶
fmt.Println(DurationToSeconds(2500 * time.Millisecond))
Output: 2.5
func EndOfDay ¶ added in v13.16.0
EndOfDay returns end of the day
Example ¶
d := time.Date(2021, 6, 15, 12, 30, 15, 0, time.UTC) fmt.Println(EndOfDay(d))
Output: 2021-06-15 23:59:59.999999999 +0000 UTC
func EndOfHour ¶ added in v13.16.0
EndOfHour returns end of the hour
Example ¶
d := time.Date(2021, 6, 15, 12, 30, 15, 0, time.UTC) fmt.Println(EndOfHour(d))
Output: 2021-06-15 12:59:59.999999999 +0000 UTC
func EndOfMonth ¶ added in v13.16.0
EndOfMonth returns the last day of the month
Example ¶
d := time.Date(2021, 6, 15, 12, 30, 15, 0, time.UTC) fmt.Println(EndOfMonth(d))
Output: 2021-06-30 23:59:59.999999999 +0000 UTC
func EndOfWeek ¶ added in v13.16.0
EndOfWeek returns the last day of the week
Example ¶
d := time.Date(2021, 6, 15, 12, 30, 15, 0, time.UTC) fmt.Println(EndOfWeek(d, time.Monday))
Output: 2021-06-20 23:59:59.999999999 +0000 UTC
func EndOfYear ¶ added in v13.16.0
EndOfYear returns the last day of the year
Example ¶
d := time.Date(2021, 6, 15, 12, 30, 15, 0, time.UTC) fmt.Println(EndOfYear(d))
Output: 2021-12-31 23:59:59.999999999 +0000 UTC
func Format ¶
Format returns formatted date as a string
Interpreted sequences:
'%%' a literal % '%a' locale's abbreviated weekday name (e.g., Sun) '%A' locale's full weekday name (e.g., Sunday) '%b' locale's abbreviated month name (e.g., Jan) '%B' locale's full month name (e.g., January) '%c' locale's date and time (e.g., Thu Mar 3 23:05:25 2005) '%C' century; like %Y, except omit last two digits (e.g., 20) '%d' day of month (e.g, 01) '%D' date; same as %m/%d/%y '%e' day of month, space padded '%F' full date; same as %Y-%m-%d '%g' last two digits of year of ISO week number (see %G) '%G' year of ISO week number (see %V); normally useful only with %V '%h' same as %b '%H' hour (00..23) '%I' hour (01..12) '%j' day of year (001..366) '%k' hour ( 0..23) '%K' milliseconds (000..999) '%l' hour ( 1..12) '%m' month (01..12) '%M' minute (00..59) '%n' a newline '%N' nanoseconds (000000000..999999999) '%p' AM or PM '%P' like %p, but lower case '%r' locale's 12-hour clock time (e.g., 11:11:04 PM) '%R' 24-hour hour and minute; same as %H:%M '%s' seconds since 1970-01-01 00:00:00 UTC '%S' second (00..60) '%t' a tab '%T' time; same as %H:%M:%S '%u' day of week (1..7); 1 is Monday '%U' week number of year, with Sunday as first day of week (00..53) '%V' ISO week number, with Monday as first day of week (01..53) '%w' day of week (0..6); 0 is Sunday '%W' week number of year, with Monday as first day of week (00..53) '%x' locale's date representation (e.g., 12/31/99) '%X' locale's time representation (e.g., 23:13:48) '%y' last two digits of year (00..99) '%Y' year '%z' +hhmm numeric timezone (e.g., -0400) '%:z' +hh:mm numeric timezone (e.g., -04:00) '%Z' alphabetic time zone abbreviation (e.g., EDT)
Example ¶
date := time.Date(2010, 6, 15, 15, 30, 45, 1234, time.Local) fmt.Println(Format(date, "%A %d/%b/%Y %H:%M:%S.%N"))
Output: Tuesday 15/Jun/2010 15:30:45.000001234
func FromISOWeek ¶
FromISOWeek returns date for given week number in given year
Example ¶
t := FromISOWeek(25, 2021, time.UTC) fmt.Println(t)
Output: 2021-06-18 00:00:00 +0000 UTC
func IsWeekend ¶ added in v13.16.0
IsWeekend returns true if given day is weekend (saturday or sunday)
Example ¶
d := time.Date(2021, 6, 6, 12, 30, 15, 0, time.UTC) fmt.Println(IsWeekend(d))
Output: true
func MiniDuration ¶
MiniDuration returns formatted value of duration (d/hr/m/s/ms/us/ns)
Example ¶
fmt.Println(MiniDuration(36 * HOUR)) fmt.Println(MiniDuration(18 * time.Second)) fmt.Println(MiniDuration(time.Second / 125)) // You can remove or change separator fmt.Println(MiniDuration(time.Second/2000, ""))
Output: 2 d 18 s 8 ms 500μs
func NextDay ¶
NextDay returns next day date
Example ¶
d := time.Date(2021, 6, 1, 12, 30, 15, 0, time.UTC) fmt.Println(NextDay(d))
Output: 2021-06-02 00:00:00 +0000 UTC
func NextMonth ¶
NextMonth returns next month date
Example ¶
d := time.Date(2021, 6, 1, 12, 30, 15, 0, time.UTC) fmt.Println(NextMonth(d))
Output: 2021-07-01 00:00:00 +0000 UTC
func NextWeek ¶ added in v13.19.0
NextWeek returns next week date
Example ¶
d := time.Date(2021, 6, 1, 12, 30, 15, 0, time.UTC) fmt.Println(NextWeek(d, time.Monday))
Output: 2021-06-07 00:00:00 +0000 UTC
func NextWeekend ¶
NextWeekend returns next weekend
Example ¶
d := time.Date(2021, 6, 6, 12, 30, 15, 0, time.UTC) fmt.Println(NextWeekend(d))
Output: 2021-06-12 00:00:00 +0000 UTC
func NextWorkday ¶
NextWorkday returns next workday
Example ¶
d := time.Date(2021, 6, 6, 12, 30, 15, 0, time.UTC) fmt.Println(NextWorkday(d))
Output: 2021-06-07 00:00:00 +0000 UTC
func NextYear ¶
NextYear returns next year date
Example ¶
d := time.Date(2021, 6, 1, 12, 30, 15, 0, time.UTC) fmt.Println(NextYear(d))
Output: 2022-01-01 00:00:00 +0000 UTC
func ParseDuration ¶
ParseDuration parses duration in 1w2d3h5m6s format and return as seconds
Example ¶
d, _ := ParseDuration("2w3d10h20m35s") fmt.Println(PrettyDuration(d))
Output: 2 weeks 3 days 10 hours 20 minutes and 35 seconds
func PrettyDuration ¶
PrettyDuration returns pretty duration (e.g. 1 hour 45 seconds)
Example ¶
// you can use int/int64/uint as seconds... fmt.Println(PrettyDuration(300)) // ...or time.Duration types fmt.Println(PrettyDuration(123456 * time.Second))
Output: 5 minutes 1 day 10 hours 17 minutes and 36 seconds
func PrettyDurationInDays ¶
PrettyDurationInDays returns pretty duration in days (e.g. 15 days)
Example ¶
fmt.Println(PrettyDurationInDays(2 * time.Hour)) fmt.Println(PrettyDurationInDays(168 * HOUR))
Output: 1 day 7 days
func PrettyDurationSimple ¶
PrettyDurationSimple returns simple pretty duration (seconds → minutes → hours → days)
Example ¶
// you can use int/int64/uint as seconds... fmt.Println(PrettyDurationSimple(300)) // ...or time.Duration types fmt.Println(PrettyDurationSimple(50400 * time.Second))
Output: 5 minutes 14 hours
func PrevDay ¶
PrevDay returns previous day date
Example ¶
d := time.Date(2021, 6, 1, 12, 30, 15, 0, time.UTC) fmt.Println(PrevDay(d))
Output: 2021-05-31 00:00:00 +0000 UTC
func PrevMonth ¶
PrevMonth returns previous month date
Example ¶
d := time.Date(2021, 6, 1, 12, 30, 15, 0, time.UTC) fmt.Println(PrevMonth(d))
Output: 2021-05-01 00:00:00 +0000 UTC
func PrevWeek ¶ added in v13.19.0
PrevWeek returns previous week date
Example ¶
d := time.Date(2021, 6, 1, 12, 30, 15, 0, time.UTC) fmt.Println(PrevWeek(d, time.Monday))
Output: 2021-05-24 00:00:00 +0000 UTC
func PrevWeekend ¶
NextWeekend returns previous weekend
Example ¶
d := time.Date(2021, 6, 6, 12, 30, 15, 0, time.UTC) fmt.Println(PrevWeekend(d))
Output: 2021-06-05 00:00:00 +0000 UTC
func PrevWorkday ¶
NextWorkday returns previous workday
Example ¶
d := time.Date(2021, 6, 6, 12, 30, 15, 0, time.UTC) fmt.Println(PrevWorkday(d))
Output: 2021-06-04 00:00:00 +0000 UTC
func PrevYear ¶
PrevYear returns previous year date
Example ¶
d := time.Date(2021, 6, 1, 12, 30, 15, 0, time.UTC) fmt.Println(PrevYear(d))
Output: 2020-01-01 00:00:00 +0000 UTC
func SecondsToDuration ¶
SecondsToDuration converts float64 to time.Duration
Example ¶
fmt.Println(SecondsToDuration(3600))
Output: 1h0m0s
func ShortDuration ¶
ShortDuration returns pretty short duration (e.g. 1:37)
Example ¶
fmt.Println(ShortDuration(time.Second * 85)) fmt.Println(ShortDuration(3215*time.Millisecond, true))
Output: 1:25 0:03.215
func Since ¶ added in v13.20.0
Since returns time since given moment in given units
Example ¶
d := time.Date(2012, 6, 6, 12, 30, 15, 0, time.UTC) fmt.Println(Since(d, DAY))
Output:
func StartOfDay ¶
StartOfDay returns start of the day
Example ¶
d := time.Date(2021, 6, 15, 12, 30, 15, 0, time.UTC) fmt.Println(StartOfDay(d))
Output: 2021-06-15 00:00:00 +0000 UTC
func StartOfHour ¶
StartOfHour returns start of the hour
Example ¶
d := time.Date(2021, 6, 15, 12, 30, 15, 0, time.UTC) fmt.Println(StartOfHour(d))
Output: 2021-06-15 12:00:00 +0000 UTC
func StartOfMonth ¶
StartOfMonth returns the first day of the month
Example ¶
d := time.Date(2021, 6, 15, 12, 30, 15, 0, time.UTC) fmt.Println(StartOfMonth(d))
Output: 2021-06-01 00:00:00 +0000 UTC
func StartOfWeek ¶
StartOfWeek returns the first day of the week
Example ¶
d := time.Date(2021, 6, 15, 12, 30, 15, 0, time.UTC) fmt.Println(StartOfWeek(d, time.Monday))
Output: 2021-06-14 00:00:00 +0000 UTC
func StartOfYear ¶
StartOfYear returns the first day of the year
Example ¶
d := time.Date(2021, 6, 15, 12, 30, 15, 0, time.UTC) fmt.Println(StartOfYear(d))
Output: 2021-01-01 00:00:00 +0000 UTC
Types ¶
type Date ¶
type Date uint32
Date is tiny date
Example ¶
StartDate = 1577836800 t := int64(1583020800) d := TinyDate(t) fmt.Println(t) fmt.Println(d)
Output: 1583020800 5184000
type Period ¶ added in v13.20.0
Period is a struct with the start and end date of the period
func (Period) Contains ¶ added in v13.20.0
Contains returns true if period contains given date
Example ¶
p := Period{ time.Date(2021, 1, 1, 12, 30, 15, 0, time.Local), time.Date(2023, 6, 15, 18, 45, 30, 0, time.Local), } d := time.Date(2021, 1, 1, 12, 30, 16, 0, time.Local) fmt.Printf("Period contains date: %t\n", p.Contains(d))
Output: Period contains date: true
func (Period) Days ¶ added in v13.20.0
Days returns duration in days
Example ¶
p := Period{ time.Date(2021, 1, 1, 12, 30, 15, 0, time.Local), time.Date(2021, 6, 15, 14, 15, 45, 0, time.Local), } fmt.Printf("Period duration: %d\n", p.Days())
Output: Period duration: 165
func (Period) Duration ¶ added in v13.20.0
Duration returns duration of period
Example ¶
p := Period{ time.Date(2021, 1, 1, 12, 30, 15, 0, time.Local), time.Date(2021, 1, 1, 14, 15, 45, 0, time.Local), } fmt.Printf("Period duration: %s\n", p.Duration())
Output: Period duration: 1h45m30s
func (Period) DurationAs ¶ added in v13.20.2
DurationAs returns duration of period in given units
Example ¶
p := Period{ time.Date(2021, 1, 1, 12, 30, 15, 0, time.Local), time.Date(2021, 6, 15, 14, 15, 45, 0, time.Local), } fmt.Printf("Period duration (hours): %d\n", p.DurationAs(HOUR)) fmt.Printf("Period duration (days): %d\n", p.DurationAs(DAY)) fmt.Printf("Period duration (weeks): %d\n", p.DurationAs(WEEK))
Output: Period duration (hours): 3962 Period duration (days): 165 Period duration (weeks): 24
func (Period) Hours ¶ added in v13.20.0
Hours returns duration in hours
Example ¶
p := Period{ time.Date(2021, 1, 1, 12, 30, 15, 0, time.Local), time.Date(2021, 6, 15, 14, 15, 45, 0, time.Local), } fmt.Printf("Period duration: %d\n", p.Hours())
Output: Period duration: 3962
func (Period) Minutes ¶ added in v13.20.0
Minutes returns duration in minutes
Example ¶
p := Period{ time.Date(2021, 1, 1, 12, 30, 15, 0, time.Local), time.Date(2021, 6, 15, 14, 15, 45, 0, time.Local), } fmt.Printf("Period duration: %d\n", p.Minutes())
Output: Period duration: 237706
func (Period) Seconds ¶ added in v13.20.0
Seconds returns duration in seconds
Example ¶
p := Period{ time.Date(2021, 1, 1, 12, 30, 15, 0, time.Local), time.Date(2021, 6, 15, 14, 15, 45, 0, time.Local), } fmt.Printf("Period duration: %d\n", p.Seconds())
Output: Period duration: 14262330
func (Period) String ¶ added in v13.20.0
String returns string representation of period
Example ¶
p := Period{ time.Date(2021, 1, 1, 12, 30, 15, 0, time.Local), time.Date(2021, 1, 1, 14, 15, 45, 0, time.Local), } fmt.Println(p)
Output: 2021/01/01 12:30:15 → 2021/01/01 14:15:45
func (Period) Stringf ¶ added in v13.20.2
Stringf returns string representation of period using given format
Example ¶
p := Period{ time.Date(2021, 1, 1, 12, 30, 15, 0, time.Local), time.Date(2021, 1, 1, 14, 15, 45, 0, time.Local), } fmt.Println(p.Stringf(`%H:%M`))
Output: 12:30 → 14:15