Python: Calculate DST offset for a given date

Every year some day in march, our government steals a whole hour from us, holds it ransom over the summer and gives it back only in october – only to steal it again next year. I’m talking about daylight savings time of course, which we switched to just this night. And while it is annoying and useless enough on its own, it means a world of pain in Python.

Problem

As it turned out this DST-business caused some unwanted behavior in a little project I’m working on. I had to figure out how to reliable compute the offset that is added to the correct time during DST on any given date. This is what I came up with.

Solution

First you will need the Pytz package. It’s not part of the standard library for one reason or another, but it comes preinstalled in a lot of Linux distributions. easy_install will happily install it for you if it isn’t, even on Windows.

To get the DST offset, there are really only three and a half steps to take:

 

tz = pytz.timezone('Europe/Berlin')
date = datetime.datetime(2012,3,25,12)
dstoffset = tz.localize(date).dst()

 

This will give you a nice timedelta(0,3600) object which tells you that on the 25th of march 2012 at noon, the DST offsets the correct time by 3600 seconds. Likewise on the 24th, the offset is 0 seconds.

Explanation

First you need to create a tzone object for the desired timezone. For me, this is Europe/Berlin.
tz = pytz.timezone('Europe/Berlin')

Then you create a datetime object for the given date and time you want to compute the DST for.
date = datetime.datetime(2012,3,25,12)

The trick is in the localize function of your tzone object. The datetime object you created does not contain any timezone information at all. localize assumes that the timezone information is simply missing and magically creates a new datetime object with the same date and time and correct timezone. Luckily for us, this includes DST. We then just need to call the dst method, which gives you the offset as a timedelta object.
dstoffset = tz.localize(date).dst()

Sent from my iPad

(19:24:36) dot: hurrawba
(19:24:41) dot: nciqef c sind C PVC
(19:24:49) dot: Janisch ISDN Hindi fusionier BSC hdc heftig ifvbhfiv

Krasses Design und so

Kreava 2 nennt sich das hier wohl. Ganz trendy in grau und weiß. Voll von buzzwordigen Sachen, wie CSS3 und Media Queries für schlaue Telefone und Tabletts und Liquid Layouts. Runde Ecken und Schatten natürlich inklusive, ohne die ist man gar nicht Web 13.5. Und natürlich optimiert und schnell und so.

The Perils of Automatische Übersetzung

Wie einige vielleicht wissen lässt Microsoft seine in englischer Sprache verfassten Support-Artikel automatisch in andere Sprachen übersetzen. Dass dabei mitunter merkwürdige Sprachkonstrukte entstehen können, die dem Hilfesuchenden noch weniger verständlich sind, als der Originaltext, dürfte klar sein. Es geht immerhin um ein System von Microsoft – das sind die, die einem mit einem Update für das Updatesystem, dass es ermöglichen soll irgendwann mal ein Update einzuspielen das Telefon kaputtmachen.

Aber eine richtige Glanzleistung, die bei der Übersetzung entstanden ist, findet sich in diesem Artikel. Dort wird beschrieben, wie man eine Batch-Datei anlegt… die dann den Rechner wieder heile macht oder so in der Art. Der Inhalt ist folgender:

net Stop wuauserv
CD-%systemroot%\SoftwareDistribution
Ren Download.old herunterladen
Net start Wuauserv

Das sieht ein wenig merkwürdig aus. Alles englisch, plötzlich ein “herunterladen”, Groß- und Kleinschreibung und was ist “CD-%systemroot%\SoftwareDistribution” für ein Befehl? Die Zeilen aus dem englischen Original bieten Klarheit:

net stop wuauserv
cd %systemroot%\SoftwareDistribution
ren Download Download.old
net start wuauserv

Diese Clowns haben den Code mit übersetzt. Aus “Download” wurde “herunterladen” und weil es an der falschen Stelle steht passiert dort gar nichts. Wobei das eigentlich auch Wurst ist, denn mit “CD-irgendwas” kommt man auch nirgends an.

Ist das jetzt ein Einzelfall oder ist sämtlicher Code in diesen Artikeln potentiell nicht zu gebrauchen? Hat jemand nur vergessen, den Abschnitt mit “nicht übersetzen” zu markieren – oder ist das System dort womöglich dazu gar nicht in der Lage?!