MariaDB CURRENT_TIME() Explained

In MariaDB, CURRENT_TIME and CURRENT_TIME() are synonyms for CURTIME().

The CURTIME() function 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.

Syntax

CURRENT_TIME() can be used in the following ways:

CURRENT_TIME
CURRENT_TIME([precision])

Where precision is the microsecond precision.

You can alternatively call CURTIME() like this:

CURTIME([precision])

Example

Here’s an example:

SELECT 
    CURRENT_TIME,
    CURRENT_TIME(),
    CURTIME();

Result:

+--------------+----------------+-----------+
| CURRENT_TIME | CURRENT_TIME() | CURTIME() |
+--------------+----------------+-----------+
| 09:59:13     | 09:59:13       | 09:59:13  |
+--------------+----------------+-----------+

We can see that all three return the same result.

Numeric Context

When used in a numeric context, the resulting time is in HHMMSS.uuuuuu format.

Example:

SELECT 
    CURRENT_TIME,
    CURRENT_TIME + 0,
    CURRENT_TIME() + 0;

Result:

+--------------+------------------+--------------------+
| CURRENT_TIME | CURRENT_TIME + 0 | CURRENT_TIME() + 0 |
+--------------+------------------+--------------------+
| 10:00:29     |           100029 |             100029 |
+--------------+------------------+--------------------+

Precision

When used with the CURRENT_TIME([precision]) syntax, you can specify the microsecond precision for the result.

Example:

SELECT 
    CURRENT_TIME(3),
    CURRENT_TIME(3) + 0;

Result:

+-----------------+---------------------+
| CURRENT_TIME(3) | CURRENT_TIME(3) + 0 |
+-----------------+---------------------+
| 10:02:00.088    |          100200.088 |
+-----------------+---------------------+

The maximum value for the microsecond precision is 6. Here’s what happens when a higher number is passed for the precision:

SELECT CURRENT_TIME(12);

Result:

ERROR 1426 (42000): Too big precision 12 specified for 'curtime'. Maximum is 6

Adding to the Current Time

There are many ways to perform arithmetic on dates and times in MariaDB. You can use such methods to add a number of days, weeks, hours, minutes, etc.

Here’s an example of using the addition operator (+) to add 2 hours to the time:

SELECT 
    CURRENT_TIME,
    CURRENT_TIME + INTERVAL 2 HOUR;

Result:

+--------------+--------------------------------+
| CURRENT_TIME | CURRENT_TIME + INTERVAL 2 HOUR |
+--------------+--------------------------------+
| 10:04:19     | 12:04:19                       |
+--------------+--------------------------------+

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 |
+-----------+-----------------------------+
| 10:05:14  | 08:05:14                    |
+-----------+-----------------------------+

See functions like SUBTIME() for an alternative way to subtract from the current time.