Dates and calendars
The time lesson keeps two clocks apart. This one writes a timestamp for another program to read: the RFC 3339 string, the offset it carries, and the week number a report needs. A timestamp that names its offset is unambiguous; a local timestamp is not, and that difference is the lesson.
RFC 3339
calendar:system_time_to_rfc3339/2 turns a count into the Erlang-friendly date string of RFC 3339. The options name the unit of the count and the offset of the output: "Z" for UTC, or a signed hour and minute. The same instant prints as two different strings, and both are correct:
rfc.erl
-module(rfc). -export([main/0]). main() -> Utc = calendar:system_time_to_rfc3339(1700000000, [{unit, second}, {offset, "Z"}]), Paris = calendar:system_time_to_rfc3339(1700000000, [{unit, second}, {offset, "+01:00"}]), io:format("~s~n", [Utc]), io:format("~s~n", [Paris]), io:format("~p~n", [calendar:rfc3339_to_system_time(Utc, [{unit, second}])]).
erlc rfc.erl
erl -noshell -eval 'rfc:main(), halt().'
2023-11-14T22:13:20Z 2023-11-14T23:13:20+01:00 1700000000
The strings differ by the offset and describe one instant; the parse at the end returns the original count. A log line, an HTTP header and a database column all take this form, and the offset is the part that makes it portable.
Parsing and the offset
calendar:rfc3339_to_system_time/2 demands the offset. A string without Z or +01:00 names a local time, and the function refuses it rather than guess the zone of the machine that parses it:
no_offset.erl
-module(no_offset). -export([main/0]). main() -> Result = try calendar:rfc3339_to_system_time("2023-11-14T22:13:20", [{unit, second}]) of Seconds -> {ok, Seconds} catch error:_ -> no_offset end, io:format("~p~n", [Result]), io:format("~p~n", [calendar:rfc3339_to_system_time("2023-11-14T22:13:20Z", [{unit, second}])]).
erlc no_offset.erl
erl -noshell -eval 'no_offset:main(), halt().'
no_offset 1700000000
The first call raises, and the program turns that into its own atom; the second, with the Z, returns the count. The refusal is a feature: a parser that guessed would be right on one machine and wrong on the next, and the wrong answer is still a valid timestamp. A service stores UTC and prints the offset of the reader only at the edge.
ISO weeks
A report that groups by week cannot use the week of the month. ISO 8601 numbers the weeks from Monday, and the first week of a year is the one that holds the first Thursday. That is why 1 January can belong to the last week of the previous year, and why calendar:iso_week_number/1 answers a pair of year and week:
week.erl
-module(week). -export([main/0]). main() -> io:format("~p~n", [calendar:iso_week_number({2026, 1, 1})]), io:format("~p~n", [calendar:iso_week_number({2026, 12, 31})]).
erlc week.erl
erl -noshell -eval 'week:main(), halt().'
{2026,1} {2026,53}
2026 begins on a Thursday, so 1 January is in week 1, and the year ends in week 53. A program that computed the week as (day_of_year div 7) + 1 would disagree with every calendar in the world at the edges, which is why the library function exists.
Time zones and DST
A time zone is not a fixed offset: it changes with the date and with the government of
the day. During the hour when a clock goes back, one local time happens twice, and
during the hour it goes forward, one local time never happens.
calendar:local_time_to_universal_time_dst/1 takes a local
timestamp and answers a list of the UTC times it can mean: empty for a time that never
happened, one for an ordinary time, two for the repeated hour.
For that reason a program stores UTC, or a count of seconds, and never a local
timestamp as the source of truth. The offset travels with the value in RFC 3339, and a
display converts at the edge with the zone of the reader. The
time lesson shows the monotonic clock for
elapsed time; this rule is about the wall clock that a human reads.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
calendar:system_time_to_rfc3339/2need besides the count?Answer
The unit and the offset of the output
The options name the unit of the count and the offset string of the result.
-
What does the
Zat the end of an RFC 3339 string mean?Answer
UTC, an offset of zero
Zis the military letter for zero, so the timestamp is UTC. -
Why does
rfc3339_to_system_time/2refuse a string without an offset?Answer
A local time does not name its zone
A timestamp without an offset names a local time, and guessing the machine zone would be wrong elsewhere.
-
What does
calendar:iso_week_number/1answer?Answer
A pair of year and week
The first week can belong to the previous year, so the answer carries both numbers.
-
What does this module print?
-module(q1). -export([main/0]). main() -> io:format("~s~n", [calendar:system_time_to_rfc3339(0, [{unit, second}, {offset, "Z"}])]).Answer
1970-01-01T00:00:00ZTime zero is midnight UTC on 1 January 1970, and the offset is Z.
-
What does this module print?
-module(q2). -export([main/0]). main() -> io:format("~p~n", [calendar:rfc3339_to_system_time( "1970-01-01T00:00:01+00:00", [{unit, second}])]).Answer
1The string is one second after the epoch, and the unit asked for is seconds.
-
What does this module print?
-module(q3). -export([main/0]). main() -> io:format("~p~n", [calendar:iso_week_number({2026, 1, 1})]).Answer
{2026,1}1 January 2026 is a Thursday, so the date falls in week 1 of 2026.
-
What does this module print?
-module(q4). -export([main/0]). main() -> io:format("~p~n", [calendar:rfc3339_to_system_time( "2023-11-14T23:13:20+01:00", [{unit, second}])]).Answer
1700000000The offset is one hour ahead of UTC, so the instant is the same as 22:13:20Z.
-
What does
calendar:local_time_to_universal_time_dst/1answer for a local time in the repeated DST hour?Answer
Two UTC times
The hour happens twice, so the function answers both possible UTC times.
-
Why should a service store UTC rather than a local timestamp?
Answer
A local timestamp does not name its zone or DST state
Without the offset, one local string can mean two instants or none, so it cannot be the source of truth.
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