Bug #12654 64-bit unix timestamp is not supported in MySQL functions
Submitted: 18 Aug 2005 17:38 Modified: 27 Aug 2021 17:08
Reporter: Dmitry Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: DML Severity:S4 (Feature request)
Version:4.1, 5.0, 5.1, 5.5 OS:Any
Assigned to: CPU Architecture:Any

[18 Aug 2005 17:38] Dmitry
Description:
// mysql-standard-4.1.13a-apple-darwin8.2.0-powerpc-64bit

both UNIX_TIMESTAMP() and FROM_UNIXTIME() don't work with unix timestamp after year of 2037

OS itself returns correct values in functions mktime() and gmtime() if compiled with "-m64" flag

How to repeat:
mysql> select unix_timestamp('2038-01-01');
+------------------------------+
| unix_timestamp('2038-01-01') |
+------------------------------+
|                            0 |
+------------------------------+
1 row in set (0.00 sec)

mysql> select from_unixtime(2548990800);
+---------------------------+
| from_unixtime(2548990800) |
+---------------------------+
| NULL                      |
+---------------------------+
1 row in set (0.02 sec)

Suggested fix:
probably because of
sql/mysql_priv.h:

/* Time handling defaults */
#define TIMESTAMP_MAX_YEAR 2038
#define YY_PART_YEAR       70
#define TIMESTAMP_MIN_YEAR (1900 + YY_PART_YEAR - 1)
#define TIMESTAMP_MAX_VALUE 2145916799
#define TIMESTAMP_MIN_VALUE 1
#define PRECISION_FOR_DOUBLE 53
#define PRECISION_FOR_FLOAT  24
[18 Oct 2005 23:50] Jim Winstead
Support for 64-bit TIMESTAMP data (or similar) will be part of a later release. Whether the underlying operating system uses a 64-bit time_t type is not really material.
[2 Apr 2007 11:47] Valeriy Kravchuk
Bug #24625 was marked as a duplicate of this one.
[23 Jul 2014 23:04] Jonathon Lamon
~9 years since opened and less than 25 years away from this being an issue, is there any plan on the horizon for fixing it?
[27 Oct 2015 12:31] Daniël van Eeden
This can break things before 2038 if some expiration is stored as a timestamp.

https://en.wikipedia.org/wiki/Year_2038_problem
[4 Nov 2015 8:22] Zhenjiang Liu
still not fixed. 
some Country has start to prepare the Olympic Games 2028
[22 Mar 2017 9:57] Daniël van Eeden
https://github.com/mysql/mysql-server/pull/130

mysql> select unix_timestamp('2038-01-01');
+------------------------------+
| unix_timestamp('2038-01-01') |
+------------------------------+
|                   2145913200 |
+------------------------------+
1 row in set (0.00 sec)

mysql> select unix_timestamp('2039-01-01');
+------------------------------+
| unix_timestamp('2039-01-01') |
+------------------------------+
|                   2177449200 |
+------------------------------+
1 row in set (0.00 sec)

