Documentation ¶
Overview ¶
Package timeutil provides methods for working with time and date
Index ¶
- Variables
- func DurationToSeconds(d time.Duration) float64
- func Format(d time.Time, f string) string
- func FromISOWeek(week, year int, loc *time.Location) time.Time
- func MiniDuration(d time.Duration, separator ...string) string
- func NextDay(t time.Time) time.Time
- func NextMonth(t time.Time) 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 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 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
- type Date
Examples ¶
- Date
- Date.Time
- Date.Unix
- DurationToSeconds
- Format
- FromISOWeek
- MiniDuration
- NextDay
- NextMonth
- NextWeekend
- NextWorkday
- NextYear
- ParseDuration
- PrettyDuration
- PrettyDurationInDays
- PrettyDurationSimple
- PrevDay
- PrevMonth
- PrevWeekend
- PrevWorkday
- PrevYear
- SecondsToDuration
- ShortDuration
- StartOfDay
- StartOfHour
- StartOfMonth
- StartOfWeek
- StartOfYear
Constants ¶
This section is empty.
Variables ¶
var StartDate = uint32(1322611200)
StartDate contains start date timestamp
Functions ¶
func DurationToSeconds ¶
DurationToSeconds converts time.Duration to float64
Example ¶
fmt.Println(DurationToSeconds(2500 * time.Millisecond))
Output: 2.5
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 MiniDuration ¶
MiniDuration returns formatted value of duration (d/hr/m/s/ms/us/ns)
Example ¶
fmt.Println(MiniDuration(36 * time.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 12:30:15 +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 12:30:15 +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 12:30:15 +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 12:30:15 +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-06-01 12:30:15 +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 * time.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 12:30:15 +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 12:30:15 +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 12:30:15 +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 12:30:15 +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-06-01 12:30:15 +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 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
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