timeutil

package
v12.41.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2022 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Overview

Package timeutil provides methods for working with time and date

Index

Examples

Constants

This section is empty.

Variables

View Source
var StartDate = uint32(1322611200)

StartDate contains start date timestamp

Functions

func DurationToSeconds

func DurationToSeconds(d time.Duration) float64

DurationToSeconds converts time.Duration to float64

Example
fmt.Println(DurationToSeconds(2500 * time.Millisecond))
Output:

2.5

func Format

func Format(d time.Time, f string) string

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 NextDay

func NextDay(t time.Time) time.Time

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

func NextMonth(t time.Time) time.Time

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

func NextWeekend(t time.Time) time.Time

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

func NextWorkday(t time.Time) time.Time

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

func NextYear(t time.Time) time.Time

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

func ParseDuration(dur string, defMod ...rune) (int64, error)

ParseDuration parses duration in 1w2d3h5m6s format and return as seconds

Example
d, _ := ParseDuration("2w3d10h20m35s")

fmt.Println(d)
fmt.Println(PrettyDuration(d))
Output:

1506035
2 weeks 3 days 10 hours 20 minutes and 35 seconds

func PrettyDuration

func PrettyDuration(d interface{}) string

PrettyDuration returns pretty duration (e.g. 1 hour 45 seconds)

Example
// you can use int ...
fmt.Println(PrettyDuration(300))

// ... and time.Duration types
fmt.Println(PrettyDuration(123456 * time.Second))
Output:

5 minutes
1 day 10 hours 17 minutes and 36 seconds

func PrettyDurationInDays

func PrettyDurationInDays(d interface{}) string

PrettyDurationInDays returns pretty duration in days (e.g. 15 days)

Example
// you can use int ...
fmt.Println(PrettyDurationInDays(650))

// ... and time.Duration types
fmt.Println(PrettyDurationInDays(168 * time.Hour))
Output:

today
7 days

func PrevDay

func PrevDay(t time.Time) time.Time

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

func PrevMonth(t time.Time) time.Time

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

func PrevWeekend(t time.Time) time.Time

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

func PrevWorkday(t time.Time) time.Time

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

func PrevYear(t time.Time) time.Time

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

func SecondsToDuration(d float64) time.Duration

SecondsToDuration converts float64 to time.Duration

Example
fmt.Println(SecondsToDuration(3600))
Output:

1h0m0s

func ShortDuration

func ShortDuration(d interface{}, highPrecision ...bool) string

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

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

func TinyDate

func TinyDate(t int64) Date

TinyDate creates tiny date struct by timestamp

func (Date) Time

func (d Date) Time() time.Time

Time returns time struct

Example
StartDate = 1577836800

d := TinyDate(1583020800)

fmt.Println(d.Time().In(time.UTC))
Output:

2020-03-01 00:00:00 +0000 UTC

func (Date) Unix

func (d Date) Unix() int64

Unix returns unix timestamp

Example
StartDate = 1577836800

d := TinyDate(1583020800)

fmt.Println(d.Unix())
Output:

1583020800

Jump to

Keyboard shortcuts

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