Time interval addition. | |
Time interval and date/time subtraction. | |
Comparison operators. | |
Render a date/time value to a string. | |
Create a datetime value. | |
Create a cal::local_datetime value. | |
Create a cal::local_date value. | |
Create a cal::local_time value. | |
Create a duration value. | |
Create a cal::relative_duration value. | |
Extract a specific element of input datetime by name. | |
Extract a specific element of input time by name. | |
Extract a specific element of input date by name. | |
Truncate the input datetime to a particular precision. | |
Truncate the input duration to a particular precision. | |
Return the current server date and time. | |
Return the date and time of the start of the current transaction. | |
Return the date and time of the start of the current statement. |
Time interval addition.
This operator is commutative.
db>
SELECT <cal::local_time>'22:00' + <duration>'1 hour';
{<cal::local_time>'23:00:00'}
db>
SELECT <duration>'1 hour' + <cal::local_time>'22:00';
{<cal::local_time>'23:00:00'}
db>
SELECT <duration>'1 hour' + <duration>'2 hours';
{10800s}
Time interval and date/time subtraction.
db> ...
SELECT <datetime>'2019-01-01T01:02:03+00' -
<duration>'24 hours';
{<datetime>'2018-12-31T01:02:03Z'}
db> ...
SELECT <datetime>'2019-01-01T01:02:03+00' -
<datetime>'2019-02-01T01:02:03+00';
{-2678400s}
db> ...
SELECT <duration>'1 hour' -
<duration>'2 hours';
{-3600s}
It is an error to subtract a date/time object from a time interval:
db> ...
SELECT <duration>'1 day' -
<datetime>'2019-01-01T01:02:03+00';
QueryError: operator '-' cannot be applied to operands ...
It is also an error to subtract timezone-aware std::datetime
to or from cal::local_datetime
:
db> ...
SELECT <datetime>'2019-01-01T01:02:03+00' -
<cal::local_datetime>'2019-02-01T01:02:03';
QueryError: operator '-' cannot be applied to operands ...
Return the current server date and time.
db>
SELECT datetime_current();
{<datetime>'2018-05-14T20:07:11.755827Z'}
Return the date and time of the start of the current transaction.
Return the date and time of the start of the current statement.
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
db> ... ...
SELECT datetime_get(
<datetime>'2018-05-07T15:01:22.306916+00',
'epochseconds');
{1525705282.306916}
db> ... ...
SELECT datetime_get(
<datetime>'2018-05-07T15:01:22.306916+00',
'year');
{2018}
db> ... ...
SELECT datetime_get(
<datetime>'2018-05-07T15:01:22.306916+00',
'quarter');
{2}
db> ... ...
SELECT datetime_get(
<datetime>'2018-05-07T15:01:22.306916+00',
'doy');
{127}
db> ... ...
SELECT datetime_get(
<datetime>'2018-05-07T15:01:22.306916+00',
'hour');
{15}
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()
.
db> ...
SELECT cal::time_get(
<cal::local_time>'15:01:22.306916', 'minutes');
{1}
db> ...
SELECT cal::time_get(
<cal::local_time>'15:01:22.306916', 'milliseconds');
{22306.916}
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
db> ...
SELECT cal::date_get(
<cal::local_date>'2018-05-07', 'century');
{21}
db> ...
SELECT cal::date_get(
<cal::local_date>'2018-05-07', 'year');
{2018}
db> ...
SELECT cal::date_get(
<cal::local_date>'2018-05-07', 'month');
{5}
db> ...
SELECT cal::date_get(
<cal::local_date>'2018-05-07', 'doy');
{127}
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'
db> ...
SELECT datetime_truncate(
<datetime>'2018-05-07T15:01:22.306916+00', 'years');
{<datetime>'2018-01-01T00:00:00Z'}
db> ...
SELECT datetime_truncate(
<datetime>'2018-05-07T15:01:22.306916+00', 'quarters');
{<datetime>'2018-04-01T00:00:00Z'}
db> ...
SELECT datetime_truncate(
<datetime>'2018-05-07T15:01:22.306916+00', 'days');
{<datetime>'2018-05-07T00:00:00Z'}
db> ...
SELECT datetime_truncate(
<datetime>'2018-05-07T15:01:22.306916+00', 'hours');
{<datetime>'2018-05-07T15:00:00Z'}
Truncate the input duration to a particular precision.
The valid unit values are:
- 'microseconds'
- 'milliseconds'
- 'seconds'
- 'minutes'
- 'hours'
db> ...
SELECT duration_truncate(
<duration>'15:01:22', 'hours');
{54000s}
db> ...
SELECT duration_truncate(
<duration>'15:01:22.306916', 'minutes');
{54060s}
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.
db>
SELECT to_datetime('2018-05-07T15:01:22.306916+00');
{<datetime>'2018-05-07T15:01:22.306916Z'}
db>
SELECT to_datetime('2018-05-07T15:01:22+00');
{<datetime>'2018-05-07T15:01:22Z'}
db> ...
SELECT to_datetime('May 7th, 2018 15:01:22 +00',
'Mon DDth, YYYY HH24:MI:SS TZH');
{<datetime>'2018-05-07T15:01:22Z'}
Alternatively, the datetime
value can be constructed
from a cal::local_datetime
value:
db> ...
SELECT to_datetime(
<cal::local_datetime>'2019-01-01T01:02:03', 'HKT');
{<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
db> ...
SELECT to_datetime(
2018, 5, 7, 15, 1, 22.306916, 'UTC');
{<datetime>'2018-05-07T15:01:22.306916000Z'}
Finally, it is also possible to convert a Unix timestamp to a
datetime
db>
SELECT to_datetime(1590595184.584);
{<datetime>'2020-05-27T15:59:44.584000000Z'}
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.
db>
SELECT cal::to_local_datetime('2018-05-07T15:01:22.306916');
{<cal::local_datetime>'2018-05-07T15:01:22.306916'}
db> ...
SELECT cal::to_local_datetime('May 7th, 2018 15:01:22',
'Mon DDth, YYYY HH24:MI:SS');
{<cal::local_datetime>'2018-05-07T15:01:22'}
db> ...
SELECT cal::to_local_datetime(
2018, 5, 7, 15, 1, 22.306916);
{<cal::local_datetime>'2018-05-07T15:01:22.306916'}
A timezone-aware datetime
type can be converted
to local datetime in the specified timezone:
db> ... ...
SELECT cal::to_local_datetime(
<datetime>'2018-12-31T22:00:00+08',
'US/Central');
{<cal::local_datetime>'2018-12-31T08:00:00'}
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.
db>
SELECT cal::to_local_date('2018-05-07');
{<cal::local_date>'2018-05-07'}
db>
SELECT cal::to_local_date('May 7th, 2018', 'Mon DDth, YYYY');
{<cal::local_date>'2018-05-07'}
db>
SELECT cal::to_local_date(2018, 5, 7);
{<cal::local_date>'2018-05-07'}
A timezone-aware datetime
type can be converted
to local date in the specified timezone:
db> ... ...
SELECT cal::to_local_date(
<datetime>'2018-12-31T22:00:00+08',
'US/Central');
{<cal::local_date>'2019-01-01'}
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.
db>
SELECT cal::to_local_time('15:01:22.306916');
{<cal::local_time>'15:01:22.306916'}
db>
SELECT cal::to_local_time('03:01:22pm', 'HH:MI:SSam');
{<cal::local_time>'15:01:22'}
db>
SELECT cal::to_local_time(15, 1, 22.306916);
{<cal::local_time>'15:01:22.306916'}
A timezone-aware datetime
type can be converted
to local date in the specified timezone:
db> ... ...
SELECT cal::to_local_time(
<datetime>'2018-12-31T22:00:00+08',
'US/Pacific');
{<cal::local_time>'06:00:00'}
Create a duration
value.
This function uses NAMED ONLY
arguments to create a
duration
value. The available duration fields are:
hours, minutes, seconds, microseconds.
db> ... ...
SELECT to_duration(hours := 1,
minutes := 20,
seconds := 45);
{4845s}
db>
SELECT to_duration(seconds := 4845);
{4845s}
Return duration as total number of seconds in interval.
db>
SELECT duration_to_seconds(<duration>'1 hour');
{3600.000000n}
db>
SELECT duration_to_seconds(<duration>'10 second 123 ms');
{10.123000n}
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.
db>
SELECT cal::to_relative_duration(years := 5, minutes := 1);
{P5YT1S}
db>
SELECT cal::to_relative_duration(months := 3, days := 27);
{P3M27D}