Python 中时间日期对象之间的秒数差异
Difference in seconds between timedate objects in Python
我有以下代码可以根据实际时间和日期以及 Excel 电子表格中的 sunset/sunrise 时间和日期更改 Windows 10 上的背景图片.
from datetime import datetime
import pandas
import ctypes
file_path = "myfile.xlsx"
data = pandas.read_excel(file_path, header=0) #Header on line 0
#Today as day number in reference to 1st of Jan
day = datetime.now().timetuple().tm_yday
#Today's parameters
#sr and ss are column names in the Excel spreadsheet
#Minus 1 to account for 0 based indexing
**sunrise = data["sr"][day-1]
sunset = data["ss"][day-1]**
#Time right now
**now = datetime.now().time()**
#Setting up the day_night variable depending on the now variable
if now > sunrise and now < sunset:
day_night = 'day'
else:
day_night = 'night'
#The path to the wallpapers being used
path = 'C:\wallpapers\'+ day_night +'.jpg'
SPI_SETDESKWALLPAPER = 20
#Function to change the wallpaper
def changeBG(path):
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path, 3) #SystemParametersInfoW for x64 architecture
changeBG(path)
有没有办法计算 now
变量和 sunrise
或 sunset
变量之间的时间差(以秒为单位)?
到目前为止,我尝试了一个像 print (now-sunset)
这样的简单减法,它会 return 给我一个像这样的错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-c713ddbffa2f> in <module>
18 now = datetime.now().time()
19
---> 20 print (now-sunset)
TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'
最直接的方法是手动将时间转换为秒:
from datetime import datetime, timedelta, time
def seconds_in_time(time_value: time):
return (time_value.hour * 60 + time_value.minute) * 60 + time_value.second
# Examples of time objects
t1 = datetime.now().time()
t2 = (datetime.now() + timedelta(minutes=2)).time()
print(seconds_in_time(t2) - seconds_in_time(t1)) # 120
不确定是否有更好的方法,就您使用 time
个对象而言。
我有以下代码可以根据实际时间和日期以及 Excel 电子表格中的 sunset/sunrise 时间和日期更改 Windows 10 上的背景图片.
from datetime import datetime
import pandas
import ctypes
file_path = "myfile.xlsx"
data = pandas.read_excel(file_path, header=0) #Header on line 0
#Today as day number in reference to 1st of Jan
day = datetime.now().timetuple().tm_yday
#Today's parameters
#sr and ss are column names in the Excel spreadsheet
#Minus 1 to account for 0 based indexing
**sunrise = data["sr"][day-1]
sunset = data["ss"][day-1]**
#Time right now
**now = datetime.now().time()**
#Setting up the day_night variable depending on the now variable
if now > sunrise and now < sunset:
day_night = 'day'
else:
day_night = 'night'
#The path to the wallpapers being used
path = 'C:\wallpapers\'+ day_night +'.jpg'
SPI_SETDESKWALLPAPER = 20
#Function to change the wallpaper
def changeBG(path):
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path, 3) #SystemParametersInfoW for x64 architecture
changeBG(path)
有没有办法计算 now
变量和 sunrise
或 sunset
变量之间的时间差(以秒为单位)?
到目前为止,我尝试了一个像 print (now-sunset)
这样的简单减法,它会 return 给我一个像这样的错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-c713ddbffa2f> in <module>
18 now = datetime.now().time()
19
---> 20 print (now-sunset)
TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'
最直接的方法是手动将时间转换为秒:
from datetime import datetime, timedelta, time
def seconds_in_time(time_value: time):
return (time_value.hour * 60 + time_value.minute) * 60 + time_value.second
# Examples of time objects
t1 = datetime.now().time()
t2 = (datetime.now() + timedelta(minutes=2)).time()
print(seconds_in_time(t2) - seconds_in_time(t1)) # 120
不确定是否有更好的方法,就您使用 time
个对象而言。