数据库函数

您所在的位置:网站首页 strftime函数的库函数 数据库函数

数据库函数

2023-08-24 01:55| 来源: 网络整理| 查看: 265

Trunc¶ class Trunc(expression, kind, output_field=None, tzinfo=None, is_dst=None, **extra)¶

将一个日期截断到一个重要的部分。

当你只关心某事是否发生在某年、某小时或某天,而不关心确切的秒数时,那么 Trunc (及其子类)可以用来过滤或汇总你的数据。例如,你可以使用 Trunc 来计算每天的销售数量。

Trunc takes a single expression, representing a DateField, TimeField, or DateTimeField, a kind representing a date or time part, and an output_field that's either DateTimeField(), TimeField(), or DateField(). It returns a datetime, date, or time 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 zoneinfo, can be passed to truncate a value in a specific timezone.

4.0 版后已移除: is_dst 参数表示 pytz 是否应该解释夏令时中不存在的和模糊的日期。默认情况下(当 is_dst=None),pytz 会对这种日期时间产生异常。

is_dst 参数已被废弃,将在 Django 5.0 中删除。

给定日期时间 2015-06-15 14:30:50.000321+00:00,内置 kind 返回:

"year": 2015-01-01 00:00:00+00:00 "quarter": 2015-04-01 00:00:00+00:00 "month": 2015-06-01 00:00:00+00:00 "week": 2015-06-15 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

如果在 Django 中使用了不同的时区,比如 Australia/Melbourne,那么日期时间会在被截断之前转换为新的时区。在上面的例子中,墨尔本的时区偏移是 +10:00。当这个时区被激活时,返回的值将是:

"year": 2015-01-01 00:00:00+11:00 "quarter": 2015-04-01 00:00:00+10:00 "month": 2015-06-01 00:00:00+10:00 "week": 2015-06-16 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

年的偏移量为 +11:00,因为结果过渡到夏令时。

以上每个 kind 都有一个对应的 Trunc 子类(下面列出的),通常应该用这个子类来代替比较啰嗦的等价物,例如使用 TruncYear(...) 而不是 Trunc(...,kind='year')。

子类都被定义为变换,但它们没有注册任何字段,因为查找名称已经被 Extract 子类保留。

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 DateField 截断¶ class TruncYear(expression, output_field=None, tzinfo=None, is_dst=None, **extra)¶ kind = 'year' class TruncMonth(expression, output_field=None, tzinfo=None, is_dst=None, **extra)¶ kind = 'month' class TruncWeek(expression, output_field=None, tzinfo=None, is_dst=None, **extra)¶

截断到每周一的午夜。

kind = 'week' class TruncQuarter(expression, output_field=None, tzinfo=None, is_dst=None, **extra)¶ kind = 'quarter'

4.0 版后已移除: is_dst 参数已被废弃,将在 Django 5.0 中删除。

这些在逻辑上等同于 Trunc('date_field', kind)。它们截断日期的所有部分,直至 kind,允许以较低的精度对日期进行分组或过滤。expression 可以有一个 output_field 的 DateField 或 DateTimeField。

Since DateFields don't have a time component, only Trunc subclasses that deal with date-parts can be used with DateField:

>>> from datetime import datetime, timezone >>> from django.db.models import Count >>> from django.db.models.functions import TruncMonth, TruncYear >>> start1 = datetime(2014, 6, 15, 14, 30, 50, 321, tzinfo=timezone.utc) >>> start2 = datetime(2015, 6, 15, 14, 40, 2, 123, tzinfo=timezone.utc) >>> start3 = datetime(2015, 12, 31, 17, 5, 27, 999, tzinfo=timezone.utc) >>> Experiment.objects.create(start_datetime=start1, start_date=start1.date()) >>> Experiment.objects.create(start_datetime=start2, start_date=start2.date()) >>> Experiment.objects.create(start_datetime=start3, start_date=start3.date()) >>> experiments_per_year = ( ... Experiment.objects.annotate(year=TruncYear("start_date")) ... .values("year") ... .annotate(experiments=Count("id")) ... ) >>> for exp in experiments_per_year: ... print(exp["year"], exp["experiments"]) ... 2014-01-01 1 2015-01-01 2 >>> import zoneinfo >>> melb = zoneinfo.ZoneInfo("Australia/Melbourne") >>> experiments_per_month = ( ... Experiment.objects.annotate(month=TruncMonth("start_datetime", tzinfo=melb)) ... .values("month") ... .annotate(experiments=Count("id")) ... ) >>> for exp in experiments_per_month: ... print(exp["month"], exp["experiments"]) ... 2015-06-01 00:00:00+10:00 1 2016-01-01 00:00:00+11:00 1 2014-06-01 00:00:00+10:00 1 DateTimeField 截断¶ class TruncDate(expression, tzinfo=None, **extra)¶ lookup_name = 'date' output_field = DateField()

TruncDate 将 expression 投射到一个日期,而不是使用内置的 SQL truncate 函数。在 DateTimeField 上,它也被注册为 __date 的转换。

