Pelago
stdTime

Text forms

std.timefmt: RFC 3339, ISO 8601, RFC 2822, HTTP dates, durations and your own patterns, for formatting and parsing.

import std.timefmt

std.timefmt turns dates and times into text and back. Everything that reads text returns ! time_error and never stops the program. Parsing is strict: the whole text must be used, numbers have the widths the pattern gives, and the date must exist.

import std.time
import std.calendar
import std.zone
import std.timefmt
import { time_error } = std.time

fn main() -> void ! time_error {
    const z = timefmt.parseRfc3339("2026-11-17T09:47:23.5+01:00")?
    print(z, z.moment())
    print(timefmt.formatZoned(z, "EEEE d MMMM yyyy, HH:mm xxx")?)
    const d = timefmt.parse("17/11/2026 9:05 PM", "dd/MM/yyyy h:mm a")?
    print(d)
    print(timefmt.parseDuration("PT1H30M")?, timefmt.formatDuration(time.minutes(90)))
}
2026-11-17T09:47:23.5+01:00 1794905243.500000000
Tuesday 17 November 2026, 09:47 +01:00
2026-11-17T21:05:00
5400s PT1H30M

Standard forms

FunctionResultNotes
rfc3339(zoned)string2026-11-17T09:47:23+01:00, Z for UTC, with the fraction when there is one.
rfc2822(zoned)stringMail form: Tue, 17 Nov 2026 09:47:23 +0100.
httpDate(moment)stringHTTP header form, always GMT: Tue, 17 Nov 2026 09:47:23 GMT.
formatDuration(d)stringISO 8601 with days, hours, minutes and seconds: PT1H30M, P1DT12H, PT0.5S, -PT5S.
parseRfc3339(text)zoned ! time_errorNeeds an offset; t, z and a space for T are accepted, and a fraction has one to nine digits.
parseRfc2822(text)zoned ! time_errorThe weekday is optional and checked; GMT and UT mean +0000.
parseHttpDate(text)moment ! time_error
parseDate(text), parseTime(text), parseDateTime(text)date, time_of_day, date_time2026-11-17, 09:47:23.5, 2026-11-17T09:47:23.5.
parseDuration(text)duration ! time_errorYears and months are an error: they are not a fixed length.
import std.time
import std.calendar
import std.zone
import std.timefmt
import { time_error } = std.time

fn main() -> void ! time_error {
    const paris = zone.named("Europe/Paris")?
    const z = paris.zoned(time.fromUnix(1700000000))
    print(timefmt.rfc3339(z))
    print(timefmt.rfc2822(z))
    print(timefmt.httpDate(time.fromUnix(1700000000)))
    print(timefmt.formatZoned(z, "yyyy-MM-dd HH:mm:ss z VV")?)
    print(timefmt.format(z.dateTime(), "MMM d, yyyy 'at' h:mm a")?)
    print(timefmt.formatDuration(time.hours(36)), timefmt.formatDuration(time.millis(500)))
    print(timefmt.parseRfc2822("Tue, 17 Nov 2026 09:47:23 +0100")?)
    print(timefmt.parseHttpDate("Tue, 17 Nov 2026 09:47:23 GMT")?)
    print(timefmt.parseDate("2026-02-30").isErr(), timefmt.parse("2026-13-01", "yyyy-MM-dd").isErr())
    print(timefmt.parseDuration("P1M").isErr())
}
2023-11-14T23:13:20+01:00
Tue, 14 Nov 2023 23:13:20 +0100
Tue, 14 Nov 2023 22:13:20 GMT
2023-11-14 23:13:20 CET Europe/Paris
Nov 14, 2023 at 11:13 PM
P1DT12H PT0.5S
2026-11-17T09:47:23+01:00
1794908843.000000000
true true
true

Patterns

format, formatZoned, parse and parseZoned take a pattern. The letters read like what they print, and the same letters format and parse, in the style of Unicode's date patterns.

LettersMeaningExample
yyyy yyYear, four digits or two2026 26
M MM MMM MMMMMonth: number, two digits, short name, long name3 03 Mar March
d ddDay of the month7 07
EEE EEEEWeekday name, short or long (checked when parsing)Tue Tuesday
H HH, h hhHour 0 to 23, hour 1 to 129 09
aAM or PM, with hPM
m mm, s ssMinute, second05
S to SSSSSSSSSFraction of a second, that many digits500
x xx xxxOffset +01, +0100, +01:00+01:00
X XX XXXThe same, Z for zeroZ
z, VVZone abbreviation, zone name (formatting only)CET, Europe/Paris

Text between single quotes is literal ('at'; '' is a quote), and any other character that is not a letter is literal. A letter that is not in the table is a time_error. Names are English only.

FunctionResultNotes
format(date_time, pattern)string ! time_errorNo zone, so x, X, z and VV are errors.
formatZoned(zoned, pattern)string ! time_error
parse(text, pattern)date_time ! time_errorA pattern with no offset letter.
parseZoned(text, pattern)zoned ! time_errorThe pattern must have x or X; the zone is that fixed offset.

Zones come from std.zone and dates from std.calendar.

On this page