class Trunc(expression, kind, output_field=None, tzinfo=None, **extra)
[source]
Truncates a date up to a significant component.
When you only care if something happened in a particular year, hour, or day, but not the exact second, then Trunc
(and its subclasses) can be useful to filter or aggregate your data. For example, you can use Trunc
to calculate the number of sales per day.
Trunc
takes a single expression
, representing a DateField
or DateTimeField
, a kind
representing a date part, and an output_field
that’s either DateTimeField()
or DateField()
. It returns a datetime or date, depending on output_field
, with fields up to kind
set to their minimum value. If output_field
is omitted, it will default to the output_field
of expression
. A tzinfo
subclass, usually provided by pytz
, can be passed to truncate a value in a specific timezone.
Given the datetime 2015-06-15 14:30:50.000321+00:00
, the built-in kind
s return:
- “year”: 2015-01-01 00:00:00+00:00
- “month”: 2015-06-01 00:00:00+00:00
- “day”: 2015-06-15 00:00:00+00:00
- “hour”: 2015-06-15 14:00:00+00:00
- “minute”: 2015-06-15 14:30:00+00:00
- “second”: 2015-06-15 14:30:50+00:00
If a different timezone like Australia/Melbourne
is active in Django, then the datetime is converted to the new timezone before the value is truncated. The timezone offset for Melbourne in the example date above is +10:00. The values returned when this timezone is active will be:
- “year”: 2015-01-01 00:00:00+11:00
- “month”: 2015-06-01 00:00:00+10:00
- “day”: 2015-06-16 00:00:00+10:00
- “hour”: 2015-06-16 00:00:00+10:00
- “minute”: 2015-06-16 00:30:00+10:00
- “second”: 2015-06-16 00:30:50+10:00
The year has an offset of +11:00 because the result transitioned into daylight saving time.
Each kind
above has a corresponding Trunc
subclass (listed below) that should typically be used instead of the more verbose equivalent, e.g. use TruncYear(...)
rather than Trunc(..., kind='year')
.
The subclasses are all defined as transforms, but they aren’t registered with any fields, because the obvious lookup names are already reserved by the Extract
subclasses.
Usage example:
>>> from datetime import datetime >>> from django.db.models import Count, DateTimeField >>> from django.db.models.functions import Trunc >>> Experiment.objects.create(start_datetime=datetime(2015, 6, 15, 14, 30, 50, 321)) >>> Experiment.objects.create(start_datetime=datetime(2015, 6, 15, 14, 40, 2, 123)) >>> Experiment.objects.create(start_datetime=datetime(2015, 12, 25, 10, 5, 27, 999)) >>> experiments_per_day = Experiment.objects.annotate( ... start_day=Trunc('start_datetime', 'day', output_field=DateTimeField()) ... ).values('start_day').annotate(experiments=Count('id')) >>> for exp in experiments_per_day: ... print(exp['start_day'], exp['experiments']) ... 2015-06-15 00:00:00 2 2015-12-25 00:00:00 1 >>> experiments = Experiment.objects.annotate( ... start_day=Trunc('start_datetime', 'day', output_field=DateTimeField()) ... ).filter(start_day=datetime(2015, 6, 15)) >>> for exp in experiments: ... print(exp.start_datetime) ... 2015-06-15 14:30:50.000321 2015-06-15 14:40:02.000123
Please login to continue.