timehelper

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ceil

func Ceil(x time.Time, d time.Duration) time.Time

func CeilDuration

func CeilDuration(x, d time.Duration) time.Duration

func Floor

func Floor(x time.Time, d time.Duration) time.Time

func FloorDuration

func FloorDuration(x, d time.Duration) time.Duration

func Mod

func Mod(action ModAction, x time.Time, d time.Duration) time.Time

Mod returns a new time based on t in timezone of the time t

time.{Round, Truncat} operates on UTC time

e.g. 01:01:31 in TZ -11:30 (12:31:31 in UTC)

timezone offset = -11:30

thus will have default behavior when dur is 1hr

time.Round(12:31:31): 13:00:00 UTC -> 01:30:00 in TZ -11:30 time.Truncat(12:31:31): 12:00:00 UTC -> 00:30:00 in TZ -11:30

what actually expected to local time is

floor: 01:00:00 round: 01:00:00 ceil: 02:00:00

func ModDuration

func ModDuration(action ModAction, x, d time.Duration) time.Duration

ModDuration return a new duration of x that is multiple of d

func ParseDuration

func ParseDuration(s string, base time.Time) (dur int64, err error)

ParseDuration parses durations like

1000ns, 200us, 100ms, 1s, 2m, 3h, 4d, 5w, 6M, 6y
1d2h, 5w3d

following units are supported:

ns - nanoseconds
us - microseconds
ms - milliseconds
s - seconds
m - minutes
h or hr - hours
d - days
w - weeks
M or mt - months
y or yr - years

only supports decimal numbers, other number format may cause silent error

months and years are normalized on the basis of the base time and MUST be integer values, other units are deterministic

parsed value is the duration in nanoseconds

func ParseTime

func ParseTime(s string, base time.Time) (t time.Time, err error)

ParseTime parses time string s in following format preference

  • 15:04
  • 2006-01-02
  • 2006-01-02T15:04:05
  • 2006-01-02T15:04:05Z07:00
  • 15:04:05
  • 15

func Round

func Round(x time.Time, d time.Duration) time.Time

func RoundDuration

func RoundDuration(x, d time.Duration) time.Duration

Types

type ModAction

type ModAction uint8
const (
	ModAction_Round ModAction = iota
	ModAction_Floor
	ModAction_Ceil
)

Jump to

Keyboard shortcuts

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