且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python3.4 datetime.today() 和 datetime.now()

更新时间:2023-01-28 10:59:57

根据datetime.now(tz=None)的文档:

返回当前本地日期和时间.如果可选参数 tzNone 或未指定,则类似于 today(),但是,如果可能,提供比可以获得的精度更高的精度通过 time.time() 时间戳(例如,这在提供 C gettimeofday() 函数的平台上是可能的).

Return the current local date and time. If optional argument tz is None or not specified, this is like today(), but, if possible, supplies more precision than can be gotten from going through a time.time() timestamp (for example, this may be possible on platforms supplying the C gettimeofday() function).

Else tz 必须是 tzinfo 类子类的实例,当前日期和时间转换为 tz 的时区.在这种情况下,结果等效于 tz.fromutc(datetime.utcnow().replace(tzinfo=tz)).

Else tz must be an instance of a class tzinfo subclass, and the current date and time are converted to tz‘s time zone. In this case the result is equivalent to tz.fromutc(datetime.utcnow().replace(tzinfo=tz)).

因此 datetime.now() 尽可能提供更高的精度.

Thus datetime.now() supplies more precision if possible.