MCPcopy Create free account
hub / github.com/celery/celery / LocalTimezone

Class LocalTimezone

celery/utils/time.py:69–123  ·  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

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

Callers 4

test_daylightMethod · 0.90
test_local_timezoneMethod · 0.90
localMethod · 0.85

Calls

no outgoing calls

Tested by 3

test_daylightMethod · 0.72
test_local_timezoneMethod · 0.72