日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發布時間:2023/12/19 数据库 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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字段可以使用的范围是多少的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。