我没有意识到这一点,但显然 Python 的 strftime
函数不支持 1900 年之前的日期:
>>> from datetime import datetime
>>> d = datetime(1899, 1, 1)
>>> d.strftime('%Y-%m-%d')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: year=1899 is before 1900; the datetime strftime() methods require year >= 1900
我确信我可以自己编写一些东西来做到这一点,但我认为 strftime< /code> 函数的存在是有原因的(而且它不支持 1900 年之前的日期也是有原因的)。我需要能够支持 1900 年之前的日期。我只使用 str
,但变化太多。换句话说,它可能有也可能没有微秒,或者可能有也可能没有时区。有什么办法解决这个问题吗?
如果有影响,我这样做是为了可以将数据写入文本文件并使用 Oracle SQL*Loader 将其加载到数据库中。
我基本上最终完成了亚历克斯·马泰利的回答。下面是更完整的实现:
>>> from datetime import datetime
>>> d = datetime.now()
>>> d = d.replace(microsecond=0, tzinfo=None)
>>> str(d)
'2009-10-29 11:27:27'
唯一的区别是 str(d)
相当于 d.isoformat(' ')
。
I didn't realize this, but apparently Python's strftime
function doesn't support dates before 1900:
>>> from datetime import datetime
>>> d = datetime(1899, 1, 1)
>>> d.strftime('%Y-%m-%d')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: year=1899 is before 1900; the datetime strftime() methods require year >= 1900
I'm sure I could hack together something myself to do this, but I figure the strftime
function is there for a reason (and there also is a reason why it can't support pre-1900 dates). I need to be able to support dates before 1900. I'd just use str
, but there's too much variation. In other words, it may or may not have microseconds or it may or may not have a timezone. Is there any solution to this?
If it makes a difference, I'm doing this so that I can write the data to a text file and load it into a database using Oracle SQL*Loader.
I essentially ended up doing Alex Martelli's answer. Here's a more complete implementation:
>>> from datetime import datetime
>>> d = datetime.now()
>>> d = d.replace(microsecond=0, tzinfo=None)
>>> str(d)
'2009-10-29 11:27:27'
The only difference is that str(d)
is equivalent to d.isoformat(' ')
.
发布评论
评论(5)
isoformat 适用于
datetime 实例没有范围限制:
如果您需要不同格式的字符串,那么对从
isoformat
获得的字符串进行切片、切块和重新混合并不难,这是非常一致的(< code>YYYY-MM-DDTHH:MM:SS.mmmmmm,如果微秒为零,则省略点和后面的微秒)。isoformat works on
datetime
instances w/o limitation of range:If you need a different-format string it's not too hard to slice, dice and remix pieces of the string you get from
isoformat
, which is very consistent (YYYY-MM-DDTHH:MM:SS.mmmmmm
, with the dot and following microseconds omitted if microseconds are zero).文档似乎对此非常清楚:
因此不会有使用
strftime()
的解决方案。幸运的是,“手动”执行此操作非常简单:The documentation seems pretty clear about this:
So there isn't going to be a solution that uses
strftime()
. Luckily, it's pretty straightforward to do this "by hand":mxDateTime
可以处理任意日期。 Python 的time
和datetime
模块在内部使用 UNIX 时间戳,这就是它们的范围有限的原因。mxDateTime
can handle arbitrary dates. Python'stime
anddatetime
modules use UNIX timestamps internally, that's why they have limited range.这是来自 matplotlib 源。可以为您自己的开发提供一个良好的起点。
This is from the matplotlib source. Could provide a good starting point for rolling your own.
这就是ctime库(UTF)的“特性”。
另外你可能会在 2038 以上遇到问题。
This is the "feature" of the ctime library (UTF).
Also You may have problem above 2038.