Python中的时间调整 relativedelta (注意函数中加s , 如minutes)

json文件中的值: 

{
"t0_hour": 0,
"t0_minute": 0,
"t1_hour": 7,
"t1_minute": 30,
"t2_hour": 12,}

1 实时时间的获取

2 设置固定的时间的 

config = Config('./config/config.json')
now_temp = datetime.now()
t = datetime(year=now_temp.year, month=now_temp.month, day=now_temp.day, hour=now_temp.hour,
             minute=now_temp.minute)

t0 = datetime(t.year, t.month, t.day, config.t0_hour, config.t0_minute)
t1 = datetime(t.year, t.month, t.day, config.t1_hour, config.t1_minute)
t = t1 + relativedelta(minute=25)
print('t1时间: ',t1)
print('时间: ',t)

if t.hour < t1.hour or (t.hour == t1.hour and t.minute < t1.minute):
    print('***** 7.30以下')
else:
    print('7.30+++++++++')

3 时间的调整

config = Config('./config/config.json')
now_temp = datetime.now()
t = datetime(year=now_temp.year, month=now_temp.month, day=now_temp.day, hour=now_temp.hour,
             minute=now_temp.minute)
target_time = t - relativedelta(minute=2)

print(t, target_time)
next_hours = (t - target_time).seconds / 3600  # 时间差,小时
print(t - target_time)
next_hours

 4 注意 relativedelta(minutes=2) 中minutes 和 minute 差别较大,

config = Config('./config/config.json')
now_temp = datetime.now()
t = datetime(year=now_temp.year, month=now_temp.month, day=now_temp.day, hour=now_temp.hour,
             minute=now_temp.minute)
target_time = t - relativedelta(minutes=2)

print(t, target_time)
next_hours = (t - target_time).seconds / 3600  # 时间差,小时
print(t - target_time)
next_hours

你可能感兴趣的:(公司项目思路,python,numpy,开发语言)