A good
[https://www.reddit.com/r/django/comments/49p8dg/excluding_hours_range_ex_from_215am_to_5am_from_a/
usage example] would be:
{{{#!python
Event.objects.filter(
datetime__time__range=(start_time, end_time),
)
}}}
To select all activities that occurred in a timezone aware time range.
Here's an implementation for PostgreSQL and MySQL missing tests:
{{{#!python
from django.conf import settings
from django.db import models
from django.utils import timezone
from django.utils.functional import cached_property
class DateTimeTimeTransform(models.Transform):
lookup_name = 'time'
@cached_property
def output_field(self):
return models.TimeField()
def as_mysql(self, compiler, connection):
lhs, lhs_params = compiler.compile(self.lhs)
if settings.USE_TZ:
lhs = "CONVERT_TZ(%s, 'UTC', %%s)" % lhs
tzname = timezone.get_current_timezone_name()
lhs_params.append(tzname)
sql = "TIME(%s)" % lhs
return sql, lhs_params
def as_postgresql(self, compiler, connection):
lhs, lhs_params = compiler.compile(self.lhs)
if settings.USE_TZ:
lhs = "%s AT TIME ZONE %%s" % lhs
tzname = timezone.get_current_timezone_name()
lhs_params.append(tzname)
sql = "(%s)::time" % lhs
return sql, lhs_params
models.DateTimeField.register_lookup(DateTimeTimeTransform)
}}}
We should wait for the datetime expression refactor (#25774) to land
before proceeding with this patch.
--
Ticket URL: <https://code.djangoproject.com/ticket/26348>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:1>
* status: new => assigned
* owner: nobody => charettes
--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:2>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/6744 PR] with missing Oracle
support.
--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:3>
* needs_better_patch: 0 => 1
Comment:
Left a few comments for improvement on the PR.
--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:4>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:5>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:6>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"8a4f017f4565c51c83aabb61a816e334e8638432" 8a4f017f]:
{{{
#!CommitTicketReference repository=""
revision="8a4f017f4565c51c83aabb61a816e334e8638432"
Fixed #26348 -- Added TruncTime and exposed it through the __time lookup.
Thanks Tim for the review.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:8>
Comment (by Simon Charette <charette.s@…>):
In [changeset:"082c52dbedd76c312cebf3b23e04c449a94c20b6" 082c52db]:
{{{
#!CommitTicketReference repository=""
revision="082c52dbedd76c312cebf3b23e04c449a94c20b6"
Refs #25774, #26348 -- Allowed Trunc functions to operate with time
fields.
Thanks Josh for the amazing testing setup and Tim for the review.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26348#comment:7>