<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Python `time`模塊 > 原文: [https://www.programiz.com/python-programming/time](https://www.programiz.com/python-programming/time) #### 在本文中,我們將詳細探討時間模塊。 通過示例,我們將學習使用在時間模塊中定義的與時間相關的不同函數。 Python 有一個名為`time`的模塊來處理與時間有關的任務。 要使用模塊中定義的函數,我們需要首先導入模塊。 這是如何做: ```py import time ``` * * * 這是常用的時間相關函數。 ### Python `time.time()` `time()`函數返回自紀元以來經過的秒數。 對于 Unix 系統,在 **UTC** 處的`January 1, 1970, 00:00:00`是時代(時間開始的點)。 ```py import time seconds = time.time() print("Seconds since epoch =", seconds) ``` * * * ### Python `time.ctime()` `time.ctime()`函數從紀元起經過了幾秒鐘,并將其作為代表本地時間的字符串。 ```py import time # seconds passed since epoch seconds = 1545925769.9618232 local_time = time.ctime(seconds) print("Local time:", local_time) ``` 如果運行該程序,輸出將類似于: ```py Local time: Thu Dec 27 15:49:29 2018 ``` * * * ### Python `time.sleep()` `sleep()`函數在給定的秒數內暫停(延遲)當前線程的執行。 ```py import time print("This is printed immediately.") time.sleep(2.4) print("This is printed after 2.4 seconds.") ``` 要了解更多信息,請訪問: [Python `sleep()`](/python-programming/time/sleep)。 * * * 在討論其他與時間相關的函數之前,讓我們簡要探討`time.struct_time`類。 * * * ## `time.struct_time`類別 `time`模塊中的某些函數,例如`gmtime()`,`asctime()`等,都可以將`time.struct_time`對象作為參數或將其返回。 這是`time.struct_time`對象的示例。 ```py time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=6, tm_min=35, tm_sec=17, tm_wday=3, tm_yday=361, tm_isdst=0) ``` | 序號 | 屬性 | 值 | | --- | --- | --- | | 0 | `tm_year` | 0000,....,2018,...,9999 | | 1 | `tm_mon` | 1,2,...,12 | | 2 | `tm_mday` | 1,2,...,31 | | 3 | `tm_hour` | 0,1,...,23 | | 4 | `tm_min` | 0,1,...,59 | | 5 | `tm_sec` | 0,1,...,61 | | 6 | `tm_wday` | 0,1,...,6;星期一是 0 | | 7 | `tm_yday` | 1,2,...,366 | | 8 | `tm_isdst` | 0、1 或 -1 | 可以使用索引和屬性訪問`time.struct_time`對象的值(元素)。 * * * ### Python `time.localtime()` `localtime()`函數將自紀元以來經過的秒數作為參數,并在**當地時間**中返回`struct_time`。 ```py import time result = time.localtime(1545925769) print("result:", result) print("\nyear:", result.tm_year) print("tm_hour:", result.tm_hour) ``` 當您運行程序時,輸出將類似于: ```py result: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3, tm_yday=361, tm_isdst=0) year: 2018 tm_hour: 15 ``` 如果沒有參數或`None`傳遞給`localtime()`,則使用`time()`返回的值。 * * * ### Python `time.gmtime()` `gmtime()`函數將自紀元以來經過的秒數作為參數,并在 **UTC** 中返回`struct_time`。 ```py import time result = time.gmtime(1545925769) print("result:", result) print("\nyear:", result.tm_year) print("tm_hour:", result.tm_hour) ``` 運行該程序時,輸出為: ```py result = time.struct_time(tm_year=2018, tm_mon=12, tm_mday=28, tm_hour=8, tm_min=44, tm_sec=4, tm_wday=4, tm_yday=362, tm_isdst=0) year = 2018 tm_hour = 8 ``` 如果沒有參數或`None`傳遞給`gmtime()`,則使用`time()`返回的值。 * * * ### Python `time.mktime()` `mktime()`函數將`struct_time`(或包含 9 個元素的元組,與`struct_time`對應的元組)作為參數,并返回自當地時間的紀元以來經過的秒數。 基本上,它是`localtime()`的反函數。 ```py import time t = (2018, 12, 28, 8, 44, 4, 4, 362, 0) local_time = time.mktime(t) print("Local time:", local_time) ``` * * * 下面的示例顯示`mktime()`和`localtime()`之間的關系。 ```py import time seconds = 1545925769 # returns struct_time t = time.localtime(seconds) print("t1: ", t) # returns seconds from struct_time s = time.mktime(t) print("\s:", seconds) ``` 當您運行程序時,輸出將類似于: ```py t1: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3, tm_yday=361, tm_isdst=0) s: 1545925769.0 ``` * * * ### Python `time.asctime()` `asctime()`函數將`struct_time`(或包含 9 個與`struct_time`相對應的元素的元組)作為參數,并返回表示它的字符串。 這是一個例子: ```py import time t = (2018, 12, 28, 8, 44, 4, 4, 362, 0) result = time.asctime(t) print("Result:", result) ``` 當你運行程序時,輸出將是: ```py Result: Fri Dec 28 08:44:04 2018 ``` * * * ### Python `time.strftime()` `strftime()`函數將`struct_time`(或與其對應的元組)作為參數,并根據所使用的格式代碼返回表示它的字符串。 例如, ```py import time named_tuple = time.localtime() # get struct_time time_string = time.strftime("%m/%d/%Y, %H:%M:%S", named_tuple) print(time_string) ``` 當您運行程序時,輸出將類似于: ```py 12/28/2018, 09:47:41 ``` 這里,`%Y`,`%m`,`%d`,`%H`等是格式代碼。 * `%Y` - 年`0001, ..., 2018, 2019, ..., 9999` * `%m` - 月`01, 02, ..., 11, 12` * `%d` - 天`01, 02, ..., 30, 31` * `%H` - 小時`00, 01, ..., 22, 23` * `%M` - 分鐘`00, 01, ..., 58, 59` * `%S` - 秒`00, 01, ..., 58, 59` 要了解更多信息,請訪問: [`time.strftime()`](https://docs.python.org/3/library/time.html#time.strftime)。 * * * ### Python `time.strptime()` `strptime()`函數解析表示時間的字符串,然后返回`struct_time`。 ```py import time time_string = "21 June, 2018" result = time.strptime(time_string, "%d %B, %Y") print(result) ``` 當你運行程序時,輸出將是: ```py time.struct_time(tm_year=2018, tm_mon=6, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=172, tm_isdst=-1) ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看