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 %y format 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() and sleep() is better than their Unix equivalents: times are expressed as floating-point numbers, time() 返回最精确可用时间 (使用 Unix gettimeofday() 若可用),而 sleep() 将接受具有非零小数的时间 (Unix select() 用于实现这,若可用)。

  • 时间值如返回通过 gmtime() , localtime() ,和 strptime() ,和接受通过 asctime() , mktime() and strftime() ,是 9 整数序列。返回值的 gmtime() , localtime() ,和 strptime() 还为单个字段提供属性名。

    struct_time 了解这些对象的描述。

    3.3 版改变: The struct_time 类型被扩展以提供 tm_gmtoff and tm_zone 属性当平台支持相应 struct tm 成员。

    3.6 版改变: The struct_time 属性 tm_gmtoff and tm_zone 现在可用于所有平台。