def timezone_offset(timezone):
"""Takes a tz name like 'US/Eastern' and returns 'US/Eastern - UTC-05:00'.
Intended to be used by the timezone notification page fragment. The pytz
package should gracefully handle DST so the above will render 'US/Eastern -
UTC-04:00' when DST is in effect.
"""
if timezone == 'UTC':
return 'UTC'
now = datetime.datetime.now()
try:
seconds = pytz.timezone(timezone).utcoffset(now).total_seconds()
except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError):
# If we're in the midst of a DST transition, add an hour and try again.
now = now + datetime.timedelta(hours=1)
seconds = pytz.timezone(timezone).utcoffset(now).total_seconds()
sign = '+'
if seconds < 0:
# The minus sign is added automatically!
sign = ''
hours, remainder = divmod(seconds, 60*60)
minutes, _ = divmod(remainder, 60)
offset = '%02d:%02d' % (hours, minutes)
display = '%s (UTC%s%s)' % (timezone, sign, offset)
return display
apptags.py 文件源码
python
阅读 19
收藏 0
点赞 0
评论 0
评论列表
文章目录