ptime

package
v0.4.95 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2023 License: ISC Imports: 9 Imported by: 4

Documentation

Overview

EpochValue is a timestamp with Thread-Safe atomic access.

Epoch translates a time value to a 64-bit value that can be used atomically.

Period provides real-time based fixed-length interval perioding ordered using a uint64 zero-based index.

Package parltime provides time utility functions

Index

Constants

View Source
const (
	FractionScale = uint64(1) << 63
)
View Source
const (

	// RFC3339NanoSpace RFC3339 format, ns precision, space separator
	RFC3339NanoSpace string = "2006-01-02 15:04:05.999999999Z07:00"
)

Variables

This section is empty.

Functions

func Date added in v0.4.12

func Date(year int, month int, day int, hour int, min int, sec int, nsec int, loc *time.Location) (t time.Time, valid bool)

ptime.Date is like time.Date with a flag valid that indicates whether the provided values represents a valid date. year is a 4-digit year, ie. 2022 is 2022. month 1–12, day 1–31, hour 0–23 minute 0-59, sec 0–59

func Date1k added in v0.4.12

func Date1k(year int, month int, day int, hour int, min int, sec int, nsec int, loc *time.Location) (t time.Time, valid bool)

ptime.Date is like time.Date with a flag valid that indicates whether the provided values represents a valid date. year is a 4-digit year, ie. 2022 is 2022. month 1–12, day 1–31, hour 0–23 minute 0-59, sec 0–59 A year below 1000 is not considered valid

func Duration added in v0.4.26

func Duration(d time.Duration) (printableDuration string)

Duration formats time.Duration to string with 2 digits precision or better. Duration is more readable than time.Duration.String() that has full ns precision.

func Duro

func Duro(period time.Duration, atTime time.Time) (d time.Duration)

Duro returns the duration in nanoseconds until the next duration-multiple from zero time.

  • The period starts from atTime
  • time zone for multiple-calculation is defined by atTime, often time.Local
  • time zone matters for 24 h or longer durations

func GetTimeString

func GetTimeString(wallTime *time.Time) (s string)

GetTimeString rfc 3339: email time format 2020-12-04 20:20:17-08:00

func Ms

func Ms(d time.Duration) string

Ms gets duration in milliseconds

func Ns

func Ns(t time.Time) string

Ns converts time to string with nanosecond accuracy and UTC location

func NsLocal

func NsLocal(t time.Time) string

NsLocal converts time to string with nanosecond accuracy and local time zone

func OnTimedThread deprecated added in v0.4.22

func OnTimedThread(send func(at time.Time), period time.Duration, loc *time.Location, g0 Go)

OnTimedThread invokes a callback on period-multiples since zero-time. thread-safe. OnTimedThread is invoked in a go statement, running in its own thread. The OnTimedThread thread invokes the send callback function periodically.

  • the send function callback needs to be thread-safe and is invoked with the trig time provided
  • loc is scheduling time zone for durations 24 h or greater eg. time.Local or time.UTC
  • the timer thread is cancelled via g0.Context
  • OnTimedThread uses g0.Done to provide thread error and to be waited-upon
  • period must be greater than zero or panic
  • if the send callback panics, that is a g0 fatal error

Usage:

gc := g0.NewGoGroup(context.Background())
defer gc.Wait()
defer gc.Cancel()
go ptime.OnTimedThread(someFunc, time.Second, time.Local, gc.Add(parl.EcSharedChan, parl.ExCancelOnExit).Go())
…

Deprecated: use NewOnTicker2

func OnTimer

func OnTimer(period time.Duration, t ...time.Time) (timer *time.Timer)

OnTimer returns a time.Timer that waits until the next period-multiple since zero time.

  • default starting time until the period multiple is now: time.Now(), optionally the absolute time t
  • t contains time zone that matters for periods over 1 hour, typically this is time.Local
  • the other time zone for Go is time.UTC
  • period must be greater than zero or panic
  • time.NewTimer does not offer the period calculation

func ParseNs

func ParseNs(timeString string) (t time.Time, err error)

ParseNs parses an RFC3339 time string with nanosecond accuracy

func ParseRfc3339nsz added in v0.4.12

func ParseRfc3339nsz(timeString string) (t time.Time, err error)

ParseRfc3339nsz parses a UTC time string at nanoseconds precision.

"2022-01-01T08:00:00.000000000Z"

func ParseS

func ParseS(timeString string) (t time.Time, err error)

ParseS parses an RFC3339 time string with nanosecond accuracy

func ParseTime

func ParseTime(dateString string) (tm time.Time, err error)

ParseTime parses output from Rfc3339

