Types, Functions, and Operators for Dates and Times

datetime

Timezone-aware point in time

duration

Absolute time span

cal::local_datetime

Date and time w/o timezone

cal::local_date

Date type

cal::local_time

Time type

cal::relative_duration

Relative time span

cal::date_duration

Relative time span in days

dt + dt

Time interval addition.

dt - dt

Time interval and date/time subtraction.

= != ?= ?!= < > <= >=

Comparison operators

to_str()

Render a date/time value to a string.

to_datetime()

Create a datetime value.

cal::to_local_datetime()

Create a cal::local_datetime value.

cal::to_local_date()

Create a cal::local_date value.

cal::to_local_time()

Create a cal::local_time value.

to_duration()

Create a duration value.

cal::to_relative_duration()

Create a cal::relative_duration value.

cal::to_date_duration()

Create a cal::date_duration value.

datetime_get()

Extract a specific element of input datetime by name.

cal::time_get()

Extract a specific element of input time by name.

cal::date_get()

Extract a specific element of input date by name.

duration_get()

Extract a specific element of input duration by name.

datetime_truncate()

Truncate the input datetime to a particular precision.

duration_truncate()

Truncate the input duration to a particular precision.

datetime_current()

Return the current server date and time.

datetime_of_transaction()

Return the date and time of the start of the current transaction.

datetime_of_statement()

Return the date and time of the start of the current statement.

cal::duration_normalize_hours()

Convert 24-hour chunks into days.

cal::duration_normalize_days()

Convert 30-day chunks into months.

EdgeDB has two classes of date/time types:

There are also two different ways of measuring duration:

  • duration using absolute and unambiguous units;

  • cal::relative_duration using fuzzy units like years, months and days in addition to the absolute units.

All date/time operators and functions and type casts are designed to maintain a strict separation between timezone-aware and “local” date/time values.

EdgeDB stores and outputs timezone-aware values in UTC.

All the date/time types are restricted to years between 1 and 9999, including the end points.

Although many systems support ISO 8601 date formatting in theory, in practice the formatting before year 1 and after 9999 tends to be inconsistent. As such dates outside that range are not reliably portable.

type

datetime

Dates and Times - 图1

Dates and Times - 图2

Dates and Times - 图3

datetime

A timezone-aware type representing a moment in time.

All dates must correspond to dates that exist in the proleptic Gregorian calendar.

Casting is a simple way to obtain a datetime value in an expression:

  1. select <datetime>'2018-05-07T15:01:22.306916+00';
  2. select <datetime>'2018-05-07T15:01:22+00';

Note that when casting from strings, the string should be in ISO 8601 format with timezone included:

  1. db>
  1. select <datetime>'January 01 2019 UTC';
  1. InvalidValueError: invalid input syntax for type
  2. std::datetime: 'January 01 2019 UTC'
  3. Hint: Please use ISO8601 format. Alternatively "to_datetime"
  4. function provides custom formatting options.
  1. db>
  1. select <datetime>'2019-01-01T15:01:22';
  1. InvalidValueError: invalid input syntax for type
  2. std::datetime: '2019-01-01T15:01:22'
  3. Hint: Please use ISO8601 format. Alternatively "to_datetime"
  4. function provides custom formatting options.

All datetime values are restricted to the range from year 1 to 9999.

See functions datetime_get(), to_datetime(), and to_str() for more ways of working with datetime.

type

cal::local_datetime

Dates and Times - 图4

Dates and Times - 图5

Dates and Times - 图6

local_datetime

A type representing date and time without time zone.

Casting is a simple way to obtain a cal::local_datetime value in an expression:

  1. select <cal::local_datetime>'2018-05-07T15:01:22.306916';
  2. select <cal::local_datetime>'2018-05-07T15:01:22';

Note that when casting from strings, the string should be in ISO 8601 format without timezone:

  1. db>
  1. select <cal::local_datetime>'2019-01-01T15:01:22+00';
  1. InvalidValueError: invalid input syntax for type
  2. cal::local_datetime: '2019-01-01T15:01:22+00'
  3. Hint: Please use ISO8601 format. Alternatively
  4. "cal::to_local_datetime" function provides custom formatting
  5. options.
  1. db>
  1. select <cal::local_datetime>'January 01 2019';
  1. InvalidValueError: invalid input syntax for type
  2. cal::local_datetime: 'January 01 2019'
  3. Hint: Please use ISO8601 format. Alternatively
  4. "cal::to_local_datetime" function provides custom formatting
  5. options.

All datetime values are restricted to the range from year 1 to 9999.

See functions datetime_get(), cal::to_local_datetime(), and to_str() for more ways of working with cal::local_datetime.

type

cal::local_date

Dates and Times - 图7

Dates and Times - 图8

Dates and Times - 图9

local_date

A type representing a date without a time zone.

