timex

package
v1.1.8 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

* @Author: cnzf1 * @Date: 2023-01-04 09:58:25 * @LastEditors: cnzf1 * @LastEditTime: 2023-01-04 09:58:29 * @Description:

* @Author: cnzf1 * @Date: 2022-07-20 13:56:45 * @LastEditors: cnzf1 * @LastEditTime: 2023-01-09 17:26:51 * @Description:

* @Author: cnzf1 * @Date: 2022-08-26 11:42:04 * @LastEditors: cnzf1 * @LastEditTime: 2022-12-15 16:31:34 * @Description:

Index

Constants

View Source
const (
	TIME_LAYOUT_YEAR                  = "2006"                          // YYYY
	TIME_LAYOUT_MONTH                 = "2006-01"                       // YYYY-MM
	TIME_LAYOUT_DAY                   = "2006-01-02"                    // YYYY-MM-DD
	TIME_LAYOUT_MINUTE                = "2006-01-02 15:04"              // YYYY-MM-DD HH:mm
	TIME_LAYOUT_SECOND                = "2006-01-02 15:04:05"           // YYYY-MM-DD HH-mm:SS
	TIME_LAYOUT_MILLSECOND            = "2006-01-02 15:04:05.000"       // YYYY-MM-DD HH-mm:SS.NNN
	TIME_LAYOUT_MICROSECOND           = "2006-01-02 15:04:05.000000"    // YYYY-MM-DD HH-mm:SS.NNNNNN
	TIME_LAYOUT_NANOSECOND            = "2006-01-02 15:04:05.000000000" // YYYY-MM-DD HH-mm:SS.NNNNNNNNN
	TIME_LAYOUT_COMPACT_DAY           = "20060102"                      // YYYYMMDD
	TIME_LAYOUT_COMPACT_SIMPLE_SECOND = "150405"                        // hhmmss
	TIME_LAYOUT_COMPACT_EXT_SECOND    = "0102150405"                    // MMDDhhmmss
	TIME_LAYOUT_COMPACT_SECOND        = "20060102150405"                // YYYYMMDDhhmmss
)

Variables

This section is empty.

Functions

func BackoffUntil

func BackoffUntil(f func(), backoff BackoffManager, sliding bool, stopCh <-chan struct{})

func JitterAround

func JitterAround(duration time.Duration, jitter float64) time.Duration

JitterAround return duration which added a random jitter

This adds or subtracts time from the duration within a given jitter fraction. For example for 10s and jitter 0.1, it will return a time within [9s, 11s])

func JitterUntil

func JitterUntil(f func(), period time.Duration, jitterFactor float64, sliding bool, stopCh <-chan struct{})

func JitterUp

func JitterUp(duration time.Duration, factor float64) time.Duration

JitterUp return duration which added factor times the cardinality For example for 10s and jitter 1, it will return a time within [10s, 20s])

func MsToUTCTime

func MsToUTCTime(t int64) time.Time

MillisToTime returns the UTC time corresponding to the given Unix time, t milliseconds since January 1, 1970 UTC.

func NowMs

func NowMs() int64

NowMs returns the current milliseconds.

func NowS

func NowS() int64

NowS returns the current seconds.

func NowUs

func NowUs() int64

NowUs returns the current microseconds.

func StrToTime

func StrToTime(s string) time.Time

input format is 2022-01-01 01:00:00

func SubDaySets

func SubDaySets(before, last time.Time, seps ...string) []string

SubDaySets return the date set during before and last.

For example:

before := time.Date(2020, 12, 28, 1, 5, 10, 0, time.Local)
after := time.Date(2021, 1, 2, 13, 10, 30, 0, time.Local)
Call SubDaySets(before, after)
	-> []string{"2020-12-28","2020-12-29","2020-12-30", "2020-12-31","2021-01-01","2021-01-02"}

func SubDaySetsEx

func SubDaySetsEx(before, last time.Time, useFirst bool, useLast bool, seps ...string) []string

SubDaySetsEx return the date set during before and last. useFirst represent include the date of before. useLast represent include the date of last.

For example:

before := time.Date(2020, 12, 28, 1, 5, 10, 0, time.Local)
after := time.Date(2021, 1, 2, 13, 10, 30, 0, time.Local)
Call SubDaySetsEx(before, after, true, true)
	-> []string{"2020-12-28","2020-12-29","2020-12-30", "2020-12-31","2021-01-01","2021-01-02"}

func SubDays

func SubDays(before, last time.Time) int

SubDays return the days between before and last

func SubMonSets

func SubMonSets(before, last time.Time, seps ...string) []string

SubMonSets return the month set during before and last.

For example:

before := time.Date(2020, 12, 28, 1, 5, 10, 0, time.Local)
after := time.Date(2021, 1, 2, 13, 10, 30, 0, time.Local)
Call SubMonSets(before, after)
	-> []string{"2020-12", "2021-01"}

func SubMonSetsEx

func SubMonSetsEx(before, last time.Time, useFirst bool, useLast bool, seps ...string) []string

SubMonSetsEx return the month set during before and last. useFirst represent include the month of before. useLast represent include the month of last.

For example:

before := time.Date(2020, 12, 28, 1, 5, 10, 0, time.Local)
after := time.Date(2021, 1, 2, 13, 10, 30, 0, time.Local)
Call SubMonSetsEx(before, after, true, true)
	-> []string{"2020-12", "2021-01"}

