Time Methods
Time values in Risor represent a specific moment in time. They support formatting, arithmetic, and comparison through built-in methods.
Note: Time values are created through Go host functions. The
time_valuein the examples below represents a time value obtained from your Go integration.
Method Reference
| Method | Description |
|---|---|
format(layout) | Format the time as a string using a Go layout |
unix() | Unix timestamp as an integer (seconds since epoch) |
utc() | Convert to UTC time zone |
add_date(years, months, days) | Return a new time with a date offset |
after(other) | True if this time is after other |
before(other) | True if this time is before other |
Formatting
format
Format a time value as a string. Risor uses Go's time layout convention, where a specific reference time is used as the format template.
The Go reference time is:
Mon Jan 2 15:04:05 MST 2006
Each component of this reference time maps to a part of the output:
| Component | Reference Value | Meaning |
|---|---|---|
| Year | 2006 | Four-digit year |
| Month | 01 or Jan | Numeric or abbreviated month |
| Day | 02 | Day of month |
| Hour (24h) | 15 | Hour in 24-hour format |
| Hour (12h) | 3 | Hour in 12-hour format |
| Minute | 04 | Minute |
| Second | 05 | Second |
| Time zone | MST | Time zone abbreviation |
let t = time_value
t.format("2006-01-02") // "2024-03-15"
t.format("Jan 2, 2006") // "Mar 15, 2024"
t.format("15:04:05") // "14:30:00"
t.format("2006-01-02T15:04:05Z07:00") // ISO 8601 formatConversion
unix
Return the Unix timestamp as an integer (seconds since January 1, 1970 UTC).
let t = time_value
t.unix() // e.g. 1710504600utc
Return a new time value converted to the UTC time zone.
let t = time_value
let u = t.utc()
u.format("15:04:05 MST") // e.g. "19:30:00 UTC"Arithmetic
add_date
Return a new time value offset by the given number of years, months, and days. The original time is not modified.
let t = time_value
// One year from now
let next_year = t.add_date(1, 0, 0)
// Three months ago
let past = t.add_date(0, -3, 0)
// Tomorrow
let tomorrow = t.add_date(0, 0, 1)
// Combined: 1 year, 2 months, 15 days from now
let future = t.add_date(1, 2, 15)Comparison
after
Return true if this time is after the given time.
let t1 = time_value
let t2 = t1.add_date(0, 0, 1) // one day later
t2.after(t1) // true
t1.after(t2) // falsebefore
Return true if this time is before the given time.
let t1 = time_value
let t2 = t1.add_date(0, 0, 1) // one day later
t1.before(t2) // true
t2.before(t1) // falseExample: Date Comparison
let start = time_value
let deadline = start.add_date(0, 0, 30) // 30 days from start
if (time_value.after(deadline)) {
"past deadline"
} else {
"within deadline"
}