If I convert a UTC datetime to swedish format, summertime is included (CEST). However, while creating a datetime with sweden as the timezone, it gets CET instead of CEST. Why is this?
>>> # Modified for readability
>>> import pytz
>>> import datetime
>>> sweden = pytz.timezone('Europe/Stockholm')
>>>
>>> datetime.datetime(2010, 4, 20, 16, 20, tzinfo=pytz.utc).astimezone(sweden)
datetime(2010, 4, 20, 18, 20, tzinfo=<... 'Europe/Stockholm' CEST+2:00:00 DST>)
>>>
>>> datetime.datetime(2010, 4, 20, 18, 20, tzinfo=sweden)
datetime(2010, 4, 20, 18, 20, tzinfo=<... 'Europe/Stockholm' CET+1:00:00 STD>)
>>>
The
swedenobject specifies the CET time zone by default but contains enough information to know when CEST starts and stop.In the first example, you create a
datetimeobject and convert it to local time. Theswedenobject knows that the UTC time you passed occurs during daylight savings time and can convert it appropriately.In the second example, the
datetimeconstructor always interprets your input as not-daylight-savings-time and returns an appropriate object.If
datetimetreated your input as wall-clock time and chose the appropriate daylight-savings setting for you, there would be an ambiguity during the time of year when clocks are set back. On a wall-clock the same hour occurs twice. Hence,datetimeforces you to specify which timezone you’re using when you create thedatetimeobject.