ptime

package
v0.4.44 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2023 License: ISC Imports: 6 Imported by: 4

Documentation

Overview

Package parltime provides time utility functions

Index

Constants

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 added in v0.4.22

func OnTimedThread(send func(at time.Time), period time.Duration, loc *time.Location, g0 recover.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())
…

func OnTimer

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

OnTimer returns a time.Timer that waits from t to the next period-multiple from zero time.

  • period must be greater than zero or panic
  • t contains time zone that matters for durations 24 h or longer, typicaly this is time.Local
  • default t is time.Now()

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 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

Jump to

Keyboard shortcuts

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