Python日期时间与时间模块之间的区别

时间:2022-12-28 21:28:32

I am trying to figure out the differences between the datetime and time modules, and what each should be used for.

我试图找出日期时间和时间模块之间的差异,以及每个模块应该用于什么。

I know that datetime provides both dates and time. What is the use of the time module?

我知道datetime提供日期和时间。时间模块有什么用?

Examples would be appreciated and differences concerning timezones would especially be of interest.

可以理解的例子和关于时区的差异尤其令人感兴趣。

4 个解决方案

#1


66  

the time module is principally for working with unix time stamps; expressed as a floating point number taken to be seconds since the unix epoch. the datetime module can support many of the same operations, but provides a more object oriented set of types, and also has some limited support for time zones.

时间模块主要用于处理unix时间戳;表示为自unix时代以来的秒数浮点数。 datetime模块可以支持许多相同的操作,但提供了一组面向对象的类型,并且对时区也有一些有限的支持。

#2


3  

The time module can be used when you just need the time of a particular record - like lets say you have a seperate table/file for the transactions for each day, then you would just need the time. However the time datatype is usually used to store the time difference between 2 points of time.

当您只需要特定记录的时间时,可以使用时间模块 - 比如让我们说每天的交易都有一个单独的表/文件,那么您只需要时间。但是,time数据类型通常用于存储2个时间点之间的时间差。

This can also be done using datetime, but if we are only dealing with time for a particular day, then time module can be used.

这也可以使用日期时间来完成,但如果我们只处理特定日期的时间,则可以使用时间模块。

Datetime is used to store a particular data and time for a record. Like in a rental agency. The due date would be a datetime datatype.

日期时间用于存储记录的特定数据和时间。就像在租赁公司。截止日期是datetime数据类型。

#3


3  

If you are interested in timezones, you should consider the use of pytz.

如果您对时区感兴趣,您应该考虑使用pytz。

#4


2  

Stick to time to prevent DST ambiguity.

坚持时间防止DST模糊。

Personally, I prefer to use only the system time module in order to prevent ambiguity issues with daylight savings time (DST).

就个人而言,我更喜欢只使用系统时间模块,以防止夏令时(DST)的模糊问题。

Conversion to any time format including local time from there is pretty easy:

从那里转换到任何时间格式(包括当地时间)非常简单:

import time
t = time.time()
t_str = time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(t))

If the system additionally runs the network time protocol (NTP) dæmon, one ends up with a pretty solid time base.

如果系统另外运行网络时间协议(NTP),那么最终会有一个非常可靠的时基。

#1


66  

the time module is principally for working with unix time stamps; expressed as a floating point number taken to be seconds since the unix epoch. the datetime module can support many of the same operations, but provides a more object oriented set of types, and also has some limited support for time zones.

时间模块主要用于处理unix时间戳;表示为自unix时代以来的秒数浮点数。 datetime模块可以支持许多相同的操作,但提供了一组面向对象的类型,并且对时区也有一些有限的支持。

#2


3  

The time module can be used when you just need the time of a particular record - like lets say you have a seperate table/file for the transactions for each day, then you would just need the time. However the time datatype is usually used to store the time difference between 2 points of time.

当您只需要特定记录的时间时,可以使用时间模块 - 比如让我们说每天的交易都有一个单独的表/文件,那么您只需要时间。但是,time数据类型通常用于存储2个时间点之间的时间差。

This can also be done using datetime, but if we are only dealing with time for a particular day, then time module can be used.

这也可以使用日期时间来完成,但如果我们只处理特定日期的时间,则可以使用时间模块。

Datetime is used to store a particular data and time for a record. Like in a rental agency. The due date would be a datetime datatype.

日期时间用于存储记录的特定数据和时间。就像在租赁公司。截止日期是datetime数据类型。

#3


3  

If you are interested in timezones, you should consider the use of pytz.

如果您对时区感兴趣,您应该考虑使用pytz。

#4


2  

Stick to time to prevent DST ambiguity.

坚持时间防止DST模糊。

Personally, I prefer to use only the system time module in order to prevent ambiguity issues with daylight savings time (DST).

就个人而言,我更喜欢只使用系统时间模块,以防止夏令时(DST)的模糊问题。

Conversion to any time format including local time from there is pretty easy:

从那里转换到任何时间格式(包括当地时间)非常简单:

import time
t = time.time()
t_str = time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(t))

If the system additionally runs the network time protocol (NTP) dæmon, one ends up with a pretty solid time base.

如果系统另外运行网络时间协议(NTP),那么最终会有一个非常可靠的时基。