timeutil

package
v1.20240719.1 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package timeutil includes helpers for working with timestamps.

Index

Constants

View Source
const (
	SecondsPerHour = 60 * 60
	SecondsPerDay  = 60 * 60 * 24
)

SecondsPerXYZ

View Source
const (
	TimeFormatCompleteDate24TimeUTC      = "2006-01-02 15:04Z07:00"
	TimeFormatCompleteDate24TimeUTCSlash = "2006/01/02 15:04Z07:00"
	TimeFormatCompleteDate               = "2006-01-02"
	TimeFormatCompleteDateSlash          = "2006/01/02"
	TimeFormatKitchen24                  = "15:04"
)

Extra time formats

Variables

View Source
var (
	// DefaultTimeFormats are the default time formats used in parsing.
	// They are ordered from _most_ specific to least specific.
	DefaultTimeFormats = []string{
		time.RFC3339Nano,
		time.RFC3339,
		TimeFormatCompleteDate24TimeUTC,
		TimeFormatCompleteDate24TimeUTCSlash,
		TimeFormatCompleteDate,
		TimeFormatCompleteDateSlash,
		TimeFormatKitchen24,
		time.Kitchen,
	}
)

Functions

func BeginningOfMonth

func BeginningOfMonth(t time.Time) time.Time

BeginningOfMonth returns the date that represents the last day of the month for a given time.

func DiffHours

func DiffHours(t1, t2 time.Time) (hours int)

DiffHours returns the difference in hours between two times.

func EndOfMonth

func EndOfMonth(t time.Time) time.Time

EndOfMonth returns the date that represents the last day of the month for a given time.

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration formats a duration to it's nearest major increment.

func FromMilliseconds

func FromMilliseconds(millis float64) time.Duration

FromMilliseconds returns a duration from a given float64 millis value.

func Max

func Max(times ...time.Time) time.Time

Max returns the earliest (min) time in a list of times.

func Milliseconds

func Milliseconds(d time.Duration) float64

Milliseconds returns a duration as milliseconds.

func Min

func Min(times ...time.Time) (min time.Time)

Min returns the earliest (min) time in a list of times.

func MinMax

func MinMax(times ...time.Time) (min time.Time, max time.Time)

MinMax returns the minimum and maximum times in a given range.

func ParseTime

func ParseTime(timeStr string, timeFormats ...string) (output time.Time, err error)

ParseTime parses a time string with a given set of formats.

func ParseTimeDefaults

func ParseTimeDefaults(timeStr string) (time.Time, error)

ParseTimeDefaults parses a time string with the default time formats.

func ToFloat64

func ToFloat64(t time.Time) float64

ToFloat64 returns a float64 representation of a time.

func UnixMilliseconds

func UnixMilliseconds(t time.Time) float64

UnixMilliseconds returns the time in unix (seconds) format with a floating point remainder for subsecond fraction.

Types

type Ascending

type Ascending []time.Time

Ascending sorts a given list of times ascending, or min to max.

func (Ascending) Len

func (a Ascending) Len() int

Len implements sort.Sorter

func (Ascending) Less

func (a Ascending) Less(i, j int) bool

Less implements sort.Sorter

func (Ascending) Swap

func (a Ascending) Swap(i, j int)

Swap implements sort.Sorter

type Descending

type Descending []time.Time

Descending sorts a given list of times ascending, or min to max.

func (Descending) Len

func (d Descending) Len() int

Len implements sort.Sorter

func (Descending) Less

func (d Descending) Less(i, j int) bool

Less implements sort.Sorter

func (Descending) Swap

func (d Descending) Swap(i, j int)

Swap implements sort.Sorter

Jump to

Keyboard shortcuts

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