libtime

package
v1.76.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: BSD-3-Clause Imports: 7 Imported by: 1

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

View Source
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.

View Source
const DefaultPackageName = "time"

DefaultPackageName is the package name used by LoadPackage.

Variables

View Source
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.

View Source
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 BuiltinDurationBetween(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func BuiltinDurationMS

func BuiltinDurationMS(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

BuiltinDuriationNS returns a float equal to the the number of nanoseconds in the given duration.

func BuiltinDurationNS

func BuiltinDurationNS(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

BulitinDuriationNS returns an integer equal to the the number of nanoseconds in the given duration.

func BuiltinDurationSeconds

func BuiltinDurationSeconds(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

BulitinDurationSecods returns a float equal to the the number of seconds in the given duration.

func BuiltinFormatRFC3339

func BuiltinFormatRFC3339(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func BuiltinFormatRFC3339Nano

func BuiltinFormatRFC3339Nano(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func BuiltinParseDuration

func BuiltinParseDuration(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func BuiltinParseRFC3339

func BuiltinParseRFC3339(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func BuiltinParseRFC3339Nano

func BuiltinParseRFC3339Nano(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func BuiltinSleep

func BuiltinSleep(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

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 BuiltinTimeAdd

func BuiltinTimeAdd(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func BuiltinTimeEq

func BuiltinTimeEq(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func BuiltinTimeGT

func BuiltinTimeGT(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func BuiltinTimeLT

func BuiltinTimeLT(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func Duration

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

Duration returns an LVal representing duration d.

func Get

func Get(v *lisp.LVal) (time.Time, bool)

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

func GetDuration(v *lisp.LVal) (time.Duration, bool)

GetDuration gets a time.Duration value from v and returns it.

func LoadPackage

func LoadPackage(env *lisp.LEnv) *lisp.LVal

LoadPackage adds the time package to env

func Time

func Time(t time.Time) *lisp.LVal

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.

Jump to

Keyboard shortcuts

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