前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL的timestamp字段可以使用的范围是多少

MySQL的timestamp字段可以使用的范围是多少

作者头像
用户3579639
发布2018-10-22 15:04:39
2.2K0
发布2018-10-22 15:04:39
举报

还是之前工作中遇到的一个小问题。我在做一个收据采集的程序,需要记录起始时间和结束时间,在数据库中是用timestamp字段来保存的,有些情况下不存在起始时间,此时就需要设置一个默认的起始时间,当初想着是使用timestamp类型的『最小值』。

然而,从1970-01-01 00:00:001970-01-01 00:00:01,保存到数据库的时候总是报错,后面使用1970-01-01 08:00:01终于就正常了。后来又参考了 http://blog.itpub.net/25281640/viewspace-710118/ 这篇文章的观点论证了。

从此开始我就认为timestamp的范围,就像文章的观点,真正的范围是从1970-01-01 08:00:01 到 2038-01-19 11:14:07。这个08:00:01当时并没有引起我的注意,最近不知怎么突然想起来,感觉不对。

从MySQL的官方文档就可以得到结论,http://dev.mysql.com/doc/refman/5.0/en/datetime.html

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.) By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis. As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions. The current time zone is available as the value of the time_zone system variable. For more information, see Section 10.6, “MySQL Server Time Zone Support”.

timestamp类型的起始时间是1970-01-01 00:00:01 UTC,和时区是关系的。如果我没有理解错的话,MySQL将timestamp类型的值保存的时候,会从当前时区转成UTC时间,正好解释了前面1970-01-01 00:00:001970-01-01 00:00:01两个值保存时出错的问题了。从当前时区转成UTC时间需要减去『8小时』,结果就不在timestamp类型的范围内了。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档