The built-in time module deals with time in its three common forms; the date and hour on the calendar, the interval between two moments, and the passing of time itself, when a program has to wait. Like any standard module, it is brought in with import:

import time

The module has five functions, and they all revolve around one convention.

A moment in time is represented by a plain number, a count of seconds. The function time.time produces such a number, time.format turns it into text, and time.diff subtracts two of them. On the vast majority of systems the count starts at midnight of January 1st, 1970, UTC, an instant called the epoch, but the language does not promise it, and a program should treat these numbers as opaque values to pass between the functions of the module, and not as something to do calendar arithmetic with.

The Current Time

Called without arguments, time.time returns the current moment:

var now = time.time()
print(now)      // 1796284800, or whatever the clock says

The number is of little use by itself.

It is meant to be stored, compared with another one, or given to the other functions of the module.

Formatting

The function time.format converts a moment into a string.

It takes a format string, which describes the layout of the result, and, optionally, the moment to describe, which defaults to the current one. Both arguments are optional:

print(time.format())                        // the current date and time
print(time.format("%Y-%m-%d"))              // 2026-12-03
print(time.format("%H:%M", time.time()))    // 14:05

Every % followed by a letter in the format string is replaced by a part of the date, and any other character is copied as it is.

The letters are those of the strftime function of C, so, besides the ones above, %d is the day of the month, %B the full name of the month, %A the name of the day of the week, %S the seconds, and so on.

The default format, %c, is the preferred representation of the date and time for the current language and region of the system. The complete list of letters is documented by the C library of each platform, and there are small differences between them, so a program that must run in several places should keep to the common ones.

Dates are expressed in the local time zone of the system.

A format that starts with ! is expressed in UTC instead, which is the right choice when the result is going to be stored or exchanged, since it does not depend on where the program happens to be running. The mark is not part of the format:

print(time.format("!%Y-%m-%d %H:%M:%S", 0))     // 1970-01-01 00:00:00
print(time.format("!%Y-%m-%d %H:%M:%S", 86400)) // 1970-01-02 00:00:00

The last two examples print the same results everywhere, which is also why they are a good way of making sure of what a program does. Using the local time zone would have printed a different hour in most countries.

If the amount cannot be represented as a date, the result is nil. An empty format gives back an empty string.

Dates as Maps

The special format *t, with or without !, asks for something else; instead of text, it returns a map holding every part of the date as a number. This is how a program gets hold of the individual components of a date, without parsing a string:

var d = time.format("!*t", 86400)

print(d["year"])    // 1970
print(d["month"])   // 1
print(d["day"])     // 2

The map has the following fields:

FieldMeaning
yearThe year, as in 2026
monthThe month, from 1 to 12
dayThe day of the month, from 1 to 31
hourThe hour, from 0 to 23
minThe minutes, from 0 to 59
secThe seconds, normally from 0 to 59
wdayThe day of the week, from 1 to 7, where 1 is Sunday
ydayThe day of the year, from 1 to 366
isdsttrue when summer time is in effect, false otherwise

The field isdst is only present if the system knows the answer, so, as for any map, it is read with get when it may be missing.

Notice also that months, week days and year days count from one, unlike the lists and ranges of the language, which count from zero.

Building a Moment

The same map can be used in the opposite direction.

Given to time.time, it describes a date in local time, and the function returns the moment that corresponds to it:

var t = time.time({ year = 2026, month = 12, day = 25 })

print(time.format("%A, %d %B %Y", t))   // Friday, 25 December 2026

The fields year, month and day are required, and omitting any of them raises an error.

The others are optional. The seconds and minutes default to zero, and the hour to twelve, noon, a choice that keeps a date that is given without an hour away from the edges of the day, where a change of time zone or of summer time could make it appear to be the one before or after.

Finally, isdst can state whether summer time is in effect, and is best left out, so that the system decides.

The values do not need to be in their usual ranges.

They are normalized, so a program can do calendar arithmetic by changing one field and letting the system carry the excess into the others. Adding thirty days to a date, for example, requires no knowledge of the length of the months:

var d = time.format("*t")           // today
d["day"] += 30

var later = time.time(d)
print(time.format("%Y-%m-%d", later))   // the date thirty days from now

Since the map returned by *t has the same fields that time.time expects, one call feeds the other directly. The extra fields, such as wday, are ignored.

Differences

The function time.diff returns the number of seconds between two moments.

It takes the later one first, followed by the earlier one, which defaults to zero, the epoch:

var a = time.time({ year = 2026, month = 12, day = 3 })
var b = time.time({ year = 2026, month = 12, day = 25 })

print(time.diff(b, a))              // 1900800
print(time.diff(b, a) / 86400)      // 22 days

A day has 86400 seconds, except in the days in which the clocks change, which have one hour more or less.

A computation of days that spans such a change will therefore be off by a fraction, and should be rounded. The example above is exact because both dates are in the same season.

Since moments are numbers, the operator - produces the same difference. The function exists for the sake of clarity, and for systems where the representation of time is not a plain count of seconds.

Measuring and Waiting

The calendar clock is not the right tool to measure how long a piece of code takes.

It has a resolution of one second, and it can be adjusted, by the user or the network, while the program is still running. For that purpose there is time.clock, which returns the processor time used by the program so far, in seconds, as a fraction. The way to use it is to read it twice and subtract:

var start = time.clock()

var total = 0
for var i in 0..1000000
{
    total += i
}

print("took ${time.clock() - start} seconds")

The important word here is processor.

The clock advances only while the program is actually computing, so time spent waiting for a file, for the user, or inside time.sleep, is not counted. That makes it a good measure of the cost of a computation, and an unsuitable one of how long the user has been waiting.

The function time.sleep suspends the program for a number of seconds, which can be fractional. It returns nil:

print("wait for it...")
time.sleep(1.5)
print("done")

Nothing else runs in the meantime, since the whole program is paused.

Also, the pause is at least as long as requested, but the system makes no promise that it will not be a little longer.

An Example

The following program is a countdown to a date.

It takes the target from a map, computes the whole days left, and then shows a short animated count of the last seconds, redrawing the same line on the terminal. It uses a date as a map, time.diff for the difference, time.format for the output, and time.sleep for the pauses:

// countdown.tea
import io, time

var target = time.time({ year = 2026, month = 12, day = 25 })
var now = time.time()

var left = time.diff(target, now)
if left <= 0
{
    print("the date has already passed")
}
else
{
    var days = left / 86400
    print("until ${time.format('%d %B %Y', target)}: ${days} days")
}

for var i in 3..0..-1
{
    io.stdout.write(i, "...\r")
    io.stdout.flush()
    time.sleep(1)
}
print("go!   ")
$ tea countdown.tea
until 25 December 2026: 21.5 days
go!

The number of days is fractional because the target is set at noon and the program runs at an arbitrary hour.

A program that needs whole days must round, or compare dates and not moments. The flush in the loop is needed so that every number is shown right away, as explained in the IO Module chapter, and the carriage return \r brings the cursor back to the start of the line, so each number replaces the previous one.