Casting is a simple way to obtain a cal::local_date value in an expression:

  1. select <cal::local_date>'2018-05-07';

Note that when casting from strings, the string should be in ISO 8601 date format.

See functions cal::date_get(), cal::to_local_date(), and to_str() for more ways of working with cal::local_date.

type

cal::local_time

Dates and Times - 图10

Dates and Times - 图11

Dates and Times - 图12

local_time

A type representing time without a time zone.

Casting is a simple way to obtain a cal::local_time value in an expression:

  1. select <cal::local_time>'15:01:22.306916';
  2. select <cal::local_time>'15:01:22';

Note that when casting from strings, the string should be in ISO 8601 time format.

See functions cal::time_get(), cal::to_local_time(), and to_str() for more ways of working with cal::local_time.

type

duration

Dates and Times - 图13

Dates and Times - 图14

Dates and Times - 图15

duration

A type representing a span of time.

Valid units when converting from a string (and combinations of them): 'microseconds', 'milliseconds', 'seconds', 'minutes', 'hours'.

  1. select <duration>'45.6 seconds';
  2. select <duration>'15 milliseconds';
  3. select <duration>'48 hours 45 minutes';
  4. select <duration>'-7 minutes';

All date/time types support the + and - arithmetic operations with durations:

  1. db>
  1. select <datetime>'2019-01-01T00:00:00Z' - <duration>'24 hours';
  1. {<datetime>'2018-12-31T00:00:00+00:00'}
  1. db>
  1. select <cal::local_time>'22:00' + <duration>'1 hour';
  1. {<cal::local_time>'23:00:00'}

Duration is a fixed number of seconds and microseconds and isn’t adjusted by timezone, length of month or anything else in datetime calculations.

See functions to_duration(), and to_str() and date/time operators for more ways of working with duration.

type

cal::relative_duration

Dates and Times - 图16

Dates and Times - 图17

Dates and Times - 图18

relative_duration

A type representing a span of time.

Unlike std::duration a relative_duration is not a precise measurement because it uses 3 different units under the hood: months, days and seconds. However not all months have the same number of days and not all days have the same number of seconds. For example 2020 was a leap year and had 366 days. Notice how the number of hours in each year below is different.

  1. db>
  2. ...
  3. ...
  4. ...
  5. ...
  1. with
  2. first_day_of_2020 := <datetime>'2020-01-01T00:00:00Z',
  3. one_year := <cal::relative_duration>'1 year',
  4. first_day_of_next_year := first_day_of_2020 + one_year
  5. select first_day_of_next_year - first_day_of_2020;
  1. {<duration>'8784:00:00'}
  1. db>
  2. ...
  3. ...
  4. ...
  5. ...
  1. with
  2. first_day_of_2019 := <datetime>'2019-01-01T00:00:00Z',
  3. one_year := <cal::relative_duration>'1 year',
  4. first_day_of_next_year := first_day_of_2019 + one_year
  5. select first_day_of_next_year - first_day_of_2019;
  1. {<duration>'8760:00:00'}

Valid units when converting from a string (and combinations of them): 'microseconds', 'milliseconds', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years', 'decades', 'centuries', 'millennia'.

  1. select <cal::relative_duration>'45.6 seconds';
  2. select <cal::relative_duration>'15 milliseconds';
  3. select <cal::relative_duration>'3 weeks 45 minutes';
  4. select <cal::relative_duration>'-7 millennia';

All date/time types support the + and - arithmetic operations with relative_durations:

  1. db>
  2. ...
  1. select <datetime>'2019-01-01T00:00:00Z' -
  2. <cal::relative_duration>'3 years';
  1. {<datetime>'2016-01-01T00:00:00+00:00'}
  1. db>
  2. ...
  1. select <cal::local_time>'22:00' +
  2. <cal::relative_duration>'1 hour';
  1. {<cal::local_time>'23:00:00'}

If an arithmetic operation results in a day that doesn’t exist in the given month, the last day of the month is used instead.

  1. db>
  2. ...
  1. select <cal::local_datetime>"2021-01-31T15:00:00" +
  2. <cal::relative_duration>"1 month";
  1. {<cal::local_datetime>'2021-02-28T15:00:00'}

During arithmetic operations involving a relative_duration consisting of multiple components (units), higher-order components are applied first, followed by lower-order elements.

  1. db>
  2. ...
  1. select <cal::local_datetime>"2021-04-30T15:00:00" +
  2. <cal::relative_duration>"1 month 1 day";
  1. {<cal::local_datetime>'2021-05-31T15:00:00'}

Compare this to adding up the same duration components separately with higher-order units first and then lower-order, which produces the same result as above:

  1. db>
  2. ...
  3. ...
  1. select <cal::local_datetime>"2021-04-30T15:00:00" +
  2. <cal::relative_duration>"1 month" +
  3. <cal::relative_duration>"1 day";
  1. {<cal::local_datetime>'2021-05-31T15:00:00'}

