How I Found Myself Learning About Time Changes in the Western Soviet Union
Tags: calendarapi, python, api, pytz
Published: 2018-09-27 18:14:43

---
title: How I Found Myself Learning About Time Changes in the Western Soviet Union
published: True 
tags: calendar Api, python, API,PYTZ 
canonical_url: https://www.nylas.com/blog/how-i-found-myself-learning-about-time-changes-in-the-western-soviet-union
---

[![Nylas_Blog_TimeChange_V1](https://www.nylas.com/hubfs/Nylas_Blog_TimeChange_V1.png?t=1537995683990) ](https://www.nylas.com/blog/how-i-found-myself-learning-about-time-changes-in-the-western-soviet-union)

Here at [Nylas](https://www.nylas.com/), we’re building an API to help [developers build apps](https://dev.to/nylas/nylas-api-contacts-crud-now-supports-icloud-2ch3) on top of email, calendar & contacts. As it turns out, doing so teaches you a lot about time changes in the Western Soviet Union. I joined Nylas just a few months back, and learned early on that one important aspect of setting up an event in a calendar is understanding what timezone that event is in, relative both to the creator of the event, as well as to any other participants. Naturally, this involves lots of timezone math. Most of this is done by the [pytz](https://pypi.org/project/pytz/) library, and this works well enough for us. Why do yourself what someone else has already done, right?

<p>Enter Microsoft. A lot of companies use Microsoft Exchange for their email and work calendar. Historically, Exchange has used a protocol called Exchange Active Sync (EAS). As with any Microsoft protocol, their attitude towards their calendar API seems to have been, “let’s provide all the raw data and make the user do something useful with it.” What they provide is:</p>
<ul>
<li>The offset from UTC in minutes</li>
<li>The date during that calendar year when the timezone transitions to Daylight Saving Time</li>
<li>The date during that calendar year when the timezone transitions to Standard Time</li>
<li>The offset during Daylight Saving Time</li>
</ul>
<p>…From which you are to derive a friendly name to show the user. It’s like if I asked you for the time, and you responded by telling me about Earth’s prograde rotation and tilt, the eccentricity of Earth’s orbit, the tidal effects of the moon on the rotational velocity, and how to adjust for leap seconds. I mean, sure, but can’t you just tell me it’s 5:30?*</p>
<p>Pytz has a fairly sparse public API (where “public” is defined using the Python idiom of classes and methods not prefixed by an underscore). You, the programmer, instantiate a timezone object from a friendly string like <code>US/Eastern</code>, and you can use the <code>localize</code>, <code>normalize</code>, and <code>utcoffset</code> methods to work with it. Pytz takes care of keeping track of DST transitions and offsets. From a normal user’s perspective, this makes sense. Pytz’s whole value proposition is to abstract away the pain that is dealing with timezones and the bureaucracy and politics about changing offsets. But ok, Redmond, game on.</p>
<p>We decided to use pytz internals to build a lookup table to store these. We need to use the information we get from Microsoft as the key, and the timezone name as the value. For each timezone, pytz stores an attribute <code>_utc_transition_times</code></p>
<pre><code lang="python">&gt;&gt;&gt; tz = pytz.timezone('US/Eastern')
&gt;&gt;&gt; pprint(tz._utc_transition_times[-5:])
[datetime.datetime(2035, 11, 4, 6, 0),
 datetime.datetime(2036, 3, 9, 7, 0),
 datetime.datetime(2036, 11, 2, 6, 0),
 datetime.datetime(2037, 3, 8, 7, 0),
 datetime.datetime(2037, 11, 1, 6, 0)]
</code></pre>
<p>Oh cool, so we can use this to build the lookup table., right? We’ll take the first transition of a given year as the transition to DST, the second transition of a given year as the transition to ST, pick a time outside of DST and look up the UTC offset, and store a tuple of this as the key for our table!</p>
<pre><code lang="python">&gt;&gt;&gt; daylight_time = tz._utc_transition_times[n]
&gt;&gt;&gt; standard_time = tz._utc_transition_times[n+1]
&gt;&gt;&gt; utc_offset = tz.utc_offset(date_not_in_dst)
&gt;&gt;&gt; timezone_table[(daylight_time, standard_time, utc_offset)] = timezone_name
</code></pre>
<p>This worked well, until we got a bug report that calendar events in Auckland were losing timezone information. Looking into this, it appeared that our lookup table had the reverse <code>daylight_time</code> and <code>standard_time </code>entries. How could this be? Well, it turns out that we made a very bad assumption when we assumed that the first transition of a year is a transition to DST. Because the Southern Hemisphere’s summer is from December to March, they go into DST around November and exit around April of the following year. Maybe the <a href="https://youtu.be/OH1bZ0F3zVU" target="_blank">Cartographers for Social Equality</a> were right! We hadn’t realized this difference between the Northern and Southern Hemispheres, and were flawed because of it. Back to the drawing board.</p>
<h2 style="text-align: left;">Digging into Pytz</h2>
<p>Ok, so a little more digging into Pytz is warranted. How exactly does this library work? Turns out, it’s really fascinating. Bundled with the codebase is the Olson database, otherwise known as the <em>Zoneinfo</em> or <em>tz</em> database. The whole database is written Unix-style, with plaintext files describing the timezone names and historical offsets for each geographic region. But even more prominent than the actual data are all the historical anecdotes and context scattered throughout as comments. Some of these are big, some small, and some seemingly only there as inside jokes between the maintainers.</p>
<p>For instance, on Alaska:</p>
<p>From Paul Eggert(2017-06-15):</p>
<p>...Many of Alaska's inhabitants were unaware of the US acquisition of Alaska, much less of any calendar or time change. However, the Russian-influenced part of Alaska did observe Russian time, and it is more accurate to model this than to ignore it. The database format requires an exact transition time; use the Russian salute as a somewhat-arbitrary time for the formal transfer of control for all of Alaska. Sitka's UTC offset is -9:01:13; adjust its 15:30 to the local times of other Alaskan locations so that they change simultaneously.</p>
<p>From Paul Eggert (2014-07-18):</p>
<p>One opinion of the early-1980s turmoil in Alaska over time zones and daylight saving time appeared as graffiti on a Juneau airport wall:</p>
<p>"Welcome to Juneau. Please turn your watch back to the 19th century."</p>
<p>See: Turner W. Alaska's four time zones now two. NY Times 1983-11-01 <a href="http://www.nytimes.com/1983/11/01/us/alaska-s-four-time-zones-now-two.html" target="_blank">http://www.nytimes.com/1983/11/01/us/alaska-s-four-time-zones-now-two.html</a></p>
<p>And on Michigan:</p>
<p>From Paul Eggert (1999-03-31):</p>
<p>Shanks writes that Michigan started using standard time on 1885-09-18, but Howse writes (pp 124-125, referring to Popular Astronomy, 1901-01) that Detroit kept local time until 1900 when the City Council decreed that clocks should be put back twenty-eight minutes to Central Standard Time. Half the city obeyed, half refused. After considerable debate, the decision was rescinded and the city reverted to Sun time. A derisive offer to erect a sundial in front of the city hall was referred to the Committee on Sewers. Then, in 1905, Central time was adopted by city vote.</p>
<p>This story is too entertaining to be false, so go with Howse over Shanks.</p>
<p>Timezones are political—who knew? Jokes aside, I wanted to see how pytz actually <em>deals</em> with time. This happens in the <code>localize</code> method on the timezone class. <code>localize</code> attempts to cover all the bases for weird timezone transitions. To do this, it bisects <code>_utc_transition_times</code> to find where the given time falls in the list of transitions. Then, it retrieves information about the transition from <code>_transition_info</code>.</p>
<pre><code lang="python">&gt;&gt;&gt; tz = pytz.timezone('US/Eastern')
&gt;&gt;&gt; pprint(tz._utc_transition_times[:5])
[datetime.datetime(1, 1, 1, 0, 0),
 datetime.datetime(1901, 12, 13, 20, 45, 52)
 datetime.datetime(1918, 3, 31, 7, 0),
 datetime.datetime(1918, 10, 27, 6, 0),
 datetime.datetime(1919, 3, 30, 7, 0)]
&gt;&gt;&gt; pprint(tz._transition_info[:5])
[(datetime.timedelta(-1, 68640), datetime.timedelta(0), 'LMT'),
 (datetime.timedelta(-1, 68400), datetime.timedelta(0), 'EST'),
 (datetime.timedelta(-1, 72000), datetime.timedelta(0, 3600), 'EDT'),
 (datetime.timedelta(-1, 68400), datetime.timedelta(0), 'EST'),
 (datetime.timedelta(-1, 72000), datetime.timedelta(0, 3600), 'EDT')]</code></pre>
<p>This tells a much better story. For each entry in <code>_utc_transition_times</code>, there is a corresponding entry in <code>_transition_info</code>. The DST offset in that entry tells us whether it is a transition to ST (if it’s zero) or DST (if it’s not). Further, the UTC offset is there to use directly, rather than requiring a reference time to test against.</p>
<p>So what are the edge cases here? Well, it turns out that when you use information about a time zone to identify a name, there are multiple possible entries. It may not surprise you entirely to learn that there are different names for the time zone representing both Moscow and Turkey. But if you’re not careful, it’s easy to overwrite “Europe/Moscow” or “Europe/Istanbul” (the common representations of this time zone) with “W-SU” (for Western Soviet Union).</p>
<p>Additionally, just looking at the <code>localize</code> method shows a whole host of edge cases. There is the seemingly obvious case of trying to deal with a time within a transition, e.g. is 1:30 AM on the day of a transition from DST to ST the time before or after the transition? But there are other cases as well, when countries wind their clocks back outside of a DST transition. This can cause problems for anyone trying to do real time conversions between timezones.</p>
<p>Luckily, that is the sort of thing we can rely on pytz for. While not perfect, pytz covers about as many edge cases as possible when dealing with one of the worst issues to program around. While we need to work with the finer points of timezone transitions to decipher Exchange events, we can usually just use the fine pytz API elsewhere.</p>
<p>If you’d like to learn more about the Nylas Calendar API, you can get a free API key by<a href="https://www.nylas.com/developers/" target="_blank"> registering here</a>.&nbsp;</p>
<hr>
<p>&nbsp;</p>
<p><em>*</em>I put this analogy in specifically so that I could glare at the first person to ask me what time zone it’s 5:30 in.</p>