Documentation
¶
Overview ¶
Package libtime provides immutable wall-clock timestamps and duration operations. Owned timestamps preserve timezone/calendar rules without exposing shared mutable Go timezone objects; use Time and Get at Go boundaries.
Index ¶
- Constants
- Variables
- func BuiltinDurationBetween(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal
- func BuiltinDurationMS(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal
- func BuiltinDurationNS(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal
- func BuiltinDurationSeconds(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal
- func BuiltinFormatRFC3339(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal
- func BuiltinFormatRFC3339Nano(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal
- func BuiltinParseDuration(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal
- func BuiltinParseRFC3339(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal
- func BuiltinParseRFC3339Nano(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal
- func BuiltinSleep(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal
- func BuiltinTimeAdd(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal
- func BuiltinTimeEq(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal
- func BuiltinTimeGT(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal
- func BuiltinTimeLT(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal
- func Duration(d time.Duration) *lisp.LVal
- func Get(v *lisp.LVal) (time.Time, bool)
- func GetDuration(v *lisp.LVal) (time.Duration, bool)
- func LoadPackage(env *lisp.LEnv) *lisp.LVal
- func Time(t time.Time) *lisp.LVal
Constants ¶
const ( DurableTimeName = "elps:time" DurableDurationName = "elps:duration" )
Durable typed JSON names of the codecs this package declares. An embedder that already saves documents under another name registers the codec with WithName.
const DefaultPackageName = "time"
DefaultPackageName is the package name used by LoadPackage.
Variables ¶
var DurableDurationCodec = libjson.DurableCodec[time.Duration]{ Name: DurableDurationName, Version: 1, Save: func(_ *lisp.LEnv, v *lisp.LVal) (*lisp.LVal, error) { d, ok := GetDuration(v) if !ok { return nil, fmt.Errorf("%s: not a duration", DurableDurationName) } return lisp.Int(int(d)), nil }, Load: func(_ *lisp.LEnv, _ int, p *lisp.LVal) (*lisp.LVal, error) { if p.Type != lisp.LInt { return nil, fmt.Errorf("%s: payload is not an integer", DurableDurationName) } return Duration(time.Duration(p.Int)), nil }, }
DurableDurationCodec is the durable codec of durations (Duration). A duration saves as its integer number of nanoseconds.
var DurableTimeCodec = libjson.DurableCodec[ownedTime]{ Name: DurableTimeName, Version: 1, Save: func(_ *lisp.LEnv, v *lisp.LVal) (*lisp.LVal, error) { t, ok := Get(v) if !ok { return nil, fmt.Errorf("%s: not a time value", DurableTimeName) } return lisp.String(t.UTC().Format(time.RFC3339Nano)), nil }, Load: func(_ *lisp.LEnv, _ int, p *lisp.LVal) (*lisp.LVal, error) { if p.Type != lisp.LString { return nil, fmt.Errorf("%s: payload is not a string", DurableTimeName) } t, err := time.Parse(time.RFC3339Nano, p.Str) if err != nil { return nil, fmt.Errorf("%s: %w", DurableTimeName, err) } return Time(t.UTC()), nil }, }
DurableTimeCodec is the durable codec of time values (Time). A time saves as its instant in UTC, RFC 3339 with nanoseconds, and loads in UTC. The location is not saved, because its rules come from the host's timezone database, which peers do not share.
Functions ¶
func BuiltinDurationBetween ¶
func BuiltinDurationMS ¶
BuiltinDuriationNS returns a float equal to the the number of nanoseconds in the given duration.
func BuiltinDurationNS ¶
BulitinDuriationNS returns an integer equal to the the number of nanoseconds in the given duration.
func BuiltinDurationSeconds ¶
BulitinDurationSecods returns a float equal to the the number of seconds in the given duration.
func BuiltinParseRFC3339Nano ¶
func BuiltinSleep ¶
BuiltinSleep sleeps for the given duration before returning.
LoadPackage does not register it: the time package offers no builtin whose result depends on goroutine timing (#757). It stays exported for hosts that register a sleep in their own package, with formals (time-duration &key max).
The sleep is bounded by the evaluation's context (see LEnv.Context): it wakes early if the context is cancelled, and never sleeps past the context's deadline. See sleepContext for the full rationale.
func Get ¶
Get returns a Go time.Time with an independent timezone object. Mutating its Location cannot change v or another export. The timezone's value is preserved, not its pointer identity. Nil and non-native values return the zero time and false. Raw host time.Time natives remain supported, but are not automatically approved for template sharing.
func GetDuration ¶
GetDuration gets a time.Duration value from v and returns it.
func LoadPackage ¶
LoadPackage adds the time package to env
func Time ¶
Time creates an immutable native wall-clock value representing t. It takes an independent snapshot of t's timezone object, preserving named-zone and daylight-saving rules but dropping Go monotonic clock metadata. The native payload is private; use Get to obtain an independent Go time.Time value. Callers must not mutate t's timezone concurrently with this call. This does not make capturing wall-clock time during template initialization equivalent to reading the clock independently during each cold load.
Types ¶
This section is empty.