Skip to content

Instantly share code, notes, and snippets.

@michaelkarrer81
Last active December 13, 2017 08:53
Show Gist options
  • Save michaelkarrer81/d020b7400775d5e7a71ef4053cfb4ae4 to your computer and use it in GitHub Desktop.
Save michaelkarrer81/d020b7400775d5e7a71ef4053cfb4ae4 to your computer and use it in GitHub Desktop.
import datetime
import pytz
# http://pytz.sourceforge.net/
# HINT: Do not use tzinfo on datetimes or pelace on datetimes because the pytz lib will randomly choose
# a daylight saving time offset. For vienne this would be in most cases 5 minutes off :(
# Instead us .localize() and .astimezone()
# Create timezone object with Europe/Vienna as the timzone (the timzone that your naive datetime is really ment for)
vtz = pytz.timezone("Europe/Vienna")
# Just a naive datetime with no furhter timzone or daylight saving time info
yearend_naive = datetime.datetime(r.meldungs_jahr, 12, 31, 23, 59, 59, 999)
# Now we convert the datetime object to a timezone object with the correct timezone and daylight saving time information
yearend_vtz = vtz.localize(yearend_naive, is_dst=True)
# Create an other timzone object with the desired timzone - in this example UTC e.g.: to store it in odoo fields ;)
yearend_utc = yearend_vtz.astimezone(pytz.UTC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment