python3時間 Python3 時間處理

2021-10-13 08:55:55 字數 2879 閱讀 2801

#!/usr/bin/python3

import time; #引入time模組#python3 日期和時間

#當前時間戳

ticks =time.time()print ("當前時間戳為:", ticks)#當前時間戳為: 1522024006.313911

#完全版本地時間

localtime =time.localtime(time.time())print ("本地時間為 :", localtime)#輸出 本地時間為 : time.struct_time(tm_year=2018, tm_mon=3, tm_mday=26, tm_hour=0, tm_min=26, tm_sec=46, tm_wday=0, tm_yday=85, tm_isdst=0)

#簡單本地時間

localtime =time.asctime( time.localtime(time.time()) )print ("本地時間為 :", localtime)#輸出 本地時間為 : mon mar 26 00:26:46 2018

#格式化日期#使用 time 模組的 strftime 方法來格式化日期#格式化成2016-03-20 11:45:39形式

print (time.strftime("%y-%m-%d %h:%m:%s", time.localtime()))#輸出 2018-03-26 00:32:09

#格式化成sat mar 28 22:24:24 2016形式

print (time.strftime("%a %b %d %h:%m:%s %y", time.localtime()))#輸出 mon mar 26 00:32:09 2018

#將格式字串轉換為時間戳

a = "sat mar 28 22:24:24 2016"

print (time.mktime(time.strptime(a,"%a %b %d %h:%m:%s %y")))#輸出 1459203864.0

#獲取某月日曆

importcalendar

cal= calendar.month(2016, 1)print ("以下輸出2023年1月份的日曆:")print(cal)#print ("time.ctime() : %s" %time.ctime())#高階4 time.clock() 浮點數計算的秒數返回當前的cpu時間#在第一次呼叫的時候,返回的是程式執行的實際時間;#以第二次之後的呼叫,返回的是自第一次呼叫後,到這次呼叫的時間間隔#計算程式執行間隔

t0 =time.clock()print(t0)

time.sleep(2.5)print(time.clock()-t0)

t0=time.time()

time.sleep(2.5)print (time.time() -t0)#時間元組->時間戳 time.strftime(fmt[,tupletime])#time.ctime() asctime(localtime(secs)),未給引數相當於asctime()

print (time.strftime("%y-%m-%d %h:%m:%s", time.localtime()))

struct_time= time.strptime("30 nov 00", "%d %b %y")#時間戳->格林威治時間元組 time.gmtime([secs]) (格林威治天文時間下的時間元組)

print ("gmtime :", time.gmtime(1455508609.34375))#輸出 gmtime : time.struct_time(tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=3, tm_min=56, tm_sec=49, tm_wday=0, tm_yday=46, tm_isdst=0)

#時間戳->當地時間元組 time.localtime([secs]

print ("localtime():", time.localtime(1455508609.34375))#輸出 localtime(): time.struct_time(tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=3, tm_min=56, tm_sec=49, tm_wday=0, tm_yday=46, tm_isdst=0)

#time.mktime#執行與gmtime(), localtime()相反的操作

t = (2016, 2, 17, 17, 3, 38, 1, 48, 0)

secs=time.mktime( t )print ("time.mktime(t) : %f" %secs)print ("asctime(localtime(secs)): %s" %time.asctime(time.localtime(secs)))#輸出 time.mktime(t) : 1455699818.000000#輸出 asctime(localtime(secs)): wed feb 17 17:03:38 2016

#時間字串轉化為元組 ime.strptime(str,fmt='%a %b %d %h:%m:%s %y')

struct_time = time.strptime("30 nov 00", "%d %b %y")print ("返回元組:", struct_time)#返回元組: time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)

#推遲呼叫執行緒的執行,time.sleep(secs) secs指秒數

print ("start : %s" %time.ctime())

time.sleep(5)print ("end : %s" %time.ctime())#輸出 start : mon mar 26 01:21:18 2018#輸出 mon mar 26 01:21:23 2018

Python3 時間格式

coding utf 8 import time import datetime defget tentime 獲取精確到秒的時間戳 十位 return int time.time defget timesecond 獲取精確到毫秒的時間戳 十三位 millis int round time.tim...

python 3 時間處理

python time strftime 方法 描述python time strftime 函式接收以時間元組,並返回以可讀字串表示的當地時間,格式由引數format決定。語法strftime 方法語法 time.strftime format t 引數format 格式字串。t 可選的引數t是乙...

python3 時間模組常用方法

import time import datetime 時間戳 用於計算 print time.time 時間戳轉換成結構化時間 print time.localtime 15465268214 當地時間 print time.gmtime 世界標準時間utc 將結構化時間轉化成時間戳 print ...