您好,登錄后才能下訂單哦!
這篇文章主要介紹了python將格式字符串轉(zhuǎn)換為時(shí)間戳的方法,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
常用的時(shí)間函數(shù)如下
獲取當(dāng)前日期:time.time()
獲取元組形式的時(shí)間戳:time.local(time.time())
格式化日期的函數(shù)(基于元組的形式進(jìn)行格式化):
(1)time.asctime(time.local(time.time()))
(2)time.strftime(format[,t])
將格式字符串轉(zhuǎn)換為時(shí)間戳:
time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')
延遲執(zhí)行:time.sleep([secs]),單位為秒
例1:
# -*- coding:utf-8 -*- import time #當(dāng)前時(shí)間 print time.time() #時(shí)間戳形式 print time.localtime(time.time()) #簡(jiǎn)單可讀形式 print time.asctime( time.localtime(time.time()) ) # 格式化成2016-03-20 11:45:39形式 print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 格式化成Sat Mar 28 22:24:24 2016形式 print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) # 將格式字符串轉(zhuǎn)換為時(shí)間戳 a = "Sat Mar 28 22:24:24 2016" print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
輸出為
1481036968.19 time.struct_time(tm_year=2016, tm_mon=12, tm_mday=6, tm_hour=23, tm_min=9, tm_sec=28, tm_wday=1, tm_yday=341, tm_isdst=0) Tue Dec 06 23:09:28 2016 2016-12-06 23:09:28 Tue Dec 06 23:09:28 2016 1459175064.0
例2:某時(shí)間與當(dāng)前比較,如果大于當(dāng)前時(shí)間則調(diào)用某個(gè)腳本,否則等待半個(gè)小時(shí)候后繼續(xù)判斷
# -*- coding:utf-8 -*- import time import sys import os #判斷當(dāng)前時(shí)間是否超過(guò)某個(gè)輸入的時(shí)間 def Fuctime(s): if time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))>s: return True else: return False while(1): if Fuctime('2016-12-05 00:00:00'): #調(diào)用某個(gè)路徑下的腳本的簡(jiǎn)便方法 os.system("python ./../day_2/Prime.py ./../day_2/inti_prime.txt ./../day_2/res_prime.txt") break else: time.sleep(1800) continue
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享python將格式字符串轉(zhuǎn)換為時(shí)間戳的方法內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問(wèn)題就找億速云,詳細(xì)的解決方法等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。