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
python类NonExistentTimeError()的实例源码
test_timezones.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def test_localize_utc_conversion(self):
# Localizing to time zone should:
# 1) check for DST ambiguities
# 2) convert to UTC
rng = date_range('3/10/2012', '3/11/2012', freq='30T')
converted = rng.tz_localize(self.tzstr('US/Eastern'))
expected_naive = rng + offsets.Hour(5)
self.assert_numpy_array_equal(converted.asi8, expected_naive.asi8)
# DST ambiguity, this should fail
rng = date_range('3/11/2012', '3/12/2012', freq='30T')
# Is this really how it should fail??
self.assertRaises(NonExistentTimeError, rng.tz_localize,
self.tzstr('US/Eastern'))
test_timezones.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def test_tz_localize_dti(self):
dti = DatetimeIndex(start='1/1/2005', end='1/1/2005 0:00:30.256',
freq='L')
dti2 = dti.tz_localize(self.tzstr('US/Eastern'))
dti_utc = DatetimeIndex(start='1/1/2005 05:00',
end='1/1/2005 5:00:30.256', freq='L', tz='utc')
self.assert_numpy_array_equal(dti2.values, dti_utc.values)
dti3 = dti2.tz_convert(self.tzstr('US/Pacific'))
self.assert_numpy_array_equal(dti3.values, dti_utc.values)
dti = DatetimeIndex(start='11/6/2011 1:59', end='11/6/2011 2:00',
freq='L')
self.assertRaises(pytz.AmbiguousTimeError, dti.tz_localize,
self.tzstr('US/Eastern'))
dti = DatetimeIndex(start='3/13/2011 1:59', end='3/13/2011 2:00',
freq='L')
self.assertRaises(pytz.NonExistentTimeError, dti.tz_localize,
self.tzstr('US/Eastern'))
test_timezones.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def test_with_tz_ambiguous_times(self):
tz = self.tz('US/Eastern')
# March 13, 2011, spring forward, skip from 2 AM to 3 AM
dr = date_range(datetime(2011, 3, 13, 1, 30), periods=3,
freq=datetools.Hour())
self.assertRaises(pytz.NonExistentTimeError, dr.tz_localize, tz)
# after dst transition, it works
dr = date_range(datetime(2011, 3, 13, 3, 30), periods=3,
freq=datetools.Hour(), tz=tz)
# November 6, 2011, fall back, repeat 2 AM hour
dr = date_range(datetime(2011, 11, 6, 1, 30), periods=3,
freq=datetools.Hour())
self.assertRaises(pytz.AmbiguousTimeError, dr.tz_localize, tz)
# UTC is OK
dr = date_range(datetime(2011, 3, 13), periods=48,
freq=datetools.Minute(30), tz=pytz.utc)
def gen_tzinfos():
for zone in pytz.common_timezones:
try:
tzdate = pytz.timezone(zone).localize(dt.utcnow(), is_dst=None)
except pytz.NonExistentTimeError:
pass
else:
tzinfo = gettz(zone)
if tzinfo:
yield tzdate.tzname(), tzinfo
def _CombineDateAndTime(date, time, tzinfo):
"""Creates a datetime object from date and time objects.
This is similar to the datetime.combine method, but its timezone
calculations are designed to work with pytz.
Arguments:
date: a datetime.date object, in any timezone
time: a datetime.time object, in any timezone
tzinfo: a pytz timezone object, or None
Returns:
a datetime.datetime object, in the timezone 'tzinfo'
"""
naive_result = datetime.datetime(
date.year, date.month, date.day, time.hour, time.minute, time.second)
if tzinfo is None:
return naive_result
try:
return tzinfo.localize(naive_result, is_dst=None)
except AmbiguousTimeError:
return min(tzinfo.localize(naive_result, is_dst=True),
tzinfo.localize(naive_result, is_dst=False))
except NonExistentTimeError:
while True:
naive_result += datetime.timedelta(minutes=1)
try:
return tzinfo.localize(naive_result, is_dst=None)
except NonExistentTimeError:
pass
def _CombineDateAndTime(date, time, tzinfo):
"""Creates a datetime object from date and time objects.
This is similar to the datetime.combine method, but its timezone
calculations are designed to work with pytz.
Arguments:
date: a datetime.date object, in any timezone
time: a datetime.time object, in any timezone
tzinfo: a pytz timezone object, or None
Returns:
a datetime.datetime object, in the timezone 'tzinfo'
"""
naive_result = datetime.datetime(
date.year, date.month, date.day, time.hour, time.minute, time.second)
if tzinfo is None:
return naive_result
try:
return tzinfo.localize(naive_result, is_dst=None)
except AmbiguousTimeError:
return min(tzinfo.localize(naive_result, is_dst=True),
tzinfo.localize(naive_result, is_dst=False))
except NonExistentTimeError:
while True:
naive_result += datetime.timedelta(minutes=1)
try:
return tzinfo.localize(naive_result, is_dst=None)
except NonExistentTimeError:
pass
def parse_time(input_time):
"""Parse an Atom time stamp."""
parsed = None
try:
parsed = timezone.make_aware(timezone.datetime(*input_time[:-3]),
timezone.utc)
except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError):
added_hour = (timezone.datetime(*input_time[:-3]) +
datetime.timedelta(hours=1))
parsed = timezone.make_aware(added_hour, timezone.utc)
return parsed.astimezone(TZ)
def test_start_dst_invalid_occurrence(self):
"""Test that tzcron updates the offset when moving to DST"""
cron_expression = "30 1 * * * *"
timezone = pytz.timezone("Europe/London")
start = dt.datetime.strptime('2015-03-29T00:00:00',
"%Y-%m-%dT%H:%M:%S")
start = timezone.localize(start, is_dst=False)
testee = tzcron.Schedule(cron_expression, timezone, start)
self.assertRaises(pytz.NonExistentTimeError,
lambda: next(testee))
test_timezones.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def test_localize_utc_conversion_explicit(self):
# Localizing to time zone should:
# 1) check for DST ambiguities
# 2) convert to UTC
rng = date_range('3/10/2012', '3/11/2012', freq='30T')
converted = rng.tz_localize(self.tz('US/Eastern'))
expected_naive = rng + offsets.Hour(5)
self.assertTrue(np.array_equal(converted.asi8, expected_naive.asi8))
# DST ambiguity, this should fail
rng = date_range('3/11/2012', '3/12/2012', freq='30T')
# Is this really how it should fail??
self.assertRaises(NonExistentTimeError, rng.tz_localize,
self.tz('US/Eastern'))
def _CombineDateAndTime(date, time, tzinfo):
"""Creates a datetime object from date and time objects.
This is similar to the datetime.combine method, but its timezone
calculations are designed to work with pytz.
Arguments:
date: a datetime.date object, in any timezone
time: a datetime.time object, in any timezone
tzinfo: a pytz timezone object, or None
Returns:
a datetime.datetime object, in the timezone 'tzinfo'
"""
naive_result = datetime.datetime(
date.year, date.month, date.day, time.hour, time.minute, time.second)
if tzinfo is None:
return naive_result
try:
return tzinfo.localize(naive_result, is_dst=None)
except AmbiguousTimeError:
return min(tzinfo.localize(naive_result, is_dst=True),
tzinfo.localize(naive_result, is_dst=False))
except NonExistentTimeError:
while True:
naive_result += datetime.timedelta(minutes=1)
try:
return tzinfo.localize(naive_result, is_dst=None)
except NonExistentTimeError:
pass
def _CombineDateAndTime(date, time, tzinfo):
"""Creates a datetime object from date and time objects.
This is similar to the datetime.combine method, but its timezone
calculations are designed to work with pytz.
Arguments:
date: a datetime.date object, in any timezone
time: a datetime.time object, in any timezone
tzinfo: a pytz timezone object, or None
Returns:
a datetime.datetime object, in the timezone 'tzinfo'
"""
naive_result = datetime.datetime(
date.year, date.month, date.day, time.hour, time.minute, time.second)
if tzinfo is None:
return naive_result
try:
return tzinfo.localize(naive_result, is_dst=None)
except AmbiguousTimeError:
return min(tzinfo.localize(naive_result, is_dst=True),
tzinfo.localize(naive_result, is_dst=False))
except NonExistentTimeError:
while True:
naive_result += datetime.timedelta(minutes=1)
try:
return tzinfo.localize(naive_result, is_dst=None)
except NonExistentTimeError:
pass
def _CombineDateAndTime(date, time, tzinfo):
"""Creates a datetime object from date and time objects.
This is similar to the datetime.combine method, but its timezone
calculations are designed to work with pytz.
Arguments:
date: a datetime.date object, in any timezone
time: a datetime.time object, in any timezone
tzinfo: a pytz timezone object, or None
Returns:
a datetime.datetime object, in the timezone 'tzinfo'
"""
naive_result = datetime.datetime(
date.year, date.month, date.day, time.hour, time.minute, time.second)
if tzinfo is None:
return naive_result
try:
return tzinfo.localize(naive_result, is_dst=None)
except AmbiguousTimeError:
return min(tzinfo.localize(naive_result, is_dst=True),
tzinfo.localize(naive_result, is_dst=False))
except NonExistentTimeError:
while True:
naive_result += datetime.timedelta(minutes=1)
try:
return tzinfo.localize(naive_result, is_dst=None)
except NonExistentTimeError:
pass