Is there a way in MySQL to calculate the offset for any timezone?
For example, to get the local time in the timezone Asia/calcutta
. What I want to do is calculate the offset for this timezone and add that offset to GMT to get the local time.
相关问题
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Why sometimes there is one of more gap(s) in the v
- mySQL alter table on update, current timestamp
The offset will depend on the time that you're interested in - for instance, I'd currently have an offset of one hour from UTC, but during the winter my offset would be zero.
Judging by the docs page on MySQL time zone support, you want to use the
convert_tz
function. If you're trying to convert UTC to local time, pass in "Etc/GMT+0" as the "from" time zone, and "Asia/Calcutta" as the "to".You can just pass in an offset
http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html
SELECT TIMESTAMPDIFF(HOUR, UTC_TIMESTAMP(), NOW());
If the server's timezone is PST this will return
-8
.SELECT TIMESTAMPDIFF(SECOND, NOW(), UTC_TIMESTAMP());
Add the result of the above to any unix timestamp if you want to compare it to MySQL DateTimes.
Giving
If you want to calculate the offset of a time zone such as America/Vancouver from UTC you can do it as follows:
For this to work you will first need to load the time zone information into mysql as outlined here: http://dev.mysql.com/doc/refman/5.0/en/mysql-tzinfo-to-sql.html