I don't want to use the pytz
library as the project I am working on requires paperwork to introduce dependencies. If I can achieve this without a third party library I'll be happier.
I'm having trouble converting a date between CET and UTC when the date is in daylight savings. It's an hour different to what I expect:
>>> print from_cet_to_utc(year=2017, month=7, day=24, hour=10, minute=30)
2017-07-24T09:30Z
2017-07-24T08:30Z # expected
CET is an hour ahead of UTC and in summertime is 2 hours ahead. So I would expect 10:30 in central Europe in midsummer to actually to 8:30 UTC.
The function is:
from datetime import datetime, tzinfo, timedelta
def from_cet_to_utc(year, month, day, hour, minute):
cet = datetime(year, month, day, hour, minute, tzinfo=CET())
utc = cet.astimezone(tz=UTC())
return '{:%Y-%m-%d:T%H:%MZ}'.format(utc)
I use two timezone info objects:
class CET(tzinfo):
def utcoffset(self, dt):
return timedelta(hours=1)
def dst(self, dt):
return timedelta(hours=2)
class UTC(tzinfo):
def utcoffset(self, dt):
return timedelta(0)
def dst(self, dt):
return timedelta(0)