MySQL CONVERT_TZ() [英] MySQL CONVERT_TZ()

查看:368
本文介绍了MySQL CONVERT_TZ()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个数据库,该数据库存储用户指定的每日警报时间.例如,如果每天7:00 AM至7:30 AM之间满足某些条件,则用户希望接收警报.在尝试实现这一点时,我需要适应夏令时.这是我尝试的解决方案:

I am trying to set up a database that stores daily alert times as specified by users. For example, the user wants to receive an alert if some criterion is met each day between 7:00 AM and 7:30 AM. In trying to implement this, I need to accommodate daylight saving time. Here's my attempted solution:

  1. 在一个表中(例如userInfo)存储用户本地时区信息(以长格式,例如"US/Eastern"),在另一个表中存储警报时间(例如userAlarms).
  2. 查询userAlarms表时,通过CONVERT_TZ(UTC_TIME(), 'UTC', userInfo.tz)将UTC时间转换为userInfo表中存储的tz列所指定的用户本地时间.
  1. Store the users local time zone (in long form, e.g. "US/Eastern") information in one table (say userInfo), and the alarm times in another table (say userAlarms).
  2. When querying the userAlarms table, convert UTC time into the users local time as specified by the tz column stored in the userInfo table via CONVERT_TZ(UTC_TIME(), 'UTC', userInfo.tz).

问题1.据我了解,指定时区名称(例如美国/美国东部时间)应考虑夏令时.例如,在1月1日呼叫CONVERT_TZ('00:00:00', 'UTC', 'US/EASTERN')应该产生'19:00:00',但是在7月1日呼叫应该产生'20:00:00'.我说得对吗?

Question 1. From my understanding, specifying the time zone name (like US/Eastern) should take daylight saving time into account. For example, calling CONVERT_TZ('00:00:00', 'UTC', 'US/EASTERN') on January 1 should yield '19:00:00', but on July 1 the call should yield '20:00:00'. Am I correct?

问题2.如果Q1正确,我是否需要不断更新MySQL的时区表以使时区UTC偏移量保持最新状态?

Question 2. If Q1 is correct, do I need to constantly update MySQL's time zone table to keep the time zone UTC offsets up to date?

问题3.在我的服务器上运行时,MySQL文档SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET')中给出的示例会产生"NULL".这可能是由于未设置时区表引起的吗?

Question 3. The sample given in the MySQL documentation SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET') yields "NULL" when run on my server. Could this be caused by not having the time zone tables set-up?

我该如何检查?

推荐答案

如果产生null,则尚未设置TZ表:

If this yields null then the TZ tables have not been set up:

SELECT CONVERT_TZ(now(),'US/Eastern','US/Central');

如果您未设置时区表,则可以更新小时 在用户表中偏移,然后执行以下操作:

If you do not have the time zone tables set up you could update the hour offset in the user table and then do:

select utc_timezone() - interval user_timezone_offset_in_hours hour
from userinfo a
where user_id = 999;

但是,您仍然需要一种方法来更新用户的时区.

You'd still need a way to update the user's time zone however.

如果您是为Web应用程序编写的,则可以通过javascript获取时区,这是文章,它描述了如何操作(没有尝试过,但是看起来可以使用).

If you are writing this for a web application you can get the time zone via javascript, here's an article that describes how (haven't tried this but it looks like it'll work).

关于上述间隔"的一些解释...

A bit of an explanation with respect to 'interval' above...

MySQL中更技巧性的构造之一是使用INTERVAL 关键字,最好以示例形式展示(数字值可以是表达式或字段值)

One of the more trick constructs in MySQL is the use of the INTERVAL keyword, best shown by example the (numeric value can be an expression or the field value)

select now() today, now() - interval 1 day yesterday;
+---------------------+---------------------+
| today               | yesterday           |
+---------------------+---------------------+
| 2011-05-26 13:20:55 | 2011-05-25 13:20:55 |
+---------------------+---------------------+

您可以随意添加和减去它们,这就是为什么我从不 烦扰日期/时间的添加/减去/转换功能

You can add them and subtract them anyway you like, this is why I never bother with the date/time add/subtract/convert functions

select now() a, now() - interval 1 day + interval 4 hour + interval 8 minute b;
+---------------------+---------------------+
| a                   | b                   |
+---------------------+---------------------+
| 2011-05-26 13:24:16 | 2011-05-25 17:32:16 |
+---------------------+---------------------+

您可以使用负数(对于负时区偏移量应该很好) 这些是相同的:

You can use negative numbers (should be good for negative time zone offsets) these are the same:

select now() - interval 1 month a, now() + interval -1 month b;
+---------------------+---------------------+
| a                   | b                   |
+---------------------+---------------------+
| 2011-04-26 13:38:05 | 2011-04-26 13:38:05 |
+---------------------+---------------------+

这篇关于MySQL CONVERT_TZ()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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