Documentation
¶
Overview ¶
Package fasttime provides a high-performance alternative to time.Now() by caching the current time and updating it periodically in a background goroutine.
The cached time reduces system call overhead significantly, making it suitable for high-frequency operations where exact millisecond precision is not required.
By default, time is updated every 200ms (configurable via FASTTIME_HIGH_PRECISION environment variable). All times are returned in UTC for consistency across distributed systems.
Index ¶
- Constants
- func Now() time.Time
- func Since(t time.Time) time.Duration
- func UnixDate() int64
- func UnixHour() int64
- func UnixNano() int64
- func UnixTime() int64
- func Until(t time.Time) time.Duration
- type XID
- func (x XID) Counter() uint32
- func (x XID) Machine() []byte
- func (x XID) MarshalJSON() (dst []byte, err error)
- func (x XID) MarshalText() (dst []byte, err error)
- func (x XID) Pid() uint16
- func (x XID) String() string
- func (x XID) Time() time.Time
- func (x *XID) UnmarshalJSON(b []byte) (err error)
- func (x *XID) UnmarshalText(text []byte) (err error)
Constants ¶
const ( // DefaultUpdateInterval is the default time update interval. DefaultUpdateInterval = 200 * time.Millisecond // HighPrecisionUpdateInterval is the interval used when FASTTIME_HIGH_PRECISION is set. HighPrecisionUpdateInterval = 10 * time.Millisecond // SecondsPerDay is the number of seconds in a day. SecondsPerDay = 24 * 3600 // SecondsPerHour is the number of seconds in an hour. SecondsPerHour = 3600 )
const ErrInvalidXID = xidError("xid: invalid XID")
ErrInvalidXID is returned when trying to parse an invalid XID
Variables ¶
This section is empty.
Functions ¶
func Since ¶
Since returns the time elapsed since t. It is shorthand for time.Now().Sub(t) but uses the cached time.
Note: If t is in the future, this will return a negative duration. For past times, the result may be up to UpdateInterval ahead of the actual elapsed time.
func UnixNano ¶
func UnixNano() int64
UnixNano returns the current unix timestamp in nanoseconds. It is faster than time.Now().UnixNano().
Types ¶
type XID ¶ added in v1.0.3
type XID [12]byte
XID represents a unique request id
func NewXIDWithTime ¶ added in v1.0.3
NewXIDWithTime generates a globally unique XID with unix timestamp
func (XID) MarshalJSON ¶ added in v1.0.3
MarshalJSON implements encoding/json Marshaler interface
func (XID) MarshalText ¶ added in v1.0.3
MarshalText implements encoding/text TextMarshaler interface
func (XID) String ¶ added in v1.0.3
String returns a base32 hex lowercased representation of the id.
func (*XID) UnmarshalJSON ¶ added in v1.0.3
UnmarshalJSON implements encoding/json Unmarshaler interface
func (*XID) UnmarshalText ¶ added in v1.0.3
UnmarshalText implements encoding/text TextUnmarshaler interface