timeutil

package
v0.0.0-...-39628cf Latest Latest
Warning

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

Go to latest
Published: May 4, 2024 License: AGPL-3.0 Imports: 4 Imported by: 1

Documentation

Index

Constants

View Source
const (
	Day    = 24 * time.Hour
	Week   = 7 * Day
	Mounth = 30 * Day
	Year   = 364 * Day
)

Approximate time units.

Variables

This section is empty.

Functions

func NowFunc

func NowFunc(f func() time.Time) func() time.Time

NowFunc returns f if f is not nil, otherwise returns time.Now

func ReadTime

func ReadTime(f func() time.Time) time.Time

ReadTime runs the function and returns the time if f is not null, or returns time.Now() if f is null.

func Time

func Time(ts *Timestamp) time.Time

Time converts timestamp to time.Time .

func TimeDuration

func TimeDuration(d *Duration) time.Duration

TimeDuration converts d to time.Duration. If d is nil, 0 duration is returned.

Types

type Challenge

type Challenge struct {
	N string // Nonce.
	T *Timestamp
}

Challenge is a timestamp with a crypto random nonce. A server can provide an HMAC signed challenge for clients via RPC, and use it as a structure to restrict the client into a time window that is defined by the server. It also provides a reliable clock source and a nonce source. The nonce can be used as a one-time token to defend replay attacks.

func NewChallenge

func NewChallenge(t time.Time, rand io.Reader) (*Challenge, error)

NewChallenge creates a challenge with the given timestamp t. rand is used to generate the nonce.

type Duration

type Duration struct {
	Sec  int64
	Nano int64 `json:",omitempty"`
}

Duration is a struct to record a duration. It is designed to be directly usable in Javascript.

func NewDuration

func NewDuration(d time.Duration) *Duration

NewDuration creates a new duration of d.

func (*Duration) Duration

func (d *Duration) Duration() time.Duration

Duration converts to time.Duration type.

type Timestamp

type Timestamp struct {
	Sec  int64
	Nano int64 `json:",omitempty"`
}

Timestamp is a struct to record a UTC timestamp. It is designed to be directly usable in Javascript.

func CopyTimestamp

func CopyTimestamp(ts *Timestamp) *Timestamp

CopyTimestamp copies a timestamp.

func NewTimestamp

func NewTimestamp(t time.Time) *Timestamp

NewTimestamp creates a new timestamp from the given time.

func ReadTimestamp

func ReadTimestamp(f func() time.Time) *Timestamp

ReadTimestamp runs the function and returns the time as a Timestamp, or return the result of time.Now() as a timestamp.

func TimestampNow

func TimestampNow() *Timestamp

TimestampNow creates a time stamp of the time now.

func (*Timestamp) Clone

func (t *Timestamp) Clone() *Timestamp

Clone clones the timestamp.

func (*Timestamp) Time

func (t *Timestamp) Time() time.Time

Time returns the time of this timestamp in UTC.

Jump to

Keyboard shortcuts

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