我发现_datetimemodule.c
似乎是正确的文件,但由于C不是我的强项,我需要一点帮助。
>>> import datetime
>>> import sys
>>> d = datetime.datetime.now()
>>> sys.getsizeof(d)
48
>>> d = datetime.datetime(2018, 12, 31, 23, 59, 59, 123)
>>> sys.getsizeof(d)
48
因此,时区未知的datetime对象需要48个字节。从PyDateTime_DateTimeType
看,它似乎是PyDateTime_DateType
和PyDateTime_TimeType
。也许还_PyDateTime_BaseTime
?
通过查看代码,我给YYYY-mm-dd HH:MM:ss
中的每个字段存储了一个组件,这意味着:
int16_t
为16位)int8_t
int8_t
int8_t
int8_t
int8_t
uint16_t
但是那将是2 * 16 + 5 * 8 = 72位= 9字节,而不是Python告诉我的48字节。
我对日期时间内部结构的假设在哪里?如何在代码中看到这一点?
(我想这在Python实现之间可能会有所不同-如果是这样,请关注cPython)
答案 0 :(得分:5)
您缺少图片的关键部分:实际的日期时间结构定义,位于Include/datetime.h
中。那里也有重要的评论。以下是一些关键摘录:
/* Fields are packed into successive bytes, each viewed as unsigned and
* big-endian, unless otherwise noted:
*
* byte offset
* 0 year 2 bytes, 1-9999
* 2 month 1 byte, 1-12
* 3 day 1 byte, 1-31
* 4 hour 1 byte, 0-23
* 5 minute 1 byte, 0-59
* 6 second 1 byte, 0-59
* 7 usecond 3 bytes, 0-999999
* 10
*/
...
/* # of bytes for year, month, day, hour, minute, second, and usecond. */
#define _PyDateTime_DATETIME_DATASIZE 10
...
/* The datetime and time types have hashcodes, and an optional tzinfo member,
* present if and only if hastzinfo is true.
*/
#define _PyTZINFO_HEAD \
PyObject_HEAD \
Py_hash_t hashcode; \
char hastzinfo; /* boolean flag */
...
/* All datetime objects are of PyDateTime_DateTimeType, but that can be
* allocated in two ways too, just like for time objects above. In addition,
* the plain date type is a base class for datetime, so it must also have
* a hastzinfo member (although it's unused there).
*/
...
#define _PyDateTime_DATETIMEHEAD \
_PyTZINFO_HEAD \
unsigned char data[_PyDateTime_DATETIME_DATASIZE];
typedef struct
{
_PyDateTime_DATETIMEHEAD
} _PyDateTime_BaseDateTime; /* hastzinfo false */
typedef struct
{
_PyDateTime_DATETIMEHEAD
unsigned char fold;
PyObject *tzinfo;
} PyDateTime_DateTime; /* hastzinfo true */
您看到的48字节计数细分如下:
char[10]
这当然是所有实施细节。在不同的Python实现,不同的CPython版本,32位CPython构建或CPython调试构建(在定义了Py_TRACE_REFS的情况下编译CPython时,PyObject_HEAD中还有其他内容)上,它可能会有所不同。