0
votes

I have the following python code to calculate how many days, hours, minutes, seconds and milliseconds an amount of marsdays is. I want the output to be:

x sols = x days, x hours, x minutes, x seconds, x milliseconds

I found out I had to use timedelta because it uses milliseconds, which I need.

I have written the following code but it doesn't give a correct output for the values. When they're >60 they need to be passed on because 60 seconds equals 1 hour. But the values can go >60. for example, when I enter a value >1, like 4, I get the aswer: 4 sol = 4 days, 2 hours, 38 minutes, 7220 seconds, 976 milliseconds.

from datetime import datetime, timedelta
sol = timedelta(hours=24,minutes=39,seconds=35,milliseconds=244)
amount = input("Amount of sols: ")
earth=int(amount)*sol
hours = int(earth.seconds/3600)
minutes = int((earth.seconds-(hours*3600))/60)
secondes = int(earth.seconds-(minutes*60))
millisecondes = int(earth.microseconds/1000)
print (amount + " sol = " + str(earth.days) + ' days, ' + str(hours) + ' hours, ' + str(minutes) + ' minutes, ' + str(secondes) + ' secondes, ' + str(millisecondes) + ' millisecondes')

The hours, minutes and milliseconds are calculated this way because timedelta can't give them apparently.

earth.days
earth.seconds

exist, but

earth.hours
earth.minutes
earth.milliseconds

don't exist. which I don't understand because I can give them values as seen on line 2.

I think the way I try to achieve what i want isn't correct.

1
Off-topic, but divmod might be useful to you. - leewz
Hint: You forgot a term when calculating secondes. I bet that your secondes is off by a multiple of 3600. - leewz
I don't think I have, when i enter 1 as input the values presented are correct. Just when I enter a value >1, like 4, I get the aswer: 4 sol = 4 days, 2 hours, 38 minutes, 7220 seconds, 976 milliseconds. - Sjoerd1234
7220 is off by 2*3600. You forgot to subtract something else when calculating secondes. - leewz

1 Answers

0
votes

I think if you use "seconds" as a floating point number instead of the datetime library it will work better. The datetime library has a lot of assumptions around Earth dates and times that won't really be valid on Mars.