MCPcopy
hub / github.com/celery/celery / localize

Function localize

celery/utils/time.py:360–369  ·  view source on GitHub ↗

Convert aware :class:`~datetime.datetime` to another timezone. Using a ZoneInfo timezone will give the most flexibility in terms of ambiguous DST handling.

(dt: datetime, tz: tzinfo)

Source from the content-addressed store, hash-verified

358
359
360def localize(dt: datetime, tz: tzinfo) -> datetime:
361 """Convert aware :class:`~datetime.datetime` to another timezone.
362
363 Using a ZoneInfo timezone will give the most flexibility in terms of ambiguous DST handling.
364 """
365 if is_naive(dt): # Ensure timezone aware datetime
366 dt = make_aware(dt, tz)
367 if dt.tzinfo == ZoneInfo("UTC"):
368 dt = dt.astimezone(tz) # Always safe to call astimezone on utc zones
369 return dt
370
371
372def to_utc(dt: datetime) -> datetime:

Callers 10

test_standard_tzMethod · 0.90
test_when_zoneinfoMethod · 0.90
test_nowMethod · 0.90
remaining_estimateMethod · 0.85
to_localMethod · 0.85
to_local_fallbackMethod · 0.85
maybe_make_awareFunction · 0.85

Calls 2

is_naiveFunction · 0.85
make_awareFunction · 0.85

Tested by 6

test_standard_tzMethod · 0.72
test_when_zoneinfoMethod · 0.72
test_nowMethod · 0.72