python date 和 datetime 的取值范围(对比 Mysql 的 datetime 和 timestamp)

雾绡析取
• 阅读 2318

Python 的情况

先来看看 date 类型吧!

In [1]: from datetime import date

In [2]: date.min
Out[2]: datetime.date(1, 1, 1)

In [3]: str(date.min)
Out[3]: '0001-01-01'

In [4]: str(date.max)
Out[4]: '9999-12-31'

可以看到 python 的 date 的取值范围是 0001-01-019999-12-31

再来看看 datetime 吧!

In [5]: from datetime import datetime

In [6]: str(datetime.min)
Out[6]: '0001-01-01 00:00:00'

In [7]: str(datetime.max)
Out[7]: '9999-12-31 23:59:59.999999'

可以看到 python 的 datetime 的取值范围是 0001-01-01 00:00:009999-12-31 23:59:59.999999

Mysql 的情况

来看看 mysql 的情况吧,下面的官方文档中的内容:

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.
The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD hh:mm:ss' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

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.

总结一下就是:

  • date 的取值范围:1000-01-019999-12-31
  • timestamp 的取值范围:1970-01-01 00:00:012038-01-19 03:14:07
  • datetime 的取值范围:1000-01-01 00:00:009999-12-31 23:59:59

参考资料:mysql 文档

但是需要注意:

Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00' or '0000-00-00 00:00:00'), if the SQL mode permits this conversion. The precise behavior depends on which if any of strict SQL mode and the NO_ZERO_DATE SQL mode are enabled; see Section 5.1.10, “Server SQL Modes”.

还有这部分:
python date 和 datetime 的取值范围(对比 Mysql 的 datetime 和 timestamp)

翻译成人话的意思,就是 myql 会把无效的 date 设为 0000-00-00,无效的 datetime 和 timestamp 设为 0000-00-00 00:00:00

比如 2022-13-35 就是一个无效的 date,当把这个值插入到 mysql 中的时候,开启了严格模式的 mysql 会报错,未开启严格模式的 mysql 会转为 0000-00-00 保存。

‼️ 所以我们使用 Python 从 mysql 读取时间类型的时候,一定要注意这个坑!因为 0000-00-00 没有办法转成 Python 的 date 类型,会报错的!‼️


顺便可以看看 Mysql 的 date、timestamp、datetime 各占用几个字节:

  • date 类型 3 字节
  • timestamp 类型 4 字节
  • datetime 类型 8 字节

python date 和 datetime 的取值范围(对比 Mysql 的 datetime 和 timestamp)

参考文章:Data Type Storage Requirements


关于 Mysql 插入的时间类型可以是 0000-00-00 这种格式的原因可以参考:MySQL Incorrect datetime value: '0000-00-00 00:00:00'

在 Mysql8 中默认是不可以插入这种无效时间的

点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Souleigh ✨ Souleigh ✨
5年前
python时间模块的使用 white_study
前言:在开发中经常会与时间打交道,如:获取事件戳,时间戳的格式化等,这里简要记录一下python操作时间的方法。python中常见的处理时间的模块:time:处理时间的模块,如获取时间戳,格式化日期等datetime:date和time的结合体,处理日期和时间calendar:日历相关的模块,如:处理年历/月历tim
黎明之道 黎明之道
4年前
python数据分析与可视化——时间序列数据分析
时间序列数据分析日期和时间数据类型datetime构造Python标准库中包含了用于日期(date)、时间(time)、日历(calendar)等功能的数据类型,主要会用到datetime、time、ca
Wesley13 Wesley13
4年前
Java:String和Date、Timestamp之间的转换
一、String与Date(java.util.Date)互转1.1StringDateStringdateStr"2010/05/0412:34:23";DatedatenewDate();//注意format的格式要与日期String的格式相匹配DateFormat
Stella981 Stella981
4年前
Python3:sqlalchemy对mysql数据库操作,非sql语句
Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''
Wesley13 Wesley13
4年前
Java爬虫之JSoup使用教程
title:Java爬虫之JSoup使用教程date:201812248:00:000800update:201812248:00:000800author:mecover:https://imgblog.csdnimg.cn/20181224144920712(https://www.oschin
Wesley13 Wesley13
4年前
Java计算年月日时分秒时间差(两个时间相减)
//测试主方法 publicstaticvoidmain(Stringargs){     Date currentTime  df.parse("20040326 13:31:40");  //当前系统时间           Date firstTime  df.parse("200
Wesley13 Wesley13
4年前
MySQL中日期与时间类型
!(http://static.oschina.net/uploads/space/2014/0926/110202_V4dH_1582989.jpg)1、MySQL的五种日期和时间类型MySQl中有多种表示日期和时间的数据类型。其中YEAR表示年份,DATE表示日期,TIME表示时间,DATETIME和TIMESTAMP分别
Stella981 Stella981
4年前
Python之time模块的时间戳、时间字符串格式化与转换
Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。关于时间戳的几个概念时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。时间元组(struct_time),包含9个元素。 time.struct_time(tm_y
Wesley13 Wesley13
4年前
Java日期时间API系列30
  实际使用中,经常需要使用不同精确度的Date,比如保留到天2020042300:00:00,保留到小时,保留到分钟,保留到秒等,常见的方法是通过格式化到指定精确度(比如:yyyyMMdd),然后再解析为Date。Java8中可以用更多的方法来实现这个需求,下面使用三种方法:使用Format方法、 使用Of方法和使用With方法,性能对比,使用
小万哥 小万哥
2年前
Python 日期和时间处理教程:datetime 模块的使用
Python中的日期不是独立的数据类型,但我们可以导入一个名为datetime的模块来使用日期作为日期对象。示例:导入datetime模块并显示当前日期:pythonimportdatetimexdatetime.datetime.now()print(x