時間間隔輸出函式 python3

2021-10-09 15:31:41 字數 1377 閱讀 3936

因為專案需要輸出指定時間間隔和間隔內模組上報的次數,為了避免每次用計算器算,所以自己封裝了乙個函式,以供自己使用:

import time

deftime_unix

(old_time)

:"""將格式為:'2020-9-18 17:50:00'的時間轉成unix表示的時間戳"""

data_sj = time.strptime(old_time,

'%y-%m-%d %h:%m:%s'

) time_int =

int(time.mktime(data_sj)

)return time_int

defcount_t

(old_t,

type

,count =15)

:"""返回實際指定時間與現在的時間間隔

old_t:代表以前的時間

count:代表模組上報週期

type:代表型別,值為:『s』,'m','h'

"""now_t = time.time(

) now_time = time.strftime(

'%y-%m-%d %h:%m:%s'

,time.localtime(

int(time.time())

))end_t_s = now_t - time_unix(old_t)

if count ==0:

iftype

=='s'

:return

.format

(old_t,now_time,end_t_s)

iftype

=='m'

:return

.format

(old_t,now_time,end_t_s/60)

iftype

=='h'

:return

.format

(old_t,now_time,end_t_s/60/

60)else

:return

'輸入有誤'

else

: end_t_c = end_t_s /

60/ count

return

.format

(old_t,now_time,end_t_s,count,end_t_c)

if __name__ ==

'__main__'

: test1 =

'2020-9-18 17:50:00'

print

(count_t(test1,

'd',15)

)print

(count_t(test1,

'h',0)

)

python3 獲取整分鐘數的時間,如間隔半小時

關於作用 像實時資料檔案,假如要每隔半小時匯出副本,並傳到其他伺服器。自動傳輸軟體一般要格式化點的檔名,寫實時時間不一定支援。演算法 大體思路是,獲取當前的整點時間和下乙個整點時間,用時間間隔來分成多個區間,看看當前時間在哪個區間。假如當前是20 56,時間間隔30分鐘 20 00 x1 x2 落在...

python3函式語法 Python3

python3 degrees 函式 描述degrees 將弧度轉換為角度。語法以下是 degrees 方法的語法 import math math.degrees x 注意 degrees 是不能直接訪問的,需要匯入 math 模組,然後通過 math 靜態物件呼叫該方法。引數x 乙個數值。返回值...

python3時間 Python3 時間處理

usr bin python3 import time 引入time模組 python3 日期和時間 當前時間戳 ticks time.time print 當前時間戳為 ticks 當前時間戳為 1522024006.313911 完全版本地時間 localtime time.localtime ...