MCPcopy
hub / github.com/celery/celery / LocalTimezone

Class LocalTimezone

celery/utils/time.py:68–118  ·  view source on GitHub ↗

Local time implementation. Provided in _Zone to the app when `enable_utc` is disabled. Otherwise, _Zone provides a UTC ZoneInfo instance as the timezone implementation for the application. Note: Used only when the :setting:`enable_utc` setting is disabled.

Source from the content-addressed store, hash-verified

66
67
68class LocalTimezone(tzinfo):
69 """Local time implementation. Provided in _Zone to the app when `enable_utc` is disabled.
70 Otherwise, _Zone provides a UTC ZoneInfo instance as the timezone implementation for the application.
71
72 Note:
73 Used only when the :setting:`enable_utc` setting is disabled.
74 """
75
76 _offset_cache: dict[int, tzinfo] = {}
77
78 def __init__(self) -> None:
79 # This code is moved in __init__ to execute it as late as possible
80 # See get_default_timezone().
81 self.STDOFFSET = timedelta(seconds=-_time.timezone)
82 if _time.daylight:
83 self.DSTOFFSET = timedelta(seconds=-_time.altzone)
84 else:
85 self.DSTOFFSET = self.STDOFFSET
86 self.DSTDIFF = self.DSTOFFSET - self.STDOFFSET
87 super().__init__()
88
89 def __repr__(self) -> str:
90 return f'<LocalTimezone: UTC{int(self.DSTOFFSET.total_seconds() / 3600):+03d}>'
91
92 def utcoffset(self, dt: datetime) -> timedelta:
93 return self.DSTOFFSET if self._isdst(dt) else self.STDOFFSET
94
95 def dst(self, dt: datetime) -> timedelta:
96 return self.DSTDIFF if self._isdst(dt) else ZERO
97
98 def tzname(self, dt: datetime) -> str:
99 return _time.tzname[self._isdst(dt)]
100
101 def fromutc(self, dt: datetime) -> datetime:
102 # The base tzinfo class no longer implements a DST
103 # offset aware .fromutc() in Python 3 (Issue #2306).
104 offset = int(self.utcoffset(dt).seconds / 60.0)
105 try:
106 tz = self._offset_cache[offset]
107 except KeyError:
108 tz = self._offset_cache[offset] = datetime_timezone(
109 timedelta(minutes=offset))
110 return tz.fromutc(dt.replace(tzinfo=tz))
111
112 def _isdst(self, dt: datetime) -> bool:
113 tt = (dt.year, dt.month, dt.day,
114 dt.hour, dt.minute, dt.second,
115 dt.weekday(), 0, 0)
116 stamp = _time.mktime(tt)
117 tt = _time.localtime(stamp)
118 return tt.tm_isdst > 0
119
120
121class _Zone:

Callers 2

test_daylightMethod · 0.90
localMethod · 0.85

Calls

no outgoing calls

Tested by 1

test_daylightMethod · 0.72