When the order is reversed the result may actually be different for some corner cases:

  1. db>
  2. ...
  3. ...
  1. select <cal::local_datetime>"2021-04-30T15:00:00" +
  2. <cal::relative_duration>"1 day" +
  3. <cal::relative_duration>"1 month";
  1. {<cal::local_datetime>'2021-06-01T15:00:00'}

Gotchas

Due to the implementation of relative_duration logic, arithmetic operations may behave counterintuitively.

Non-associative

  1. db>
  2. ...
  3. ...
  1. select <cal::local_datetime>'2021-01-31T00:00:00' +
  2. <cal::relative_duration>'1 month' +
  3. <cal::relative_duration>'1 month';
  1. {<cal::local_datetime>'2021-03-28T00:00:00'}
  1. db>
  2. ...
  3. ...
  1. select <cal::local_datetime>'2021-01-31T00:00:00' +
  2. (<cal::relative_duration>'1 month' +
  3. <cal::relative_duration>'1 month');
  1. {<cal::local_datetime>'2021-03-31T00:00:00'}

Lossy

  1. db>
  2. ...
  3. ...
  4. ...
  1. with m := <cal::relative_duration>'1 month'
  2. select <cal::local_date>'2021-01-31' + m
  3. =
  4. <cal::local_date>'2021-01-30' + m;
  1. {true}

Asymmetric

  1. db>
  2. ...
  1. with m := <cal::relative_duration>'1 month'
  2. select <cal::local_date>'2021-01-31' + m - m;
  1. {<cal::local_date>'2021-01-28'}

Non-monotonic

  1. db>
  2. ...
  3. ...
  4. ...
  1. with m := <cal::relative_duration>'1 month'
  2. select <cal::local_datetime>'2021-01-31T01:00:00' + m
  3. <
  4. <cal::local_datetime>'2021-01-30T23:00:00' + m;
  1. {true}
  1. db>
  2. ...
  3. ...
  4. ...
  1. with m := <cal::relative_duration>'2 month'
  2. select <cal::local_datetime>'2021-01-31T01:00:00' + m
  3. <
  4. <cal::local_datetime>'2021-01-30T23:00:00' + m;
  1. {false}

See functions cal::to_relative_duration(), and to_str() and date/time operators for more ways of working with cal::relative_duration.

type

cal::date_duration

Dates and Times - 图19

Dates and Times - 图20

Dates and Times - 图21

date_duration

A type representing a span of time in days.

This type is only available in EdgeDB 2.0 or later.

The date_duration type is similar to relative_duration, but it only uses 2 different units under the hood: months and days. It is the result of subtracting one cal::local_date from another. The purpose of this type is to allow performing + and - operations on cal::local_date and produce cal::local_date as the result.

  1. db>
  2. ...
  1. select <cal::local_date>'2022-06-30' -
  2. <cal::local_date>'2022-06-25';
  1. {<cal::date_duration>'P5D'}
  1. db>
  2. ...
  1. select <cal::local_date>'2022-06-25' +
  2. <cal::date_duration>'5 days';
  1. {<cal::local_date>'2022-06-30'}
  1. db>
  2. ...
  1. select <cal::local_date>'2022-06-25' -
  2. <cal::date_duration>'5 days';
  1. {<cal::local_date>'2022-06-20'}

Valid units when converting from a string (and combinations of them): 'days', 'weeks', 'months', 'years', 'decades', 'centuries', 'millennia'.

  1. select <cal::date_duration>'45 days';
  2. select <cal::date_duration>'3 weeks 5 days';
  3. select <cal::date_duration>'-7 millennia';

Generally date_duration is fully compatible with cal::relative_duration and has the same general behaviour and caveats. It will even to implicitly cast to cal::relative_duration in any situation where only cal::relative_duration is expected.

See function cal::to_date_duration() and date/time operators for more ways of working with cal::date_duration.

operator

dt + dt

Dates and Times - 图22

Dates and Times - 图23

Dates and Times - 图24

datetime + duration -> datetimedatetime + cal::relative_duration -> cal::relative_durationduration + duration -> durationduration + cal::relative_duration -> cal::relative_durationcal::relative_duration + cal::relative_duration -> cal::relative_durationcal::local_datetime + cal::relative_duration -> cal::relative_durationcal::local_datetime + duration -> cal::local_datetimecal::local_time + cal::relative_duration -> cal::relative_durationcal::local_time + duration -> cal::local_timecal::local_date + cal::date_duration -> cal::local_datecal::date_duration + cal::date_duration -> cal::date_durationcal::local_date + cal::relative_duration -> cal::local_datetimecal::local_date + duration -> cal::local_datetime

Time interval addition.

