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

Function localize

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

445
446
447def localize(dt: datetime, tz: tzinfo) -> datetime:
448 """Convert aware :class:`~datetime.datetime` to another timezone.
449
450 Using a ZoneInfo timezone will give the most flexibility in terms of ambiguous DST handling.
451 """
452 if is_naive(dt): # Ensure timezone aware datetime
453 dt = make_aware(dt, tz)
454 if dt.tzinfo == ZoneInfo("UTC"):
455 dt = dt.astimezone(tz) # Always safe to call astimezone on utc zones
456 return dt
457
458
459def to_utc(dt: datetime) -> datetime:

Callers 11

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 7

test_standard_tzMethod · 0.72
test_when_zoneinfoMethod · 0.72
test_nowMethod · 0.72