std.time
Clocks, dates and pauses: now() reads a monotonic clock to measure how long something takes, utcNow() and localNow() give the date and the time, unix() the seconds from 1970, and sleep(), msleep() and usleep() pause the program. A DateTime goes to another time zone, is written with a format and read from a text.
import std.time
time.Instant start = time.now()
time.msleep(50)
println("{} ms", start.elapsed().milliseconds())
println("{}", time.localNow().text()) // 2026-10-29T14:03:12.250+01:00
time.Zone rome = time.zone("Europe/Rome")
time.DateTime meeting = time.date(2026, 7, 1, 9, 30, 0, rome)
println("{}", meeting.inZone(time.zone("America/New_York")).format("%A %H:%M")) // Wednesday 03:30
time.DateTime read = time.parse("2026-07-01 09:30", "%Y-%m-%d %H:%M")The guide: Files, processes and time.
Generata dalla documentazione nei sorgenti della libreria, che è in inglese.
#Tipi
#Duration
struct DurationA span of time, as elapsed() and since() give it.
i64 nanoseconds: the whole span, in nanoseconds
#Duration.seconds
f64 seconds() constThe span in seconds, with their fractions.
#Duration.milliseconds
i64 milliseconds() constThe whole milliseconds of the span, without the rest.
#Duration.microseconds
i64 microseconds() constThe whole microseconds of the span, without the rest.
#Instant
struct InstantA moment of the monotonic clock, from now(). That clock never goes back, also when the date of the system changes: it serves to measure how long something takes.
#Instant.elapsed
Duration elapsed() constThe time from this instant to now.
#Instant.since
Duration since(Instant earlier) constThe time from earlier to this instant; negative if earlier comes after it.
#Stopwatch
struct StopwatchA stopwatch, to measure how long the parts of a work take, on the monotonic clock. It starts when it is made, as time.Stopwatch watch = time.Stopwatch(): elapsed() is the time from its start, lap() the time of each part.
Stopwatch()#Stopwatch.start
void start()Starts again from now: the time and the laps count from here.
#Stopwatch.elapsed
Duration elapsed() constThe time from the start to now. The stopwatch goes on.
#Stopwatch.lap
Duration lap()The time from the last lap(), or from the start for the first one, to now: the time of a part of the work, when it is called at the end of each.
#Zone
struct ZoneA time zone: UTC, the one of the system, one with a name of the IANA database as "Europe/Rome", or a fixed offset. It says how far from UTC the clocks of a place are at each moment, with its summer time and the changes of its history.
#Zone.name
string name() constThe name of the zone: "Europe/Rome", "UTC", "Local" for the one of the system, "+05:30" for a fixed offset.
#Zone.offsetAt
i32 offsetAt(i64 seconds) constThe offset from UTC of the zone, in minutes, at seconds from 1970-01-01 UTC: 60 in Rome in winter and 120 in summer.
#DateTime
struct DateTimeA date with the time of day and its offset from UTC: from utcNow() and localNow(), from date() and fromUnix() in a zone, from parse(). It is a moment: inZone() gives the same moment on the clocks of another zone.
i32 year: with all its digits: 2026i32 month: 1 to 12i32 day: 1 to 31i32 hour: 0 to 23i32 minute: 0 to 59i32 second: 0 to 59i32 nanosecond: 0 to 999999999i32 offset: from UTC, in minutes: 60 in Rome in winter, 0 in UTC
#DateTime.text
string text() constThe date and the time in the format of ISO 8601, with the milliseconds: "2026-10-29T14:03:12.250+01:00", or with Z at the end in UTC.
#DateTime.unix
i64 unix() constThe seconds from 1970-01-01 UTC of this moment.
#DateTime.unixNano
i64 unixNano() constThe nanoseconds from 1970-01-01 UTC of this moment.
#DateTime.inZone
DateTime inZone(Zone zone) constThe same moment on the clocks of zone.
#DateTime.weekday
i32 weekday() constThe day of the week: 0 is Sunday, 1 Monday, up to 6, Saturday.
#DateTime.yearDay
i32 yearDay() constThe day of the year: 1 is the first of January.
#DateTime.add
DateTime add(Duration span) constThe moment span later, or earlier with a negative one, with the same offset from UTC: inZone() then gives it on the clocks of a zone, which may have changed their offset in between.
#DateTime.addSeconds
DateTime addSeconds(i64 seconds) constThe moment seconds seconds later, as add(): 86400 are a day.
#DateTime.since
Duration since(DateTime earlier) constThe time from earlier to this moment; negative if earlier comes after it.
#DateTime.format
string format(string pattern) constThe date and the time written as pattern says, with the letters of strftime of C after a %: %Y 2026 %m 07 %d 01 %e " 1" %j 182 (the day of the year) %y 26 %H 09 %I 09 (of 12) %M 30 %S 05 %p AM or PM %f 250000 (microseconds) %3f 250 %6f 250000 %9f 250000000 %A Wednesday %a Wed %B July %b Jul %u 3 (1 is Monday) %w 3 (0 is Sunday) %z +0200 %:z +02:00 %s the seconds from 1970 %F %Y-%m-%d %T %H:%M:%S %D %m/%d/%y %R %H:%M %% a % The names are in English. A letter that is not of these throws an Error.
#Funzioni
#now
Instant now()The present moment of the monotonic clock, to measure the time until a later one with elapsed() or since().
#unix
i64 unix()The seconds from 1970-01-01 UTC, the time of Unix, which fs.modified() and the times of a JSON Web Token use too.
#utcNow
DateTime utcNow()The date and the time now, in UTC.
#localNow
DateTime localNow()The date and the time now, in the zone of the system, whose offset from UTC is in the field offset. On Linux the zone is that of /etc/localtime, and UTC without it.
#utc
Zone utc()UTC, the zone with no offset.
#local
Zone local()The zone of the system, the one of localNow(): its name is "Local".
#fixedZone
Zone fixedZone(i32 minutes)A zone that is always minutes minutes from UTC, east with a positive number: fixedZone(330) is "+05:30".
#zone
Zone zone(string name)The zone with a name of the IANA Time Zone Database, as "Europe/Rome", "America/New_York" or "UTC". Its rules are the ones of the system when it has them, as Linux and Android do, and otherwise the ones that the library carries, which go into the program: so the name is known on every system. It throws an Error for a name that is not a zone. A program asks for a zone once and keeps it.
#fromUnix
DateTime fromUnix(i64 seconds, Zone zone)The moment at seconds from 1970-01-01 UTC, on the clocks of zone.
#date
DateTime date(i32 year, i32 month, i32 day, i32 hour, i32 minute, i32 second, Zone zone)The moment when the clocks of zone show this date and this time. The numbers out of their range move the others, as in mktime of C: the month 13 is January of the next year, the day 0 the last one of the month before. A time that the clocks skip when the summer time begins is taken with the offset of before, and one that they show twice when it ends is the first of the two.
#parse
DateTime parse(string text, string pattern)Reads a date and a time written as pattern says, with the letters of DateTime.format(): parse("2026-07-01 09:30", "%Y-%m-%d %H:%M"). The text must be all and only what the pattern says; a space of the pattern is any number of spaces, the names of the days and of the months are in English, in full or with three letters, in capitals or not. What the pattern does not have is the first of January, midnight. The offset is the one of the text, with %z, and UTC without it. It throws an Error for a text that is not as the pattern, or not a date.
#parseIn
DateTime parseIn(string text, string pattern, Zone zone)Reads a date and a time as parse() does, on the clocks of zone when the text has no offset.
#parseIso
DateTime parseIso(string text)Reads a date of ISO 8601 and RFC 3339, as text() writes it: "2026-07-01", "2026-07-01T09:30", "2026-07-01T09:30:05.250+02:00", with a space for the T too, and Z or an offset at the end; UTC without it. It throws an Error for another text.
#sleep
void sleep(i64 seconds)Pauses for seconds seconds; in a task, the task waits and its thread runs the other tasks. Nothing happens for 0 or less.
#msleep
void msleep(i64 milliseconds)Pauses for milliseconds milliseconds, as sleep() does.
#usleep
void usleep(i64 microseconds)Pauses for microseconds microseconds, as sleep() does.