本文整理匯總了Python中django.utils.timezone.get_fixed_timezone方法的典型用法代碼示例。如果您正苦於以下問題:Python timezone.get_fixed_timezone方法的具體用法?Python timezone.get_fixed_timezone怎麽用?Python timezone.get_fixed_timezone使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.utils.timezone
的用法示例。
在下文中一共展示了timezone.get_fixed_timezone方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: parse_datetime
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def parse_datetime(value):
"""Parse a string and return a datetime.datetime.
This function supports time zone offsets. When the input contains one,
the output uses a timezone with a fixed offset from UTC.
Raise ValueError if the input is well formatted but not a valid datetime.
Return None if the input isn't well formatted.
"""
match = datetime_re.match(value)
if match:
kw = match.groupdict()
kw['microsecond'] = kw['microsecond'] and kw['microsecond'].ljust(6, '0')
tzinfo = kw.pop('tzinfo')
if tzinfo == 'Z':
tzinfo = utc
elif tzinfo is not None:
offset_mins = int(tzinfo[-2:]) if len(tzinfo) > 3 else 0
offset = 60 * int(tzinfo[1:3]) + offset_mins
if tzinfo[0] == '-':
offset = -offset
tzinfo = get_fixed_timezone(offset)
kw = {k: int(v) for k, v in kw.items() if v is not None}
kw['tzinfo'] = tzinfo
return datetime.datetime(**kw)
示例2: test_timezones
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def test_timezones(self):
# Saving and updating with timezone-aware datetime Python objects.
# Regression test for #10443.
# The idea is that all these creations and saving should work without
# crashing. It's not rocket science.
dt1 = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=get_fixed_timezone(600))
dt2 = datetime.datetime(2008, 8, 31, 17, 20, tzinfo=get_fixed_timezone(600))
obj = Article.objects.create(
headline="A headline", pub_date=dt1, article_text="foo"
)
obj.pub_date = dt2
obj.save()
self.assertEqual(
Article.objects.filter(headline="A headline").update(pub_date=dt1),
1
)
示例3: test_serialize_datetime
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def test_serialize_datetime(self):
self.assertSerializedEqual(datetime.datetime.utcnow())
self.assertSerializedEqual(datetime.datetime.utcnow)
self.assertSerializedEqual(datetime.datetime.today())
self.assertSerializedEqual(datetime.datetime.today)
self.assertSerializedEqual(datetime.date.today())
self.assertSerializedEqual(datetime.date.today)
self.assertSerializedEqual(datetime.datetime.now().time())
self.assertSerializedEqual(datetime.datetime(2014, 1, 1, 1, 1, tzinfo=get_default_timezone()))
self.assertSerializedEqual(datetime.datetime(2013, 12, 31, 22, 1, tzinfo=get_fixed_timezone(180)))
self.assertSerializedResultEqual(
datetime.datetime(2014, 1, 1, 1, 1),
("datetime.datetime(2014, 1, 1, 1, 1)", {'import datetime'})
)
self.assertSerializedResultEqual(
datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc),
(
"datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc)",
{'import datetime', 'from django.utils.timezone import utc'},
)
)
示例4: test_parse_datetime
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def test_parse_datetime(self):
valid_inputs = (
('2012-04-23T09:15:00', datetime(2012, 4, 23, 9, 15)),
('2012-4-9 4:8:16', datetime(2012, 4, 9, 4, 8, 16)),
('2012-04-23T09:15:00Z', datetime(2012, 4, 23, 9, 15, 0, 0, get_fixed_timezone(0))),
('2012-4-9 4:8:16-0320', datetime(2012, 4, 9, 4, 8, 16, 0, get_fixed_timezone(-200))),
('2012-04-23T10:20:30.400+02:30', datetime(2012, 4, 23, 10, 20, 30, 400000, get_fixed_timezone(150))),
('2012-04-23T10:20:30.400+02', datetime(2012, 4, 23, 10, 20, 30, 400000, get_fixed_timezone(120))),
('2012-04-23T10:20:30.400-02', datetime(2012, 4, 23, 10, 20, 30, 400000, get_fixed_timezone(-120))),
)
for source, expected in valid_inputs:
with self.subTest(source=source):
self.assertEqual(parse_datetime(source), expected)
# Invalid inputs
self.assertIsNone(parse_datetime('20120423091500'))
with self.assertRaises(ValueError):
parse_datetime('2012-04-56T09:15:90')
示例5: parse_datetime
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def parse_datetime(value):
"""Parses a string and return a datetime.datetime.
This function supports time zone offsets. When the input contains one,
the output uses a timezone with a fixed offset from UTC.
Raises ValueError if the input is well formatted but not a valid datetime.
Returns None if the input isn't well formatted.
"""
match = datetime_re.match(value)
if match:
kw = match.groupdict()
if kw['microsecond']:
kw['microsecond'] = kw['microsecond'].ljust(6, '0')
tzinfo = kw.pop('tzinfo')
if tzinfo == 'Z':
tzinfo = utc
elif tzinfo is not None:
offset_mins = int(tzinfo[-2:]) if len(tzinfo) > 3 else 0
offset = 60 * int(tzinfo[1:3]) + offset_mins
if tzinfo[0] == '-':
offset = -offset
tzinfo = get_fixed_timezone(offset)
kw = {k: int(v) for k, v in six.iteritems(kw) if v is not None}
kw['tzinfo'] = tzinfo
return datetime.datetime(**kw)
示例6: parse_datetime
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def parse_datetime(value):
"""Parse a string and return a datetime.datetime.
This function supports time zone offsets. When the input contains one,
the output uses a timezone with a fixed offset from UTC.
Raise ValueError if the input is well formatted but not a valid datetime.
Return None if the input isn't well formatted.
"""
match = datetime_re.match(value)
if match:
kw = match.groupdict()
if kw['microsecond']:
kw['microsecond'] = kw['microsecond'].ljust(6, '0')
tzinfo = kw.pop('tzinfo')
if tzinfo == 'Z':
tzinfo = utc
elif tzinfo is not None:
offset_mins = int(tzinfo[-2:]) if len(tzinfo) > 3 else 0
offset = 60 * int(tzinfo[1:3]) + offset_mins
if tzinfo[0] == '-':
offset = -offset
tzinfo = get_fixed_timezone(offset)
kw = {k: int(v) for k, v in kw.items() if v is not None}
kw['tzinfo'] = tzinfo
return datetime.datetime(**kw)
示例7: test_serialise_with_aware_datetime
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def test_serialise_with_aware_datetime(self):
"""
This tests that aware datetimes are converted to as UTC
"""
# make an aware datetime, consisting of WAGTAIL_05_RELEASE_DATETIME
# in a timezone 1hr west of UTC
one_hour_west = timezone.get_fixed_timezone(-60)
local_time = timezone.make_aware(self.WAGTAIL_05_RELEASE_DATETIME, one_hour_west)
log = Log(time=local_time, data="Wagtail 0.5 released")
log_json = json.loads(log.to_json())
# Now check that the time is stored correctly with the timezone information at the end
self.assertEqual(log_json['time'], '2014-08-01T12:01:42Z')
示例8: _format_time_ago
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def _format_time_ago(dt, now=None, full=False, ago_in=False, two_days=False):
if not isinstance(dt, timedelta):
if now is None:
now = timezone.localtime(
timezone=timezone.get_fixed_timezone(-int(t.timezone / 60))
)
original_dt = dt
dt = _parse(dt)
now = _parse(now)
if dt is None:
raise ValueError(
"The parameter `dt` should be datetime timedelta, or datetime formatted string."
)
if now is None:
raise ValueError(
"the parameter `now` should be datetime, or datetime formatted string."
)
result = relativedelta.relativedelta(dt, now)
flag, result = _format_relativedelta(result, full, two_days, original_dt)
if ago_in and flag is not None:
result = "in {}".format(result) if flag else "{} ago".format(result)
return result
示例9: utc_tzinfo_factory
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def utc_tzinfo_factory(offset):
if offset != 0:
return get_fixed_timezone(offset)
return utc
示例10: test_naturalday_tz
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def test_naturalday_tz(self):
today = datetime.date.today()
tz_one = get_fixed_timezone(-720)
tz_two = get_fixed_timezone(720)
# Can be today or yesterday
date_one = datetime.datetime(today.year, today.month, today.day, tzinfo=tz_one)
naturalday_one = humanize.naturalday(date_one)
# Can be today or tomorrow
date_two = datetime.datetime(today.year, today.month, today.day, tzinfo=tz_two)
naturalday_two = humanize.naturalday(date_two)
# As 24h of difference they will never be the same
self.assertNotEqual(naturalday_one, naturalday_two)
示例11: test_make_aware_no_tz
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def test_make_aware_no_tz(self):
self.assertEqual(
timezone.make_aware(datetime.datetime(2011, 9, 1, 13, 20, 30)),
datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=timezone.get_fixed_timezone(-300))
)
示例12: test_fixedoffset_timedelta
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def test_fixedoffset_timedelta(self):
delta = datetime.timedelta(hours=1)
self.assertEqual(timezone.get_fixed_timezone(delta).utcoffset(''), delta)
示例13: test_fixedoffset_negative_timedelta
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def test_fixedoffset_negative_timedelta(self):
delta = datetime.timedelta(hours=-2)
self.assertEqual(timezone.get_fixed_timezone(delta).utcoffset(''), delta)
示例14: test_rfc2822_date_with_timezone
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def test_rfc2822_date_with_timezone(self):
"""
rfc2822_date() correctly formats datetime objects with tzinfo.
"""
self.assertEqual(
feedgenerator.rfc2822_date(datetime.datetime(2008, 11, 14, 13, 37, 0, tzinfo=get_fixed_timezone(60))),
"Fri, 14 Nov 2008 13:37:00 +0100"
)
示例15: test_rfc3339_date_with_timezone
# 需要導入模塊: from django.utils import timezone [as 別名]
# 或者: from django.utils.timezone import get_fixed_timezone [as 別名]
def test_rfc3339_date_with_timezone(self):
"""
rfc3339_date() correctly formats datetime objects with tzinfo.
"""
self.assertEqual(
feedgenerator.rfc3339_date(datetime.datetime(2008, 11, 14, 13, 37, 0, tzinfo=get_fixed_timezone(120))),
"2008-11-14T13:37:00+02:00"
)