times

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2022 License: MIT Imports: 8 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertTZ

func ConvertTZ(t time.Time, tz string) (time.Time, error)

ConvertTZ converts a time.Time into a new time.Time in a timezone specified by tz. This is unlike OverwriteTZ, where its tz is overwritten, resulting in a completely different point of time: ConvertTZ keeps the point of time unchanged - it merely switches the input time.Time to a different timezone.

func NewOSClock added in v0.0.17

func NewOSClock() *osClock

NewOSClock returns a Clock interface implementation that uses time.Now.

func OverwriteTZ

func OverwriteTZ(t time.Time, tz string) (time.Time, error)

OverwriteTZ takes the literal values of y/m/d/h/m/s of a time.Time and uses them together with a supplied timezone to form a new time.Time, effectively "overwriting" the original time.Time's tz. If tz is empty, the original time.Time is returned.

func SmartParse

func SmartParse(s string) (t time.Time, tz bool, err error)

SmartParse parses a date time string and returns a time.Time and a tz flag indicates whether the date time string contains tz info. The date time string can be either date only, or date + time, or date + time + tz. The date part of the string has the following supported formats:

yyyy-mm-dd
mm-dd-yyyy
yyyy/mm/dd
mm/dd/yyyy
mm/dd/yy
yyyymmdd

The time part of the string has the following supported formats:

hh:mm:ss[.sssssssss]  (sub-second precision is up to nano-second, or 9-digit)
hh:mm
hhmmss
hhmm

Note: all formats above can also be followed by 'AM/PM'. The tz part of the string has the following supported formats:

Z
-/+hh
-/+hhmm
-/+hh:mm
-America/New_York

Note 1: the tz name in the last one above must come from standard IANA timezone names. Upon successful parsing, SmartParse returns the time.Time, and a flag whether the input date time string has tz info in it or not. Note the time.Time returned will always have tz baked in, because golang time.Time doesn't really have a notion of un-timezone'ed timestamp. So if your date time string is a un-tz'ed relative time stamp, such as "2020/09/10T12:34:56", which really means different point of time depending on which time zone you try to interpret it in, the returned tz flag will be false but the returned time.Time will be "2020/09/10T12:34:56Z" (note the 'Z') if you format it using RFC3339. That's the key subtlety one must understand well.

Types

type Clock added in v0.0.17

type Clock interface {
	Now() time.Time
}

Clock tells the current time.

type TimedSlidingWindowI64 added in v0.0.17

type TimedSlidingWindowI64 struct {
	// contains filtered or unexported fields
}

TimedSlidingWindowI64 offers a way to aggregate int64 values over a time-based sliding window.

func NewTimedSlidingWindowI64 added in v0.0.17

func NewTimedSlidingWindowI64(window, bucket time.Duration, clock ...Clock) *TimedSlidingWindowI64

NewTimedSlidingWindowI64 creates a new time-based sliding window for int64 value aggregation. window is the sliding window "width", and bucket is the granularity of how the window is divided. Both must be non-zero and window must be of an integer multiple of bucket. Be careful of not making bucket too small as it would increase the internal bucket memory allocation. If no clock is passed in, then os time.Now clock will be used.

func (*TimedSlidingWindowI64) Add added in v0.0.17

func (t *TimedSlidingWindowI64) Add(amount int64)

Add adds a new int64 value into the current sliding window.

func (*TimedSlidingWindowI64) Reset added in v0.0.17

func (t *TimedSlidingWindowI64) Reset()

Reset resets the sliding window and clear the existing aggregated value.

func (*TimedSlidingWindowI64) Total added in v0.0.17

func (t *TimedSlidingWindowI64) Total() int64

Total returns the aggregated int64 value over the current sliding window.

Jump to

Keyboard shortcuts

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