Lindorm time functions can convert data between a specified type and the TIMESTAMP type, and calculate the difference in days between two timestamps. This topic describes the time functions supported by Lindorm and how to use the time functions.
Applicable engines and versions
The time functions described in this topic are applicable only to LindormTable.
The LindormTable version is 2.7.8 or later. For information about how to view or update the minor version of LindormTable, see Release notes of LindormTable and Upgrade the minor engine version of a Lindorm instance.
The version of Lindorm SQL must be 2.8.7.0 or later. For more information about how to view the version of Lindorm SQL, see SQL versions.
Supported time functions
The following table describes the time functions supported by Lindorm.
Function | Description |
Formats the timestamp in the time zone of the current session. | |
Converts data of the Number types, such as BIGINT, INTEGER, SMALLINT, and TINYINT, into the timestamp in the time zone of the current session. If you specify two parameters, the format of the output is based on the second parameter. | |
Calculates the difference in days between two timestamps in the time zone of the current session. | |
Converts a timestamp or timestamp string to a UNIX timestamp. Unit: seconds. |
DATE_FORMAT
Formats the timestamp in the time zone of the current session. For more information about how to view and specify the time zone of a session, see Specify a time zone for a LindormTable connection
Syntax
DATE_FORMAT(TIMESTAMP ts, STRING format)
Parameters
Parameter | Required | Description |
ts | Yes | The timestamp that you want to format. |
format | Yes | The format specifier, such as %<Reserved letter> or %%. Refer to the following table for the supported format specifiers. |
The following table describes the supported format specifiers, output, and output description.
Format specifier | Output | Description |
%a | Sun to Sat | The day in a week, which is abbreviated. |
%b | Jan to Dec | The month, which is abbreviated. |
%c | 0 to 12 | The month, which is a numeric value. |
%D | 1st, 2nd, 3rd... | The day of the month, which has an ordinal suffix. |
%d | 00 to 31 | The day of the month, which is a two-digit numeric value. |
%e | 00 to 31 | The day of the month, which is a two-digit numeric value. |
%f | 000000 to 999999 | The number of microseconds. |
%H | 00 to 23 | The hour in a 24-hour clock, which is a two-digit numeric value. |
%h | 01 to 12 | The hour in a 12-hour clock, which is a two-digit numeric value. |
%I | 01 to 12 | The hour in a 12-hour clock, which is a two-digit numeric value. |
%i | 00 to 59 | The minute, which is a two-digit numeric value. |
%j | 001 to 366 | The day of the year, which is a three-digit numeric value. |
%k | 0 to 23 | The hour in a 24-hour clock, which is a numeric value. |
%l | 1~ to 12 | The hour in a 12-hour clock, which is a numeric value. |
%M | January to December | The month, which contains the full name. |
%m | 01 to 12 | The month, which is a two-digit numeric value. |
%p | AM or PM | Morning or afternoon. |
%r | hh:mm:ss AM/PM | The 12-hour clock in the hh:mm:ss format, which has the morning (AM) or afternoon (PM) information. |
%S | 00 to 59 | The second of the time clock, which is a two-digit numeric value. |
%s | 00 to 59 | The second of the time clock, which is a two-digit numeric value. |
%T | hh:mm:ss | The 24-hour clock in the hh:mm:ss format. |
%W | Sunday to Saturday | The day in a week, which is in full name. |
%w | 0 to 6 | The day in a week, which is a numeric value. For example, |
%Y | A four-digit numeric value | The year. Example: |
%y | A two-digit numeric value based on the last two digits of the year. | The year. For example, the value |
%% | % | The character %. |
%x | x | A letter other than reserved letters that are contained in the preceding format specifiers. For example, the letter U is not a reserved letter. The format specifier %U represents the letter U. |
If you do not configure a format specifier, all characters that do not start with %
are returned in the original format.
Examples
Formats the timestamp 2024-01-01 17:20:35
in the time zone of the current session as at 17:20:35 on Jan 1st, 2024
.
SELECT DATE_FORMAT('2024-01-01 17:20:35', 'at %T on %b %D, %Y');
Sample output:
+------------------------------+
| EXPR$0 |
+------------------------------+
| at 17:20:35 on JAN 1st, 2024 |
+------------------------------+
The returned result is a string whose format is based on the format specifier. The string represents the timestamp in the time zone of the current session.
FROM_UNIXTIME
Converts data of the Number types, such as BIGINT, INTEGER, SMALLINT, and TINYINT, into the timestamp in the time zone of the current session. If you specify two parameters, the format of the output is based on the second parameter.
The Number-type value is in seconds. Up to three valid digits are retained.
Syntax
Converts Number-type data to a timestamp in the time zone of the current session.
FROM_UNIXTIME (Number seconds)
Converts Number-type data to a timestamp in the time zone of the current session, and then formats the timestamp.
FROM_UNIXTIME (Number seconds, STRING format)
Parameters
Parameter | Required | Description |
seconds | Yes | The UNIX timestamp. Unit: seconds. Up to three decimal places are retained. The value of the seconds parameter indicates the number of seconds that have elapsed since |
format | No | The format specifier. For more information, see Parameters. |
Examples
Example 1:
Converts the UNIX timestamp
10.1578
into the timestamp in the time zone of the current session.SELECT FROM_UNIXTIME(10.1578);
Sample output:
+-------------------------+ | EXPR$0 | +-------------------------+ | 1970-01-01 08:00:10.158 | +-------------------------+
Changes the time_zone field to
+03:00
and converts the UNIX timestamp10.1578
into the timestamp in the new time zone.-- Change the time_zone field to +03:00. SET @@time_zone='+03:00'; -- Convert to the timestamp in the new time zone. SELECT FROM_UNIXTIME(10.1578);
NoteOnly the MySQL protocol allows you to change the time zone of a connection. For more information, see Use the MySQL protocol to develop applications (recommended) and Use a MySQL client to connect to and use LindormTable.
Sample output:
+-------------------------+ | EXPR$0 | +-------------------------+ | 1970-01-01 03:00:10.158 | +-------------------------+
Example 2:
Converts the UNIX timestamp
10.1578
into the timestamp in the time zone of the current session and then formats the timestamp.SELECT FROM_UNIXTIME(10.1578, 'at %T on %b %D, %Y');
Sample output:
+------------------------------+ | EXPR$0 | +------------------------------+ | at 08:00:10 on JAN 1st, 1970 | +------------------------------+
Changes the time_zone field to
+03:00
, converts the UNIX timestamp10.1578
into the timestamp in the new time zone, and then formats the timestamp.-- Change the time_zone field to +03:00. SET @@time_zone='+03:00'; -- Convert to the timestamp in the new time zone. SELECT FROM_UNIXTIME(10.1578, 'at %T on %b %D, %Y');
Sample output:
+------------------------------+ | EXPR$0 | +------------------------------+ | at 03:00:10 on JAN 1st, 1970 | +------------------------------+
DATEDIFF
Calculates the difference in days between two timestamps in the time zone of the current session.
To calculate the difference, you can subtract the second timestamp value from the first timestamp value. The following section describes the result:
If the result is positive, the first timestamp represents a later date.
If the result is negative, the first timestamp represents an earlier date.
If the result is
0
, the two timestamps fall on the same day.
The system automatically compares only the date values, regardless of the hour, minute and second information.
Syntax
DATEDIFF(TIMESTAMP ts1, TIMESTAMP ts2)
Parameters
Parameter | Required | Description |
ts1 | Yes | The first timestamp. |
ts2 | Yes | The second timestamp. |
Examples
Example 1:
SELECT DATEDIFF('2024-01-01 15:30:00', '2024-01-02 00:59:59');
Sample output:
+--------+ | EXPR$0 | +--------+ | -1 | +--------+
Example 2:
SELECT DATEDIFF('2024-01-05 15:30:00', '2024-01-02 00:59:59');
Sample output:
+--------+ | EXPR$0 | +--------+ | 3 | +--------+
Example 3:
SELECT DATEDIFF('2024-01-02 15:30:00', '2024-01-02 00:59:59');
Sample output:
+--------+ | EXPR$0 | +--------+ | 0 | +--------+
UNIX_TIMESTAMP
Converts a timestamp or timestamp string to a UNIX timestamp accurate to seconds and returns data of DOUBLE type in the specified column.
When the ts parameter indicates a timestamp, three decimal places are retained in the returned value. When the ts parameter indicates a timestamp string, the timestamp string represents the timestamp in the time zone of the current session and six decimal places are retained in the returned value.
The version of LindormTable must be 2.8.0 or later and the version of Lindorm SQL must be 2.9.0.0 or later. You can view the versions of LindormTable and Lindorm SQL and update the minor version of LindormTable to 2.8.0 or later in the Lindorm console.
The
UNIX_TIMESTAMP
function can be used in the projection and the WHERE clause of a SELECT statement and can also be used in the VALUES clause of an UPSERT statement.
Syntax
UNIX_TIMESTAMP (ts)
Parameters
Parameter | Required | Description |
ts | Yes | The timestamp in the time zone of the current session. The data type must be TIMESTAMP or VARCHAR. |
Examples
Create a table named t1
. Sample code:
-- Create a table named t1.
CREATE TABLE t1(p1 INT, c1 TIMESTAMP, c2 VARCHAR, c3 DOUBLE, PRIMARY KEY(p1));
-- Insert data into the table.
UPSERT INTO t1(p1, c1, c2, c3) VALUES
(1, '2025-01-01 08:30:00', '2025-01-01 08:30:00', '12.24'),
(2, '1970-01-01 08:00:00.123', '1970-01-01 08:00:00.123', '10.32'),
(3, '1970-01-01 08:00:10.13579', '1970-01-01 08:00:10.13579', '10.12'),
(4, '1970-01-01 08:00:10.123456789', '1970-01-01 08:00:10.123456789', '17.33');
Example 1: Query the UNIX timestamp corresponding to the timestamp or timestamp string.
SELECT p1, UNIX_TIMESTAMP(c1), UNIX_TIMESTAMP(c2) FROM t1;
Sample output:
+----+------------+------------+ | p1 | EXPR$1 | EXPR$2 | +----+------------+------------+ | 1 | 1735691400 | 1735691400 | | 2 | 0.123 | 0.123 | | 3 | 10.135 | 10.13579 | | 4 | 10.123 | 10.123457 | +----+------------+------------+
Example 2: Calculate the number of records that meet the
UNIX_TIMESTAMP(c1) > 10
condition.SELECT COUNT(*) FROM t1 WHERE UNIX_TIMESTAMP(c1) > 10;
Sample output:
+--------+ | EXPR$0 | +--------+ | 3 | +--------+
Example 3: Write the result of the
UNIX_TIMESTAMP
function to thec3
column whose data type is DOUBLE.UPSERT INTO t1(p1, c3) VALUES(1, UNIX_TIMESTAMP('1970-01-01 08:00:00.123'));
Verify the result
You can execute the
SELECT * FROM t1
statement to check whether the write operation was successful.