Date / time functions
Note
Timestream for LiveAnalytics does not support negative time values. Any operation resulting in negative time results in error.
Timestream for LiveAnalytics uses UTC timezone for date and time. Timestream supports the following functions for date and time.
General and conversion
Timestream for LiveAnalytics supports the following general and conversion functions for date and time.
Function | Output data type | Description |
---|---|---|
current_date |
date |
Returns current date in UTC. No parentheses used.
Example result: NoteThis is also a reserved keyword. For a list of reserved keywords, see Reserved keywords. |
current_time |
time |
Returns current time in UTC. No parentheses used.
Example result: NoteThis is also a reserved keyword. For a list of reserved keywords, see Reserved keywords. |
current_timestamp or now() |
timestamp |
Returns current timestamp in UTC.
Example result: NoteThis is also a reserved keyword. For a list of reserved keywords, see Reserved keywords. |
current_timezone() |
varchar The value will be 'UTC.' |
Timestream uses UTC timezone for date and time.
Example result: |
date(varchar(x)), date(timestamp) |
date |
Example result: |
last_day_of_month(timestamp), last_day_of_month(date) |
date |
Example result: |
from_iso8601_timestamp(string) |
timestamp |
Parses the ISO 8601 timestamp into internal timestamp format.
Example result: |
from_iso8601_date(string) |
date |
Parses the ISO 8601 date string into internal timestamp format for UTC 00:00:00 of the specified date.
Example result: |
to_iso8601(timestamp), to_iso8601(date) |
varchar |
Returns an ISO 8601 formatted string for the input.
Example result: |
from_milliseconds(bigint) |
timestamp |
Example result: |
from_nanoseconds(bigint) |
timestamp |
Example result: |
from_unixtime(double) |
timestamp |
Returns a timestamp which corresponds to the provided unixtime.
Example result: |
localtime |
time |
Returns current time in UTC. No parentheses used.
Example result: NoteThis is also a reserved keyword. For a list of reserved keywords, see Reserved keywords. |
localtimestamp |
timestamp |
Returns current timestamp in UTC. No parentheses used.
Example result: NoteThis is also a reserved keyword. For a list of reserved keywords, see Reserved keywords. |
to_milliseconds(interval day to second), to_milliseconds(timestamp) |
bigint |
Example result:
Example result: |
to_nanoseconds(interval day to second), to_nanoseconds(timestamp) |
bigint |
Example result:
Example result: |
to_unixtime(timestamp) |
double |
Returns unixtime for the provided timestamp.
Example result: |
date_trunc(unit, timestamp) |
timestamp |
Returns the timestamp truncated to unit, where unit is one of [second, minute, hour, day, week, month, quarter, or year].
Example result: |
Interval and duration
Timestream for LiveAnalytics supports the following interval and duration functions for date and time.
Function | Output data type | Description |
---|---|---|
date_add(unit, bigint, date), date_add(unit, bigint, time), date_add(varchar(x), bigint, timestamp) |
timestamp |
Adds a bigint of units, where unit is one of [second, minute, hour, day, week, month, quarter, or year].
Example result: |
date_diff(unit, date, date) , date_diff(unit, time, time) , date_diff(unit, timestamp, timestamp) |
bigint |
Returns a difference, where unit is one of [second, minute, hour, day, week, month, quarter, or year].
Example result: |
parse_duration(string) |
interval |
Parses the input string to return an
Example result:
Example result: |
bin(timestamp, interval) |
timestamp |
Rounds down the The meaning of this return value may not be obvious. It is calculated using integer arithmetic first by dividing the timestamp integer by the interval integer and then by multiplying the result by the interval integer. Keeping in mind that a timestamp specifies a UTC point in time as a number of fractions of a second elapsed since the POSIX epoch (January 1, 1970), the return value will seldom align with calendar units. For example, if you specify an interval of 30 days, all the days since the epoch are divided into 30-day increments, and the start of the most recent 30-day increment is returned, which has no relationship to calendar months. Here are some examples:
|
ago(interval) |
timestamp |
Returns the value corresponding to current_timestamp
Example result: |
interval literals such as 1h, 1d, and 30m |
interval |
Interval literals are a convenience for parse_duration(string).
For example, |
Some interval literals act as shorthand for parse_duration. For example,
parse_duration('1day')
, 1day
,
parse_duration('1d')
, and 1d
each return 1
00:00:00.000000000
where the type is interval day to second
.
Space is allowed in the format provided to parse_duration
. For example
parse_duration('1day')
also returns 00:00:00.000000000
.
But 1 day
is not an interval literal.
The units related to interval day to second
are ns, nanosecond, us,
microsecond, ms, millisecond, s, second, m, minute, h, hour, d, and day.
There is also interval year to month
. The units related to interval year
to month are y, year, and month. For example, SELECT 1year
returns
1-0
. SELECT 12month
also returns 1-0
.
SELECT 8month
returns 0-8
.
Although the unit of quarter
is also available for some functions such as
date_trunc
and date_add
, quarter
is not
available as part of an interval literal.
Formatting and parsing
Timestream for LiveAnalytics supports the following formatting and parsing functions for date and time.
Function | Output data type | Description |
---|---|---|
date_format(timestamp, varchar(x)) |
varchar |
For more information about the format specifiers used by this
function, see https://trino.io/docs/current/functions/datetime.html#mysql-date-functions
Example result: |
date_parse(varchar(x), varchar(y)) |
timestamp |
For more information about the format specifiers used by this
function, see https://trino.io/docs/current/functions/datetime.html#mysql-date-functions
Example result: |
format_datetime(timestamp, varchar(x)) |
varchar |
For more information about the format string used by this
function, see http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html
Example result: |
parse_datetime(varchar(x), varchar(y)) |
timestamp |
For more information about the format string used by this
function, see http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html
Example result: |
Extraction
Timestream for LiveAnalytics supports the following extraction functions for date and time. The extract function is the basis for the remaining convenience functions.
Function | Output data type | Description |
---|---|---|
extract |
bigint |
Extracts a field from a timestamp, where field is one of [YEAR, QUARTER, MONTH, WEEK, DAY, DAY_OF_MONTH, DAY_OF_WEEK, DOW, DAY_OF_YEAR, DOY, YEAR_OF_WEEK, YOW, HOUR, MINUTE, or SECOND].
Example result: |
day(timestamp), day(date), day(interval day to second) |
bigint |
Example result: |
day_of_month(timestamp), day_of_month(date), day_of_month(interval day to second) |
bigint |
Example result: |
day_of_week(timestamp), day_of_week(date) |
bigint |
Example result: |
day_of_year(timestamp), day_of_year(date) |
bigint |
Example result: |
dow(timestamp), dow(date) |
bigint |
Alias for day_of_week |
doy(timestamp), doy(date) |
bigint |
Alias for day_of_year |
hour(timestamp), hour(time), hour(interval day to second) |
bigint |
Example result: |
millisecond(timestamp), millisecond(time), millisecond(interval day to second) |
bigint |
Example result: |
minute(timestamp), minute(time), minute(interval day to second) |
bigint |
Example result: |
month(timestamp), month(date), month(interval year to month) |
bigint |
Example result: |
nanosecond(timestamp), nanosecond(time), nanosecond(interval day to second) |
bigint |
Example result: |
quarter(timestamp), quarter(date) |
bigint |
Example result: |
second(timestamp), second(time), second(interval day to second) |
bigint |
Example result: |
week(timestamp), week(date) |
bigint |
Example result: |
week_of_year(timestamp), week_of_year(date) |
bigint |
Alias for week |
year(timestamp), year(date), year(interval year to month) |
bigint |
Example result: |
year_of_week(timestamp), year_of_week(date) |
bigint |
Example result: |
yow(timestamp), yow(date) |
bigint |
Alias for year_of_week |