mysql> select from_unixtime(2548990800);
+---------------------------+
| from_unixtime(2548990800) |
+---------------------------+
| 2050-10-10 07:00:00       |
+---------------------------+
1 row in set (0.00 sec)
[27 Mar 2017 15:25] OCA Admin
Contribution submitted via Github - Allow dates beyond 2038 
(*) Contribution by Daniël van Eeden (Github dveeden, mysql-server/pull/130#issuecomment-288409625): I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: git_patch_111968598.txt (text/plain), 2.39 KiB.

[28 Mar 2017 14:38] Ståle Deraas
Posted by developer:
 
Hi Daniel,

We have some concerns how this will interact with our timezone code. And we are also concered about any 32-bit platform support, so it will take some time to evaluate this contribution properly.
[16 Apr 2018 10:18] Fabrizio Carimati
I write two simple functions as temporary workaround.
Anyone see a drawback not related to performance?

`
CREATE OR REPLACE FUNCTION from_unixtime_fixed (v BIGINT)
	RETURNS DATETIME DETERMINISTIC
	RETURN DATE_ADD(FROM_UNIXTIME(0), INTERVAL v second);

mysql> select from_unixtime(1523872418);
+---------------------------+
| from_unixtime(1523872418) |
+---------------------------+
| 2018-04-16 09:53:38       |
+---------------------------+
1 row in set

mysql> select from_unixtime_fixed(1523872418);
+---------------------------------+
| from_unixtime_fixed(1523872418) |
+---------------------------------+
| 2018-04-16 09:53:38             |
+---------------------------------+
1 row in set

mysql> select from_unixtime(2549008418);
+---------------------------+
| from_unixtime(2549008418) |
+---------------------------+
| NULL                      |
+---------------------------+
1 row in set

mysql> select from_unixtime_fixed(2549008418);
+---------------------------------+
| from_unixtime_fixed(2549008418) |
+---------------------------------+
| 2050-10-10 09:53:38             |
+---------------------------------+
1 row in set

-------------------------------------

CREATE OR REPLACE FUNCTION unix_timestamp_fixed (v DATETIME)
	RETURNS BIGINT DETERMINISTIC
	RETURN TIMESTAMPDIFF(SECOND, FROM_UNIXTIME(0), v);

mysql> select unix_timestamp('2018-04-16 09:53:38');
+---------------------------------------+
| unix_timestamp('2018-04-16 09:53:38') |
+---------------------------------------+
|                            1523872418 |
+---------------------------------------+
1 row in set

mysql> select unix_timestamp_fixed('2018-04-16 09:53:38');
+---------------------------------------------+
| unix_timestamp_fixed('2018-04-16 09:53:38') |
+---------------------------------------------+
|                                  1523872418 |
+---------------------------------------------+
1 row in set

mysql> select unix_timestamp('2050-10-10 09:53:38');
+---------------------------------------+
| unix_timestamp('2050-10-10 09:53:38') |
+---------------------------------------+
| NULL                                  |
+---------------------------------------+
1 row in set

mysql> select unix_timestamp_fixed('2050-10-10 09:53:38');
+---------------------------------------------+
| unix_timestamp_fixed('2050-10-10 09:53:38') |
+---------------------------------------------+
|                                  2549008418 |
+---------------------------------------------+
1 row in set
`
[7 May 2019 18:00] Ben Schwartz
This is still an issue in 2019. 19 years away, but still nerve-racking to see that this has gone untouched by the MySQL powers that be for 14 years.
[17 May 2019 13:54] Ben J
@Ben Schwartz While I agree with your sentiment overall, the fact that we're only 19 years away from 2038 is immaterial to the fact that we cannot store dates after 2038 *now*! And that's the real problem.

Candidly, I'm flabbergasted that this bug report is not littered with angry commentary, demanding that "Something must be done!"
[17 Oct 2019 11:09] Nicolai Cornelis
Why is this not being handled? Are you guys serious??!!!
[13 Nov 2019 12:17] Milen Mihalev
I can't believe that this issue has not been resolved yet. We are not far away from 2038. Many apps depend on this, and I don't see any technical issues implementing a fix. Even there are some fixes waiting on the queue from 2017. Come on guys!
[22 Jan 2020 8:25] ch Ryu
Long live Oracle
[25 Jun 2020 1:40] Juan Bolívar
I managed to create some weird code that works to replace UNIX_TIMESTAMP. However it is too long.

You can replace:
    SELECT UNIX_TIMESTAMP('2040-01-01');
with:
    SELECT TIMESTAMPDIFF(SECOND, CONVERT_TZ('1970-01-01', @@SESSION.TIME_ZONE, '+00:00'), '2040-01-01')+(TIMESTAMPDIFF(SECOND, NOW(), CONVERT_TZ(NOW(), @@SESSION.TIME_ZONE, '+00:00'))*2);
[25 Jun 2020 14:25] Juan Bolívar
Ignore my previous code. This one is a bit better:
SELECT TIMESTAMPDIFF(SECOND, '1970-01-01', '2040-01-01')+TIMESTAMPDIFF(SECOND, NOW(), CONVERT_TZ(NOW(), @@SESSION.TIME_ZONE, '+00:00'));
[3 Mar 2021 12:28] John King
I can confirm that Fabrizio Carimati's code seems to work.
[3 Mar 2021 12:31] John King
... but does not handle microseconds properly
[3 Mar 2021 12:49] John King
Slightly altered fixed functions using decimals with a 6-decimal precision (which seems to be supported by implementation):

CREATE FUNCTION from_unixtime_fixed (v DECIMAL(16,6))
	RETURNS DATETIME(6) DETERMINISTIC
	RETURN DATE_ADD(FROM_UNIXTIME(0), INTERVAL v second);
CREATE FUNCTION unix_timestamp_fixed (v DATETIME(6))
	RETURNS DECIMAL(16,6) DETERMINISTIC
	RETURN TIMESTAMPDIFF(SECOND, FROM_UNIXTIME(0), v);
[3 Mar 2021 12:50] John King
Slightly altered fixed functions using decimals with a 6-decimal precision (which seems to be supported by implementation):

CREATE FUNCTION from_unixtime_fixed (v DECIMAL(16,6))
	RETURNS DATETIME(6) DETERMINISTIC
	RETURN DATE_ADD(FROM_UNIXTIME(0), INTERVAL v second);
CREATE FUNCTION unix_timestamp_fixed (v DATETIME(6))
	RETURNS DECIMAL(16,6) DETERMINISTIC
	RETURN TIMESTAMPDIFF(SECOND, FROM_UNIXTIME(0), v);
[5 Aug 2021 9:28] Dag Wanvik
Posted by developer:
 
Work on this bug was moved to a work log.
Closed by the completion of work log WL#14630 Make UNIX_TIMESTAMP, FROM_UNIXTIME work with 64 bits time when available
Pushed at rev 7df4fc0bc.
[27 Aug 2021 17:08] Christine Cole
Posted by developer:
 
Work was moved to WL-14630, no changelog for this bug report is needed.
[22 Dec 2021 8:52] MySQL Verification Team
https://bugs.mysql.com/bug.php?id=105955 marked as duplicate of this one.
[22 Dec 2021 8:52] MySQL Verification Team
https://bugs.mysql.com/bug.php?id=105955 marked as duplicate of this one.
[22 Dec 2021 8:52] MySQL Verification Team
https://bugs.mysql.com/bug.php?id=105955 marked as duplicate of this one.
[22 Dec 2021 9:43] MySQL Verification Team
this is fixed in 8.0.28 in case it's unclear.
[3 Jan 2022 20:22] Dag Wanvik
Alas, the implemented solution did not use the contributed patch, as the
solution had to be a lot more extensive, but thank you for providing it.
[16 Aug 2022 7:34] Sam Bryan
I was reading the 8.0.28 release notes, I see they say:

"The behavior of the TIMESTAMP type is also unaffected by this change; its maximum allowed value remains '2038-01-19 03:14:07.999999' UTC, regardless of platform. For dates futureward of this, use the MySQL DATETIME type instead."

Does this mean that TIMESTAMP will never support > 2038 dates, i.e. it's deprecated and we should migrate applications to DATETIME
[14 Oct 2022 10:25] Francesco Montanari
IMHO timestamps are intended to be used to record when a certain event occurs at the time when it happens, not to store events in the future or in the past (like birth dates, people were born before 1970)

So this is not an urgent issue. Also I guess you can't just upgrade columns from 4 bytes to 8, a new type needs to be implemented like it was done with utf8 > utf8mb4