Python之time模塊的時間戳,時間字符串格式化與轉(zhuǎn)換方法(13位時間戳)
Python處理時間和時間戳的內(nèi)置模塊就有time,和datetime兩個,本文先說time模塊。
關于時間戳的幾個概念
時間戳,根據(jù)1970年1月1日00:00:00開始按秒計算的偏移量。
時間元組(struct_time),包含9個元素。
time.struct_time(tm_year=2017, tm_mon=10, tm_mday=1, tm_hour=14, tm_min=21, tm_sec=57, tm_wday=6, tm_yday=274, tm_isdst=0)
時間格式字符串,字符串形式的時間。
time模塊與時間戳和時間相關的重要函數(shù)
time.time() 生成當前的時間戳,格式為10位整數(shù)的浮點數(shù)。
time.strftime()根據(jù)時間元組生成時間格式化字符串。
time.strptime()根據(jù)時間格式化字符串生成時間元組。time.strptime()與time.strftime()為互操作。
time.localtime()根據(jù)時間戳生成當前時區(qū)的時間元組。
time.mktime()根據(jù)時間元組生成時間戳。
示例
關于時間戳和格式化字符串的簡單示例如下
import time #生成當前時間的時間戳,只有一個參數(shù)即時間戳的位數(shù),默認為10位,輸入位數(shù)即生成相應位數(shù)的時間戳,比如可以生成常用的13位時間戳 def now_to_timestamp(digits = 10): time_stamp = time.time() digits = 10 ** (digits -10) time_stamp = int(round(time_stamp*digits)) return time_stamp #將時間戳規(guī)范為10位時間戳 def timestamp_to_timestamp10(time_stamp): time_stamp = int (time_stamp* (10 ** (10-len(str(time_stamp))))) return time_stamp #將當前時間轉(zhuǎn)換為時間字符串,默認為2017-10-01 13:37:04格式 def now_to_date(format_string="%Y-%m-%d %H:%M:%S"): time_stamp = int(time.time()) time_array = time.localtime(time_stamp) str_date = time.strftime(format_string, time_array) return str_date #將10位時間戳轉(zhuǎn)換為時間字符串,默認為2017-10-01 13:37:04格式 def timestamp_to_date(time_stamp, format_string="%Y-%m-%d %H:%M:%S"): time_array = time.localtime(time_stamp) str_date = time.strftime(format_string, time_array) return str_date #將時間字符串轉(zhuǎn)換為10位時間戳,時間字符串默認為2017-10-01 13:37:04格式 def date_to_timestamp(date, format_string="%Y-%m-%d %H:%M:%S"): time_array = time.strptime(date, format_string) time_stamp = int(time.mktime(time_array)) return time_stamp #不同時間格式字符串的轉(zhuǎn)換 def date_style_transfomation(date, format_string1="%Y-%m-%d %H:%M:%S",format_string2="%Y-%m-%d %H-%M-%S"): time_array = time.strptime(date, format_string1) str_date = time.strftime(format_string2, time_array) return str_date
實驗
print(now_to_date())
print(timestamp_to_date(1506816572))
print(date_to_timestamp('2017-10-01 08:09:32'))
print(timestamp_to_timestamp10(1506816572546))
print(date_style_transfomation('2017-10-01 08:09:32'))
結(jié)果為
1506836224000 2017-10-01 13:37:04 2017-10-01 08:09:32 1506816572 1506816572 2017-10-01 08-09-32
以上這篇Python之time模塊的時間戳,時間字符串格式化與轉(zhuǎn)換方法(13位時間戳)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- python datetime 和時間戳互相轉(zhuǎn)換問題
- python中時間轉(zhuǎn)換datetime和pd.to_datetime詳析
- Python datetime和unix時間戳之間相互轉(zhuǎn)換的講解
- Python中時間datetime的處理與轉(zhuǎn)換用法總結(jié)
- python utc datetime轉(zhuǎn)換為時間戳的方法
- python sys,os,time模塊的使用(包括時間格式的各種轉(zhuǎn)換)
- Python中實現(xiàn)對Timestamp和Datetime及UTC時間之間的轉(zhuǎn)換
- python 時間的訪問和轉(zhuǎn)換 time示例小結(jié)
相關文章
Python腳本簡單實現(xiàn)打開默認瀏覽器登錄人人和打開QQ的方法
這篇文章主要介紹了Python腳本簡單實現(xiàn)打開默認瀏覽器登錄人人和打開QQ的方法,涉及Python針對瀏覽器及應用程序的相關操作技巧,代碼非常簡單實用,需要的朋友可以參考下2016-04-04
Python之Anaconda啟動過程中的異常錯誤問題及解決
這篇文章主要介紹了Python之Anaconda啟動過程中的異常錯誤問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Python使用random模塊生成隨機數(shù)操作實例詳解
這篇文章主要介紹了Python使用random模塊生成隨機數(shù)操作,結(jié)合具體實例形式詳細分析了random模塊生成隨機數(shù)的各種常用技巧與相關操作注意事項,需要的朋友可以參考下2019-09-09
Python?torch.fft.rfft()函數(shù)用法示例代碼
大家應該都知道新舊版的torch中的傅里葉變換函數(shù)在定義和用法上有所不同,下面這篇文章主要給大家介紹了關于Python?torch.fft.rfft()函數(shù)用法的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-04-04

