time
— 时间的访问和转换
¶
此模块提供各种时间相关函数。对于相关功能,另请参阅
datetime
and
calendar
模块。
Although this module is always available, not all functions are available on all platforms. Most of the functions defined in this module call platform C library functions with the same name. It may sometimes be helpful to consult the platform documentation, because the semantics of these functions varies among platforms.
按次序解释一些术语和约定。
-
The epoch 是时间起始点,返回值为
time.gmtime(0)。在所有平台都是 1970 年 1 月 1 日 00:00:00 (UTC)。
-
术语 从纪元起的秒数 refers to the total number of elapsed seconds since the epoch, typically excluding 闰秒 . Leap seconds are excluded from this total on all POSIX-compliant platforms.
-
The functions in this module may not handle dates and times before the epoch or far in the future. The cut-off point in the future is determined by the C library; for 32-bit systems, it is typically in 2038.
-
函数
strptime()can parse 2-digit years when given%yformat code. When 2-digit years are parsed, they are converted according to the POSIX and ISO C standards: values 69–99 are mapped to 1969–1999, and values 0–68 are mapped to 2000–2068.
-
UTC is Coordinated Universal Time (formerly known as Greenwich Mean Time, or GMT). The acronym UTC is not a mistake but a compromise between English and French.
-
DST is Daylight Saving Time, an adjustment of the timezone by (usually) one hour during part of the year. DST rules are magic (determined by local law) and can change from year to year. The C library has a table containing the local rules (often it is read from a system file for flexibility) and is the only source of True Wisdom in this respect.
-
The precision of the various real-time functions may be less than suggested by the units in which their value or argument is expressed. E.g. on most Unix systems, the clock “ticks” only 50 or 100 times a second.
-
另一方面,精度对于
time()andsleep()is better than their Unix equivalents: times are expressed as floating-point numbers,time()返回最精确可用时间 (使用 Unixgettimeofday()若可用),而sleep()将接受具有非零小数的时间 (Unixselect()用于实现这,若可用)。 -
时间值如返回通过
gmtime(),localtime(),和strptime(),和接受通过asctime(),mktime()andstrftime(),是 9 整数序列。返回值的gmtime(),localtime(),和strptime()还为单个字段提供属性名。见
struct_time了解这些对象的描述。3.3 版改变: The
struct_time类型被扩展以提供tm_gmtoffandtm_zone属性当平台支持相应struct tm成员。3.6 版改变: The
struct_time属性tm_gmtoffandtm_zone现在可用于所有平台。