在MySQL中计算时区的偏移量 [英] In MySQL caculating offset for a time zone

查看:252
本文介绍了在MySQL中计算时区的偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MySQL中是否有一种方法可以计算任何时区的偏移量? 例如,要获取时区Asia/calcutta中的本地时间.我想做的是计算该时区的偏移量,并将该偏移量添加到GMT以获得本地时间.

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.

推荐答案

如果要计算某个时区(例如,美国/温哥华)与UTC的时差,您可以按照以下步骤进行操作:

If you want to calculate the offset of a time zone such as America/Vancouver from UTC you can do it as follows:

SELECT (unix_timestamp() -  unix_timestamp(convert_tz(now(), 'Etc/UTC', 'America/Vancouver'))) / 3600   as offset;

为此,您首先需要按照以下概述将时区信息加载到mysql中:

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

这篇关于在MySQL中计算时区的偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