func SubYearSets

func SubYearSets(before, last time.Time) []string

SubYearSets return the year set during before and last.

For example:

before := time.Date(2020, 12, 28, 1, 5, 10, 0, time.Local)
after := time.Date(2021, 1, 2, 13, 10, 30, 0, time.Local)
Call SubYearSets(before, after)
	-> []string{"2020", "2021"}

func SubYearSetsEx

func SubYearSetsEx(before, last time.Time, useFirst bool, useLast bool) []string

SubYearSetsEx return the year set during before and last. useFirst represent include the year of before. useLast represent include the year of last.

For example:

before := time.Date(2020, 12, 28, 1, 5, 10, 0, time.Local)
after := time.Date(2021, 1, 2, 13, 10, 30, 0, time.Local)
Call SubYearSetsEx(before, after, true, true)
	-> []string{"2020", "2021"}

func TSToStr

func TSToStr(sec int64, nsec int64) string

output format is 2022-01-01 01:00:00

func TSToTime

func TSToTime(sec int64, nsec int64) time.Time

output format is 2022-01-01 01:00:00

func TimeToMs

func TimeToMs(t time.Time) int64

TimeToMs returns an integer number, which represents t in milliseconds.

func TimeToStr

func TimeToStr(s time.Time) string

output format is 2022-01-01 01:00:00

func Until

func Until(f func(), period time.Duration, stopCh <-chan struct{})

Types

type BackoffManager

type BackoffManager interface {
	Backoff() Timer
}

func NewJitteredBackoffManager

func NewJitteredBackoffManager(duration time.Duration, jitter float64, c Clock) BackoffManager

NewJitteredBackoffManager returns a BackoffManager that backoffs with given duration plus given jitter. If the jitter is negative, backoff will not be jittered.

type Clock

type Clock interface {
	PassiveClock
	// After returns the channel of a new Timer.
	// This method does not allow to free/GC the backing timer before it fires. Use
	// NewTimer instead.
	After(d time.Duration) <-chan time.Time
	// NewTimer returns a new Timer.
	NewTimer(d time.Duration) Timer
	// Sleep sleeps for the provided duration d.
	// Consider making the sleep interruptible by using 'select' on a context channel and a timer channel.
	Sleep(d time.Duration)
	// Tick returns the channel of a new Ticker.
	// This method does not allow to free/GC the backing ticker. Use
	// NewTicker from WithTicker instead.
	Tick(d time.Duration) <-chan time.Time
}

Clock allows for injecting fake or real clocks into code that needs to do arbitrary things based on time.

type ElapsedTimer

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

func NewElapsedTimer

func NewElapsedTimer() *ElapsedTimer

NewElapsedTimer returns an ElapsedTimer.

func (*ElapsedTimer) Duration

func (et *ElapsedTimer) Duration() time.Duration

Duration returns the elapsed time.

func (*ElapsedTimer) Elapsed

func (et *ElapsedTimer) Elapsed() time.Duration

Elapsed returns the string representation of elapsed time.

func (*ElapsedTimer) ElapsedMs

func (et *ElapsedTimer) ElapsedMs() float32

ElapsedMs returns the elapsed time of string on milliseconds.

type PassiveClock

type PassiveClock interface {
	Now() time.Time
	Since(time.Time) time.Duration
}

PassiveClock allows for injecting fake or real clocks into code that needs to read the current time but does not support scheduling activity in the future.

type RealClock

type RealClock struct{}

RealClock really calls time.Now()

func (RealClock) After

func (RealClock) After(d time.Duration) <-chan time.Time

After is the same as time.After(d). This method does not allow to free/GC the backing timer before it fires. Use NewTimer instead.

func (RealClock) AfterFunc

func (RealClock) AfterFunc(d time.Duration, f func()) Timer

AfterFunc is the same as time.AfterFunc(d, f).

func (RealClock) NewTicker

func (RealClock) NewTicker(d time.Duration) Ticker

NewTicker returns a new Ticker.

func (RealClock) NewTimer

func (RealClock) NewTimer(d time.Duration) Timer

NewTimer is the same as time.NewTimer(d)

func (RealClock) Now

func (RealClock) Now() time.Time

Now returns the current time.

func (RealClock) Since

func (RealClock) Since(ts time.Time) time.Duration

Since returns time since the specified timestamp.

func (RealClock) Sleep

func (RealClock) Sleep(d time.Duration)

Sleep is the same as time.Sleep(d) Consider making the sleep interruptible by using 'select' on a context channel and a timer channel.

func (RealClock) Tick

func (RealClock) Tick(d time.Duration) <-chan time.Time

Tick is the same as time.Tick(d) This method does not allow to free/GC the backing ticker. Use NewTicker instead.

type Ticker

type Ticker interface {
	Chan() <-chan time.Time
	Stop()
}

Ticker defines the Ticker interface.

type Timer

type Timer interface {
	Chan() <-chan time.Time
	Stop() bool
	Reset(d time.Duration) bool
}

Timer allows for injecting fake or real timers into code that needs to do arbitrary things based on time.

type WithTicker

type WithTicker interface {
	Clock
	// NewTicker returns a new Ticker.
	NewTicker(time.Duration) Ticker
}

WithTicker allows for injecting fake or real clocks into code that needs to do arbitrary things based on time.

Jump to

Keyboard shortcuts

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