timex

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: Apache-2.0 Imports: 5 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-03-25 23:07:36 * @Description:

* @Author: cnzf1 * @Date: 2022-08-26 11:42:04 * @LastEditors: cnzf1 * @LastEditTime: 2023-03-28 20:53:28 * @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
	TIME_LAYOUT_COMPACT_MILLSECOND    = "20060102150405.000"            // YYYYMMDDHHmmSS.NNN
)

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 NowStr added in v1.2.1

func NowStr(layout string) string

NowStr returns the current time string.

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. seps represent separator, default is '-'

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. seps represent separator, default is '-'

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. seps represent separator, default is '-'

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. seps represent separator, default is '-'

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() Ticker
}

func NewJitteredBackoffManager

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

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

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 FakeTicker added in v1.2.2

type FakeTicker interface {
	Ticker
	Done()
	Tick()
	Wait(d time.Duration) error
}

FakeTicker interface is used for unit testing.

func NewFakeTicker added in v1.2.2

func NewFakeTicker() FakeTicker

NewFakeTicker returns a FakeTicker.

type Ticker

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

Ticker interface wraps the Chan and Stop methods.

func NewTicker added in v1.2.2

func NewTicker(d time.Duration) Ticker

NewTicker returns a Ticker.

Jump to

Keyboard shortcuts

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