1 | """Implementation of a tzinfo-classes for use with datetime.datetime."""
|
---|
2 |
|
---|
3 | import time
|
---|
4 | from datetime import timedelta, tzinfo
|
---|
5 |
|
---|
6 | class FixedOffset(tzinfo):
|
---|
7 | """Fixed offset in minutes east from UTC."""
|
---|
8 |
|
---|
9 | def __init__(self, offset):
|
---|
10 | self.__offset = timedelta(minutes=offset)
|
---|
11 | # FIXME -- Not really a name...
|
---|
12 | self.__name = "%+03d%02d" % (offset / 60, offset % 60)
|
---|
13 |
|
---|
14 | def __repr__(self):
|
---|
15 | return self.__name
|
---|
16 |
|
---|
17 | def utcoffset(self, dt):
|
---|
18 | return self.__offset
|
---|
19 |
|
---|
20 | def tzname(self, dt):
|
---|
21 | return self.__name
|
---|
22 |
|
---|
23 | def dst(self, dt):
|
---|
24 | return ZERO
|
---|
25 |
|
---|
26 | class LocalTimezone(tzinfo):
|
---|
27 | """Proxy timezone information from time module."""
|
---|
28 | def __init__(self, dt):
|
---|
29 | tzinfo.__init__(self, dt)
|
---|
30 | self._tzname = time.tzname[self._isdst(dt)]
|
---|
31 | def __repr__(self):
|
---|
32 | return self._tzname
|
---|
33 |
|
---|
34 | def utcoffset(self, dt):
|
---|
35 | if self._isdst(dt):
|
---|
36 | return timedelta(seconds=-time.altzone)
|
---|
37 | else:
|
---|
38 | return timedelta(seconds=-time.timezone)
|
---|
39 | def dst(self, dt):
|
---|
40 | if self._isdst(dt):
|
---|
41 | return timedelta(seconds=-time.altzone) - timedelta(seconds=-time.timezone)
|
---|
42 | else:
|
---|
43 | return timedelta(0)
|
---|
44 | def tzname(self, dt):
|
---|
45 | return time.tzname[self._isdst(dt)]
|
---|
46 | def _isdst(self, dt):
|
---|
47 | tt = (dt.year, dt.month, dt.day,
|
---|
48 | getattr(dt, 'hour', 0), getattr(dt, 'minute', 0), getattr(dt, 'second', 0),
|
---|
49 | dt.weekday(), 0, -1)
|
---|
50 | stamp = time.mktime(tt)
|
---|
51 | tt = time.localtime(stamp)
|
---|
52 | return tt.tm_isdst > 0
|
---|