timeperiod

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2022 License: MIT Imports: 2 Imported by: 0

README

timeperiod

A GO module
See pkg.go.dev for documentation

Convert strings like "3m", "2w" and "5d".

It can convert to:

  • either int values for seconds, milli-, micro- or nanoseconds
  • or time.Duration

Invalid strings will return 0 (zero)

If no unit is given it will assume that the value is in seconds

Units

ns // nanoseconds
us // microseconds
ms // milliseconds
s // seconds
m // minutes
h // hours
d // days
w // weeks

Examples

timeperiod.Seconds("26w") // 15724800
timeperiod.Seconds("2d") // 172800
timeperiod.Seconds("5m") // 300
timeperiod.Milliseconds("5m") // 300000
timeperiod.Microconds("1h") // 3600000000
timeperiod.Nanoseconds("1us") // 1000
timeperiod.Duration("5ns") // 5

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Duration added in v1.1.0

func Duration(period string) time.Duration

Duration converts a time string with unit to time.Duration Valid time units are 'ns', 'us', 'ms', 's', 'm', 'h', `d` and `w`

Example
d := Duration("5ms")
fmt.Printf("duration is %d milliseconds", d.Milliseconds())
Output:

duration is 5 milliseconds

func Microseconds

func Microseconds(period string) int

Microseconds converts a time string with unit to an integer number of microseconds Valid time units are 'ns', 'us', 'ms', 's', 'm', 'h', `d` and `w`

Example
us := Microseconds("3ms")
fmt.Printf("period is %d microseconds", us)
Output:

period is 3000 microseconds

func Milliseconds

func Milliseconds(period string) int

Milliseconds converts a time string with unit to an integer number of milliseconds Valid time units are 'ns', 'us', 'ms', 's', 'm', 'h', `d` and `w`

Example
ms := Milliseconds("5s")
fmt.Printf("period is %d milliseconds", ms)
Output:

period is 5000 milliseconds

func Nanoseconds

func Nanoseconds(period string) int

Nanoseconds converts a time string with unit to an integer number of nanoseconds Valid time units are 'ns', 'us', 'ms', 's', 'm', 'h', `d` and `w`

Example
ns := Nanoseconds("4us")
fmt.Printf("period is %d nanoseconds", ns)
Output:

period is 4000 nanoseconds

func Seconds

func Seconds(period string) int

Seconds converts a time string with unit to an integer number of seconds Valid time units are 'ns', 'us', 'ms', 's', 'm', 'h', `d` and `w`

Example
s := Seconds("1h")
fmt.Printf("period is %d seconds", s)
Output:

period is 3600 seconds

Types

This section is empty.

Jump to

Keyboard shortcuts

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