In MariaDB, CURTIME()
is a built-in date and time function that returns the current time.
The time is returned in either 'HH:MM:SS'
or HHMMSS.uuuuuu
format, depending on whether the function is being used in a string or numeric context.
You also have the option of specifying the microsecond precision.
Syntax
The syntax goes like this:
CURTIME([precision])
Where precision
is an optional argument that specifies the microsecond precision.
It also has the following synonyms:
CURRENT_TIME
CURRENT_TIME([precision])
Example
Here’s an example:
SELECT CURTIME();
Result:
+-----------+ | CURTIME() | +-----------+ | 09:30:12 | +-----------+
Numeric Context
When CURTIME()
is used in a numeric context, the resulting time is in HHMMSS.uuuuuu
format.
Example:
SELECT
CURTIME(),
CURTIME() + 0;
Result:
+-----------+---------------+ | CURTIME() | CURTIME() + 0 | +-----------+---------------+ | 09:31:39 | 93139 | +-----------+---------------+
Precision
Here’s an example of specifying microsecond precision:
SELECT
CURTIME(3),
CURTIME(3) + 0;
Result:
+--------------+----------------+ | CURTIME(3) | CURTIME(3) + 0 | +--------------+----------------+ | 09:32:44.678 | 93244.678 | +--------------+----------------+
The maximum value for the microsecond precision is 6. Here’s what happens when a higher number is passed for the precision:
SELECT CURTIME(12);
Result:
ERROR 1426 (42000): Too big precision 12 specified for 'curtime'. Maximum is 6
Synonyms
CURRENT_TIME
and CURRENT_TIME()
are synonyms for CURTIME()
. Therefore, we can use either one to get the same result.
Example:
SELECT
CURTIME(),
CURRENT_TIME,
CURRENT_TIME();
Result:
+-----------+--------------+----------------+ | CURTIME() | CURRENT_TIME | CURRENT_TIME() | +-----------+--------------+----------------+ | 09:33:32 | 09:33:32 | 09:33:32 | +-----------+--------------+----------------+
Adding to the Current Time
There are many ways to perform arithmetic on time values in MariaDB. You can use such methods to add a number of hours, minutes, seconds, etc.
Here’s an example of using the addition operator (+
) to add 2 hours to the time:
SELECT
CURTIME(),
CURTIME() + INTERVAL 2 HOUR;
Result:
+-----------+-----------------------------+ | CURTIME() | CURTIME() + INTERVAL 2 HOUR | +-----------+-----------------------------+ | 09:35:03 | 11:35:03 | +-----------+-----------------------------+
Also see functions like ADDTIME()
for an alternative way to add to the current time.
Subtracting from the Current Time
Here’s an example of using the subtraction operator (-
) to subtract 2 hours from the current time:
SELECT
CURTIME(),
CURTIME() - INTERVAL 2 HOUR;
Result:
+-----------+-----------------------------+ | CURTIME() | CURTIME() - INTERVAL 2 HOUR | +-----------+-----------------------------+ | 09:35:41 | 07:35:41 | +-----------+-----------------------------+
See functions like SUBTIME()
for an alternative way to subtract from the current time.