This operator is commutative.

  1. db>
  1. select <cal::local_time>'22:00' + <duration>'1 hour';
  1. {<cal::local_time>'23:00:00'}
  1. db>
  1. select <duration>'1 hour' + <cal::local_time>'22:00';
  1. {<cal::local_time>'23:00:00'}
  1. db>
  1. select <duration>'1 hour' + <duration>'2 hours';
  1. {10800s}

operator

dt - dt

Dates and Times - 图25

Dates and Times - 图26

Dates and Times - 图27

duration - duration -> durationdatetime - datetime -> durationdatetime - duration -> datetimedatetime - cal::relative_duration -> datetimecal::relative_duration - cal::relative_duration -> cal::relative_durationcal::local_datetime - cal::local_datetime -> cal::relative_durationcal::local_datetime - cal::relative_duration -> cal::local_datetimecal::local_datetime - duration -> cal::local_datetimecal::local_time - cal::local_time -> cal::relative_durationcal::local_time - cal::relative_duration -> cal::local_timecal::local_time - duration -> cal::local_timecal::date_duration - cal::date_duration -> cal::date_durationcal::local_date - cal::local_date -> cal::date_durationcal::local_date - cal::date_duration -> cal::local_datecal::local_date - cal::relative_duration -> cal::local_datetimecal::local_date - duration -> cal::local_datetimeduration - cal::relative_duration -> cal::relative_durationcal::relative_duration - duration -> cal::relative_duration

Time interval and date/time subtraction.

  1. db>
  2. ...
  1. select <datetime>'2019-01-01T01:02:03+00' -
  2. <duration>'24 hours';
  1. {<datetime>'2018-12-31T01:02:03Z'}
  1. db>
  2. ...
  1. select <datetime>'2019-01-01T01:02:03+00' -
  2. <datetime>'2019-02-01T01:02:03+00';
  1. {-2678400s}
  1. db>
  2. ...
  1. select <duration>'1 hour' -
  2. <duration>'2 hours';
  1. {-3600s}

It is an error to subtract a date/time object from a time interval:

  1. db>
  2. ...
  1. select <duration>'1 day' -
  2. <datetime>'2019-01-01T01:02:03+00';
  1. QueryError: operator '-' cannot be applied to operands ...

It is also an error to subtract timezone-aware std::datetime to or from cal::local_datetime:

  1. db>
  2. ...
  1. select <datetime>'2019-01-01T01:02:03+00' -
  2. <cal::local_datetime>'2019-02-01T01:02:03';
  1. QueryError: operator '-' cannot be applied to operands ...

When subtracting one cal::local_date from another the result is given in whole number of days using cal::date_duration:

  1. db>
  2. ...
  1. select <cal::local_date>'2022-06-25' -
  2. <cal::local_date>'2019-02-01';
  1. {<cal::date_duration>'P1240D'}

function

datetime_current()

Dates and Times - 图28

Dates and Times - 图29

Dates and Times - 图30

std::datetime_current() -> datetime

Return the current server date and time.

  1. db>
  1. select datetime_current();
  1. {<datetime>'2018-05-14T20:07:11.755827Z'}

function

datetime_of_transaction()

Dates and Times - 图31

Dates and Times - 图32

Dates and Times - 图33

std::datetime_of_transaction() -> datetime

Return the date and time of the start of the current transaction.

function

datetime_of_statement()

Dates and Times - 图34

Dates and Times - 图35

Dates and Times - 图36

std::datetime_of_statement() -> datetime

Return the date and time of the start of the current statement.

function

datetime_get()

Dates and Times - 图37

Dates and Times - 图38

Dates and Times - 图39

std::datetime_get(dt: datetime, el: str) -> float64std::datetime_get(dt: cal::local_datetime, el: str) -> float64

Extract a specific element of input datetime by name.

