Skip to content

Commit b085ad2

Browse files
committed
Improve DateTime docs
1 parent 8afac93 commit b085ad2

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

lib/elixir/lib/calendar/datetime.ex

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ defmodule DateTime do
22
@moduledoc """
33
A datetime implementation with a time zone.
44
5-
This datetime can be seen as an ephemeral snapshot
6-
of a datetime at a given time zone. For such purposes,
7-
it also includes both UTC and Standard offsets, as
8-
well as the zone abbreviation field used exclusively
9-
for formatting purposes.
5+
This datetime can be seen as a snapshot of a date and time
6+
at a given time zone. For such purposes, it also includes both
7+
UTC and Standard offsets, as well as the zone abbreviation
8+
field used exclusively for formatting purposes. Note future
9+
datetimes are not necessarily guaranteed to exist, as time
10+
zones may change any time in the future due to geopolitical
11+
reasons. See the "Datetimes as snapshots" section for more
12+
information.
1013
1114
Remember, comparisons in Elixir using `==/2`, `>/2`, `</2` and friends
1215
are structural and based on the DateTime struct fields. For proper
@@ -41,6 +44,56 @@ defmodule DateTime do
4144
Calendar.put_time_zone_database(Tzdata.TimeZoneDatabase)
4245
4346
See the proper names in the library installation instructions.
47+
48+
## Datetimes as snapshots
49+
50+
In the first section, we described datetimes as a "snapshot of
51+
a date and time at a given time zone". To understand precisely
52+
what we mean, let's see an example.
53+
54+
Imagine someone in Poland wants to schedule a meeting with someone
55+
in Brazil in the next year. The meeting will happen at 2:30 AM
56+
in the Polish time zone. At what time will the meeting happen in
57+
Brazil?
58+
59+
You can consult the time zone database today, one year before,
60+
using the API in this module and it will give you an answer that
61+
is valid right now. However, this answer may not be valid in the
62+
future. Why? Because both Brazil and Poland may change their timezone
63+
rules, ultimately affecting the result. For example, a country may
64+
choose to enter or abandon "Daylight Saving Time", which is a
65+
process where we adjust the clock one hour forward or one hour
66+
back once per year. Whenener the rules change, the exact instant
67+
that 2:30 AM in Polish time will be in Brazil may change.
68+
69+
In other words, whenever working with future DateTimes, there is
70+
no guarantee the results you get will always be correct, until
71+
the event actually happens. Therefore, when you ask for a future
72+
time, the answers you get are a snapshot that reflects the current
73+
state of the time zone rules. For datetimes in the past, this is
74+
not a problem, because time zone rules do not change for past
75+
events.
76+
77+
To make matters worse, it may be that the 2:30 AM in Polish time
78+
does not actually even exist or it is ambiguous. If a certain
79+
time zone observes "Daylight Saving Time", they will move their
80+
clock forward once a year. When this happens, there is a whole
81+
hour that does not exist. Then, when they move the clock back,
82+
there is a certain hour that will happen twice. So if you want
83+
to schedule a meeting when this shift back happens, you would
84+
need to explicitly say which of the 2:30 AM you precisely mean.
85+
Applications that are date and time sensitive, need to take
86+
these scenarios into account and correctly communicate them to
87+
users.
88+
89+
The good news is: Elixir contains all of the building blocks
90+
necessary to tackle those problems. The default timezone database
91+
used by Elixir, `Calendar.UTCOnlyTimeZoneDatabase`, only works
92+
with UTC, which does not observe those issues. Once you bring
93+
a proper time zone database, the functions in this module will
94+
query the database and return the relevant information. For
95+
example, look at how `DateTime.new/4` returns different results
96+
based on the scenarios described in this section.
4497
"""
4598

4699
@enforce_keys [:year, :month, :day, :hour, :minute, :second] ++

0 commit comments

Comments
 (0)