Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/isodate/tzinfo.py: 54%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2This module provides some datetime.tzinfo implementations.
4All those classes are taken from the Python documentation.
5"""
7import time
8from datetime import timedelta, tzinfo
10ZERO = timedelta(0)
11# constant for zero time offset.
14class Utc(tzinfo):
15 """UTC
17 Universal time coordinated time zone.
18 """
20 def utcoffset(self, dt):
21 """
22 Return offset from UTC in minutes east of UTC, which is ZERO for UTC.
23 """
24 return ZERO
26 def tzname(self, dt):
27 """
28 Return the time zone name corresponding to the datetime object dt,
29 as a string.
30 """
31 return "UTC"
33 def dst(self, dt):
34 """
35 Return the daylight saving time (DST) adjustment, in minutes east
36 of UTC.
37 """
38 return ZERO
40 def __reduce__(self):
41 """
42 When unpickling a Utc object, return the default instance below, UTC.
43 """
44 return _Utc, ()
47UTC = Utc()
48# the default instance for UTC.
51def _Utc():
52 """
53 Helper function for unpickling a Utc object.
54 """
55 return UTC
58class FixedOffset(tzinfo):
59 """
60 A class building tzinfo objects for fixed-offset time zones.
62 Note that FixedOffset(0, 0, "UTC") or FixedOffset() is a different way to
63 build a UTC tzinfo object.
64 """
66 def __init__(self, offset_hours=0, offset_minutes=0, name="UTC"):
67 """
68 Initialise an instance with time offset and name.
69 The time offset should be positive for time zones east of UTC
70 and negate for time zones west of UTC.
71 """
72 self.__offset = timedelta(hours=offset_hours, minutes=offset_minutes)
73 self.__name = name
75 def utcoffset(self, dt):
76 """
77 Return offset from UTC in minutes of UTC.
78 """
79 return self.__offset
81 def tzname(self, dt):
82 """
83 Return the time zone name corresponding to the datetime object dt, as a
84 string.
85 """
86 return self.__name
88 def dst(self, dt):
89 """
90 Return the daylight saving time (DST) adjustment, in minutes east of
91 UTC.
92 """
93 return ZERO
95 def __repr__(self):
96 """
97 Return nicely formatted repr string.
98 """
99 return "<FixedOffset %r>" % self.__name
102STDOFFSET = timedelta(seconds=-time.timezone)
103# locale time zone offset
105# calculate local daylight saving offset if any.
106if time.daylight:
107 DSTOFFSET = timedelta(seconds=-time.altzone)
108else:
109 DSTOFFSET = STDOFFSET
111DSTDIFF = DSTOFFSET - STDOFFSET
112# difference between local time zone and local DST time zone
115class LocalTimezone(tzinfo):
116 """
117 A class capturing the platform's idea of local time.
118 """
120 def utcoffset(self, dt):
121 """
122 Return offset from UTC in minutes of UTC.
123 """
124 if self._isdst(dt):
125 return DSTOFFSET
126 else:
127 return STDOFFSET
129 def dst(self, dt):
130 """
131 Return daylight saving offset.
132 """
133 if self._isdst(dt):
134 return DSTDIFF
135 else:
136 return ZERO
138 def tzname(self, dt):
139 """
140 Return the time zone name corresponding to the datetime object dt, as a
141 string.
142 """
143 return time.tzname[self._isdst(dt)]
145 def _isdst(self, dt):
146 """
147 Returns true if DST is active for given datetime object dt.
148 """
149 tt = (
150 dt.year,
151 dt.month,
152 dt.day,
153 dt.hour,
154 dt.minute,
155 dt.second,
156 dt.weekday(),
157 0,
158 -1,
159 )
160 stamp = time.mktime(tt)
161 tt = time.localtime(stamp)
162 return tt.tm_isdst > 0
165# the default instance for local time zone.
166LOCAL = LocalTimezone()