The datetime scalar has the following elements available for extraction:

  • 'epochseconds' - the number of seconds since 1970-01-01 00:00:00 UTC (Unix epoch) for datetime or local time for cal::local_datetime. It can be negative.

  • 'century' - the century according to the Gregorian calendar

  • 'day' - the day of the month (1-31)

  • 'decade' - the decade (year divided by 10 and rounded down)

  • 'dow' - the day of the week from Sunday (0) to Saturday (6)

  • 'doy' - the day of the year (1-366)

  • 'hour' - the hour (0-23)

  • 'isodow' - the ISO day of the week from Monday (1) to Sunday (7)

  • 'isoyear' - the ISO 8601 week-numbering year that the date falls in. See the 'week' element for more details.

  • 'microseconds' - the seconds including fractional value expressed as microseconds

  • 'millennium' - the millennium. The third millennium started on Jan 1, 2001.

  • 'milliseconds' - the seconds including fractional value expressed as milliseconds

  • 'minutes' - the minutes (0-59)

  • 'month' - the month of the year (1-12)

  • 'quarter' - the quarter of the year (1-4)

  • 'seconds' - the seconds, including fractional value from 0 up to and not including 60

  • 'week' - the number of the ISO 8601 week-numbering week of the year. ISO weeks are defined to start on Mondays and the first week of a year must contain Jan 4 of that year.

  • 'year' - the year

  1. db>
  2. ...
  3. ...
  1. select datetime_get(
  2. <datetime>'2018-05-07T15:01:22.306916+00',
  3. 'epochseconds');
  1. {1525705282.306916}
  1. db>
  2. ...
  3. ...
  1. select datetime_get(
  2. <datetime>'2018-05-07T15:01:22.306916+00',
  3. 'year');
  1. {2018}
  1. db>
  2. ...
  3. ...
  1. select datetime_get(
  2. <datetime>'2018-05-07T15:01:22.306916+00',
  3. 'quarter');
  1. {2}
  1. db>
  2. ...
  3. ...
  1. select datetime_get(
  2. <datetime>'2018-05-07T15:01:22.306916+00',
  3. 'doy');
  1. {127}
  1. db>
  2. ...
  3. ...
  1. select datetime_get(
  2. <datetime>'2018-05-07T15:01:22.306916+00',
  3. 'hour');
  1. {15}

function

cal::time_get()

Dates and Times - 图40

Dates and Times - 图41

Dates and Times - 图42

cal::time_get(dt: cal::local_time, el: str) -> float64

Extract a specific element of input time by name.

The cal::local_time scalar has the following elements available for extraction: 'midnightseconds', 'hour', 'microseconds', 'milliseconds', 'minutes', 'seconds'.

For full description of what these elements extract see datetime_get().

  1. db>
  2. ...
  1. select cal::time_get(
  2. <cal::local_time>'15:01:22.306916', 'minutes');
  1. {1}
  1. db>
  2. ...
  1. select cal::time_get(
  2. <cal::local_time>'15:01:22.306916', 'milliseconds');
  1. {22306.916}

function

cal::date_get()

Dates and Times - 图43

Dates and Times - 图44

Dates and Times - 图45

cal::date_get(dt: local_date, el: str) -> float64

Extract a specific element of input date by name.

The cal::local_date scalar has the following elements available for extraction:

  • 'century' - the century according to the Gregorian calendar

  • 'day' - the day of the month (1-31)

  • 'decade' - the decade (year divided by 10 and rounded down)

  • 'dow' - the day of the week from Sunday (0) to Saturday (6)

  • 'doy' - the day of the year (1-366)

  • 'isodow' - the ISO day of the week from Monday (1) to Sunday (7)

  • 'isoyear' - the ISO 8601 week-numbering year that the date falls in. See the 'week' element for more details.

  • 'millennium' - the millennium. The third millennium started on Jan 1, 2001.

  • 'month' - the month of the year (1-12)

  • 'quarter' - the quarter of the year (1-4) not including 60

  • 'week' - the number of the ISO 8601 week-numbering week of the year. ISO weeks are defined to start on Mondays and the first week of a year must contain Jan 4 of that year.

  • 'year' - the year

  1. db>
  2. ...
  1. select cal::date_get(
  2. <cal::local_date>'2018-05-07', 'century');
  1. {21}
  1. db>
  2. ...
  1. select cal::date_get(
  2. <cal::local_date>'2018-05-07', 'year');
  1. {2018}
  1. db>
  2. ...
  1. select cal::date_get(
  2. <cal::local_date>'2018-05-07', 'month');
  1. {5}
  1. db>
  2. ...
  1. select cal::date_get(
  2. <cal::local_date>'2018-05-07', 'doy');
  1. {127}

function

duration_get()

Dates and Times - 图46

Dates and Times - 图47

Dates and Times - 图48

std::duration_get(dt: duration, el: str) -> float64std::duration_get(dt: cal::relative_duration, el: str) -> float64std::duration_get(dt: cal::date_duration, el: str) -> float64

Extract a specific element of input duration by name.

Only available in EdgeDB 2.0 or later.

There units avaialble for extraction are grouped into 3 categories. The largest units category of months or greater:

  • 'millennium' - number of 1000-year chunks rounded down

  • 'century' - the number of centiries rounded down

  • 'decade' - the number of decades rounded down

  • 'year' - the numbder of years rounded down

  • 'quarter'- the quarter left over after whole years are accounted for

  • 'month' - the number of months left over after whole years are accounted for

The middle size units of days:

  • 'day' - specifically the number of days recorded in the duration

The smallest unit category of hours, minutes, seconds, etc.:

  • 'hour' - duration hours

  • 'minutes' - duration minutes (0-59)

  • 'seconds' - duration seconds, including fractional value from 0 up to and not including 60

  • 'milliseconds' - the seconds including fractional value expressed as milliseconds

  • 'microseconds' - the seconds including fractional value expressed as microseconds

