timex

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: MIT Imports: 4 Imported by: 0

README

timex

build-img pkg-img reportcard-img coverage-img version-img

Better time package for Go.

Rationale

TODO

Features

  • Simple.

See GUIDE.md for more details.

Install

Go version 1.17+

go get github.com/cristalhq/timex

Example

TODO

Documentation

See these docs.

License

MIT License.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var RealClock = &systemClock{}
View Source
var RealUTCClock = &systemUTCClock{}

Functions

func MulDur added in v0.1.1

func MulDur(d time.Duration, scale float64) time.Duration

MulDur multiplies duration by a float value.

Example
package main

import (
	"time"

	"github.com/cristalhq/timex"
)

func main() {
	window := time.Minute

	window = timex.MulDur(window, 1.5)
	if window.Seconds() != 90 {
		panic("want 90 have " + window.String())
	}

	window = time.Minute
	window = time.Duration(float64(window) * 1.5)
	if window.Seconds() != 90 {
		panic("want 90 have " + window.String())
	}

}

func Now added in v0.1.1

func Now() time.Time

Now returns currect time in UTC.

func StopTimer

func StopTimer(t time.Timer)

StopTimer in a proper manner.

Types

type Clock

type Clock interface {
	Now() time.Time
}

Clock represents interface on time. Useful in tests.

type Duration

type Duration = time.Duration

Duration is an alias for time.Duration.

type ManualClock

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

ManualClock represents clock than can be controlled manually.

func NewManualClock

func NewManualClock(curr time.Time) *ManualClock

NewManualClock returns manual Clock with a given time.

func (*ManualClock) Add

func (c *ManualClock) Add(d time.Duration)

func (*ManualClock) Now

func (c *ManualClock) Now() time.Time

func (*ManualClock) Set

func (c *ManualClock) Set(t time.Time)

type Timestamp added in v0.1.1

type Timestamp struct {
	time.Time
}

func AsTimestamp added in v0.1.1

func AsTimestamp(t time.Time) Timestamp

AsTimestamp returns time.Time wrapped as Timestamp.

func TimestampFromMilli added in v0.1.2

func TimestampFromMilli(msec int64) Timestamp

TimestampFromMilli returns Timestamp from the Unix time in milliseconds.

func TimestampFromString added in v0.1.2

func TimestampFromString(s string) (Timestamp, error)

TimestampFromString returns Timestamp fomr the string representing Unix time.

func (Timestamp) MarshalJSON added in v0.1.1

func (ts Timestamp) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface.

func (*Timestamp) UnmarshalJSON added in v0.1.1

func (ts *Timestamp) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler interface.

Jump to

Keyboard shortcuts

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