clock

package module
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2025 License: MIT Imports: 10 Imported by: 10 modules, 11 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Clock

Fast analogue of time package methods.

Benchmarks

BenchmarkClock/clock.Now()-8         	1000000000          0.8397 ns/op
BenchmarkClock/time.Now()-8          	20795817	        57.93 ns/op

Format

pattern description
%A full weekday name
%a abbreviated weekday name
%B full month name
%b abbreviated month name
%C two digit representation of the century
%c equivalent to %a %b %e %H:%M:%S %Y
%D equivalent to %m/%d/%y
%d day of the month with leading zero (01-31)
%e day of the month with leading space (1-31)
%F equivalent to %Y-%m-%d
%H the hour (24-hour clock) as a decimal number [00-23]
%h alias of %b
%I hour (12-hour clock) as a decimal number [01-12]
%j day of the year with leading zero [001-366]
%k hour (24-hour clock) with leading space [0-23]
%l hour (12-hour clock) with leading space [1-12]
%M minute with leading zero [00-59]
%m month with leading zero [01-12]
%N nanoseconds (9 digits)
%n nanoseconds (7 digits)
%o microseconds (6 digits)
%i milliseconds (3 digits)
%P am/pm
%p AM/PM
%R equivalent to %H:%M
%r equivalent to %I:%M:%S %p
%S second with leading zero [00-60]
%T equivalent to %H:%M:%S
%U week number of the year (Sunday as the first day of the week) with leading zero [00-53]
%u weekday (Monday as the first day of the week) [1-7]
%V week number of the year (Monday as the first day of the week) with leading zero [01-53]
%v equivalent to %e-%b-%Y
%W week number of the year (Monday as the first day of the week) with leading zero [00-53]
%w the weekday (Sunday as the first day of the week) as a decimal number [0-6]
%X equivalent to %H:%M:%S
%x equivalent to %m/%d/%y
%Y year with century as a decimal number
%y year without century as a decimal number [00-99]
%Z time zone name
%z time zone offset from UTC
%% symbol '%'

Documentation

Index

Constants

View Source
const (
	StatusIdle int32 = iota
	StatusActive
)
View Source
const (
	Layout      = "%m/%d %H:%M:%S%p '%y %z"
	ANSIC       = "%a %b %e %H:%M:%S %Y"
	UnixDate    = "%a %b %e %H:%M:%S %Z %Y"
	RubyDate    = "%a %b %d %H:%M:%S %z %Y"
	RFC822      = "%d %b %y %H:%M %Z"
	RFC822Z     = "%d %b %y %H:%M %z"
	RFC850      = "%A, %d-%b-%y %H:%M:%S %Z"
	RFC1123     = "%a, %d %b %Y %H:%M:%S %Z"
	RFC1123Z    = "%a, %d %b %Y %H:%M:%S %z"
	RFC3339     = "%Y-%m-%dT%H:%M:%S%z"
	RFC3339Nano = "%Y-%m-%dT%H:%M:%S.%n%z"
	Kitchen     = "%L:%M%p"
	Stamp       = "%b %e %H:%M:%S"
	StampMilli  = "%b %e %H:%M:%S.%i"
	StampMicro  = "%b %e %H:%M:%S.%o"
	StampNano   = "%b %e %H:%M:%S.%N"
)

Variables

View Source
var (
	ErrNoDur   = errors.New("no duration passed")
	ErrBadNum  = errors.New("bad span number")
	ErrBadUnit = errors.New("bad span unit")
	ErrBadEOF  = errors.New("unexpected end of file")
)

Functions

func AppendFormat

func AppendFormat(dst []byte, format string, datetime time.Time) ([]byte, error)

func Format

func Format(format string, datetime time.Time) ([]byte, error)

func FormatStr

func FormatStr(format string, datetime time.Time) (string, error)

DEPRECATED: use FormatString instead.

func FormatString added in v1.1.4

func FormatString(format string, datetime time.Time) (string, error)

func LoadLocation added in v1.1.4

func LoadLocation(name string) (*time.Location, error)

LoadLocation returns Location for given name.

func Relative

func Relative(raw string) (dur time.Duration, err error)

func TimeToBytes added in v1.1.4

func TimeToBytes(dst []byte, val any, args ...any) ([]byte, error)

TimeToBytes converts from time.

Types

type Clock

type Clock struct {
	// Clock precision.
	// Settings this param too small (less than microseconds) or too big (great than second) is counterproductive.
	Precision time.Duration
	// contains filtered or unexported fields
}

Clock is a fast replacement of base methods of time package.

func NewClock

func NewClock() *Clock

NewClock makes new clock with default precision (1 millisecond).

func NewClockWP

func NewClockWP(precision time.Duration) *Clock

NewClockWP makes new clock with given precision.

func (*Clock) Active

func (c *Clock) Active() bool

func (*Clock) Jump

func (c *Clock) Jump(delta time.Duration)

Jump performs time travel.

func (*Clock) Now

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

Now returns current time.

func (*Clock) Relative

func (c *Clock) Relative(raw string) time.Time

func (*Clock) Schedule

func (c *Clock) Schedule(dur time.Duration, fn func())

func (*Clock) Start

func (c *Clock) Start()

Start initializes and starts the clock.

func (*Clock) Stop

func (c *Clock) Stop()

type Interface

type Interface interface {
	Now() time.Time
}

Interface represents clock interface.

type Native

type Native struct{}

Native is a wrapper over default time.Now() function implements Interface.

func (Native) Now

func (c Native) Now() time.Time

type Stuck

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

Stuck represents stuck clock implements Interface.

func NewStuck

func NewStuck(sec, nsec int64) *Stuck

func (Stuck) Now

func (c Stuck) Now() time.Time

Jump to

Keyboard shortcuts

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