Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/isodate/tzinfo.py: 53%
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"""This module provides some datetime.tzinfo implementations.
3All those classes are taken from the Python documentation.
4"""
6import time
7from datetime import datetime, timedelta, tzinfo
8from typing import Literal, Optional
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: Optional[datetime]) -> timedelta:
21 """Return offset from UTC in minutes east of UTC, which is ZERO for UTC."""
22 return ZERO
24 def tzname(self, dt: Optional[datetime]) -> Literal["UTC"]:
25 """Return the time zone name corresponding to the datetime object dt,
26 as a string.
27 """
28 return "UTC"
30 def dst(self, dt: Optional[datetime]) -> timedelta:
31 """Return the daylight saving time (DST) adjustment, in minutes east
32 of UTC.
33 """
34 return ZERO
36 def __reduce__(self):
37 """When unpickling a Utc object, return the default instance below, UTC."""
38 return _Utc, ()
41UTC = Utc()
42# the default instance for UTC.
45def _Utc() -> Utc:
46 """Helper function for unpickling a Utc object."""
47 return UTC
50class FixedOffset(tzinfo):
51 """A class building tzinfo objects for fixed-offset time zones.
53 Note that FixedOffset(0, 0, "UTC") or FixedOffset() is a different way to
54 build a UTC tzinfo object.
55 """
57 def __init__(
58 self, offset_hours: float = 0, offset_minutes: float = 0, name: str = "UTC"
59 ) -> None:
60 """Initialise an instance with time offset and name.
62 The time offset should be positive for time zones east of UTC
63 and negate for time zones west of UTC.
64 """
65 self.__offset = timedelta(hours=offset_hours, minutes=offset_minutes)
66 self.__name = name
68 def utcoffset(self, dt: Optional[datetime]) -> timedelta:
69 """Return offset from UTC in minutes of UTC."""
70 return self.__offset
72 def tzname(self, dt: Optional[datetime]) -> str:
73 """Return the time zone name corresponding to the datetime object dt, as a
74 string.
75 """
76 return self.__name
78 def dst(self, dt: Optional[datetime]) -> timedelta:
79 """Return the daylight saving time (DST) adjustment, in minutes east of
80 UTC.
81 """
82 return ZERO
84 def __repr__(self) -> str:
85 """Return nicely formatted repr string."""
86 return "<FixedOffset %r>" % self.__name
89STDOFFSET = timedelta(seconds=-time.timezone)
90# locale time zone offset
92# calculate local daylight saving offset if any.
93DSTOFFSET = timedelta(seconds=-time.altzone) if time.daylight else STDOFFSET
95DSTDIFF = DSTOFFSET - STDOFFSET
96# difference between local time zone and local DST time zone
99class LocalTimezone(tzinfo):
100 """A class capturing the platform's idea of local time."""
102 def utcoffset(self, dt: Optional[datetime]) -> timedelta:
103 """Return offset from UTC in minutes of UTC."""
104 if self._isdst(dt):
105 return DSTOFFSET
106 else:
107 return STDOFFSET
109 def dst(self, dt: Optional[datetime]) -> timedelta:
110 """Return daylight saving offset."""
111 if self._isdst(dt):
112 return DSTDIFF
113 else:
114 return ZERO
116 def tzname(self, dt: Optional[datetime]) -> str:
117 """Return the time zone name corresponding to the datetime object dt, as a
118 string.
119 """
120 return time.tzname[self._isdst(dt)]
122 def _isdst(self, dt: Optional[datetime]) -> bool:
123 """Returns true if DST is active for given datetime object dt."""
124 if dt is None:
125 raise Exception("datetime object dt was None!")
126 tt = (
127 dt.year,
128 dt.month,
129 dt.day,
130 dt.hour,
131 dt.minute,
132 dt.second,
133 dt.weekday(),
134 0,
135 -1,
136 )
137 stamp = time.mktime(tt)
138 tt = time.localtime(stamp)
139 return tt.tm_isdst > 0
142# the default instance for local time zone.
143LOCAL = LocalTimezone()