Time and date

A program needs two different clocks. One says what time it is, so a log line, a certificate and a calendar can agree with the rest of the world. The other measures how long something took, and it must never jump when the wall clock is corrected. Erlang exposes both, and this lesson keeps them apart.

Local time and UTC

calendar:local_time/0 answers the wall clock of the machine as {{Year, Month, Day}, {Hour, Minute, Second}} in its own timezone, and calendar:universal_time/0 does the same in UTC. A server logs UTC and lets the reader convert; a local time depends on a setting that a container may not have. Dates are tuples in the order year, month, day, and calendar:valid_date/1 answers whether such a tuple can exist:

erl

1> calendar:valid_date({2026, 2, 30}). false 2> calendar:is_leap_year(2024). true 3> calendar:day_of_the_week({2026, 10, 10}). 6

The weekday count starts at Monday as 1, so 6 is a Saturday. A wall clock reading is not repeated in this lesson: it changes every second and depends on the timezone, so the examples use fixed dates and counts instead.

Monotonic time

erlang:monotonic_time/1 answers a count that only moves forward. It is the clock for a timeout, a benchmark and a cache age, because an adjustment of the wall clock cannot make it jump. convert_time_unit/3 changes the unit of any such count, and a subtraction gives the elapsed time:

timing.erl

-module(timing). -export([main/0]). main() -> Start = erlang:monotonic_time(millisecond), timer:sleep(10), Stop = erlang:monotonic_time(millisecond), Elapsed = Stop - Start, io:format("~p~n", [Elapsed >= 10]), io:format("~p~n", [erlang:convert_time_unit(1500, millisecond, second)]).

erlc timing.erl

erl -noshell -eval 'timing:main(), halt().'

true 1

The sleep of ten milliseconds can last longer on a loaded machine, so the program asks whether it lasted at least ten. The conversion truncates: 1500 milliseconds are one second, and the half second is gone. A program that needs the fraction keeps the millisecond count.

The epoch

erlang:system_time/1 is the wall clock as a count since the Unix epoch, 1 January 1970 at midnight UTC. calendar:system_time_to_universal_time/2 turns such a count into a date tuple, and the unit is part of the call:

units.erl

-module(units). -export([main/0]). main() -> Seconds = 1700000000, Milliseconds = 1700000000000, io:format("~p~n", [calendar:system_time_to_universal_time(Seconds, second)]), io:format("~p~n", [calendar:system_time_to_universal_time(Milliseconds, second)]).

erlc units.erl

erl -noshell -eval 'units:main(), halt().'

{{2023,11,14},{22,13,20}} {{55840,11,8},{22,13,20}}

The first line is the date of the count; the second passes the same instant in milliseconds while the call still reads second, so the answer lands in the year 55840. The unit is not inferred from the number, and a wrong one is a valid date far from the intended one. A timestamp from JSON or a database arrives with a unit of its own; read it before converting.

Date arithmetic

calendar:date_to_gregorian_days/1 turns a date into a running day count, and subtracting two counts gives the days between them. The count already includes every leap day, so February needs no special case. This module wraps the three questions a booking program asks:

dates.erl

-module(dates). -export([main/0, between/2, month_length/2, weekday/1]). between(From, To) -> calendar:date_to_gregorian_days(To) - calendar:date_to_gregorian_days(From). month_length(Year, Month) -> calendar:last_day_of_the_month(Year, Month). weekday(Date) -> calendar:day_of_the_week(Date). main() -> io:format("~p~n", [between({2026, 10, 10}, {2026, 12, 25})]), io:format("~p~n", [month_length(2024, 2)]), io:format("~p~n", [weekday({2026, 10, 10})]).

erlc dates.erl

erl -noshell -eval 'dates:main(), halt().'

76 29 6

February 2024 has 29 days because the day count of the calendar knows the leap year, and the difference counts it too. A month length comes from calendar:last_day_of_the_month/2 rather than a table that a programmer has to keep correct.

Units matter

Every time call in this lesson names its unit, and the unit is part of the value. The two mistakes that follow from ignoring it are adding a duration in seconds to a count in milliseconds, and reading a millisecond timestamp as seconds. Both produce a number, and both produce a wrong date rather than an error.

A program converts at the boundary with erlang:convert_time_unit/3, keeps one unit inside, and names that unit in the variable: TimeoutMs, not Timeout. The timers lesson applies the same rule to erlang:send_after/3, which also takes a unit.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How does calendar represent a date?

    Answer

    {Year, Month, Day}

    A date is {Year, Month, Day} and a time is {Hour, Minute, Second}, in that order.

  2. What does calendar:valid_date({2026, 2, 30}) answer?

    Answer

    false

    February 2026 has 28 days, so the 30th is not a valid date.

  3. Which clock should measure how long a call took?

    Answer

    erlang:monotonic_time/1

    The monotonic clock only moves forward, so a wall-clock correction cannot change the difference.

  4. Why does the lesson avoid printing calendar:local_time()?

    Answer

    It changes every second and depends on the timezone

    A wall-clock answer cannot be replayed, so the examples use fixed dates and counts instead.

  5. What does this module print? -module(q1). -export([main/0]). main() -> io:format("~p~n", [erlang:convert_time_unit(2500, millisecond, second)]).

    Answer

    2

    The conversion truncates, so 2500 milliseconds are two whole seconds.

  6. What does this module print? -module(q2). -export([main/0]). main() -> io:format("~p~n", [calendar:date_to_gregorian_days({2026, 10, 10}) >= calendar:date_to_gregorian_days({2026, 10, 9})]).

    Answer

    true

    A later date has a larger day count, so the comparison is true.

  7. What does this module print? -module(q3). -export([main/0]). main() -> io:format("~p~n", [calendar:last_day_of_the_month(2025, 2)]).

    Answer

    28

    2025 is not a leap year, so February has 28 days.

  8. What is wrong with calendar:system_time_to_universal_time(1700000000000, second)?

    Answer

    The count is in milliseconds but the unit says seconds

    The unit is not inferred from the number, so the millisecond count is read as seconds and lands in year 55840.

  9. Why does date arithmetic handle leap years for free?

    Answer

    The day count already includes every day

    Day counts are a plain running total, so subtracting them counts the extra day of a leap year automatically.

  10. What does this module print? -module(q4). -export([main/0]). main() -> io:format("~p~n", [calendar:system_time_to_universal_time(0, second)]).

    Answer

    {{1970,1,1},{0,0,0}}

    Time zero is the Unix epoch, 1 January 1970 at midnight UTC.

The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Автор статьи: Arthur Isaev