Additionally, it’s possible to convert a given duration into seconds:

  • 'totalseconds' - the number of seconds represented by the duration. It will be approximate for cal::relative_duration and cal::date_duration with usints larger than a day, because a month is assumed to be 30 days exactly.

The duration scalar only has the smallest units avaialbe for extraction.

The cal::relative_duration scalar has all of the units avaialbe for extraction.

The cal::date_duration scalar only has the largest and middle size units avaialbe for extraction.

  1. db>
  2. ...
  1. select duration_get(
  2. <cal::relative_duration>'400 months', 'year');
  1. {33}
  1. db>
  2. ...
  1. select duration_get(
  2. <cal::date_duration>'400 months', 'month');
  1. {4}
  1. db>
  2. ...
  3. ...
  1. select duration_get(
  2. <cal::relative_duration>'1 month 20 days 30 hours',
  3. 'day');
  1. {20}
  1. db>
  2. ...
  1. select duration_get(
  2. <cal::relative_duration>'30 hours', 'hour');
  1. {30}
  1. db>
  2. ...
  3. ...
  1. select duration_get(
  2. <cal::relative_duration>'1 month 20 days 30 hours',
  3. 'hour');
  1. {30}
  1. db>
  1. select duration_get(<duration>'30 hours', 'hour');
  1. {30}
  1. db>
  2. ...
  3. ...
  1. select duration_get(
  2. <cal::relative_duration>'1 month 20 days 30 hours',
  3. 'totalseconds');
  1. {4428000}
  1. db>
  2. ...
  1. select duration_get(
  2. <duration>'30 hours', 'totalseconds');
  1. {108000}

function

datetime_truncate()

Dates and Times - 图49

Dates and Times - 图50

Dates and Times - 图51

std::datetime_truncate(dt: datetime, unit: str) -> datetime

Truncate the input datetime to a particular precision.

The valid unit values in order or decreasing precision are: 'microseconds', 'milliseconds', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'quarters', 'years', 'decades', 'centuries'.

  1. db>
  2. ...
  1. select datetime_truncate(
  2. <datetime>'2018-05-07T15:01:22.306916+00', 'years');
  1. {<datetime>'2018-01-01T00:00:00Z'}
  1. db>
  2. ...
  1. select datetime_truncate(
  2. <datetime>'2018-05-07T15:01:22.306916+00', 'quarters');
  1. {<datetime>'2018-04-01T00:00:00Z'}
  1. db>
  2. ...
  1. select datetime_truncate(
  2. <datetime>'2018-05-07T15:01:22.306916+00', 'days');
  1. {<datetime>'2018-05-07T00:00:00Z'}
  1. db>
  2. ...
  1. select datetime_truncate(
  2. <datetime>'2018-05-07T15:01:22.306916+00', 'hours');
  1. {<datetime>'2018-05-07T15:00:00Z'}

function

duration_truncate()

Dates and Times - 图52

Dates and Times - 图53

Dates and Times - 图54

std::duration_truncate(dt: duration, unit: str) -> durationstd::duration_truncate(dt: cal::relative_duration, unit: str) -> cal::relative_duration

Truncate the input duration to a particular precision.

The valid unit values for duration are: 'microseconds', 'milliseconds', 'seconds', 'minutes', 'hours'.

In addition to the above the following are also valid for cal::relative_duration: 'days', 'weeks', 'months', 'years', 'decades', 'centuries'.

  1. db>
  2. ...
  1. select duration_truncate(
  2. <duration>'15:01:22', 'hours');
  1. {<duration>'15:00:00'}
  1. db>
  2. ...
  1. select duration_truncate(
  2. <duration>'15:01:22.306916', 'minutes');
  1. {<duration>'15:01:00'}
  1. db>
  2. ...
  1. select duration_truncate(
  2. <cal::relative_duration>'400 months', 'years');
  1. {<cal::relative_duration>'P33Y'}
  1. db>
  2. ...
  1. select duration_truncate(
  2. <cal::relative_duration>'400 months', 'decades');
  1. {<cal::relative_duration>'P30Y'}

function

to_datetime()

Dates and Times - 图55

Dates and Times - 图56

Dates and Times - 图57

std::to_datetime(s: str, fmt: optional str={}) -> datetimestd::to_datetime(local: cal::local_datetime, zone: str) -> datetimestd::to_datetime(year: int64, month: int64, day: int64, hour: int64, min: int64, sec: float64, timezone: str) -> datetimestd::to_datetime(epochseconds: decimal) -> datetimestd::to_datetime(epochseconds: float64) -> datetimestd::to_datetime(epochseconds: int64) -> datetime

Create a datetime value.

