Opened 22 months ago
Closed 22 months ago
#34371 closed New feature (wontfix)
Support creating aware datetime objects using django.utils.timezone
Reported by: | Ebram Shehata | Owned by: | nobody |
---|---|---|---|
Component: | Utilities | Version: | 4.1 |
Severity: | Normal | Keywords: | utils, datetime |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I think it'd be convenient if we support creating datetime objects using django.utils.timezone.
Maybe something like:
from django.utils import timezone dt = timezone.datetime(year=2023, month=2, day=25)
and that will give a timezone aware datetime (based on settings.TIME_ZONE
).
Use case: I'm currently working on a project in which I needed to manually create an initial datetime object and compare it later with another one created using timezone.now()
but I had an error raised from DRF:
can't compare offset-naive and offset-aware datetimes
As the error explains, it can't compare naive and aware offsets! It'd be good to be able to create new datetime objects using django's timezone module without having to do it using other work arounds.
Thanks for this ticket, however Django already provides multiple ways to create time-zone aware datetimes, you can use make_aware() or
timezone.now().replace(year=2023, ...)
, see docs.