【常用模塊】:time模塊淺談

日期:2020年3月3日
作者:Commas
註釋:學習就是爲了忘記,常用模塊之旅開始啦;
如果您想了解更多有關Python的知識,那麼請點《我的Python淺談系列目錄》



一、time模塊初識

前面一些博客,我們經常用到time.sleep(1),即推遲1秒鐘再執行線程,time模塊不止這個作用,還有很多關於時間相關的功能。dir()函數是一個非常好用的內置函數,不多說,直接來看看time模塊的方法與變量吧……

import time
print(dir(time))

控制檯輸出結果(加粗部分是因爲顯示問題,實則爲雙下方法,即內置方法或屬性):
[’_STRUCT_TM_ITEMS’, ‘doc’, ‘loader’, ‘name’, ‘package’, ‘spec’, ‘altzone’, ‘asctime’, ‘clock’, ‘ctime’, ‘daylight’, ‘get_clock_info’, ‘gmtime’, ‘localtime’, ‘mktime’, ‘monotonic’, ‘monotonic_ns’, ‘perf_counter’, ‘perf_counter_ns’, ‘process_time’, ‘process_time_ns’, ‘sleep’, ‘strftime’, ‘strptime’, ‘struct_time’, ‘thread_time’, ‘thread_time_ns’, ‘time’, ‘time_ns’, ‘timezone’, ‘tzname’]

二、時間的三種表示方式

import time


# (1)時間戳(time stamp)
# 返回自紀元(1970-01-01 00:00:00 )以來以秒(浮點秒數)爲單位的當前時間
# 即 (當前時間 - 紀元) 後取秒爲單位的浮點值
t = time.time()
print(t)
# (2)字符串格式時間(string format time)
# 格式化時間字符串,strftime是string format time的縮寫
t = time.strftime("%Y-%m-%d %H:%M:%S")
print(t)
# (3)時間元組(time tuple)
# 時間表示方式轉換的橋樑
# 返回值:(年,月,日,時,分,秒,一年中第幾周,一年中第幾天,是否是夏令時)
# (3-1) 當前時區的時間元組
t = time.localtime()
print(t)
# (3-2) 世界統一時(UTC)的時間元組
t = time.gmtime()
print(t)

# =======控制檯輸出結果=======
1583214633.4622803
2020-03-03 13:50:33
time.struct_time(tm_year=2020, tm_mon=3, tm_mday=3, tm_hour=13, tm_min=50, tm_sec=33, tm_wday=1, tm_yday=63, tm_isdst=0)
time.struct_time(tm_year=2020, tm_mon=3, tm_mday=3, tm_hour=5, tm_min=50, tm_sec=33, tm_wday=1, tm_yday=63, tm_isdst=0)

其中,time.strftime(format, p_tuple=None)中的format的常見格式如下:

	%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.
	%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.

三、時間表示方式的轉換

在這裏插入圖片描述
時間戳(time stamp)不可以直接與字符串格式化時間(string format time)互相轉換,必須藉助時間元組(time tuple)這個橋樑進行轉換,其他二者均可以互相轉換,如上圖所示。

(1)時間戳與時間元組的互相轉換

# (1-1)時間戳→時間元組
time_stamp_t = 1583214633.4622803
t = time.localtime(time_stamp_t)
print(t)
t = time.gmtime(time_stamp_t)
print(t)
# (1-2)時間元組→時間戳
time_tuple_t = time.localtime()
t = time.mktime(time_tuple_t)
print(t)

# =======控制檯輸出結果=======
time.struct_time(tm_year=2020, tm_mon=3, tm_mday=3, tm_hour=13, tm_min=50, tm_sec=33, tm_wday=1, tm_yday=63, tm_isdst=0)
time.struct_time(tm_year=2020, tm_mon=3, tm_mday=3, tm_hour=5, tm_min=50, tm_sec=33, tm_wday=1, tm_yday=63, tm_isdst=0)
1583214633.4622803

(2)字符串格式時間與時間元組的互相轉換

# (2-1)字符串格式時間→時間元組的互相轉換
format_string_t = "2020-03-03 13:50:33"
t = time.strptime(format_string_t, "%Y-%m-%d %H:%M:%S")
print(t)
# (2-2)時間元組的互相轉換→字符串格式時間
time_tuple_t = time.localtime()
t = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple_t)
print(t)

# =======控制檯輸出結果=======
time.struct_time(tm_year=2020, tm_mon=3, tm_mday=3, tm_hour=13, tm_min=50, tm_sec=33, tm_wday=1, tm_yday=63, tm_isdst=-1)
2020-03-03 13:50:33

(3)時間戳、時間元組可以轉換特定的字符串格式時間

# (3-1)時間戳→字符串格式時間(特定)
time_stamp_t = 1583214633.4622803
t = time.ctime(time_stamp_t)
print(t)
# (3-2)時間元組→字符串格式時間(特定)
time_tuple_t = time.localtime()
t = time.asctime(time_tuple_t)
print(t)
# =======控制檯輸出結果=======
Tue Mar  3 13:50:33 2020
Tue Mar  3 13:50:33 2020

版權聲明:本文爲博主原創文章,如需轉載,請給出:
原文鏈接:https://blog.csdn.net/qq_35844043/article/details/104630156

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章