time
Module time
provides functionality for measuring and displaying time.
This is primarily a wrapper of the Go time (opens in a new tab) package, with the one difference being duration values are represented as float values (in seconds) instead of a dedicated duration type.
Constants
Predefined layouts for use in time.parse
and time.format
include:
- ANSIC
- UnixDate
- RubyDate
- RFC822
- RFC822Z
- RFC850
- RFC1123
- RFC1123Z
- RFC3339
- RFC3339Nano
- Kitchen
- Stamp
- StampMilli
- StampMicro
- StampNano
Example Constant Usage
>>> time.parse(time.RFC3339, "2023-08-01T12:00:00-04:00")
time("2023-08-01T12:00:00-04:00")
Functions
now
now() time
Returns the current time as a time object.
>>> time.now()
time("2024-01-15T12:51:10-05:00")
unix
unix() time
Unix returns the local Time corresponding to the given Unix time, sec seconds and nsec nanoseconds since January 1, 1970 UTC
>>> time.unix(1725885470, 0)
time("2024-09-09T14:37:50+02:00")
parse
parse(layout, value string) time
Parses a string into a time.
>>> time.parse(time.RFC3339, "2023-08-07T21:19:27-04:00")
time("2023-08-07T21:19:27-04:00")
since
since(t time) float
Returns the elapsed time in seconds since the given time.
>>> t := time.now()
>>> time.since(t)
1.864104666
sleep
sleep(duration float)
Sleeps for the given duration in seconds.
>>> time.sleep(1)
Types
time
The time
type represents a moment in time.
Methods
time.before
before(t time) bool
Returns whether this time is before the given time.
>>> t := time.parse(time.RFC3339, "2023-08-01T12:00:00-04:00")
>>> t.before(time.parse(time.RFC3339, "2023-08-02T00:00:00-04:00"))
true
time.after
after(t time) bool
Returns whether this time is after the given time.
>>> t := time.parse(time.RFC3339, "2023-08-01T12:00:00-04:00")
>>> t.after(time.parse(time.RFC3339, "2023-08-02T00:00:00-04:00"))
false
time.format
format(layout string) string
Formats the time according to the given layout.
>>> t := time.parse(time.RFC3339, "2023-08-01T12:00:00-04:00")
>>> t.format(time.RFC3339)
"2023-08-01T12:00:00-04:00"
>>> t.format(time.Kitchen)
"12:00PM"
>>> t.format(time.UnixDate)
"Tue Aug 1 12:00:00 EDT 2023"
time.utc
utc() time
Returns the UTC time corresponding to this time.
>>> t := time.parse(time.RFC3339, "2023-08-01T12:00:00-04:00")
>>> t.utc()
time("2023-08-01T16:00:00Z")
time.unix
unix() int
Returns the number of seconds elapsed since the Unix epoch.
>>> t := time.parse(time.RFC3339, "2023-08-01T12:00:00-04:00")
>>> t.unix()
1690905600