patternsqlMinor
MySQL: "FROM_UNIXTIME(0)" gives "1970-01-01 01:00:00", which is 1 hour off
Viewed 0 times
1970hourfrom_unixtimemysqlgiveswhichoff
Problem
Context: MySQL version
On MySQL 5.7.17 (as shown by
Context: MySQL timezone settings
and with
...which means the server tracks the system timezone.
Context: System timezone
This being a Linux, we check the system timezone like this (output redacted):
Context: What we know
Context: Current time
It's now around :
Problem
Let's run:
The output (suitably pivoted by hand; there should be an option to do that):
```
NOW() 2018-02-14 14:15:13 -- Display "now" in SYSTEM timetone: OK
FROM_UNIXTIME(0) 1970-01-01 01:00:00 -- Displa
On MySQL 5.7.17 (as shown by
SHOW VARIABLES LIKE "%version%";)Context: MySQL timezone settings
and with
SELECT @@global.time_zone, @@session.time_zone; yielding+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| SYSTEM | SYSTEM |
+--------------------+---------------------+...which means the server tracks the system timezone.
Context: System timezone
This being a Linux, we check the system timezone like this (output redacted):
$ timedatectl
Local time: Wed 2018-02-14 13:21:30 GMT
Universal time: Wed 2018-02-14 13:21:30 UTC
RTC time: Wed 2018-02-14 13:21:30
Time zone: Europe/London (GMT, +0000)Context: What we know
- Time and date values are not stored with timezone information in MySQL (there is no clear description of this in The DATE, DATETIME, and TIMESTAMP Types, except "MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)"; note that "converts TIMESTAMP values to UTC for storage" actually makes sense only as an implementor's note, it's not useful to the user - what is important is that TiMEZONE always returns the correct value in whatever timezone one is in, while DATETIME will always return the same value in whatever timezone one is in)
Context: Current time
It's now around :
$ date +"The current time is %s alias %c"
The current time is 1518617572 alias Wed 14 Feb 2018 14:12:52 GMTProblem
Let's run:
SELECT NOW(),FROM_UNIXTIME(0),UNIX_TIMESTAMP(),FROM_UNIXTIME(UNIX_TIMESTAMP());The output (suitably pivoted by hand; there should be an option to do that):
```
NOW() 2018-02-14 14:15:13 -- Display "now" in SYSTEM timetone: OK
FROM_UNIXTIME(0) 1970-01-01 01:00:00 -- Displa
Solution
At time 0, "Time zone: Europe/London" was DST (or not DST?); you are currently not in DST (or you are?). That is probably the 1-hour discrepancy.
Europe/London is not the same as UTC.
Europe/London is not the same as UTC.
Context
StackExchange Database Administrators Q#197914, answer score: 3
Revisions (0)
No revisions yet.