cromo La guida di Cromo
Scarica PDF EN

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.

cromo
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

cromo
struct Duration

A span of time, as elapsed() and since() give it.

  • i64 nanoseconds: the whole span, in nanoseconds

#Duration.seconds

cromo
f64 seconds() const

The span in seconds, with their fractions.

#Duration.milliseconds

cromo
i64 milliseconds() const

The whole milliseconds of the span, without the rest.

#Duration.microseconds

cromo
i64 microseconds() const

The whole microseconds of the span, without the rest.

#Instant

cromo
struct Instant

A 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

cromo
Duration elapsed() const

The time from this instant to now.

#Instant.since

cromo
Duration since(Instant earlier) const

The time from earlier to this instant; negative if earlier comes after it.

#Stopwatch

cromo
struct Stopwatch

A 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.

cromo
Stopwatch()

#Stopwatch.start

cromo
void start()

Starts again from now: the time and the laps count from here.

#Stopwatch.elapsed

cromo
Duration elapsed() const

The time from the start to now. The stopwatch goes on.

#Stopwatch.lap

cromo
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

cromo
struct Zone

A 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

cromo
string name() const

The name of the zone: "Europe/Rome", "UTC", "Local" for the one of the system, "+05:30" for a fixed offset.

#Zone.offsetAt

cromo
i32 offsetAt(i64 seconds) const

The 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

cromo
struct DateTime

A 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: 2026
  • i32 month: 1 to 12
  • i32 day: 1 to 31
  • i32 hour: 0 to 23
  • i32 minute: 0 to 59
  • i32 second: 0 to 59
  • i32 nanosecond: 0 to 999999999
  • i32 offset: from UTC, in minutes: 60 in Rome in winter, 0 in UTC

#DateTime.text

cromo
string text() const

The 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

cromo
i64 unix() const

The seconds from 1970-01-01 UTC of this moment.

#DateTime.unixNano

cromo
i64 unixNano() const

The nanoseconds from 1970-01-01 UTC of this moment.

#DateTime.inZone

cromo
DateTime inZone(Zone zone) const

The same moment on the clocks of zone.

#DateTime.weekday

cromo
i32 weekday() const

The day of the week: 0 is Sunday, 1 Monday, up to 6, Saturday.

#DateTime.yearDay

cromo
i32 yearDay() const

The day of the year: 1 is the first of January.

#DateTime.add

cromo
DateTime add(Duration span) const

The 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

cromo
DateTime addSeconds(i64 seconds) const

The moment seconds seconds later, as add(): 86400 are a day.

#DateTime.since

cromo
Duration since(DateTime earlier) const

The time from earlier to this moment; negative if earlier comes after it.

#DateTime.format

cromo
string format(string pattern) const

The 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

cromo
Instant now()

The present moment of the monotonic clock, to measure the time until a later one with elapsed() or since().

#unix

cromo
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

cromo
DateTime utcNow()

The date and the time now, in UTC.

#localNow

cromo
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

cromo
Zone utc()

UTC, the zone with no offset.

#local

cromo
Zone local()

The zone of the system, the one of localNow(): its name is "Local".

#fixedZone

cromo
Zone fixedZone(i32 minutes)

A zone that is always minutes minutes from UTC, east with a positive number: fixedZone(330) is "+05:30".

#zone

cromo
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

cromo
DateTime fromUnix(i64 seconds, Zone zone)

The moment at seconds from 1970-01-01 UTC, on the clocks of zone.

#date

cromo
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

cromo
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

cromo
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

cromo
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

cromo
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

cromo
void msleep(i64 milliseconds)

Pauses for milliseconds milliseconds, as sleep() does.

#usleep

cromo
void usleep(i64 microseconds)

Pauses for microseconds microseconds, as sleep() does.