class TruncTime(expression, tzinfo=None, **extra)¶ lookup_name = 'time' output_field = TimeField()

TruncTime 将 expression 投射到一个时间,而不是使用内置的 SQL truncate 函数。在 DateTimeField 上,它也被注册为 __time 的转换。

class TruncDay(expression, output_field=None, tzinfo=None, is_dst=None, **extra)¶ kind = 'day' class TruncHour(expression, output_field=None, tzinfo=None, is_dst=None, **extra)¶ kind = 'hour' class TruncMinute(expression, output_field=None, tzinfo=None, is_dst=None, **extra)¶ kind = 'minute' class TruncSecond(expression, output_field=None, tzinfo=None, is_dst=None, **extra)¶ kind = 'second'

4.0 版后已移除: is_dst 参数已被废弃,将在 Django 5.0 中删除。

这些在逻辑上等同于 Trunc('datetime_field', kind)。它们截断日期的所有部分,直至 kind,并允许以较低的精度对日期时间进行分组或过滤。expression 必须有一个 output_field 的 DateTimeField。

Usage example:

>>> from datetime import date, datetime, timezone >>> from django.db.models import Count >>> from django.db.models.functions import ( ... TruncDate, ... TruncDay, ... TruncHour, ... TruncMinute, ... TruncSecond, ... ) >>> import zoneinfo >>> start1 = datetime(2014, 6, 15, 14, 30, 50, 321, tzinfo=timezone.utc) >>> Experiment.objects.create(start_datetime=start1, start_date=start1.date()) >>> melb = zoneinfo.ZoneInfo("Australia/Melbourne") >>> Experiment.objects.annotate( ... date=TruncDate("start_datetime"), ... day=TruncDay("start_datetime", tzinfo=melb), ... hour=TruncHour("start_datetime", tzinfo=melb), ... minute=TruncMinute("start_datetime"), ... second=TruncSecond("start_datetime"), ... ).values("date", "day", "hour", "minute", "second").get() {'date': datetime.date(2014, 6, 15), 'day': datetime.datetime(2014, 6, 16, 0, 0, tzinfo=zoneinfo.ZoneInfo('Australia/Melbourne')), 'hour': datetime.datetime(2014, 6, 16, 0, 0, tzinfo=zoneinfo.ZoneInfo('Australia/Melbourne')), 'minute': 'minute': datetime.datetime(2014, 6, 15, 14, 30, tzinfo=timezone.utc), 'second': datetime.datetime(2014, 6, 15, 14, 30, 50, tzinfo=timezone.utc) } TimeField 截断¶ class TruncHour(expression, output_field=None, tzinfo=None, is_dst=None, **extra) kind = 'hour' class TruncMinute(expression, output_field=None, tzinfo=None, is_dst=None, **extra) kind = 'minute' class TruncSecond(expression, output_field=None, tzinfo=None, is_dst=None, **extra) kind = 'second'

4.0 版后已移除: is_dst 参数已被废弃,将在 Django 5.0 中删除。

这些在逻辑上等同于 Trunc('time_field', kind)。它们截断时间的所有部分,直至 kind,这就允许以较低的精度对时间进行分组或过滤。expression 可以有一个 output_field 的 TimeField 或 DateTimeField。

Since TimeFields don't have a date component, only Trunc subclasses that deal with time-parts can be used with TimeField:

>>> from datetime import datetime, timezone >>> from django.db.models import Count, TimeField >>> from django.db.models.functions import TruncHour >>> start1 = datetime(2014, 6, 15, 14, 30, 50, 321, tzinfo=timezone.utc) >>> start2 = datetime(2014, 6, 15, 14, 40, 2, 123, tzinfo=timezone.utc) >>> start3 = datetime(2015, 12, 31, 17, 5, 27, 999, tzinfo=timezone.utc) >>> Experiment.objects.create(start_datetime=start1, start_time=start1.time()) >>> Experiment.objects.create(start_datetime=start2, start_time=start2.time()) >>> Experiment.objects.create(start_datetime=start3, start_time=start3.time()) >>> experiments_per_hour = ( ... Experiment.objects.annotate( ... hour=TruncHour("start_datetime", output_field=TimeField()), ... ) ... .values("hour") ... .annotate(experiments=Count("id")) ... ) >>> for exp in experiments_per_hour: ... print(exp["hour"], exp["experiments"]) ... 14:00:00 2 17:00:00 1 >>> import zoneinfo >>> melb = zoneinfo.ZoneInfo("Australia/Melbourne") >>> experiments_per_hour = ( ... Experiment.objects.annotate( ... hour=TruncHour("start_datetime", tzinfo=melb), ... ) ... .values("hour") ... .annotate(experiments=Count("id")) ... ) >>> for exp in experiments_per_hour: ... print(exp["hour"], exp["experiments"]) ... 2014-06-16 00:00:00+10:00 2 2016-01-01 04:00:00+11:00 1


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3