1 "Implementation of tzinfo classes for use with datetime.datetime."
3 from __future__
import unicode_literals
6 from datetime
import timedelta
, tzinfo
8 from django
.utils
.encoding
import force_str
, force_text
, DEFAULT_LOCALE_ENCODING
10 # Python's doc say: "A tzinfo subclass must have an __init__() method that can
11 # be called with no arguments". FixedOffset and LocalTimezone don't honor this
12 # requirement. Defining __getinitargs__ is sufficient to fix copy/deepcopy as
13 # well as pickling/unpickling.
15 class FixedOffset(tzinfo
):
16 "Fixed offset in minutes east from UTC."
17 def __init__(self
, offset
):
18 if isinstance(offset
, timedelta
):
19 self
.__offset
= offset
20 offset
= self
.__offset
.seconds
// 60
22 self
.__offset
= timedelta(minutes
=offset
)
24 sign
= '-' if offset
< 0 else '+'
25 self
.__name
= "%s%02d%02d" % (sign
, abs(offset
) / 60., abs(offset
) % 60)
30 def __getinitargs__(self
):
33 def utcoffset(self
, dt
):
42 # This implementation is used for display purposes. It uses an approximation
43 # for DST computations on dates >= 2038.
45 # A similar implementation exists in django.utils.timezone. It's used for
46 # timezone support (when USE_TZ = True) and focuses on correctness.
48 class LocalTimezone(tzinfo
):
49 "Proxy timezone information from time module."
50 def __init__(self
, dt
):
53 self
._tzname
= self
.tzname(dt
)
56 return force_str(self
._tzname
)
58 def __getinitargs__(self
):
61 def utcoffset(self
, dt
):
63 return timedelta(seconds
=-time
.altzone
)
65 return timedelta(seconds
=-time
.timezone
)
69 return timedelta(seconds
=-time
.altzone
) - timedelta(seconds
=-time
.timezone
)
75 return force_text(time
.tzname
[self
._isdst
(dt
)],
76 DEFAULT_LOCALE_ENCODING
)
77 except UnicodeDecodeError:
81 tt
= (dt
.year
, dt
.month
, dt
.day
,
82 dt
.hour
, dt
.minute
, dt
.second
,
85 stamp
= time
.mktime(tt
)
86 except (OverflowError, ValueError):
87 # 32 bit systems can't handle dates after Jan 2038, and certain
88 # systems can't handle dates before ~1901-12-01:
90 # >>> time.mktime((1900, 1, 13, 0, 0, 0, 0, 0, 0))
91 # OverflowError: mktime argument out of range
92 # >>> time.mktime((1850, 1, 13, 0, 0, 0, 0, 0, 0))
93 # ValueError: year out of range
95 # In this case, we fake the date, because we only care about the
98 stamp
= time
.mktime(tt
)
99 tt
= time
.localtime(stamp
)
100 return tt
.tm_isdst
> 0