timeUtils

package
v1.1.41 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetPrettyTimeDuration

func GetPrettyTimeDuration(d time.Duration, shorten bool) string

GetPrettyTimeDuration returns a human-readable string representing the time duration. If shorten is true, the output will be formatted as "1d2h3m4s" instead of "1 day 2 hours 3 minutes 4 seconds".

func RegisterUnit

func RegisterUnit(unit string, value uint64)

RegisterUnit registers a new unit with the given value. WARNING: This function is not thread-safe, it's recommended to use it only during the initialization phase of the program. If the unit is already registered, the value will be updated. If the unit contains whitespaces, they will be removed. If the unit is an empty string, nothing will happen.

Types

type Duration

type Duration = time.Duration

func ParseDuration

func ParseDuration(s string) (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". If you want to define custom units, you can use the RegisterUnit function. This code is a modified version of time.ParseDuration from the Go standard library. The original code can be found at https://golang.org/src/time/format.go or https://github.com/golang/go/blob/master/src/time/format.go

func ParseDurationWithDefault

func ParseDurationWithDefault(s string, defaultValue Duration) Duration

ParseDurationWithDefault parses a duration string and returns the duration. If the string is invalid, it will return the default value. If the string is a number without a unit, it will multiply the number by the default value.

Jump to

Keyboard shortcuts

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