Determination of if statements based on time calculation results differs from terminal to terminal

Asked 2 years ago, Updated 2 years ago, 34 views

You are calculating the time and attempting to branch the process by whether it is longer than 23400 seconds.
Depending on the terminal, the code below may branch to the else side, but I don't know why.
The version is Python 3.9.7.

syukkin_time="08:30"
taikin_time="15:00"
syukkin_time = datetime.datetime.strptime(syukkin_time, '%H:%M')
taikin_time = datetime.datetime.strptime(taikin_time, '%H:%M')
working_time=taikin_time-syukkin_time
working_time = working_time.total_seconds()

if working_time>=23400.0:
  print('6 hours and 30 minutes or more!')
else:
  print('It's less than 6 hours and 30 minutes!')

python python3

2022-09-30 11:46

1 Answers

I don't know the cause, but if syukkin_time=datetime.datetime.strptime(syukkin_time, '%H:%M'), the date (1900, 1, 1) will be given.
In some environments, such as time zones, it may interfere with time calculations.

If you want to convert from a string to seconds, you may have asked a question in the past, so you should check it out

Below is how to use pandas

>>import pandas as pd
>>>t1=pd.to_timedelta ('08:30:00')
>>>t2=pd.to_timedelta ('15:00:00')
>>>t2-t1
Timedelta ('0 days 06:30:00')
>>>(t2-t1).seconds
23400


2022-09-30 11:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.