Created
November 14, 2012 17:14
-
-
Save mattdennewitz/4073404 to your computer and use it in GitHub Desktop.
Formatted TZ offset by name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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