Skip to content

Instantly share code, notes, and snippets.

@mattdennewitz
Created November 14, 2012 17:14
Show Gist options
  • Select an option

  • Save mattdennewitz/4073404 to your computer and use it in GitHub Desktop.

Select an option

Save mattdennewitz/4073404 to your computer and use it in GitHub Desktop.
Formatted TZ offset by name
import datetime
import dateutil.tz
import pytz
def get_formatted_tz(zone):
"""Returns +/-HH:MM formatted time zone offset.
``zone`` is a time zone like 'America/Chicago'.
"""
localtz = pytz.timezone(zone)
tz_offset = localtz.utcoffset(datetime.datetime.now())
tz_hours = str((tz_offset.days * 86400 + tz_offset.seconds) / 3600)
return tz_hours[0] + tz_hours[1:].rjust(2, '0') + ':00'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment