Comments - FROM_UNIXTIME

5 years, 5 months ago Dean Trower

It is important to note that if you have a table with a TIMESTAMP column:

CREATE TABLE t (ts TIMESTAMP);

and you insert a value like so:

INSERT INTO t (ts) VALUES (FROM_UNIXTIME(1522510200));

The value will undergo a roundtrip conversion, from a number to a date, and then back again, before insertion. Due to daylight savings time and/or leap-seconds, the inserted value may not be what you started with!

SELECT UNIX_TIMESTAMP(ts) FROM t;

For example, in the Australian Eastern Daylight Time timezone, the result of the above query is 1522513800 rather than 1522510200, as the latter value falls within the hour that gets repeated at the end of daylight savings.

I'm not aware of any way to insert a "raw" UNIX time-value, without conversion to a date and back again. There certainly OUGHT to be a method provided to do this however!

 
5 years, 5 months ago Dean Trower

UPDATE:

There is a method. You can force insertion of a "raw" UNIX time-value like this:

SET timestamp := 1522510200;
INSERT INTO t (ts) VALUES (NOW());
SET timestamp := DEFAULT;

It's rather clumsy, though!

 
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.