SQLite Introduces the UNIXEPOCH() Function

SQLite 3.38.0 was released on 2nd February 2022. With it came some enhancement to the date and time functions.

One of those enhancements is the addition of the UNIXEPOCH() function. This function returns a unix timestamp. That is, the number of seconds since 1970-01-01 00:00:00 UTC.

Example

Here’s a quick example to demonstrate:

SELECT UNIXEPOCH();

Result:

1646610077

So, when I ran that example, there had been 1646610077 seconds since 1970-01-01 00:00:00 UTC.

The function always returns an integer, even if the input time-value has millisecond precision.

This is the equivalent of doing the following:

SELECT STRFTIME('%s');

Result:

1646610181

Obviously, there were a few seconds between running the above two examples, so they aren’t quite the same. But we can see that they’re very similar.

Here’s the result when they’re run at the same time:

SELECT 
    UNIXEPOCH(),
    STRFTIME('%s');

Result:

UNIXEPOCH()  STRFTIME('%s')
-----------  --------------
1646610290   1646610290    

Exactly the same.