The datetime value can be parsed from the input str s. By default, the input is expected to conform to ISO 8601 format. However, the optional argument fmt can be used to override the input format to other forms.

  1. db>
  1. select to_datetime('2018-05-07T15:01:22.306916+00');
  1. {<datetime>'2018-05-07T15:01:22.306916Z'}
  1. db>
  1. select to_datetime('2018-05-07T15:01:22+00');
  1. {<datetime>'2018-05-07T15:01:22Z'}
  1. db>
  2. ...
  1. select to_datetime('May 7th, 2018 15:01:22 +00',
  2. 'Mon DDth, YYYY HH24:MI:SS TZH');
  1. {<datetime>'2018-05-07T15:01:22Z'}

Alternatively, the datetime value can be constructed from a cal::local_datetime value:

  1. db>
  2. ...
  1. select to_datetime(
  2. <cal::local_datetime>'2019-01-01T01:02:03', 'HKT');
  1. {<datetime>'2018-12-31T17:02:03Z'}

Another way to construct a the datetime value is to specify it in terms of its component parts: year, month, day, hour, min, sec, and timezone

  1. db>
  2. ...
  1. select to_datetime(
  2. 2018, 5, 7, 15, 1, 22.306916, 'UTC');
  1. {<datetime>'2018-05-07T15:01:22.306916000Z'}

Finally, it is also possible to convert a Unix timestamp to a datetime

  1. db>
  1. select to_datetime(1590595184.584);
  1. {<datetime>'2020-05-27T15:59:44.584000000Z'}

function

cal::to_local_datetime()

Dates and Times - 图58

Dates and Times - 图59

Dates and Times - 图60

cal::to_local_datetime(s: str, fmt: optional str={}) -> local_datetimecal::to_local_datetime(dt: datetime, zone: str) -> local_datetimecal::to_local_datetime(year: int64, month: int64, day: int64, hour: int64, min: int64, sec: float64) -> local_datetime

Create a cal::local_datetime value.

Similar to to_datetime(), the cal::local_datetime value can be parsed from the input str s with an optional fmt argument or it can be given in terms of its component parts: year, month, day, hour, min, sec.

For more details on formatting see here.

  1. db>
  1. select cal::to_local_datetime('2018-05-07T15:01:22.306916');
  1. {<cal::local_datetime>'2018-05-07T15:01:22.306916'}
  1. db>
  2. ...
  1. select cal::to_local_datetime('May 7th, 2018 15:01:22',
  2. 'Mon DDth, YYYY HH24:MI:SS');
  1. {<cal::local_datetime>'2018-05-07T15:01:22'}
  1. db>
  2. ...
  1. select cal::to_local_datetime(
  2. 2018, 5, 7, 15, 1, 22.306916);
  1. {<cal::local_datetime>'2018-05-07T15:01:22.306916'}

A timezone-aware datetime type can be converted to local datetime in the specified timezone:

  1. db>
  2. ...
  3. ...
  1. select cal::to_local_datetime(
  2. <datetime>'2018-12-31T22:00:00+08',
  3. 'US/Central');
  1. {<cal::local_datetime>'2018-12-31T08:00:00'}

function

cal::to_local_date()

Dates and Times - 图61

Dates and Times - 图62

Dates and Times - 图63

cal::to_local_date(s: str, fmt: optional str={}) -> cal::local_datecal::to_local_date(dt: datetime, zone: str) -> cal::local_datecal::to_local_date(year: int64, month: int64, day: int64) -> cal::local_date

Create a cal::local_date value.

Similar to to_datetime(), the cal::local_date value can be parsed from the input str s with an optional fmt argument or it can be given in terms of its component parts: year, month, day.

For more details on formatting see here.

  1. db>
  1. select cal::to_local_date('2018-05-07');
  1. {<cal::local_date>'2018-05-07'}
  1. db>
  1. select cal::to_local_date('May 7th, 2018', 'Mon DDth, YYYY');
  1. {<cal::local_date>'2018-05-07'}
  1. db>
  1. select cal::to_local_date(2018, 5, 7);
  1. {<cal::local_date>'2018-05-07'}

A timezone-aware datetime type can be converted to local date in the specified timezone:

  1. db>
  2. ...
  3. ...
  1. select cal::to_local_date(
  2. <datetime>'2018-12-31T22:00:00+08',
  3. 'US/Central');
  1. {<cal::local_date>'2019-01-01'}

function

cal::to_local_time()

Dates and Times - 图64

Dates and Times - 图65

Dates and Times - 图66

cal::to_local_time(s: str, fmt: optional str={}) -> local_timecal::to_local_time(dt: datetime, zone: str) -> local_timecal::to_local_time(hour: int64, min: int64, sec: float64) -> local_time

Create a cal::local_time value.

Similar to to_datetime(), the cal::local_time value can be parsed from the input str s with an optional fmt argument or it can be given in terms of its component parts: hour, min, sec.

