datatracker/ietf/utils/timezone.py
Jennifer Richards 32054111df
fix: fix failing tests and eliminate naive datetime warnings (#4402)
* test: fix timestamp construction in several doc tests

* refactor: rename date2datetime to datetime_from_date and clarify code

* chore: helper to get tzinfo for PRODUCTION_TIMEZONE

* fix: fix timezone handling in make_last_call()

* test: fix datetime generation in doc.tests_charter

* refactor: remove PRODUCTION_TIMEZONE setting

Replaces the PRODUCTION_TIMEZONE setting with a constant,
DEADLINE_TZINFO, in ietf.utils.timezone.

* test: be more careful about timezone in tests_charter.py

* test: be more careful about timezone in doc/tests.py

* fix: fix timezone handling affecting doc.tests_draft

* fix: fix timezone handling affecting tests_irsg_ballot.py

* fix: fix timezone handling affecting tests_review.py

* fix: fix timezone handling affecting last ietf.doc tests

* fix: fix timezone handling affecting last ietf.group tests

* fix: fix timezone handling affecting ietf.iesg tests

* fix: handle timezones in get_8989_eligibility_querysets

* fix: handle timezones affecting ietfauth tests

* fix: return tz-aware datetime from utc_from_string

* fix: specify timezone for constants in ipr_rfc_number()

* fix: specify tz for ipr deadlines

* fix: handle timezones affecting liaisons tests

* fix: treat leap day in get_8989_eligibility_querysets()

Manual cherry-pick of 248d6474

* test: treat leap day properly in nomcom tests

* fix: fix timezone handling affecting nomcom tests

* test: fix timezone handling in review tests

* fix: fix timezone handling affecting secr.meetings tests

* fix: handle both pytz and zoneinfo timezones in ietf.utils.timezone

* fix: fix timezone handling affecting secr.proceedings tests

* refactor: use make_aware() helper in secr.meetings tests

* test: fix timezone handling in secr.telechat tests

* fix: fix timezone handling affecting stats tests

* fix: eliminate tz-naive helpers affecting sync email parsing

* fix: include timezone data when serializing DeletedEvent data

* fix: fix timezone handling affecting sync tests

* style: remove unused import
2022-09-01 13:07:28 -03:00

71 lines
2.1 KiB
Python

import pytz
import datetime
from zoneinfo import ZoneInfo
from django.conf import settings
from django.utils import timezone
# Default time zone for deadlines / expiration dates.
DEADLINE_TZINFO = ZoneInfo('PST8PDT')
def make_aware(dt, tzinfo):
"""Assign timezone to a naive datetime
Helper to deal with both pytz and zoneinfo type time zones. Can go away when pytz is removed.
"""
if hasattr(tzinfo, 'localize'):
return tzinfo.localize(dt) # pytz-style
else:
return dt.replace(tzinfo=tzinfo) # zoneinfo- / datetime.timezone-style
def local_timezone_to_utc(d):
"""Takes a naive datetime in the local timezone and returns a
naive datetime with the corresponding UTC time."""
local_timezone = pytz.timezone(settings.TIME_ZONE)
d = local_timezone.localize(d).astimezone(pytz.utc)
return d.replace(tzinfo=None)
def datetime_from_date(date, tz=pytz.utc):
"""Get datetime at midnight on a given date"""
# accept either pytz or zoneinfo tzinfos until we get rid of pytz
return make_aware(datetime.datetime(date.year, date.month, date.day), tz)
def datetime_today(tzinfo=None):
"""Get a timezone-aware datetime representing midnight today
For use with datetime fields representing a date.
"""
if tzinfo is None:
tzinfo = pytz.utc
return timezone.now().astimezone(tzinfo).replace(hour=0, minute=0, second=0, microsecond=0)
def date_today(tzinfo=None):
"""Get the date corresponding to the current moment
Note that Dates are not themselves timezone aware.
"""
if tzinfo is None:
tzinfo = pytz.utc
return timezone.now().astimezone(tzinfo).date()
def time_now(tzinfo=None):
"""Get the "wall clock" time corresponding to the current moment
The value returned by this data is a Time with no tzinfo attached. (Time
objects have only limited timezone support, even if tzinfo is filled in,
and may not behave correctly when daylight savings time shifts are relevant.)
"""
if tzinfo is None:
tzinfo = pytz.utc
return timezone.now().astimezone(tzinfo).time()