func Rfc3339

func Rfc3339(t time.Time) string

Rfc3339 converts local time to string with second precision and time offset: 2006-01-02 15:04:05-07:00

func Rfc3339msz added in v0.4.12

func Rfc3339msz(t time.Time) (s string)

Rfc3339msz prints a time using UTC and milliseconds precision.

"2022-01-01T08:00:00.000Z"

func Rfc3339nsz added in v0.4.12

func Rfc3339nsz(t time.Time) (s string)

Rfc3339nsz prints a time using UTC and nanoseconds precision.

"2022-01-01T08:00:00.000000000Z"

func Rfc3339sz added in v0.4.12

func Rfc3339sz(t time.Time) (s string)

Rfc3339sz prints a time using UTC and seconds precision.

"2022-01-01T08:00:00Z"

func Rfc3339usz added in v0.4.12

func Rfc3339usz(t time.Time) (s string)

Rfc3339usz prints a time using UTC and microseconds precision.

"2022-01-01T08:00:00.000000Z"

func S

func S(t time.Time) string

S converts time to string with second accuracy and UTC location

func SGreater

func SGreater(t1 time.Time, t2 time.Time) bool

SGreater compares two times first rouding to second

Types

type Alert

type Alert struct {
	time.Duration
	*time.Timer
	Fired bool
}

Alert impelemnts a renewable alert timer

func NewAlert

func NewAlert(d time.Duration) (al *Alert)

NewAlert allocates a renewable alert time

func (*Alert) Start

func (al *Alert) Start()

Start initializes a new alert period

func (*Alert) Stop

func (al *Alert) Stop()

Stop releases resources associated with this alert

type AverageInterval added in v0.4.48

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

AverageInterval is a container for the datapoint aggregate over a single period

func NewAverageInterval added in v0.4.48

func NewAverageInterval(index PeriodIndex) (ai *AverageInterval)

NewAverageInterval returns an avareging container for one averaging interval

func (*AverageInterval) Add added in v0.4.48

func (ai *AverageInterval) Add(value float64)

Add adds a datapoint to the averager

func (*AverageInterval) Aggregate added in v0.4.48

func (ai *AverageInterval) Aggregate(countp *uint64, floatp *float64)

Aggregate provides averaging content for calcukating the average

type Averager added in v0.4.48

type Averager[T constraints.Integer] struct {
	// contains filtered or unexported fields
}

Averager is a container for averaging providing perioding and history.

  • maxCount is the maximum interval over which average is calculated

func NewAverager added in v0.4.48

func NewAverager[T constraints.Integer]() (averager *Averager[T])

NewAverager returns an object that calculates average over a number of interval periods.

func NewAverager2 added in v0.4.51

func NewAverager2[T constraints.Integer](period time.Duration, periodCount int) (averager *Averager[T])

NewAverager2 returns an object that calculates average over a number of interval periods.

func (*Averager[T]) Add added in v0.4.48

func (av *Averager[T]) Add(value T, t ...time.Time)

Add adds a new value basis for averaging

  • value is the sample value
  • t is the sample time, default time.Now()

func (*Averager[T]) Average added in v0.4.48

func (av *Averager[T]) Average(t ...time.Time) (average float64, count uint64)

Average returns the average

  • t is time, default time.Now()
  • if sample count is zero, average is zero
  • count is number of values making up the average

func (*Averager[T]) Index added in v0.4.51

func (av *Averager[T]) Index(t ...time.Time) (index PeriodIndex)

type Averager3 added in v0.4.48

type Averager3[T constraints.Integer] struct {
	Averager[T]
	// contains filtered or unexported fields
}

Averager3 is an average container with last, average and max values.

func NewAverager3 added in v0.4.48

func NewAverager3[T constraints.Integer]() (averager *Averager3[T])

NewAverager3 returns an calculator for last, average and max values.

func (*Averager3[T]) Add added in v0.4.48

func (av *Averager3[T]) Add(value T, t ...time.Time)

Add updates last, average and max values

func (*Averager3[T]) Status added in v0.4.48

func (av *Averager3[T]) Status() (s string)

Status returns "[last]/[average]/[max]"

  • if no values at all: "-/-/-"
  • if no values avaiable to calculate average "1s/-/10s"

type ClosingTicker

type ClosingTicker struct {
	C                 <-chan time.Time
	MaxDuration       time.Duration // atomic int64
	perrors.ParlError               // panic in Shutdown or panic in tick thread
	// contains filtered or unexported fields
}

ClosingTicker is like time.Ticker but the channel C closes on shutdown. A closing channel is detectable by listening threads. If the computer is busy swapping, ticks will be lost. MaxDuration indicates the longest time observed between ticks. MaxDuration is normally 1 s

