我在做一个收据采集的程序,需要记录起始时间和结束时间,在数据库中是用timestamp字段来保存的,有些情况下不存在起始时间,此时就需要设置一个默认的起始时间,当初想着是使用timestamp类型的『最小值』。
然而,从1970-01-01 00:00:00到1970-01-01 00:00:01,保存到数据库的时候总是报错,后面使用1970-01-01 08:00:01终于就正常了
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
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:00或1970-01-01 00:00:01两个值保存时出错的问题了。从当前时区转成UTC时间需要减去『8小时』,结果就不在timestamp类型的范围内了。