日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python现在时间 命令_Python3 - 时间处理与定时任务

發布時間:2024/7/5 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python现在时间 命令_Python3 - 时间处理与定时任务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.計算明天和昨天的日期

#! /usr/bin/env python

#coding=utf-8

# 獲取今天、昨天和明天的日期

# 引入datetime模塊

import datetime

#計算今天的時間

today = datetime.date.today()

#計算昨天的時間

yesterday = today - datetime.timedelta(days = 1)

#計算明天的時間

tomorrow = today + datetime.timedelta(days = 1)

#打印這三個時間

print(yesterday, today, tomorrow)

2.計算上一個的時間

方法1:

#! /usr/bin/env python

#coding=utf-8

# 計算上一個的時間

#引入datetime,calendar兩個模塊

import datetime,calendar

last_friday = datetime.date.today()

oneday = datetime.timedelta(days = 1)

while last_friday.weekday() != calendar.FRIDAY:

last_friday -= oneday

print(last_friday.strftime('%A, %d-%b-%Y'))

方法二:借助模運算尋找上一個星期五

#! /usr/bin/env python

#coding=utf-8

# 借助模運算,可以一次算出需要減去的天數,計算上一個星期五

#同樣引入datetime,calendar兩個模塊

import datetime

import calendar

today = datetime.date.today()

target_day = calendar.FRIDAY

this_day = today.weekday()

delta_to_target = (this_day - target_day) % 7

last_friday = today - datetime.timedelta(days = delta_to_target)

print(last_friday.strftime("%d-%b-%Y"))

3.計算歌曲的總播放時間

#! /usr/bin/env python

#coding=utf-8

# 獲取一個列表中的所有歌曲的播放時間之和

import datetime

def total_timer(times):

td = datetime.timedelta(0)

duration = sum([datetime.timedelta(minutes = m, seconds = s) for m, s in times], td)

return duration

times1 = [(2, 36),

(3, 35),

(3, 45),

]

times2 = [(3, 0),

(5, 13),

(4, 12),

(1, 10),

]

assert total_timer(times1) == datetime.timedelta(0, 596)

assert total_timer(times2) == datetime.timedelta(0, 815)

print("Tests passed.\n"

"First test total: %s\n"

"Second test total: %s" % (total_timer(times1), total_timer(times2)))

4.反復執行某個命令

#! /usr/bin/env python

#coding=utf-8

# 以需要的時間間隔執行某個命令

import time, os

def re_exe(cmd, inc = 60):

while True:

os.system(cmd);

time.sleep(inc)

re_exe("echo %time%", 5)

5.定時任務

#! /usr/bin/env python

#coding=utf-8

#這里需要引入三個模塊

import time, os, sched

# 第一個參數確定任務的時間,返回從某個特定的時間到現在經歷的秒數

# 第二個參數以某種人為的方式衡量時間

schedule = sched.scheduler(time.time, time.sleep)

def perform_command(cmd, inc):

os.system(cmd)

def timming_exe(cmd, inc = 60):

# enter用來安排某事件的發生時間,從現在起第n秒開始啟動

schedule.enter(inc, 0, perform_command, (cmd, inc))

# 持續運行,直到計劃時間隊列變成空為止

schedule.run()

print("show time after 10 seconds:")

timming_exe("echo %time%", 10)

6.利用sched實現周期調用

#! /usr/bin/env python

#coding=utf-8

import time, os, sched

# 第一個參數確定任務的時間,返回從某個特定的時間到現在經歷的秒數

# 第二個參數以某種人為的方式衡量時間

schedule = sched.scheduler(time.time, time.sleep)

def perform_command(cmd, inc):

# 安排inc秒后再次運行自己,即周期運行

schedule.enter(inc, 0, perform_command, (cmd, inc))

os.system(cmd)

def timming_exe(cmd, inc = 60):

# enter用來安排某事件的發生時間,從現在起第n秒開始啟動

schedule.enter(inc, 0, perform_command, (cmd, inc))

# 持續運行,直到計劃時間隊列變成空為止

schedule.run()

print("show time after 10 seconds:")

timming_exe("echo %time%", 10)

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python现在时间 命令_Python3 - 时间处理与定时任务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。