How can I convert a unix timestamps to UTC Date in MySql?
I've a bunch of data stored in a mysql database as a bunch of unix style timestamps (in UTC). Previously, these have been formatted via Perl's gmtime()
. It seems that MySQL's FROM_UNIXTIME()
function tries to be helpful, and correct for daylight savings, but I really don't开发者_高级运维 want it to.
Is there a nice handy method to convert the results from the system timezone (GMT/BST) to UTC or alternately determine if DST was in effect for a particular time?
Have you tried:
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2011-04-04 22:54:13 |
+---------------------+
1 row in set (0.00 sec)
mysql> set @var = unix_timestamp(NOW());
Query OK, 0 rows affected (0.00 sec)
mysql> SET time_zone='+00:00';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2011-04-04 20:54:23 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT FROM_UNIXTIME(@var);
+---------------------+
| FROM_UNIXTIME(@var) |
+---------------------+
| 2011-04-04 20:54:17 |
+---------------------+
1 row in set (0.02 sec)
If you want to use named timezones, you can see this page for more information, and how to fill the timezone tables on your system.
精彩评论