在MySQL中进行时区处理的最佳实践是什么? [英] What is best practice for timezone handling in MySQL?

查看:74
本文介绍了在MySQL中进行时区处理的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已被问过,但没有找到我想要的答案.我将所有日期存储在UTC/GMT的MYSQL中.当我为引用时间的用户提取数据时,最好使用CONVERT_TZ构造...

This has been asked before but not the answer I am looking for. I am storing all my dates in MYSQL in UTC/GMT. When I extract data fora user that references time is it better to use the CONVERT_TZ construct...

SELECT CONVERT_TZ(mytime,'UTC',usertimezone) as mytime FROM table

还是最好在Mysql中临时设置会话区域,然后再进行常规查询?

or is it better to temporarily set the session zone in Mysql and then do normal queries?

SET time_zone = usertimezone;

如果我使用第二个,是否对每个用户会话都执行一次,或者如果我不使用持久打开,是否需要在每次查询之前进行设置?

And if I use the second, do I just do that once for each user session or if I am not using a persistent open, do I need to set it before each query?

推荐答案

如果您的数据存储在TIMESTAMP类型的列中,那么您应该SET time_zone,并且MySQL会在检索/插入时自动将UTC转换为UTC/从UTC转换为-不需要做更多的事情.这是推荐的方法.

If your data is stored in TIMESTAMP type columns, then you should SET time_zone and MySQL will automatically convert to/from UTC on retrieval/insertion—you don't need to do anything more. This is the recommended approach.

这篇关于在MySQL中进行时区处理的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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