Mysql – Generate random timestamp taking into account shift to daylight saving time

mariadbMySQLtimestamp

I want to generate random big data in MariaDB.
One of this columns is random timestamp which I generate like this:

select (current_timestamp 
        - interval (rand() * 60 * 60 * 24 * 365 * 10) second
       ) as record_time;

Since the end of March there is a shift to daylight saving time / summer time, so in one day there is no time from 02:00 to 03:00. And when I inserting this data I get error like:
Incorrect datetime value: '2016-03-27 02:20:24.227322' for column db.tab1.record_time.

How to modify this part of insert script to generate correct random timestamp values while accepting daylight saving time?

Link to MariaDB info about timezones

Best Answer

You can create timestamp in SYSTEM timezone with:

FROM_UNIXTIME(timestamp_in_seconds) -- This is a number, not a datetime

To convert back to a timestamp you can use UNIX_TIMESTAMP function.

This sample demonstrate how to generate random datetime based on starting point. It give you only correct timestamps:

select FROM_UNIXTIME(UNIX_TIMESTAMP('2016-03-27 00:30:00') + rand() * 10000);