mirror of https://github.com/django/django.git
Removed django.utils.tzinfo per deprecation timeline; refs #17262.
This commit is contained in:
parent
25264d4e2a
commit
1b0365ad34
|
@ -1,117 +0,0 @@
|
|||
"Implementation of tzinfo classes for use with datetime.datetime."
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from datetime import timedelta, tzinfo
|
||||
import time
|
||||
import warnings
|
||||
|
||||
from django.utils.deprecation import RemovedInDjango19Warning
|
||||
from django.utils.encoding import force_str, force_text, DEFAULT_LOCALE_ENCODING
|
||||
|
||||
warnings.warn(
|
||||
"django.utils.tzinfo will be removed in Django 1.9. "
|
||||
"Use django.utils.timezone instead.",
|
||||
RemovedInDjango19Warning, stacklevel=2)
|
||||
|
||||
|
||||
# Python's doc say: "A tzinfo subclass must have an __init__() method that can
|
||||
# be called with no arguments". FixedOffset and LocalTimezone don't honor this
|
||||
# requirement. Defining __getinitargs__ is sufficient to fix copy/deepcopy as
|
||||
# well as pickling/unpickling.
|
||||
|
||||
class FixedOffset(tzinfo):
|
||||
"Fixed offset in minutes east from UTC."
|
||||
def __init__(self, offset):
|
||||
warnings.warn(
|
||||
"django.utils.tzinfo.FixedOffset will be removed in Django 1.9. "
|
||||
"Use django.utils.timezone.get_fixed_timezone instead.",
|
||||
RemovedInDjango19Warning)
|
||||
if isinstance(offset, timedelta):
|
||||
self.__offset = offset
|
||||
offset = self.__offset.seconds // 60
|
||||
else:
|
||||
self.__offset = timedelta(minutes=offset)
|
||||
|
||||
sign = '-' if offset < 0 else '+'
|
||||
self.__name = "%s%02d%02d" % (sign, abs(offset) / 60., abs(offset) % 60)
|
||||
|
||||
def __repr__(self):
|
||||
return self.__name
|
||||
|
||||
def __getinitargs__(self):
|
||||
return self.__offset,
|
||||
|
||||
def utcoffset(self, dt):
|
||||
return self.__offset
|
||||
|
||||
def tzname(self, dt):
|
||||
return self.__name
|
||||
|
||||
def dst(self, dt):
|
||||
return timedelta(0)
|
||||
|
||||
|
||||
# This implementation is used for display purposes. It uses an approximation
|
||||
# for DST computations on dates >= 2038.
|
||||
|
||||
# A similar implementation exists in django.utils.timezone. It's used for
|
||||
# timezone support (when USE_TZ = True) and focuses on correctness.
|
||||
|
||||
class LocalTimezone(tzinfo):
|
||||
"Proxy timezone information from time module."
|
||||
def __init__(self, dt):
|
||||
warnings.warn(
|
||||
"django.utils.tzinfo.LocalTimezone will be removed in Django 1.9. "
|
||||
"Use django.utils.timezone.get_default_timezone instead.",
|
||||
RemovedInDjango19Warning)
|
||||
tzinfo.__init__(self)
|
||||
self.__dt = dt
|
||||
self._tzname = self.tzname(dt)
|
||||
|
||||
def __repr__(self):
|
||||
return force_str(self._tzname)
|
||||
|
||||
def __getinitargs__(self):
|
||||
return self.__dt,
|
||||
|
||||
def utcoffset(self, dt):
|
||||
if self._isdst(dt):
|
||||
return timedelta(seconds=-time.altzone)
|
||||
else:
|
||||
return timedelta(seconds=-time.timezone)
|
||||
|
||||
def dst(self, dt):
|
||||
if self._isdst(dt):
|
||||
return timedelta(seconds=-time.altzone) - timedelta(seconds=-time.timezone)
|
||||
else:
|
||||
return timedelta(0)
|
||||
|
||||
def tzname(self, dt):
|
||||
is_dst = False if dt is None else self._isdst(dt)
|
||||
try:
|
||||
return force_text(time.tzname[is_dst], DEFAULT_LOCALE_ENCODING)
|
||||
except UnicodeDecodeError:
|
||||
return None
|
||||
|
||||
def _isdst(self, dt):
|
||||
tt = (dt.year, dt.month, dt.day,
|
||||
dt.hour, dt.minute, dt.second,
|
||||
dt.weekday(), 0, 0)
|
||||
try:
|
||||
stamp = time.mktime(tt)
|
||||
except (OverflowError, ValueError):
|
||||
# 32 bit systems can't handle dates after Jan 2038, and certain
|
||||
# systems can't handle dates before ~1901-12-01:
|
||||
#
|
||||
# >>> time.mktime((1900, 1, 13, 0, 0, 0, 0, 0, 0))
|
||||
# OverflowError: mktime argument out of range
|
||||
# >>> time.mktime((1850, 1, 13, 0, 0, 0, 0, 0, 0))
|
||||
# ValueError: year out of range
|
||||
#
|
||||
# In this case, we fake the date, because we only care about the
|
||||
# DST flag.
|
||||
tt = (2037,) + tt[1:]
|
||||
stamp = time.mktime(tt)
|
||||
tt = time.localtime(stamp)
|
||||
return tt.tm_isdst > 0
|
|
@ -1121,26 +1121,3 @@ For a complete discussion on the usage of the following see the
|
|||
|
||||
Session key under which the active language for the current session is
|
||||
stored.
|
||||
|
||||
``django.utils.tzinfo``
|
||||
=======================
|
||||
|
||||
.. deprecated:: 1.7
|
||||
Use :mod:`~django.utils.timezone` instead.
|
||||
|
||||
.. module:: django.utils.tzinfo
|
||||
:synopsis: Implementation of ``tzinfo`` classes for use with ``datetime.datetime``.
|
||||
|
||||
.. class:: FixedOffset
|
||||
|
||||
Fixed offset in minutes east from UTC.
|
||||
|
||||
.. deprecated:: 1.7
|
||||
Use :func:`~django.utils.timezone.get_fixed_timezone` instead.
|
||||
|
||||
.. class:: LocalTimezone
|
||||
|
||||
Proxy timezone information from time module.
|
||||
|
||||
.. deprecated:: 1.7
|
||||
Use :func:`~django.utils.timezone.get_default_timezone` instead.
|
||||
|
|
|
@ -1,92 +0,0 @@
|
|||
import copy
|
||||
import datetime
|
||||
import os
|
||||
import pickle
|
||||
import time
|
||||
import unittest
|
||||
import warnings
|
||||
|
||||
from django.test import ignore_warnings
|
||||
from django.utils.deprecation import RemovedInDjango19Warning
|
||||
|
||||
|
||||
# Swallow the import-time warning to test the deprecated implementation.
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings("ignore", category=RemovedInDjango19Warning)
|
||||
from django.utils.tzinfo import FixedOffset, LocalTimezone
|
||||
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango19Warning)
|
||||
class TzinfoTests(unittest.TestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super(TzinfoTests, cls).setUpClass()
|
||||
cls.old_TZ = os.environ.get('TZ')
|
||||
os.environ['TZ'] = 'US/Eastern'
|
||||
|
||||
try:
|
||||
# Check if a timezone has been set
|
||||
time.tzset()
|
||||
cls.tz_tests = True
|
||||
except AttributeError:
|
||||
# No timezone available. Don't run the tests that require a TZ
|
||||
cls.tz_tests = False
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
if cls.old_TZ is None:
|
||||
del os.environ['TZ']
|
||||
else:
|
||||
os.environ['TZ'] = cls.old_TZ
|
||||
|
||||
# Cleanup - force re-evaluation of TZ environment variable.
|
||||
if cls.tz_tests:
|
||||
time.tzset()
|
||||
super(TzinfoTests, cls).tearDownClass()
|
||||
|
||||
def test_fixedoffset(self):
|
||||
self.assertEqual(repr(FixedOffset(0)), '+0000')
|
||||
self.assertEqual(repr(FixedOffset(60)), '+0100')
|
||||
self.assertEqual(repr(FixedOffset(-60)), '-0100')
|
||||
self.assertEqual(repr(FixedOffset(280)), '+0440')
|
||||
self.assertEqual(repr(FixedOffset(-280)), '-0440')
|
||||
self.assertEqual(repr(FixedOffset(-78.4)), '-0118')
|
||||
self.assertEqual(repr(FixedOffset(78.4)), '+0118')
|
||||
self.assertEqual(repr(FixedOffset(-5.5 * 60)), '-0530')
|
||||
self.assertEqual(repr(FixedOffset(5.5 * 60)), '+0530')
|
||||
self.assertEqual(repr(FixedOffset(-.5 * 60)), '-0030')
|
||||
self.assertEqual(repr(FixedOffset(.5 * 60)), '+0030')
|
||||
|
||||
def test_16899(self):
|
||||
if not self.tz_tests:
|
||||
return
|
||||
ts = 1289106000
|
||||
# Midnight at the end of DST in US/Eastern: 2010-11-07T05:00:00Z
|
||||
dt = datetime.datetime.utcfromtimestamp(ts)
|
||||
# US/Eastern -- we force its representation to "EST"
|
||||
tz = LocalTimezone(dt + datetime.timedelta(days=1))
|
||||
self.assertEqual(
|
||||
repr(datetime.datetime.fromtimestamp(ts - 3600, tz)),
|
||||
'datetime.datetime(2010, 11, 7, 0, 0, tzinfo=EST)')
|
||||
self.assertEqual(
|
||||
repr(datetime.datetime.fromtimestamp(ts, tz)),
|
||||
'datetime.datetime(2010, 11, 7, 1, 0, tzinfo=EST)')
|
||||
self.assertEqual(
|
||||
repr(datetime.datetime.fromtimestamp(ts + 3600, tz)),
|
||||
'datetime.datetime(2010, 11, 7, 1, 0, tzinfo=EST)')
|
||||
|
||||
def test_copy(self):
|
||||
now = datetime.datetime.now()
|
||||
self.assertIsInstance(copy.copy(FixedOffset(90)), FixedOffset)
|
||||
self.assertIsInstance(copy.copy(LocalTimezone(now)), LocalTimezone)
|
||||
|
||||
def test_deepcopy(self):
|
||||
now = datetime.datetime.now()
|
||||
self.assertIsInstance(copy.deepcopy(FixedOffset(90)), FixedOffset)
|
||||
self.assertIsInstance(copy.deepcopy(LocalTimezone(now)), LocalTimezone)
|
||||
|
||||
def test_pickling_unpickling(self):
|
||||
now = datetime.datetime.now()
|
||||
self.assertIsInstance(pickle.loads(pickle.dumps(FixedOffset(90))), FixedOffset)
|
||||
self.assertIsInstance(pickle.loads(pickle.dumps(LocalTimezone(now))), LocalTimezone)
|
Loading…
Reference in New Issue