timex

package
v0.23.2 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2022 License: MIT Imports: 5 Imported by: 3

Documentation

Overview

Package timex extends the standard package time.

Index

Constants

View Source
const (
	MinuteSec = 60           // MinuteSec = number of seconds in one minute.
	HourSec   = 3600         // HourSec = number of seconds in one hour.
	DaySec    = 24 * HourSec // DaySec = number of seconds in one day.
	WeekSec   = 7 * DaySec   // WeekSec = number of seconds in one week.
	MonthSec  = YearSec / 12 // MonthSec = number of seconds in one month.
	YearSec   = 31556952     // SecondsInOneYear is the average of the the number of seconds in one year.

	MicrosecondNs = 1000                 // Number of nanoseconds in 1 microsecond
	MillisecondNs = 1000 * MicrosecondNs // Number of nanoseconds in 1 millisecond
	SecondNs      = 1000 * MillisecondNs // Number of nanoseconds in 1 second
	MinuteNs      = SecondNs * MinuteSec // Number of nanoseconds in 1 minute
	HourNs        = SecondNs * HourSec   // Number of nanoseconds in 1 hour
	DayNs         = SecondNs * DaySec    // Number of nanoseconds in 1 day
	WeekNs        = SecondNs * WeekSec   // Number of nanoseconds in 1 week
	MonthNs       = SecondNs * MonthSec  // Number of nanoseconds in 1 month
	YearNs        = SecondNs * YearSec   // Number of nanoseconds in 1 year

	Nanosecond  = time.Nanosecond   // Nanosecond = one billionth of a second
	Microsecond = time.Microsecond  // Microsecond = 1000 nanoseconds
	Millisecond = time.Millisecond  // Millisecond = 1000 microseconds
	Second      = time.Second       // Second = 1000 milliseconds
	Minute      = time.Minute       // Minute = 60 seconds (no leap seconds)
	Hour        = time.Hour         // Hour = 60 minutes
	Day         = Second * DaySec   // Day = 24 hours (ignoring daylight savings effects)
	Week        = Second * WeekSec  // Week = 7 days
	Month       = Second * MonthSec // Month = one twelfth of a year (2629746 seconds, about 30.4 days)
	Year        = Second * YearSec  // Year = 31556952 seconds (average considering leap years, about 365.24 days)

)

Durations in nanoseconds from 1 nanosecond to 1 year.

View Source
const (
	DateOnly = "2006-01-02"
	TimeOnly = "15:04:05"
)
View Source
const Inf = "inf"
View Source
const Infinite = math.MaxInt32 - 1

Variables

View Source
var (
	// ErrMissingTimeAndUnit indicates time parser was expecting at least a time value followed by a time unit.
	ErrMissingTimeAndUnit = errors.New("time: expecting a value followed by a unit")

	// ErrExpectingInt indicates time parser was expecting one or more successive numbers.
	ErrExpectingInt = errors.New("time: expecting [0-9]*")

	// ErrNoDigits indicates time parser was expecting at least one number, but none.
	ErrNoDigits = errors.New("time: no digits (e.g. '.s' or '-.s')")

	// ErrNoUnit indicates time parser was expecting a time unit, but none.
	ErrNoUnit = errors.New("time: missing unit (ns, us, ms, s, m, h, d, w, mo, y)")

	// ErrUnknownUnit indicates time parser did not recognized the time unit.
	ErrUnknownUnit = errors.New("time: unknown unit (valid: ns, us, ms, s, m, h, d, w, mo, y)")

	// ErrOverflowTime indicates time parser is computing a time value larger that 63 bits.
	ErrOverflowTime = errors.New("time: time value overflow 63 bits")
)

Functions

func DStr

func DStr(d time.Duration) string

DStr stringifies the duration in number of days, seconds, microseconds and nanoseconds. DStr truncates the string depending on the precision.

func DT

func DT(t time.Time) string

DT stringifies a time using the layout "2006-01-02.15:04:05". DT is used in debug logs when retrieving data from InfluxDB.

func ISO

func ISO(t time.Time) string

ISO stringifies a time using the layout RFC3339Nano (ISO 8601). Used in JSON response for /{topic} endpoints.

func ISODefault

func ISODefault(t time.Time, defaultVal string) string

ISODefault uses the ISO-8601 (RFC-3339) format with the nanosecond precision, but defaults to defaultVal when t.IsZero().

func InfTime

func InfTime() time.Time

func NsStr

func NsStr(nanoseconds int64) string

NsStr stringifies nanoseconds using the DStr pretty format.

func ParseDuration

func ParseDuration(str string) (time.Duration, error)

ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h", "d", "w", "mo" (month) and "y".

func ParseTime

func ParseTime(str string) (_ time.Time, ok bool)

ParseTime converts string to time.Time.

func Relative

func Relative(t time.Time, days int) time.Time

Relative computes a relative time.

func SameDate

func SameDate(t1, t2 time.Time) bool

SameDate returns true if t1 and t2 have same YYYY-MM-DD.

func SameHour

func SameHour(t1, t2 time.Time) bool

SameHour returns true if t1 and t2 have same HH.

func SameMinuteSecond

func SameMinuteSecond(t1, t2 time.Time) bool

SameMinuteSecond returns true if t1 and t2 have same minute and second.

func SecStr

func SecStr(seconds int32) string

SecStr stringifies seconds using the standard duration format.

func YMD

func YMD(t time.Time) string

YMD stringifies a time using the layout "2006-01-02". YMD is used in responses for /ui endpoints.

Types

This section is empty.

Jump to

Keyboard shortcuts

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