MySQL的timestamp字段可以使用的范围是多少
還是之前工作中遇到的一個小問題。我在做一個收據采集的程序,需要記錄起始時間和結束時間,在數據庫中是用timestamp字段來保存的,有些情況下不存在起始時間,此時就需要設置一個默認的起始時間,當初想著是使用timestamp類型的『最小值』。
然而,從1970-01-01 00:00:00到1970-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:00或1970-01-01 00:00:01兩個值保存時出錯的問題了。從當前時區轉成UTC時間需要減去『8小時』,結果就不在timestamp類型的范圍內了。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的MySQL的timestamp字段可以使用的范围是多少的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《FPGA全程进阶---实战演练》第二十
- 下一篇: 我的Android进阶之旅------A