For more details on formatting see here.

  1. db>
  1. select cal::to_local_time('15:01:22.306916');
  1. {<cal::local_time>'15:01:22.306916'}
  1. db>
  1. select cal::to_local_time('03:01:22pm', 'HH:MI:SSam');
  1. {<cal::local_time>'15:01:22'}
  1. db>
  1. select cal::to_local_time(15, 1, 22.306916);
  1. {<cal::local_time>'15:01:22.306916'}

A timezone-aware datetime type can be converted to local date in the specified timezone:

  1. db>
  2. ...
  3. ...
  1. select cal::to_local_time(
  2. <datetime>'2018-12-31T22:00:00+08',
  3. 'US/Pacific');
  1. {<cal::local_time>'06:00:00'}

function

to_duration()

Dates and Times - 图67

Dates and Times - 图68

Dates and Times - 图69

std::to_duration( named only hours: int64=0, named only minutes: int64=0, named only seconds: float64=0, named only microseconds: int64=0 ) -> duration

Create a duration value.

This function uses named only arguments to create a duration value. The available duration fields are: hours, minutes, seconds, microseconds.

  1. db>
  2. ...
  3. ...
  1. select to_duration(hours := 1,
  2. minutes := 20,
  3. seconds := 45);
  1. {4845s}
  1. db>
  1. select to_duration(seconds := 4845);
  1. {4845s}

function

std::duration_to_seconds()

Dates and Times - 图70

Dates and Times - 图71

Dates and Times - 图72

std::duration_to_seconds(cur: duration) -> decimal

Return duration as total number of seconds in interval.

  1. db>
  1. select duration_to_seconds(<duration>'1 hour');
  1. {3600.000000n}
  1. db>
  1. select duration_to_seconds(<duration>'10 second 123 ms');
  1. {10.123000n}

function

cal::to_relative_duration()

Dates and Times - 图73

Dates and Times - 图74

Dates and Times - 图75

cal::to_relative_duration( named only years: int64=0, named only months: int64=0, named only days: int64=0, named only hours: int64=0, named only minutes: int64=0, named only seconds: float64=0, named only microseconds: int64=0 ) -> cal::relative_duration

Create a cal::relative_duration value.

This function uses named only arguments to create a cal::relative_duration value. The available duration fields are: years, months, days, hours, minutes, seconds, microseconds.

  1. db>
  1. select cal::to_relative_duration(years := 5, minutes := 1);
  1. {<cal::relative_duration>'P5YT1S'}
  1. db>
  1. select cal::to_relative_duration(months := 3, days := 27);
  1. {<cal::relative_duration>'P3M27D'}

function

cal::to_date_duration()

Dates and Times - 图76

Dates and Times - 图77

Dates and Times - 图78

cal::to_date_duration( named only years: int64=0, named only months: int64=0, named only days: int64=0 ) -> cal::date_duration

Create a cal::date_duration value.

This function uses named only arguments to create a cal::date_duration value. The available duration fields are: years, months, days.

  1. db>
  1. select cal::to_date_duration(years := 1, days := 3);
  1. {<cal::date_duration>'P1Y3D'}
  1. db>
  1. select cal::to_date_duration(days := 12);
  1. {<cal::date_duration>'P12D'}

function

cal::duration_normalize_hours()

Dates and Times - 图79

Dates and Times - 图80

Dates and Times - 图81

cal::duration_normalize_hours( dur: cal::relative_duration ) -> cal::relative_duration

Convert 24-hour chunks into days.

Only available in EdgeDB 2.0 or later.

This function converts all 24-hour chunks into day units. The resulting cal::relative_duration is guaranteed to have less than 24 hours in total in the units smaler than days.

  1. db>
  2. ...
  1. select cal::duration_normalize_hours(
  2. <cal::relative_duration>'1312 hours');
  1. {<cal::relative_duration>'P54DT16H'}

This is a lossless operation because 24 hours are always equal to 1 day in cal::relative_duration units.

This is sometimes used together with cal::duration_normalize_days().

function

cal::duration_normalize_days()

Dates and Times - 图82

Dates and Times - 图83

Dates and Times - 图84

cal::duration_normalize_days( dur: cal::relative_duration ) -> cal::relative_durationcal::duration_normalize_days( dur: cal::date_duration ) -> cal::date_duration

Convert 30-day chunks into months.

Only available in EdgeDB 2.0 or later.

This function converts all 30-day chunks into month units. The resulting cal::relative_duration or cal::date_duration is guaranteed to have less than 30 day units.

  1. db>
  2. ...
  1. select cal::duration_normalize_days(
  2. <cal::relative_duration>'1312 days');
  1. {<cal::relative_duration>'P3Y7M22D'}
  1. db>
  2. ...
  1. select cal::duration_normalize_days(
  2. <cal::date_duration>'1312 days');
  1. {<cal::date_duration>'P3Y7M22D'}

This function is a form of approximation and does not preserve the exact duration.

This is often used together with cal::duration_normalize_hours().