func NewClosingTicker

func NewClosingTicker(d time.Duration) (t *ClosingTicker)

NewClosingTicker returns a Ticker whose channel C closes on shutdown. A closing channel is detectable by a listening thread.

func (*ClosingTicker) GetError

func (t *ClosingTicker) GetError() (maxDuration time.Duration, err error)

func (*ClosingTicker) Shutdown

func (t *ClosingTicker) Shutdown()

Shutdown causes the channel C to close and resources to be released

type Epoch added in v0.4.50

type Epoch time.Duration

Epoch translates a time value to a 64-bit value that can be used atomically.

func EpochNow added in v0.4.50

func EpochNow(t ...time.Time) (epoch Epoch)

EpochNow translates a time value to a 64-bit value that can be used atomically.

func (Epoch) IsValid added in v0.4.50

func (epoch Epoch) IsValid() (isValid bool)

IsValid returns true if epoch is not zero-time, ie. Epoch(0) corredsponding to time.TIME{} and Time.IsZero

func (Epoch) Time added in v0.4.50

func (epoch Epoch) Time() (t time.Time)

Time returns the time.Time value corresponding to epoch

type EpochValue added in v0.4.50

type EpochValue Epoch

EpochValue is a timestamp with Thread-Safe atomic access.

func (*EpochValue) Get added in v0.4.50

func (ev *EpochValue) Get() (epoch Epoch)

Get returns the current Epoch value

  • zero epoch is returned as zero-time Time.IsZero, time.Time{}
  • to get time.Time: epochValue.Get().Time()
  • to check if non-zero: epochValue.Get().IsValid()

func (*EpochValue) Set added in v0.4.50

func (ev *EpochValue) Set(epoch Epoch) (oldEpoch Epoch)

Set updates the Epoch value returning the old value

  • 0 is the zero-value

func (*EpochValue) SetTime added in v0.4.50

func (ev *EpochValue) SetTime(t ...time.Time) (oldEpoch Epoch)

SetTime updates the Epoch value to a time.Time value returning the old Epoch value

  • default time value is time.Now()
  • time.IsZero or time.Time{} is the zero-value

type Go added in v0.4.58

type Go interface {
	AddError(err error)
	Done(err *error)
	Context() (ctx context.Context)
}

type OnTicker added in v0.4.58

type OnTicker struct {
	C <-chan time.Time // The channel on which the ticks are delivered.
	// contains filtered or unexported fields
}

OnTicker is a ticker triggering on period-multiples since zero time

func NewOnTicker added in v0.4.58

func NewOnTicker(period time.Duration, loc ...*time.Location) (onTicker *OnTicker)

NewOnTicker returns a ticker that ticks on period-multiples since zero time.

  • loc is optional time zone, default time.Local, other time zone is time.UTC
  • time zone matters for periods over 1 hour
  • period must be greater than zero or panic
  • OnTicker is a time.Ticker using on-period multiples

func NewOnTicker2 added in v0.4.58

func NewOnTicker2(period time.Duration, loc *time.Location, callback func(at time.Time), g0 Go) (onTicker *OnTicker)

func (*OnTicker) Stop added in v0.4.58

func (o *OnTicker) Stop()

type Period added in v0.4.48

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

Period provides real-time based fixed-length interval perioding ordered using a uint64 zero-based index.

  • Period provides first period index and fractional usage of the first period

func NewPeriod added in v0.4.48

func NewPeriod(interval time.Duration) (period *Period)

NewPeriod retuens a new numbered-interval sequence.

func (*Period) Available added in v0.4.48

func (p *Period) Available(now PeriodIndex, cap int) (periods int)

Available returns the number of possible periods 1… but no greater than cap

  • now cannot be less than period0

func (*Period) Index added in v0.4.48

func (p *Period) Index(t ...time.Time) (index PeriodIndex)

Index returns the index number for the current period or the period at time t

  • Index is zero-based

func (*Period) Since added in v0.4.48

func (p *Period) Since(now, before PeriodIndex) (periods int)

Since returns the number of periods difference that now is greater than before

  • periods is >= 0
  • now and before must be at least p.period0
  • before cannot be greater than now

func (*Period) Sub added in v0.4.48

func (p *Period) Sub(now PeriodIndex, n int) (periodIndex PeriodIndex)

Sub returns a past period index by n intervals

  • periodIndex will not be less than p.period0
  • now cannot be less than p.period0

type PeriodIndex added in v0.4.48

type PeriodIndex uint64

Jump to

Keyboard shortcuts

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