[Python3] 027 常用模塊 time


time

1. 時間戳

  • 一個時間表示,根據不同語言,可以是整數或者浮點數
  • 是從 1970年1月1日0時0分0秒 到現在經歷的秒數
  • 如果表示的時間是 1970年以前 或者 太遙遠的未來,可能出現異常
  • 32位操作系統 能夠支持到 2038年

2. UTC 時間

  • UTC 又稱 世界協調時間,以英國的格林尼治天文所在地區的時間作為參考的時間,也叫做 世界標准時間
  • 中國時間是 UTC+8 東八區

3. 夏令時

  • 在夏天的時候將時間調快一小時
  • 本意是督促大家早睡早起節省蠟燭,每天變成 25 個小時,本質沒變還是 24 小時

4. 時間元組

  • 一個包含時間內容的普通元組
索引 內容 屬性
0 tm_year 2015
1 tm_mon 1~12
2 tm_mday 1~31
3 tm_hour 0~23
4 tm_min 0~59
5 tm_sec 0~61,60表示閏秒,61保留值
6 周幾 tm_wday 0~6
7 第幾天 tm_yday 1~366
8 夏令時 tm_isdst 0, 1, -1(表示夏令時)

5. 舉例

  • 必要的准備工作
>>> import time

5.1 例子1

  • time.timezone

    • 當前時區和 UTC 時間相差的秒數
    • 在沒有夏令時的情況下,中(零)時區與東八區相差 -28800s
  • time.altzone

    • 獲取當前時區與 UTC 時間相差的秒數
    • 在有夏令時的情況下,中(零)時區與東八區相差 -32400s
  • time.daylight

    • 測當前是否是夏令時時間狀態
    • 0 表示是
>>> time.timezone
-28800
>>> time.altzone
-32400
>>> time.daylight
0

例子2

  • time.time
    • 時間戳
  • time.localtime
    • Convert seconds since the Epoch to a time tuple expressing local time.
    • When 'seconds' is not passed in, convert the current time instead.
>>> time.time()
1576070236.623945
>>> time.localtime()
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=11, tm_hour=21, tm_min=17, tm_sec=29, tm_wday=2, tm_yday=345, tm_isdst=0)
>>> t = time.localtime()
>>> t.tm_hour
21

例子3

  • time.asctime
    • Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
    • When the time tuple is not present, current time as returned by localtime() is used.
>>> t1 = time.localtime()
>>> t2 = time.asctime(t1)
>>> type(t2)
<class 'str'>
>>> t2
'Wed Dec 11 21:18:03 2019'

例子4

  • time.ctime
    • Convert a time in seconds since the Epoch to a string in local time.
    • This is equivalent to asctime(localtime(seconds)). When the time tuple is not present, current time as returned by localtime() is used.
>>> t = time.ctime()
>>> type(t)
<class 'str'>
>>> t
'Wed Dec 11 21:19:02 2019'

例子5

  • time.mktime
    • Convert a time tuple in local time to seconds since the Epoch.
    • Note that mktime(gmtime(0)) will not generally return zero for most time zones; instead the returned value will either be equal to that of the timezone or altzone attributes on the time module.
>>> lt = time.localtime()
>>> ts = time.mktime(lt)
>>> type(ts)
<class 'float'>
>>> ts
1576070374.0

例子6

  • time.clock
    • Return the CPU time or real time since the start of the process or since the first call to clock().
    • This has as much precision as the system records.
  • time.sleep
    • Delay execution for a given number of seconds.
    • The argument may be a floating point number for subsecond precision.
# run in ipython
t0 = time.clock()
time.sleep(3)
t1 = time.clock()
print(t1 - t0)

>>>

...
DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
  """Entry point for launching an IPython kernel.
3.0017606999999984
...
This is separate from the ipykernel package so we can avoid doing imports until
  • 既然官方說了,那就用 time.perf_countertime.process_time

例子7

  • strftime
    • Convert a time tuple to a string according to a format specification.
    • When the time tuple is not present, current time as returned by localtime() is used.
  • help(time.strftime)
符號 釋義
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
用 +HHMM 或 -HHMM 表示距離格林威治的時區偏移
H 代表十進制的小時數,M 代表十進制的分鍾數
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
  • 補充
符號 釋義
%j 一年中的第幾天(001 - 366)
%U 一年中的星期數(00 - 53 星期天是一個星期的開始)
第一個星期天之前的所有天數都放在第 0 周
%w 一個星期中的第幾天(0 - 6,0 是星期天)
%W 和 %U 基本相同,不同的是 %W 以星期一為一個星期的開始
%x 本地相應日期
%X 本地相應時間
%y 去掉世紀的年份(00 - 99)
%Y 完整的年份
%% %號本身
>>> lt = time.localtime()
>>> ft = time.strftime("%Y-%m-%d %H:%M", lt)
>>> ft
'2019-12-11 21:20'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM