### 導航
- [索引](../genindex.xhtml "總目錄")
- [模塊](../py-modindex.xhtml "Python 模塊索引") |
- [下一頁](argparse.xhtml "argparse --- 命令行選項、參數和子命令解析器") |
- [上一頁](io.xhtml "io --- 處理流的核心工具") |
- 
- [Python](https://www.python.org/) ?
- zh\_CN 3.7.3 [文檔](../index.xhtml) ?
- [Python 標準庫](index.xhtml) ?
- [通用操作系統服務](allos.xhtml) ?
- $('.inline-search').show(0); |
# [`time`](#module-time "time: Time access and conversions.") --- 時間的訪問和轉換
- - - - - -
該模塊提供了各種時間相關的函數。相關功能還可以參閱 [`datetime`](datetime.xhtml#module-datetime "datetime: Basic date and time types.") 和 [`calendar`](calendar.xhtml#module-calendar "calendar: Functions for working with calendars, including some emulation of the Unix cal program.") 模塊。
盡管此模塊始終可用,但并非所有平臺上都提供所有功能。 此模塊中定義的大多數函數調用都具有相同名稱的平臺C庫函數。 因為這些函數的語義因平臺而異,所以使用時最好查閱平臺相關文檔。
下面是一些術語和慣例的解釋.
- *epoch* 是時間開始的點,并且取決于平臺。對于Unix, epoch 是1970年1月1日00:00:00(UTC)。要找出給定平臺上的 epoch ,請查看 `time.gmtime(0)` 。
- 術語 *Unix 紀元秒數* 是指自國際標準時間 1970 年 1 月 1 日零時以來經過的總秒數,通常不包括 [閏秒](https://en.wikipedia.org/wiki/Leap_second) \[https://en.wikipedia.org/wiki/Leap\_second\]。 在所有符合 POSIX 標準的平臺上,閏秒都會從總秒數中被扣除。
- 此模塊中的功能可能無法處理紀元之前或將來的遠期日期和時間。未來的截止點由C庫決定;對于32位系統,它通常在2038年。
- **2000年(Y2K)問題** :Python依賴于平臺的C庫,它通常沒有2000年問題,因為所有日期和時間都在內部表示為自紀元以來的秒數。函數 [`strptime()`](#time.strptime "time.strptime") 在給出 `%y` 格式代碼時可以解析2位數年份。當解析2位數年份時,它們將根據 POSIX 和 ISO C 標準進行轉換:值 69--99 映射到 1969--1999,值 0--68 映射到2000--2068。
- UTC是協調世界時(以前稱為格林威治標準時間,或GMT)。縮寫UTC不是錯誤,而是英語和法語之間的妥協。
- DST是夏令時,在一年中的一部分時間(通常)調整時區一小時。 DST規則很神奇(由當地法律確定),并且每年都會發生變化。 C 庫有一個包含本地規則的表(通常是從系統文件中讀取以獲得靈活性),并且在這方面是True Wisdom的唯一來源。
- 各種實時函數的精度可能低于表示其值或參數的單位所建議的精度。例如,在大多數Unix系統上,時鐘 "ticks" 僅為每秒50或100次。
- 另一方面, [`time()`](#time.time "time.time") 和 [`sleep()`](#time.sleep "time.sleep") 的精度優于它們的Unix等價物:時間表示為浮點數,[`time()`](#time.time "time.time") 返回最準確的時間 (使用Unix `gettimeofday()` 如果可用),并且 [`sleep()`](#time.sleep "time.sleep") 將接受非零分數的時間(Unix `select()` 用于實現此功能,如果可用)。
- 時間值由 [`gmtime()`](#time.gmtime "time.gmtime"),[`localtime()`](#time.localtime "time.localtime") 和 [`strptime()`](#time.strptime "time.strptime") 返回,并被 [`asctime()`](#time.asctime "time.asctime"), [`mktime()`](#time.mktime "time.mktime") 和 [`strftime()`](#time.strftime "time.strftime") 接受,是一個 9 個整數的序列。 [`gmtime()`](#time.gmtime "time.gmtime"), [`localtime()`](#time.localtime "time.localtime") 和 [`strptime()`](#time.strptime "time.strptime") 的返回值還提供各個字段的屬性名稱。
請參閱 [`struct_time`](#time.struct_time "time.struct_time") 以獲取這些對象的描述。
在 3.3 版更改: 在平臺支持相應的 `struct tm` 成員時,[`struct_time`](#time.struct_time "time.struct_time") 類型被擴展提供 `tm_gmtoff` 和 `tm_zone` 屬性。
在 3.6 版更改: [`struct_time`](#time.struct_time "time.struct_time") 的屬性 `tm_gmtoff` 和 `tm_zone` 現在可在所有平臺上使用。
- 使用以下函數在時間表示之間進行轉換:
從
到
使用
seconds since the epoch
UTC 的 [`struct_time`](#time.struct_time "time.struct_time")
[`gmtime()`](#time.gmtime "time.gmtime")
seconds since the epoch
本地時間的 [`struct_time`](#time.struct_time "time.struct_time")
[`localtime()`](#time.localtime "time.localtime")
UTC 的 [`struct_time`](#time.struct_time "time.struct_time")
seconds since the epoch
[`calendar.timegm()`](calendar.xhtml#calendar.timegm "calendar.timegm")
本地時間的 [`struct_time`](#time.struct_time "time.struct_time")
seconds since the epoch
[`mktime()`](#time.mktime "time.mktime")
## 函數
`time.``asctime`(\[*t*\])轉換一個元組或 [`struct_time`](#time.struct_time "time.struct_time") 表示的時間,由 [`gmtime()`](#time.gmtime "time.gmtime") 或 [`localtime()`](#time.localtime "time.localtime") 返回為以下形式的字符串: `'Sun Jun 20 23:21:05 1993'` 。如果未提供 *t* ,則使用由 [`localtime()`](#time.localtime "time.localtime") 返回的當前時間。 區域信息不被函數 [`asctime()`](#time.asctime "time.asctime") 使用。
注解
與同名的C函數不同, [`asctime()`](#time.asctime "time.asctime") 不添加尾隨換行符。
`time.``clock`()在Unix上,將當前處理器時間返回為以秒為單位的浮點數。精確度,實際上是“處理器時間”含義的定義,取決于同名C函數的精度。
在Windows上,此函數返回自第一次調用此函數以來經過的 wallclock 秒數,作為浮點數,基于Win32函數 `QueryPerformanceCounter()`。分辨率通常優于1微秒。
Deprecated since version 3.3, will be removed in version 3.8: 此函數的行為取決于平臺:根據你的需求,使用 [`perf_counter()`](#time.perf_counter "time.perf_counter") 或 [`process_time()`](#time.process_time "time.process_time") 獲得具有明確定義的行為。
`time.``pthread_getcpuclockid`(*thread\_id*)返回指定的 *thread\_id* 的特定于線程的CPU時間時鐘的 *clk\_id* 。
使用 [`threading.Thread`](threading.xhtml#threading.Thread "threading.Thread") 對象的 [`threading.get_ident()`](threading.xhtml#threading.get_ident "threading.get_ident") 或 [`ident`](threading.xhtml#threading.Thread.ident "threading.Thread.ident") 屬性為 *thread\_id* 獲取合適的值。
警告
傳遞無效的或過期的 *thread\_id* 可能會導致未定義的行為,例如段錯誤。
[可用性](intro.xhtml#availability) : Unix(有關詳細信息,請參見 *pthread\_getcpuclockid(3)* 的手冊頁)。
3\.7 新版功能.
`time.``clock_getres`(*clk\_id*)返回指定時鐘 *clk\_id* 的分辨率(精度)。有關 *clk\_id* 的可接受值列表,請參閱 [Clock ID 常量](#time-clock-id-constants) 。
[Availability](intro.xhtml#availability): Unix.
3\.3 新版功能.
`time.``clock_gettime`(*clk\_id*) → float返回指定 *clk\_id* 時鐘的時間。有關 *clk\_id* 的可接受值列表,請參閱 [Clock ID 常量](#time-clock-id-constants) 。
[Availability](intro.xhtml#availability): Unix.
3\.3 新版功能.
`time.``clock_gettime_ns`(*clk\_id*) → int與 [`clock_gettime()`](#time.clock_gettime "time.clock_gettime") 相似,但返回時間為納秒。
[Availability](intro.xhtml#availability): Unix.
3\.7 新版功能.
`time.``clock_settime`(*clk\_id*, *time: float*)設置指定 *clk\_id* 時鐘的時間。 目前, [`CLOCK_REALTIME`](#time.CLOCK_REALTIME "time.CLOCK_REALTIME") 是 *clk\_id* 唯一可接受的值。
[Availability](intro.xhtml#availability): Unix.
3\.3 新版功能.
`time.``clock_settime_ns`(*clk\_id*, *time: int*)與 [`clock_settime()`](#time.clock_settime "time.clock_settime") 相似,但設置時間為納秒。
[Availability](intro.xhtml#availability): Unix.
3\.7 新版功能.
`time.``ctime`(\[*secs*\])將自 seconds since the epoch 表示的時間轉換為表示本地時間的字符串。如果未提供 *secs* 或為 [`None`](constants.xhtml#None "None"),則使用由 [`time()`](#time.time "time.time") 返回的當前時間。 `ctime(secs)` 相當于 `asctime(localtime(secs))` 。區域信息不被 [`ctime()`](#time.ctime "time.ctime") 使用。
`time.``get_clock_info`(*name*)獲取有關指定時鐘的信息作為命名空間對象。 支持的時鐘名稱和讀取其值的相應函數是:
- `'clock'`: [`time.clock()`](#time.clock "time.clock")
- `'monotonic'`: [`time.monotonic()`](#time.monotonic "time.monotonic")
- `'perf_counter'`: [`time.perf_counter()`](#time.perf_counter "time.perf_counter")
- `'process_time'`: [`time.process_time()`](#time.process_time "time.process_time")
- `'thread_time'`: [`time.thread_time()`](#time.thread_time "time.thread_time")
- `'time'`: [`time.time()`](#time.time "time.time")
結果具有以下屬性:
- *adjustable* : 如果時鐘可以自動更改(例如通過NTP守護程序)或由系統管理員手動更改,則為 `True` ,否則為 `False` 。
- *implementation* : 用于獲取時鐘值的基礎C函數的名稱。有關可能的值,請參閱 [Clock ID 常量](#time-clock-id-constants) 。
- *monotonic* :如果時鐘不能倒退,則為 `True` ,否則為 `False` 。
- *resolution* : 以秒為單位的時鐘分辨率( [`float`](functions.xhtml#float "float") )
3\.3 新版功能.
`time.``gmtime`(\[*secs*\])將 seconds since the epoch 為單位的時間轉換為UTC的 [`struct_time`](#time.struct_time "time.struct_time") ,其中dst標志始終為零。如果未提供 *secs* 或為 [`None`](constants.xhtml#None "None") ,則使用由 [`time()`](#time.time "time.time") 返回的當前時間。忽略一秒的分數。有關 [`struct_time`](#time.struct_time "time.struct_time") 對象的說明,請參見上文。有關此函數的反函數,請參閱 [`calendar.timegm()`](calendar.xhtml#calendar.timegm "calendar.timegm") 。
`time.``localtime`(\[*secs*\])與 [`gmtime()`](#time.gmtime "time.gmtime") 相似但轉換為當地時間。如果未提供 *secs* 或為 [`None`](constants.xhtml#None "None") ,則使用由 [`time()`](#time.time "time.time") 返回的當前時間。當 DST 適用于給定時間時,dst標志設置為 `1` 。
`time.``mktime`(*t*)這是 [`localtime()`](#time.localtime "time.localtime") 的反函數。它的參數是 [`struct_time`](#time.struct_time "time.struct_time") 或者完整的 9 元組(因為需要 dst 標志;如果它是未知的則使用 `-1` 作為dst標志),它表示 *local* 的時間,而不是 UTC 。它返回一個浮點數,以便與 [`time()`](#time.time "time.time") 兼容。如果輸入值不能表示為有效時間,則 [`OverflowError`](exceptions.xhtml#OverflowError "OverflowError") 或 [`ValueError`](exceptions.xhtml#ValueError "ValueError") 將被引發(這取決于Python或底層C庫是否捕獲到無效值)。它可以生成時間的最早日期取決于平臺。
`time.``monotonic`() → float返回單調時鐘的值(以小數秒為單位),即不能倒退的時鐘。時鐘不受系統時鐘更新的影響。返回值的參考點未定義,因此只有連續調用結果之間的差異才有效。
3\.3 新版功能.
在 3.5 版更改: 該功能現在始終可用且始終在系統范圍內。
`time.``monotonic_ns`() → int與 [`monotonic()`](#time.monotonic "time.monotonic") 相似,但是返回時間為納秒數。
3\.7 新版功能.
`time.``perf_counter`() → float返回性能計數器的值(以小數秒為單位),即具有最高可用分辨率的時鐘,以測量短持續時間。它確實包括睡眠期間經過的時間,并且是系統范圍的。返回值的參考點未定義,因此只有連續調用結果之間的差異才有效。
3\.3 新版功能.
`time.``perf_counter_ns`() → int與 [`perf_counter()`](#time.perf_counter "time.perf_counter") 相似,但是返回時間為納秒。
3\.7 新版功能.
`time.``process_time`() → float返回當前進程的系統和用戶CPU時間總和的值(以小數秒為單位)。它不包括睡眠期間經過的時間。根據定義,它在整個進程范圍中。返回值的參考點未定義,因此只有連續調用結果之間的差異才有效。
3\.3 新版功能.
`time.``process_time_ns`() → int與 [`process_time()`](#time.process_time "time.process_time") 相似,但是返回時間為納秒。
3\.7 新版功能.
`time.``sleep`(*secs*)暫停執行調用線程達到給定的秒數。參數可以是浮點數,以指示更精確的睡眠時間。實際的暫停時間可能小于請求的時間,因為任何捕獲的信號將在執行該信號的捕獲例程后終止 [`sleep()`](#time.sleep "time.sleep") 。此外,由于系統中其他活動的安排,暫停時間可能比請求的時間長任意量。
在 3.5 版更改: 即使睡眠被信號中斷,該函數現在至少睡眠 *secs* ,除非信號處理程序引發異常(參見 [**PEP 475**](https://www.python.org/dev/peps/pep-0475) \[https://www.python.org/dev/peps/pep-0475\] 作為基本原理)。
`time.``strftime`(*format*\[, *t*\])轉換一個元組或 [`struct_time`](#time.struct_time "time.struct_time") 表示的由 [`gmtime()`](#time.gmtime "time.gmtime") 或 [`localtime()`](#time.localtime "time.localtime") 返回的時間到由 *format* 參數指定的字符串。如果未提供 *t* ,則使用由 [`localtime()`](#time.localtime "time.localtime") 返回的當前時間。 *format* 必須是一個字符串。如果 *t* 中的任何字段超出允許范圍,則引發 [`ValueError`](exceptions.xhtml#ValueError "ValueError") 。
0是時間元組中任何位置的合法參數;如果它通常是非法的,則該值被強制改為正確的值。
以下指令可以嵌入 *format* 字符串中。它們顯示時沒有可選的字段寬度和精度規范,并被 [`strftime()`](#time.strftime "time.strftime") 結果中的指示字符替換:
指令
意義
注釋
`%a`
本地化的縮寫星期中每日的名稱。
`%A`
本地化的星期中每日的完整名稱。
`%b`
本地化的月縮寫名稱。
`%B`
本地化的月完整名稱。
`%c`
本地化的適當日期和時間表示。
`%d`
十進制數 \[01,31\] 表示的月中日。
`%H`
十進制數 \[00,23\] 表示的小時(24小時制)。
`%I`
十進制數 \[01,12\] 表示的小時(12小時制)。
`%j`
十進制數 \[001,366\] 表示的年中日。
`%m`
十進制數 \[01,12\] 表示的月。
`%M`
十進制數 \[00,59\] 表示的分鐘。
`%p`
本地化的 AM 或 PM 。
(1)
`%S`
十進制數 \[00,61\] 表示的秒。
(2)
`%U`
十進制數 \[00,53\] 表示的一年中的周數(星期日作為一周的第一天)作為。在第一個星期日之前的新年中的所有日子都被認為是在第0周。
(3)
`%w`
十進制數 \[0(星期日),6\] 表示的周中日。
`%W`
十進制數 \[00,53\] 表示的一年中的周數(星期一作為一周的第一天)作為。在第一個星期一之前的新年中的所有日子被認為是在第0周。
(3)
`%x`
本地化的適當日期表示。
`%X`
本地化的適當時間表示。
`%y`
十進制數 \[00,99\] 表示的沒有世紀的年份。
`%Y`
十進制數表示的帶世紀的年份。
`%z`
時區偏移以格式 +HHMM 或 -HHMM 形式的 UTC/GMT 的正或負時差指示,其中H表示十進制小時數字,M表示小數分鐘數字 \[-23:59, +23:59\] 。
`%Z`
時區名稱(如果不存在時區,則不包含字符)。
`%%`
字面的 `'%'` 字符。
注釋:
1. 當與 [`strptime()`](#time.strptime "time.strptime") 函數一起使用時,如果使用 `%I` 指令來解析小時, `%p` 指令只影響輸出小時字段。
2. 范圍真的是 `0` 到 `61` ;值 `60` 在表示 [leap seconds](https://en.wikipedia.org/wiki/Leap_second) \[https://en.wikipedia.org/wiki/Leap\_second\] 的時間戳中有效,并且由于歷史原因支持值 `61` 。
3. 當與 [`strptime()`](#time.strptime "time.strptime") 函數一起使用時, `%U` 和 `%W` 僅用于指定星期幾和年份的計算。
下面是一個示例,一個與 [**RFC 2822**](https://tools.ietf.org/html/rfc2822.html) \[https://tools.ietf.org/html/rfc2822.html\] Internet電子郵件標準以兼容的日期格式。 [1](#id2)
```
>>> from time import gmtime, strftime
>>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Thu, 28 Jun 2001 14:17:15 +0000'
```
某些平臺可能支持其他指令,但只有此處列出的指令具有 ANSI C 標準化的含義。要查看平臺支持的完整格式代碼集,請參閱 *strftime(3)* 文檔。
在某些平臺上,可選的字段寬度和精度規范可以按照以下順序緊跟在指令的初始 `'%'` 之后;這也不可移植。字段寬度通常為2,除了 `%j` ,它是3。
`time.``strptime`(*string*\[, *format*\])根據格式解析表示時間的字符串。 返回值為一個被 [`gmtime()`](#time.gmtime "time.gmtime") 或 [`localtime()`](#time.localtime "time.localtime") 返回的 [`struct_time`](#time.struct_time "time.struct_time") 。
*format* 參數使用與 [`strftime()`](#time.strftime "time.strftime") ;使用的指令相同的指令。它默認為匹配 [`ctime()`](#time.ctime "time.ctime") 返回格式的 `"%a %b %d %H:%M:%S %Y"`` 。如果 *string\*不能根據 \*format* 解析,或者解析后它有多余的數據,則引發 [`ValueError`](exceptions.xhtml#ValueError "ValueError") 。當無法推斷出更準確的值時,用于填充任何缺失數據的默認值是 `(1900, 1, 1, 0, 0, 0, 0, 1, -1)` 。 *string* 和 *format* 都必須是字符串。
例如:
```
>>> import time
>>> time.strptime("30 Nov 00", "%d %b %y") # doctest: +NORMALIZE_WHITESPACE
time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0,
tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)
```
支持 `%Z` 指令是基于 `tzname` 中包含的值以及 `daylight` 是否為真。因此,它是特定于平臺的,除了識別始終已知的 UTC 和 GMT (并且被認為是非夏令時時區)。
僅支持文檔中指定的指令。因為每個平臺都實現了 `strftime()` ,它有時會提供比列出的指令更多的指令。但是 `strptime()` 獨立于任何平臺,因此不一定支持所有未記錄為支持的可用指令。
*class* `time.``struct_time`返回的時間值序列的類型為 [`gmtime()`](#time.gmtime "time.gmtime") 、 [`localtime()`](#time.localtime "time.localtime") 和 [`strptime()`](#time.strptime "time.strptime") 。它是一個帶有 [named tuple](../glossary.xhtml#term-named-tuple) 接口的對象:可以通過索引和屬性名訪問值。 存在以下值:
索引
屬性
值
0
`tm_year`
(例如,1993)
1
`tm_mon`
range \[1, 12\]
2
`tm_mday`
range \[1, 31\]
3
`tm_hour`
range \[0, 23\]
4
`tm_min`
range \[0, 59\]
5
`tm_sec`
range \[0, 61\]; 見 [`strftime()`](#time.strftime "time.strftime") 介紹中的 **(2)**
6
`tm_wday`
range \[0, 6\] ,周一為 0
7
`tm_yday`
range \[1, 366\]
8
`tm_isdst`
0, 1 或 -1;如下所示
N/A
`tm_zone`
時區名稱的縮寫
N/A
`tm_gmtoff`
以秒為單位的UTC以東偏離
請注意,與C結構不同,月份值是 \[1,12\] 的范圍,而不是 \[0,11\] 。
在調用 [`mktime()`](#time.mktime "time.mktime") 時, `tm_isdst` 可以在夏令時生效時設置為1,而在夏令時不生效時設置為0。 值-1表示這是未知的,并且通常會導致填寫正確的狀態。
當一個長度不正確的元組被傳遞給期望 [`struct_time`](#time.struct_time "time.struct_time") 的函數,或者具有錯誤類型的元素時,會引發 [`TypeError`](exceptions.xhtml#TypeError "TypeError") 。
`time.``time`() → float返回浮點數的 seconds since the [epoch](#epoch) 。epoch 的具體日期和 [leap seconds](https://en.wikipedia.org/wiki/Leap_second) \[https://en.wikipedia.org/wiki/Leap\_second\] 的處理取決于平臺。在Windows和大多數Unix系統上, epoch 是1970年1月1日00:00:00(UTC),并且閏秒不計入 seconds since the epoch 。 這通常被稱為 [Unix time](https://en.wikipedia.org/wiki/Unix_time) \[https://en.wikipedia.org/wiki/Unix\_time\] 。 要找出給定平臺上的 epoch ,請查看 `gmtime(0)` 。
請注意,即使時間總是作為浮點數返回,但并非所有系統都提供高于1秒的精度。雖然此函數通常返回非遞減值,但如果在兩次調用之間設置了系統時鐘,則它可以返回比先前調用更低的值。
返回的數字 [`time()`](#time.time "time.time") 可以通過將其傳遞給 [`gmtime()`](#time.gmtime "time.gmtime") 函數或轉換為UTC中更常見的時間格式(即年、月、日、小時等)或通過將它傳遞給 [`localtime()`](#time.localtime "time.localtime") 函數獲得本地時間。在這兩種情況下都返回一個 [`struct_time`](#time.struct_time "time.struct_time") 對象,日歷日期組件可以從中作為屬性訪問。
`time.``thread_time`() → float返回當前線程的系統和用戶CPU時間之和的值(以小數秒為單位)。它不包括睡眠期間經過的時間。根據定義,它是特定于線程的。返回值的參考點未定義,因此只有同一線程中連續調用結果之間的差異才有效。
[可用性](intro.xhtml#availability) : Windows、 Linux、 Unix 系統支持 `CLOCK_THREAD_CPUTIME_ID` 。
3\.7 新版功能.
`time.``thread_time_ns`() → int與 [`thread_time()`](#time.thread_time "time.thread_time") 相似,但返回納秒時間。
3\.7 新版功能.
`time.``time_ns`() → int與 [`time()`](#module-time "time: Time access and conversions.") 相似,但返回時間為 [epoch](#epoch) 以來的整數納秒。
3\.7 新版功能.
`time.``tzset`()重置庫例程使用的時間轉換規則。環境變量 `TZ` 指定如何完成。它還將設置變量 `tzname` (來自 `TZ` 環境變量), `timezone` (UTC的西部非DST秒), `altzone` (UTC以西的DST秒)和 `daylight` (如果此時區沒有任何夏令時規則則為0,如果有夏令時適用的時間,無論過去、現在或未來,則為非零)。
[Availability](intro.xhtml#availability): Unix.
注解
雖然在很多情況下,更改 `TZ` 環境變量而不調用 [`tzset()`](#time.tzset "time.tzset") 可能會影響函數的輸出,例如 [`localtime()`](#time.localtime "time.localtime") ,不應該依賴此行為。
`TZ` 不應該包含空格。
`TZ` 環境變量的標準格式是(為了清晰起見,添加了空格):
```
std offset [dst [offset [,start[/time], end[/time]]]]
```
組件的位置是:
`std` 和 `dst`三個或更多字母數字,給出時區縮寫。這些將傳到 time.tzname
`offset`偏移量的形式為: `± hh[:mm[:ss]]` 。這表示添加到達UTC的本地時間的值。如果前面有 '-' ,則時區位于本初子午線的東邊;否則,在它是西邊。如果dst之后沒有偏移,則假設夏令時比標準時間提前一小時。
`start[/time], end[/time]`指示何時更改為DST和從DST返回。開始日期和結束日期的格式為以下之一:
`Jn`Julian日 *n* (1 <= *n* <= 365)。閏日不計算在內,因此在所有年份中,2月28日是第59天,3月1日是第60天。
`n`從零開始的Julian日(0 <= *n* <= 365)。 閏日計入,可以引用2月29日。
`Mm.n.d`一年中 *m* 月的第 *n* 周(1 <= *n* <= 5 ,1 <= *m* <= 12 ,第 5 周表示 “可能在 *m* 月第 4 周或第 5 周出現的最后第 *d* 日”)的第 *d* 天(0 <= *d* <= 6)。 第 1 周是第 *d* 天發生的第一周。 第 0 天是星期天。
`time` 的格式與 `offset` 的格式相同,但不允許使用前導符號( '-' 或 '+' )。如果沒有給出時間,則默認值為02:00:00。
```
>>> os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0'
>>> time.tzset()
>>> time.strftime('%X %x %Z')
'02:07:36 05/08/03 EDT'
>>> os.environ['TZ'] = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
>>> time.tzset()
>>> time.strftime('%X %x %Z')
'16:08:12 05/08/03 AEST'
```
在許多Unix系統(包括 \*BSD , Linux , Solaris 和 Darwin 上),使用系統的區域信息( *tzfile(5)* )數據庫來指定時區規則會更方便。為此,將 `TZ` 環境變量設置為所需時區數據文件的路徑,相對于系統 'zoneinfo' 時區數據庫的根目錄,通常位于 `/usr/share/zoneinfo` 。 例如,`'US/Eastern'` 、 `'Australia/Melbourne'` 、 `'Egypt'` 或 `'Europe/Amsterdam'`。
```
>>> os.environ['TZ'] = 'US/Eastern'
>>> time.tzset()
>>> time.tzname
('EST', 'EDT')
>>> os.environ['TZ'] = 'Egypt'
>>> time.tzset()
>>> time.tzname
('EET', 'EEST')
```
## Clock ID 常量
這些常量用作 [`clock_getres()`](#time.clock_getres "time.clock_getres") 和 [`clock_gettime()`](#time.clock_gettime "time.clock_gettime") 的參數。
`time.``CLOCK_BOOTTIME`與 [`CLOCK_MONOTONIC`](#time.CLOCK_MONOTONIC "time.CLOCK_MONOTONIC") 相同,除了它還包括系統暫停的任何時間。
這允許應用程序獲得一個暫停感知的單調時鐘,而不必處理 [`CLOCK_REALTIME`](#time.CLOCK_REALTIME "time.CLOCK_REALTIME") 的復雜性,如果使用 `settimeofday()` 或類似的時間更改時間可能會有不連續性。
[可用性](intro.xhtml#availability): Linux 2.6.39 或更新
3\.7 新版功能.
`time.``CLOCK_HIGHRES`Solaris OS 有一個 `CLOCK_HIGHRES` 計時器,試圖使用最佳硬件源,并可能提供接近納秒的分辨率。 `CLOCK_HIGHRES` 是不可調節的高分辨率時鐘。
[可用性](intro.xhtml#availability): Solaris.
3\.3 新版功能.
`time.``CLOCK_MONOTONIC`無法設置的時鐘,表示自某些未指定的起點以來的單調時間。
[Availability](intro.xhtml#availability): Unix.
3\.3 新版功能.
`time.``CLOCK_MONOTONIC_RAW`類似于 [`CLOCK_MONOTONIC`](#time.CLOCK_MONOTONIC "time.CLOCK_MONOTONIC") ,但可以訪問不受NTP調整影響的原始硬件時間。
[可用性](intro.xhtml#availability): Linux 2.6.28 和更新版本, macOS 10.12 和更新版本。
3\.3 新版功能.
`time.``CLOCK_PROCESS_CPUTIME_ID`來自CPU的高分辨率每進程計時器。
[Availability](intro.xhtml#availability): Unix.
3\.3 新版功能.
`time.``CLOCK_PROF`來自CPU的高分辨率每進程計時器。
[可用性](intro.xhtml#availability): FreeBSD, NetBSD 7 或更新, OpenBSD.
3\.7 新版功能.
`time.``CLOCK_THREAD_CPUTIME_ID`特定于線程的CPU時鐘。
[Availability](intro.xhtml#availability): Unix.
3\.3 新版功能.
`time.``CLOCK_UPTIME`該時間的絕對值是系統運行且未暫停的時間,提供準確的正常運行時間測量,包括絕對值和間隔值。
[可用性](intro.xhtml#availability): FreeBSD, OpenBSD 5.5 或更新。
3\.7 新版功能.
以下常量是唯一可以發送到 [`clock_settime()`](#time.clock_settime "time.clock_settime") 的參數。
`time.``CLOCK_REALTIME`系統范圍的實時時鐘。 設置此時鐘需要適當的權限。
[Availability](intro.xhtml#availability): Unix.
3\.3 新版功能.
## 時區常量
`time.``altzone`本地DST時區的偏移量,以UTC為單位的秒數,如果已定義。如果當地DST時區在UTC以東(如在西歐,包括英國),則是負數。 只有當 `daylight` 非零時才使用它。 見下面的注釋。
`time.``daylight`如果定義了DST時區,則為非零。 見下面的注釋。
`time.``timezone`本地(非DST)時區的偏移量,UTC以西的秒數(西歐大部分地區為負,美國為正,英國為零)。 見下面的注釋。
`time.``tzname`兩個字符串的元組:第一個是本地非DST時區的名稱,第二個是本地DST時區的名稱。 如果未定義DST時區,則不應使用第二個字符串。 見下面的注釋。
注解
對于上述時區常量( [`altzone`](#time.altzone "time.altzone") 、 [`daylight`](#time.daylight "time.daylight") 、 [`timezone`](#time.timezone "time.timezone") 和 [`tzname`](#time.tzname "time.tzname") ),該值由模塊加載時有效的時區規則確定,或者最后一次 [`tzset()`](#time.tzset "time.tzset") 被調用時,并且在過去的時間可能不正確。建議使用來自 [`localtime()`](#time.localtime "time.localtime") 結果的 `tm_gmtoff` 和 `tm_zone` 來獲取時區信息。
參見
模塊 [`datetime`](datetime.xhtml#module-datetime "datetime: Basic date and time types.")更多面向對象的日期和時間接口。
模塊 [`locale`](locale.xhtml#module-locale "locale: Internationalization services.")國際化服務。 區域設置會影響 [`strftime()`](#time.strftime "time.strftime") 和 [`strptime()`](#time.strptime "time.strptime") 中許多格式說明符的解析。
模塊 [`calendar`](calendar.xhtml#module-calendar "calendar: Functions for working with calendars, including some emulation of the Unix cal program.")一般日歷相關功能。這個模塊的 `timegm()` 是函數 [`gmtime()`](#time.gmtime "time.gmtime") 的反函數。
腳注
[1](#id1)現在不推薦使用 `%Z` ,但是所有 ANSI C 庫都不支持擴展為首選小時/分鐘偏移量的``%z``轉義符。 此外,嚴格的 1982 年原始 [**RFC 822**](https://tools.ietf.org/html/rfc822.html) \[https://tools.ietf.org/html/rfc822.html\] 標準要求兩位數的年份(%y而不是%Y),但是實際在2000年之前很久就轉移到了4位數年。之后, [**RFC 822**](https://tools.ietf.org/html/rfc822.html) \[https://tools.ietf.org/html/rfc822.html\] 已經廢棄了,4位數的年份首先被推薦 [**RFC 1123**](https://tools.ietf.org/html/rfc1123.html) \[https://tools.ietf.org/html/rfc1123.html\] ,然后被 [**RFC 2822**](https://tools.ietf.org/html/rfc2822.html) \[https://tools.ietf.org/html/rfc2822.html\] 強制執行。
### 導航
- [索引](../genindex.xhtml "總目錄")
- [模塊](../py-modindex.xhtml "Python 模塊索引") |
- [下一頁](argparse.xhtml "argparse --- 命令行選項、參數和子命令解析器") |
- [上一頁](io.xhtml "io --- 處理流的核心工具") |
- 
- [Python](https://www.python.org/) ?
- zh\_CN 3.7.3 [文檔](../index.xhtml) ?
- [Python 標準庫](index.xhtml) ?
- [通用操作系統服務](allos.xhtml) ?
- $('.inline-search').show(0); |
? [版權所有](../copyright.xhtml) 2001-2019, Python Software Foundation.
Python 軟件基金會是一個非盈利組織。 [請捐助。](https://www.python.org/psf/donations/)
最后更新于 5月 21, 2019. [發現了問題](../bugs.xhtml)?
使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 創建。
- Python文檔內容
- Python 有什么新變化?
- Python 3.7 有什么新變化
- 摘要 - 發布重點
- 新的特性
- 其他語言特性修改
- 新增模塊
- 改進的模塊
- C API 的改變
- 構建的改變
- 性能優化
- 其他 CPython 實現的改變
- 已棄用的 Python 行為
- 已棄用的 Python 模塊、函數和方法
- 已棄用的 C API 函數和類型
- 平臺支持的移除
- API 與特性的移除
- 移除的模塊
- Windows 專屬的改變
- 移植到 Python 3.7
- Python 3.7.1 中的重要變化
- Python 3.7.2 中的重要變化
- Python 3.6 有什么新變化A
- 摘要 - 發布重點
- 新的特性
- 其他語言特性修改
- 新增模塊
- 改進的模塊
- 性能優化
- Build and C API Changes
- 其他改進
- 棄用
- 移除
- 移植到Python 3.6
- Python 3.6.2 中的重要變化
- Python 3.6.4 中的重要變化
- Python 3.6.5 中的重要變化
- Python 3.6.7 中的重要變化
- Python 3.5 有什么新變化
- 摘要 - 發布重點
- 新的特性
- 其他語言特性修改
- 新增模塊
- 改進的模塊
- Other module-level changes
- 性能優化
- Build and C API Changes
- 棄用
- 移除
- Porting to Python 3.5
- Notable changes in Python 3.5.4
- What's New In Python 3.4
- 摘要 - 發布重點
- 新的特性
- 新增模塊
- 改進的模塊
- CPython Implementation Changes
- 棄用
- 移除
- Porting to Python 3.4
- Changed in 3.4.3
- What's New In Python 3.3
- 摘要 - 發布重點
- PEP 405: Virtual Environments
- PEP 420: Implicit Namespace Packages
- PEP 3118: New memoryview implementation and buffer protocol documentation
- PEP 393: Flexible String Representation
- PEP 397: Python Launcher for Windows
- PEP 3151: Reworking the OS and IO exception hierarchy
- PEP 380: Syntax for Delegating to a Subgenerator
- PEP 409: Suppressing exception context
- PEP 414: Explicit Unicode literals
- PEP 3155: Qualified name for classes and functions
- PEP 412: Key-Sharing Dictionary
- PEP 362: Function Signature Object
- PEP 421: Adding sys.implementation
- Using importlib as the Implementation of Import
- 其他語言特性修改
- A Finer-Grained Import Lock
- Builtin functions and types
- 新增模塊
- 改進的模塊
- 性能優化
- Build and C API Changes
- 棄用
- Porting to Python 3.3
- What's New In Python 3.2
- PEP 384: Defining a Stable ABI
- PEP 389: Argparse Command Line Parsing Module
- PEP 391: Dictionary Based Configuration for Logging
- PEP 3148: The concurrent.futures module
- PEP 3147: PYC Repository Directories
- PEP 3149: ABI Version Tagged .so Files
- PEP 3333: Python Web Server Gateway Interface v1.0.1
- 其他語言特性修改
- New, Improved, and Deprecated Modules
- 多線程
- 性能優化
- Unicode
- Codecs
- 文檔
- IDLE
- Code Repository
- Build and C API Changes
- Porting to Python 3.2
- What's New In Python 3.1
- PEP 372: Ordered Dictionaries
- PEP 378: Format Specifier for Thousands Separator
- 其他語言特性修改
- New, Improved, and Deprecated Modules
- 性能優化
- IDLE
- Build and C API Changes
- Porting to Python 3.1
- What's New In Python 3.0
- Common Stumbling Blocks
- Overview Of Syntax Changes
- Changes Already Present In Python 2.6
- Library Changes
- PEP 3101: A New Approach To String Formatting
- Changes To Exceptions
- Miscellaneous Other Changes
- Build and C API Changes
- 性能
- Porting To Python 3.0
- What's New in Python 2.7
- The Future for Python 2.x
- Changes to the Handling of Deprecation Warnings
- Python 3.1 Features
- PEP 372: Adding an Ordered Dictionary to collections
- PEP 378: Format Specifier for Thousands Separator
- PEP 389: The argparse Module for Parsing Command Lines
- PEP 391: Dictionary-Based Configuration For Logging
- PEP 3106: Dictionary Views
- PEP 3137: The memoryview Object
- 其他語言特性修改
- New and Improved Modules
- Build and C API Changes
- Other Changes and Fixes
- Porting to Python 2.7
- New Features Added to Python 2.7 Maintenance Releases
- Acknowledgements
- Python 2.6 有什么新變化
- Python 3.0
- Changes to the Development Process
- PEP 343: The 'with' statement
- PEP 366: Explicit Relative Imports From a Main Module
- PEP 370: Per-user site-packages Directory
- PEP 371: The multiprocessing Package
- PEP 3101: Advanced String Formatting
- PEP 3105: print As a Function
- PEP 3110: Exception-Handling Changes
- PEP 3112: Byte Literals
- PEP 3116: New I/O Library
- PEP 3118: Revised Buffer Protocol
- PEP 3119: Abstract Base Classes
- PEP 3127: Integer Literal Support and Syntax
- PEP 3129: Class Decorators
- PEP 3141: A Type Hierarchy for Numbers
- 其他語言特性修改
- New and Improved Modules
- Deprecations and Removals
- Build and C API Changes
- Porting to Python 2.6
- Acknowledgements
- What's New in Python 2.5
- PEP 308: Conditional Expressions
- PEP 309: Partial Function Application
- PEP 314: Metadata for Python Software Packages v1.1
- PEP 328: Absolute and Relative Imports
- PEP 338: Executing Modules as Scripts
- PEP 341: Unified try/except/finally
- PEP 342: New Generator Features
- PEP 343: The 'with' statement
- PEP 352: Exceptions as New-Style Classes
- PEP 353: Using ssize_t as the index type
- PEP 357: The 'index' method
- 其他語言特性修改
- New, Improved, and Removed Modules
- Build and C API Changes
- Porting to Python 2.5
- Acknowledgements
- What's New in Python 2.4
- PEP 218: Built-In Set Objects
- PEP 237: Unifying Long Integers and Integers
- PEP 289: Generator Expressions
- PEP 292: Simpler String Substitutions
- PEP 318: Decorators for Functions and Methods
- PEP 322: Reverse Iteration
- PEP 324: New subprocess Module
- PEP 327: Decimal Data Type
- PEP 328: Multi-line Imports
- PEP 331: Locale-Independent Float/String Conversions
- 其他語言特性修改
- New, Improved, and Deprecated Modules
- Build and C API Changes
- Porting to Python 2.4
- Acknowledgements
- What's New in Python 2.3
- PEP 218: A Standard Set Datatype
- PEP 255: Simple Generators
- PEP 263: Source Code Encodings
- PEP 273: Importing Modules from ZIP Archives
- PEP 277: Unicode file name support for Windows NT
- PEP 278: Universal Newline Support
- PEP 279: enumerate()
- PEP 282: The logging Package
- PEP 285: A Boolean Type
- PEP 293: Codec Error Handling Callbacks
- PEP 301: Package Index and Metadata for Distutils
- PEP 302: New Import Hooks
- PEP 305: Comma-separated Files
- PEP 307: Pickle Enhancements
- Extended Slices
- 其他語言特性修改
- New, Improved, and Deprecated Modules
- Pymalloc: A Specialized Object Allocator
- Build and C API Changes
- Other Changes and Fixes
- Porting to Python 2.3
- Acknowledgements
- What's New in Python 2.2
- 概述
- PEPs 252 and 253: Type and Class Changes
- PEP 234: Iterators
- PEP 255: Simple Generators
- PEP 237: Unifying Long Integers and Integers
- PEP 238: Changing the Division Operator
- Unicode Changes
- PEP 227: Nested Scopes
- New and Improved Modules
- Interpreter Changes and Fixes
- Other Changes and Fixes
- Acknowledgements
- What's New in Python 2.1
- 概述
- PEP 227: Nested Scopes
- PEP 236: future Directives
- PEP 207: Rich Comparisons
- PEP 230: Warning Framework
- PEP 229: New Build System
- PEP 205: Weak References
- PEP 232: Function Attributes
- PEP 235: Importing Modules on Case-Insensitive Platforms
- PEP 217: Interactive Display Hook
- PEP 208: New Coercion Model
- PEP 241: Metadata in Python Packages
- New and Improved Modules
- Other Changes and Fixes
- Acknowledgements
- What's New in Python 2.0
- 概述
- What About Python 1.6?
- New Development Process
- Unicode
- 列表推導式
- Augmented Assignment
- 字符串的方法
- Garbage Collection of Cycles
- Other Core Changes
- Porting to 2.0
- Extending/Embedding Changes
- Distutils: Making Modules Easy to Install
- XML Modules
- Module changes
- New modules
- IDLE Improvements
- Deleted and Deprecated Modules
- Acknowledgements
- 更新日志
- Python 下一版
- Python 3.7.3 最終版
- Python 3.7.3 發布候選版 1
- Python 3.7.2 最終版
- Python 3.7.2 發布候選版 1
- Python 3.7.1 最終版
- Python 3.7.1 RC 2版本
- Python 3.7.1 發布候選版 1
- Python 3.7.0 正式版
- Python 3.7.0 release candidate 1
- Python 3.7.0 beta 5
- Python 3.7.0 beta 4
- Python 3.7.0 beta 3
- Python 3.7.0 beta 2
- Python 3.7.0 beta 1
- Python 3.7.0 alpha 4
- Python 3.7.0 alpha 3
- Python 3.7.0 alpha 2
- Python 3.7.0 alpha 1
- Python 3.6.6 final
- Python 3.6.6 RC 1
- Python 3.6.5 final
- Python 3.6.5 release candidate 1
- Python 3.6.4 final
- Python 3.6.4 release candidate 1
- Python 3.6.3 final
- Python 3.6.3 release candidate 1
- Python 3.6.2 final
- Python 3.6.2 release candidate 2
- Python 3.6.2 release candidate 1
- Python 3.6.1 final
- Python 3.6.1 release candidate 1
- Python 3.6.0 final
- Python 3.6.0 release candidate 2
- Python 3.6.0 release candidate 1
- Python 3.6.0 beta 4
- Python 3.6.0 beta 3
- Python 3.6.0 beta 2
- Python 3.6.0 beta 1
- Python 3.6.0 alpha 4
- Python 3.6.0 alpha 3
- Python 3.6.0 alpha 2
- Python 3.6.0 alpha 1
- Python 3.5.5 final
- Python 3.5.5 release candidate 1
- Python 3.5.4 final
- Python 3.5.4 release candidate 1
- Python 3.5.3 final
- Python 3.5.3 release candidate 1
- Python 3.5.2 final
- Python 3.5.2 release candidate 1
- Python 3.5.1 final
- Python 3.5.1 release candidate 1
- Python 3.5.0 final
- Python 3.5.0 release candidate 4
- Python 3.5.0 release candidate 3
- Python 3.5.0 release candidate 2
- Python 3.5.0 release candidate 1
- Python 3.5.0 beta 4
- Python 3.5.0 beta 3
- Python 3.5.0 beta 2
- Python 3.5.0 beta 1
- Python 3.5.0 alpha 4
- Python 3.5.0 alpha 3
- Python 3.5.0 alpha 2
- Python 3.5.0 alpha 1
- Python 教程
- 課前甜點
- 使用 Python 解釋器
- 調用解釋器
- 解釋器的運行環境
- Python 的非正式介紹
- Python 作為計算器使用
- 走向編程的第一步
- 其他流程控制工具
- if 語句
- for 語句
- range() 函數
- break 和 continue 語句,以及循環中的 else 子句
- pass 語句
- 定義函數
- 函數定義的更多形式
- 小插曲:編碼風格
- 數據結構
- 列表的更多特性
- del 語句
- 元組和序列
- 集合
- 字典
- 循環的技巧
- 深入條件控制
- 序列和其它類型的比較
- 模塊
- 有關模塊的更多信息
- 標準模塊
- dir() 函數
- 包
- 輸入輸出
- 更漂亮的輸出格式
- 讀寫文件
- 錯誤和異常
- 語法錯誤
- 異常
- 處理異常
- 拋出異常
- 用戶自定義異常
- 定義清理操作
- 預定義的清理操作
- 類
- 名稱和對象
- Python 作用域和命名空間
- 初探類
- 補充說明
- 繼承
- 私有變量
- 雜項說明
- 迭代器
- 生成器
- 生成器表達式
- 標準庫簡介
- 操作系統接口
- 文件通配符
- 命令行參數
- 錯誤輸出重定向和程序終止
- 字符串模式匹配
- 數學
- 互聯網訪問
- 日期和時間
- 數據壓縮
- 性能測量
- 質量控制
- 自帶電池
- 標準庫簡介 —— 第二部分
- 格式化輸出
- 模板
- 使用二進制數據記錄格式
- 多線程
- 日志
- 弱引用
- 用于操作列表的工具
- 十進制浮點運算
- 虛擬環境和包
- 概述
- 創建虛擬環境
- 使用pip管理包
- 接下來?
- 交互式編輯和編輯歷史
- Tab 補全和編輯歷史
- 默認交互式解釋器的替代品
- 浮點算術:爭議和限制
- 表示性錯誤
- 附錄
- 交互模式
- 安裝和使用 Python
- 命令行與環境
- 命令行
- 環境變量
- 在Unix平臺中使用Python
- 獲取最新版本的Python
- 構建Python
- 與Python相關的路徑和文件
- 雜項
- 編輯器和集成開發環境
- 在Windows上使用 Python
- 完整安裝程序
- Microsoft Store包
- nuget.org 安裝包
- 可嵌入的包
- 替代捆綁包
- 配置Python
- 適用于Windows的Python啟動器
- 查找模塊
- 附加模塊
- 在Windows上編譯Python
- 其他平臺
- 在蘋果系統上使用 Python
- 獲取和安裝 MacPython
- IDE
- 安裝額外的 Python 包
- Mac 上的圖形界面編程
- 在 Mac 上分發 Python 應用程序
- 其他資源
- Python 語言參考
- 概述
- 其他實現
- 標注
- 詞法分析
- 行結構
- 其他形符
- 標識符和關鍵字
- 字面值
- 運算符
- 分隔符
- 數據模型
- 對象、值與類型
- 標準類型層級結構
- 特殊方法名稱
- 協程
- 執行模型
- 程序的結構
- 命名與綁定
- 異常
- 導入系統
- importlib
- 包
- 搜索
- 加載
- 基于路徑的查找器
- 替換標準導入系統
- Package Relative Imports
- 有關 main 的特殊事項
- 開放問題項
- 參考文獻
- 表達式
- 算術轉換
- 原子
- 原型
- await 表達式
- 冪運算符
- 一元算術和位運算
- 二元算術運算符
- 移位運算
- 二元位運算
- 比較運算
- 布爾運算
- 條件表達式
- lambda 表達式
- 表達式列表
- 求值順序
- 運算符優先級
- 簡單語句
- 表達式語句
- 賦值語句
- assert 語句
- pass 語句
- del 語句
- return 語句
- yield 語句
- raise 語句
- break 語句
- continue 語句
- import 語句
- global 語句
- nonlocal 語句
- 復合語句
- if 語句
- while 語句
- for 語句
- try 語句
- with 語句
- 函數定義
- 類定義
- 協程
- 最高層級組件
- 完整的 Python 程序
- 文件輸入
- 交互式輸入
- 表達式輸入
- 完整的語法規范
- Python 標準庫
- 概述
- 可用性注釋
- 內置函數
- 內置常量
- 由 site 模塊添加的常量
- 內置類型
- 邏輯值檢測
- 布爾運算 — and, or, not
- 比較
- 數字類型 — int, float, complex
- 迭代器類型
- 序列類型 — list, tuple, range
- 文本序列類型 — str
- 二進制序列類型 — bytes, bytearray, memoryview
- 集合類型 — set, frozenset
- 映射類型 — dict
- 上下文管理器類型
- 其他內置類型
- 特殊屬性
- 內置異常
- 基類
- 具體異常
- 警告
- 異常層次結構
- 文本處理服務
- string — 常見的字符串操作
- re — 正則表達式操作
- 模塊 difflib 是一個計算差異的助手
- textwrap — Text wrapping and filling
- unicodedata — Unicode 數據庫
- stringprep — Internet String Preparation
- readline — GNU readline interface
- rlcompleter — GNU readline的完成函數
- 二進制數據服務
- struct — Interpret bytes as packed binary data
- codecs — Codec registry and base classes
- 數據類型
- datetime — 基礎日期/時間數據類型
- calendar — General calendar-related functions
- collections — 容器數據類型
- collections.abc — 容器的抽象基類
- heapq — 堆隊列算法
- bisect — Array bisection algorithm
- array — Efficient arrays of numeric values
- weakref — 弱引用
- types — Dynamic type creation and names for built-in types
- copy — 淺層 (shallow) 和深層 (deep) 復制操作
- pprint — 數據美化輸出
- reprlib — Alternate repr() implementation
- enum — Support for enumerations
- 數字和數學模塊
- numbers — 數字的抽象基類
- math — 數學函數
- cmath — Mathematical functions for complex numbers
- decimal — 十進制定點和浮點運算
- fractions — 分數
- random — 生成偽隨機數
- statistics — Mathematical statistics functions
- 函數式編程模塊
- itertools — 為高效循環而創建迭代器的函數
- functools — 高階函數和可調用對象上的操作
- operator — 標準運算符替代函數
- 文件和目錄訪問
- pathlib — 面向對象的文件系統路徑
- os.path — 常見路徑操作
- fileinput — Iterate over lines from multiple input streams
- stat — Interpreting stat() results
- filecmp — File and Directory Comparisons
- tempfile — Generate temporary files and directories
- glob — Unix style pathname pattern expansion
- fnmatch — Unix filename pattern matching
- linecache — Random access to text lines
- shutil — High-level file operations
- macpath — Mac OS 9 路徑操作函數
- 數據持久化
- pickle —— Python 對象序列化
- copyreg — Register pickle support functions
- shelve — Python object persistence
- marshal — Internal Python object serialization
- dbm — Interfaces to Unix “databases”
- sqlite3 — SQLite 數據庫 DB-API 2.0 接口模塊
- 數據壓縮和存檔
- zlib — 與 gzip 兼容的壓縮
- gzip — 對 gzip 格式的支持
- bz2 — 對 bzip2 壓縮算法的支持
- lzma — 用 LZMA 算法壓縮
- zipfile — 在 ZIP 歸檔中工作
- tarfile — Read and write tar archive files
- 文件格式
- csv — CSV 文件讀寫
- configparser — Configuration file parser
- netrc — netrc file processing
- xdrlib — Encode and decode XDR data
- plistlib — Generate and parse Mac OS X .plist files
- 加密服務
- hashlib — 安全哈希與消息摘要
- hmac — 基于密鑰的消息驗證
- secrets — Generate secure random numbers for managing secrets
- 通用操作系統服務
- os — 操作系統接口模塊
- io — 處理流的核心工具
- time — 時間的訪問和轉換
- argparse — 命令行選項、參數和子命令解析器
- getopt — C-style parser for command line options
- 模塊 logging — Python 的日志記錄工具
- logging.config — 日志記錄配置
- logging.handlers — Logging handlers
- getpass — 便攜式密碼輸入工具
- curses — 終端字符單元顯示的處理
- curses.textpad — Text input widget for curses programs
- curses.ascii — Utilities for ASCII characters
- curses.panel — A panel stack extension for curses
- platform — Access to underlying platform's identifying data
- errno — Standard errno system symbols
- ctypes — Python 的外部函數庫
- 并發執行
- threading — 基于線程的并行
- multiprocessing — 基于進程的并行
- concurrent 包
- concurrent.futures — 啟動并行任務
- subprocess — 子進程管理
- sched — 事件調度器
- queue — 一個同步的隊列類
- _thread — 底層多線程 API
- _dummy_thread — _thread 的替代模塊
- dummy_threading — 可直接替代 threading 模塊。
- contextvars — Context Variables
- Context Variables
- Manual Context Management
- asyncio support
- 網絡和進程間通信
- asyncio — 異步 I/O
- socket — 底層網絡接口
- ssl — TLS/SSL wrapper for socket objects
- select — Waiting for I/O completion
- selectors — 高級 I/O 復用庫
- asyncore — 異步socket處理器
- asynchat — 異步 socket 指令/響應 處理器
- signal — Set handlers for asynchronous events
- mmap — Memory-mapped file support
- 互聯網數據處理
- email — 電子郵件與 MIME 處理包
- json — JSON 編碼和解碼器
- mailcap — Mailcap file handling
- mailbox — Manipulate mailboxes in various formats
- mimetypes — Map filenames to MIME types
- base64 — Base16, Base32, Base64, Base85 數據編碼
- binhex — 對binhex4文件進行編碼和解碼
- binascii — 二進制和 ASCII 碼互轉
- quopri — Encode and decode MIME quoted-printable data
- uu — Encode and decode uuencode files
- 結構化標記處理工具
- html — 超文本標記語言支持
- html.parser — 簡單的 HTML 和 XHTML 解析器
- html.entities — HTML 一般實體的定義
- XML處理模塊
- xml.etree.ElementTree — The ElementTree XML API
- xml.dom — The Document Object Model API
- xml.dom.minidom — Minimal DOM implementation
- xml.dom.pulldom — Support for building partial DOM trees
- xml.sax — Support for SAX2 parsers
- xml.sax.handler — Base classes for SAX handlers
- xml.sax.saxutils — SAX Utilities
- xml.sax.xmlreader — Interface for XML parsers
- xml.parsers.expat — Fast XML parsing using Expat
- 互聯網協議和支持
- webbrowser — 方便的Web瀏覽器控制器
- cgi — Common Gateway Interface support
- cgitb — Traceback manager for CGI scripts
- wsgiref — WSGI Utilities and Reference Implementation
- urllib — URL 處理模塊
- urllib.request — 用于打開 URL 的可擴展庫
- urllib.response — Response classes used by urllib
- urllib.parse — Parse URLs into components
- urllib.error — Exception classes raised by urllib.request
- urllib.robotparser — Parser for robots.txt
- http — HTTP 模塊
- http.client — HTTP協議客戶端
- ftplib — FTP protocol client
- poplib — POP3 protocol client
- imaplib — IMAP4 protocol client
- nntplib — NNTP protocol client
- smtplib —SMTP協議客戶端
- smtpd — SMTP Server
- telnetlib — Telnet client
- uuid — UUID objects according to RFC 4122
- socketserver — A framework for network servers
- http.server — HTTP 服務器
- http.cookies — HTTP state management
- http.cookiejar — Cookie handling for HTTP clients
- xmlrpc — XMLRPC 服務端與客戶端模塊
- xmlrpc.client — XML-RPC client access
- xmlrpc.server — Basic XML-RPC servers
- ipaddress — IPv4/IPv6 manipulation library
- 多媒體服務
- audioop — Manipulate raw audio data
- aifc — Read and write AIFF and AIFC files
- sunau — 讀寫 Sun AU 文件
- wave — 讀寫WAV格式文件
- chunk — Read IFF chunked data
- colorsys — Conversions between color systems
- imghdr — 推測圖像類型
- sndhdr — 推測聲音文件的類型
- ossaudiodev — Access to OSS-compatible audio devices
- 國際化
- gettext — 多語種國際化服務
- locale — 國際化服務
- 程序框架
- turtle — 海龜繪圖
- cmd — 支持面向行的命令解釋器
- shlex — Simple lexical analysis
- Tk圖形用戶界面(GUI)
- tkinter — Tcl/Tk的Python接口
- tkinter.ttk — Tk themed widgets
- tkinter.tix — Extension widgets for Tk
- tkinter.scrolledtext — 滾動文字控件
- IDLE
- 其他圖形用戶界面(GUI)包
- 開發工具
- typing — 類型標注支持
- pydoc — Documentation generator and online help system
- doctest — Test interactive Python examples
- unittest — 單元測試框架
- unittest.mock — mock object library
- unittest.mock 上手指南
- 2to3 - 自動將 Python 2 代碼轉為 Python 3 代碼
- test — Regression tests package for Python
- test.support — Utilities for the Python test suite
- test.support.script_helper — Utilities for the Python execution tests
- 調試和分析
- bdb — Debugger framework
- faulthandler — Dump the Python traceback
- pdb — The Python Debugger
- The Python Profilers
- timeit — 測量小代碼片段的執行時間
- trace — Trace or track Python statement execution
- tracemalloc — Trace memory allocations
- 軟件打包和分發
- distutils — 構建和安裝 Python 模塊
- ensurepip — Bootstrapping the pip installer
- venv — 創建虛擬環境
- zipapp — Manage executable Python zip archives
- Python運行時服務
- sys — 系統相關的參數和函數
- sysconfig — Provide access to Python's configuration information
- builtins — 內建對象
- main — 頂層腳本環境
- warnings — Warning control
- dataclasses — 數據類
- contextlib — Utilities for with-statement contexts
- abc — 抽象基類
- atexit — 退出處理器
- traceback — Print or retrieve a stack traceback
- future — Future 語句定義
- gc — 垃圾回收器接口
- inspect — 檢查對象
- site — Site-specific configuration hook
- 自定義 Python 解釋器
- code — Interpreter base classes
- codeop — Compile Python code
- 導入模塊
- zipimport — Import modules from Zip archives
- pkgutil — Package extension utility
- modulefinder — 查找腳本使用的模塊
- runpy — Locating and executing Python modules
- importlib — The implementation of import
- Python 語言服務
- parser — Access Python parse trees
- ast — 抽象語法樹
- symtable — Access to the compiler's symbol tables
- symbol — 與 Python 解析樹一起使用的常量
- token — 與Python解析樹一起使用的常量
- keyword — 檢驗Python關鍵字
- tokenize — Tokenizer for Python source
- tabnanny — 模糊縮進檢測
- pyclbr — Python class browser support
- py_compile — Compile Python source files
- compileall — Byte-compile Python libraries
- dis — Python 字節碼反匯編器
- pickletools — Tools for pickle developers
- 雜項服務
- formatter — Generic output formatting
- Windows系統相關模塊
- msilib — Read and write Microsoft Installer files
- msvcrt — Useful routines from the MS VC++ runtime
- winreg — Windows 注冊表訪問
- winsound — Sound-playing interface for Windows
- Unix 專有服務
- posix — The most common POSIX system calls
- pwd — 用戶密碼數據庫
- spwd — The shadow password database
- grp — The group database
- crypt — Function to check Unix passwords
- termios — POSIX style tty control
- tty — 終端控制功能
- pty — Pseudo-terminal utilities
- fcntl — The fcntl and ioctl system calls
- pipes — Interface to shell pipelines
- resource — Resource usage information
- nis — Interface to Sun's NIS (Yellow Pages)
- Unix syslog 庫例程
- 被取代的模塊
- optparse — Parser for command line options
- imp — Access the import internals
- 未創建文檔的模塊
- 平臺特定模塊
- 擴展和嵌入 Python 解釋器
- 推薦的第三方工具
- 不使用第三方工具創建擴展
- 使用 C 或 C++ 擴展 Python
- 自定義擴展類型:教程
- 定義擴展類型:已分類主題
- 構建C/C++擴展
- 在Windows平臺編譯C和C++擴展
- 在更大的應用程序中嵌入 CPython 運行時
- Embedding Python in Another Application
- Python/C API 參考手冊
- 概述
- 代碼標準
- 包含文件
- 有用的宏
- 對象、類型和引用計數
- 異常
- 嵌入Python
- 調試構建
- 穩定的應用程序二進制接口
- The Very High Level Layer
- Reference Counting
- 異常處理
- Printing and clearing
- 拋出異常
- Issuing warnings
- Querying the error indicator
- Signal Handling
- Exception Classes
- Exception Objects
- Unicode Exception Objects
- Recursion Control
- 標準異常
- 標準警告類別
- 工具
- 操作系統實用程序
- 系統功能
- 過程控制
- 導入模塊
- Data marshalling support
- 語句解釋及變量編譯
- 字符串轉換與格式化
- 反射
- 編解碼器注冊與支持功能
- 抽象對象層
- Object Protocol
- 數字協議
- Sequence Protocol
- Mapping Protocol
- 迭代器協議
- 緩沖協議
- Old Buffer Protocol
- 具體的對象層
- 基本對象
- 數值對象
- 序列對象
- 容器對象
- 函數對象
- 其他對象
- Initialization, Finalization, and Threads
- 在Python初始化之前
- 全局配置變量
- Initializing and finalizing the interpreter
- Process-wide parameters
- Thread State and the Global Interpreter Lock
- Sub-interpreter support
- Asynchronous Notifications
- Profiling and Tracing
- Advanced Debugger Support
- Thread Local Storage Support
- 內存管理
- 概述
- 原始內存接口
- Memory Interface
- 對象分配器
- 默認內存分配器
- Customize Memory Allocators
- The pymalloc allocator
- tracemalloc C API
- 示例
- 對象實現支持
- 在堆中分配對象
- Common Object Structures
- Type 對象
- Number Object Structures
- Mapping Object Structures
- Sequence Object Structures
- Buffer Object Structures
- Async Object Structures
- 使對象類型支持循環垃圾回收
- API 和 ABI 版本管理
- 分發 Python 模塊
- 關鍵術語
- 開源許可與協作
- 安裝工具
- 閱讀指南
- 我該如何...?
- ...為我的項目選擇一個名字?
- ...創建和分發二進制擴展?
- 安裝 Python 模塊
- 關鍵術語
- 基本使用
- 我應如何 ...?
- ... 在 Python 3.4 之前的 Python 版本中安裝 pip ?
- ... 只為當前用戶安裝軟件包?
- ... 安裝科學計算類 Python 軟件包?
- ... 使用并行安裝的多個 Python 版本?
- 常見的安裝問題
- 在 Linux 的系統 Python 版本上安裝
- 未安裝 pip
- 安裝二進制編譯擴展
- Python 常用指引
- 將 Python 2 代碼遷移到 Python 3
- 簡要說明
- 詳情
- 將擴展模塊移植到 Python 3
- 條件編譯
- 對象API的更改
- 模塊初始化和狀態
- CObject 替換為 Capsule
- 其他選項
- Curses Programming with Python
- What is curses?
- Starting and ending a curses application
- Windows and Pads
- Displaying Text
- User Input
- For More Information
- 實現描述器
- 摘要
- 定義和簡介
- 描述器協議
- 發起調用描述符
- 描述符示例
- Properties
- 函數和方法
- Static Methods and Class Methods
- 函數式編程指引
- 概述
- 迭代器
- 生成器表達式和列表推導式
- 生成器
- 內置函數
- itertools 模塊
- The functools module
- Small functions and the lambda expression
- Revision History and Acknowledgements
- 引用文獻
- 日志 HOWTO
- 日志基礎教程
- 進階日志教程
- 日志級別
- 有用的處理程序
- 記錄日志中引發的異常
- 使用任意對象作為消息
- 優化
- 日志操作手冊
- 在多個模塊中使用日志
- 在多線程中使用日志
- 使用多個日志處理器和多種格式化
- 在多個地方記錄日志
- 日志服務器配置示例
- 處理日志處理器的阻塞
- Sending and receiving logging events across a network
- Adding contextual information to your logging output
- Logging to a single file from multiple processes
- Using file rotation
- Use of alternative formatting styles
- Customizing LogRecord
- Subclassing QueueHandler - a ZeroMQ example
- Subclassing QueueListener - a ZeroMQ example
- An example dictionary-based configuration
- Using a rotator and namer to customize log rotation processing
- A more elaborate multiprocessing example
- Inserting a BOM into messages sent to a SysLogHandler
- Implementing structured logging
- Customizing handlers with dictConfig()
- Using particular formatting styles throughout your application
- Configuring filters with dictConfig()
- Customized exception formatting
- Speaking logging messages
- Buffering logging messages and outputting them conditionally
- Formatting times using UTC (GMT) via configuration
- Using a context manager for selective logging
- 正則表達式HOWTO
- 概述
- 簡單模式
- 使用正則表達式
- 更多模式能力
- 修改字符串
- 常見問題
- 反饋
- 套接字編程指南
- 套接字
- 創建套接字
- 使用一個套接字
- 斷開連接
- 非阻塞的套接字
- 排序指南
- 基本排序
- 關鍵函數
- Operator 模塊函數
- 升序和降序
- 排序穩定性和排序復雜度
- 使用裝飾-排序-去裝飾的舊方法
- 使用 cmp 參數的舊方法
- 其它
- Unicode 指南
- Unicode 概述
- Python's Unicode Support
- Reading and Writing Unicode Data
- Acknowledgements
- 如何使用urllib包獲取網絡資源
- 概述
- Fetching URLs
- 處理異常
- info and geturl
- Openers and Handlers
- Basic Authentication
- Proxies
- Sockets and Layers
- 腳注
- Argparse 教程
- 概念
- 基礎
- 位置參數介紹
- Introducing Optional arguments
- Combining Positional and Optional arguments
- Getting a little more advanced
- Conclusion
- ipaddress模塊介紹
- 創建 Address/Network/Interface 對象
- 審查 Address/Network/Interface 對象
- Network 作為 Address 列表
- 比較
- 將IP地址與其他模塊一起使用
- 實例創建失敗時獲取更多詳細信息
- Argument Clinic How-To
- The Goals Of Argument Clinic
- Basic Concepts And Usage
- Converting Your First Function
- Advanced Topics
- 使用 DTrace 和 SystemTap 檢測CPython
- Enabling the static markers
- Static DTrace probes
- Static SystemTap markers
- Available static markers
- SystemTap Tapsets
- 示例
- Python 常見問題
- Python常見問題
- 一般信息
- 現實世界中的 Python
- 編程常見問題
- 一般問題
- 核心語言
- 數字和字符串
- 性能
- 序列(元組/列表)
- 對象
- 模塊
- 設計和歷史常見問題
- 為什么Python使用縮進來分組語句?
- 為什么簡單的算術運算得到奇怪的結果?
- 為什么浮點計算不準確?
- 為什么Python字符串是不可變的?
- 為什么必須在方法定義和調用中顯式使用“self”?
- 為什么不能在表達式中賦值?
- 為什么Python對某些功能(例如list.index())使用方法來實現,而其他功能(例如len(List))使用函數實現?
- 為什么 join()是一個字符串方法而不是列表或元組方法?
- 異常有多快?
- 為什么Python中沒有switch或case語句?
- 難道不能在解釋器中模擬線程,而非得依賴特定于操作系統的線程實現嗎?
- 為什么lambda表達式不能包含語句?
- 可以將Python編譯為機器代碼,C或其他語言嗎?
- Python如何管理內存?
- 為什么CPython不使用更傳統的垃圾回收方案?
- CPython退出時為什么不釋放所有內存?
- 為什么有單獨的元組和列表數據類型?
- 列表是如何在CPython中實現的?
- 字典是如何在CPython中實現的?
- 為什么字典key必須是不可變的?
- 為什么 list.sort() 沒有返回排序列表?
- 如何在Python中指定和實施接口規范?
- 為什么沒有goto?
- 為什么原始字符串(r-strings)不能以反斜杠結尾?
- 為什么Python沒有屬性賦值的“with”語句?
- 為什么 if/while/def/class語句需要冒號?
- 為什么Python在列表和元組的末尾允許使用逗號?
- 代碼庫和插件 FAQ
- 通用的代碼庫問題
- 通用任務
- 線程相關
- 輸入輸出
- 網絡 / Internet 編程
- 數據庫
- 數學和數字
- 擴展/嵌入常見問題
- 可以使用C語言中創建自己的函數嗎?
- 可以使用C++語言中創建自己的函數嗎?
- C很難寫,有沒有其他選擇?
- 如何從C執行任意Python語句?
- 如何從C中評估任意Python表達式?
- 如何從Python對象中提取C的值?
- 如何使用Py_BuildValue()創建任意長度的元組?
- 如何從C調用對象的方法?
- 如何捕獲PyErr_Print()(或打印到stdout / stderr的任何內容)的輸出?
- 如何從C訪問用Python編寫的模塊?
- 如何從Python接口到C ++對象?
- 我使用Setup文件添加了一個模塊,為什么make失敗了?
- 如何調試擴展?
- 我想在Linux系統上編譯一個Python模塊,但是缺少一些文件。為什么?
- 如何區分“輸入不完整”和“輸入無效”?
- 如何找到未定義的g++符號__builtin_new或__pure_virtual?
- 能否創建一個對象類,其中部分方法在C中實現,而其他方法在Python中實現(例如通過繼承)?
- Python在Windows上的常見問題
- 我怎樣在Windows下運行一個Python程序?
- 我怎么讓 Python 腳本可執行?
- 為什么有時候 Python 程序會啟動緩慢?
- 我怎樣使用Python腳本制作可執行文件?
- *.pyd 文件和DLL文件相同嗎?
- 我怎樣將Python嵌入一個Windows程序?
- 如何讓編輯器不要在我的 Python 源代碼中插入 tab ?
- 如何在不阻塞的情況下檢查按鍵?
- 圖形用戶界面(GUI)常見問題
- 圖形界面常見問題
- Python 是否有平臺無關的圖形界面工具包?
- 有哪些Python的GUI工具是某個平臺專用的?
- 有關Tkinter的問題
- “為什么我的電腦上安裝了 Python ?”
- 什么是Python?
- 為什么我的電腦上安裝了 Python ?
- 我能刪除 Python 嗎?
- 術語對照表
- 文檔說明
- Python 文檔貢獻者
- 解決 Bug
- 文檔錯誤
- 使用 Python 的錯誤追蹤系統
- 開始為 Python 貢獻您的知識
- 版權
- 歷史和許可證
- 軟件歷史
- 訪問Python或以其他方式使用Python的條款和條件
- Python 3.7.3 的 PSF 許可協議
- Python 2.0 的 BeOpen.com 許可協議
- Python 1.6.1 的 CNRI 許可協議
- Python 0.9.0 至 1.2 的 CWI 許可協議
- 集成軟件的許可和認可
- Mersenne Twister
- 套接字
- Asynchronous socket services
- Cookie management
- Execution tracing
- UUencode and UUdecode functions
- XML Remote Procedure Calls
- test_epoll
- Select kqueue
- SipHash24
- strtod and dtoa
- OpenSSL
- expat
- libffi
- zlib
- cfuhash
- libmpdec