-1

I am currently working on a program regarding clock arithmetic. My program asks the user for two separate times, in standard format and in military time [HH:MM:SS]. I then add those two times together to get my final result. I need help with two issues that I have tried to solve, but I have struggled a lot with.

In the end, it is supposed to look like this: HH:MM:SS + HH:MM:SS = HH:MM:SS The result all depends on what the user inputs

  1. Would you be able to tell me a way I can prevent the time from going to/over 24 hours, 60 minutes, and 60 seconds? These are the cut-offs and a summed time would not make sense if the hours, minutes, or seconds went past their cut-offs. I wanted to know how I could do this, I was thinking that it would take integer division or modulus operators (%). I would greatly appreciate your help here. I need to keep my time within these boundaries because the user could pick any time that could spill over. After these cut-offs have been reached, the time should restart back to zero and wrap around from there.

  2. How do I ensure that my final time stays in [HH:MM:SS] format? I would greatly appreciate your help with this. Some of the time, the format is coming out as [H:M:S], which is what I do not want.

I would greatly appreciate your help with these two issues that I am struggling with. I am so close to having everything down right. I just need to know the code for giving my time a cut-off to restart after reaching a certain limit, as well as a way to keep the [HH:MM:SS] format in place. My code for the program is listed below. Thank You so much,

Code

ClockTime1 = input('Enter clock two timie (in military time) in 
the format HH:MM:SS , it has to be in this format in order to 
function correctly :')
ClockTime2= input('Enter clock one time (in military time) in 
the format HH:MM:SS , it has to be in this format in order to 
function correctly :')
print(ClockTime1.split(':'))
print(ClockTime2.split(':'))
ClockTime1Hours= int((ClockTime1.split(':')[0]))
ClockTime2Hours= int((ClockTime2.split(':')[0]))
ClockTime2Minutes= int((ClockTime2.split(':')[1]))
ClockTime1Seconds= int((ClockTime1.split(':')[2]))
ClockTime2Seconds= int((ClockTime2.split(':')[2]))
print(ClockTime1Hours,'hours for clock 1')
print(ClockTime2Hours,'hours for clock 2')
print(ClockTime1Minutes,'minutes for clock 1')
print(ClockTime2Minutes,'minutes for clock 2')
print(ClockTime1Seconds,'seconds for clock 1')
print(ClockTime2Seconds,'seconds for clock 2')
ClockTime1Hours += ClockTime2Hours
print('sum of clock hours=',ClockTime1Hours)
ClockTime1Minutes += ClockTime2Minutes
print('sum of clock minutes=',ClockTime1Minutes)
ClockTime1Seconds += ClockTime2Seconds
print('sum of clock seconds=',ClockTime1Seconds)

What the console shows:

Enter clock two time (in military time) in the format HH:MM:SS 
, it has to be in this format in order to function correctly 
:2:00:00

Enter clock one time (in military time) in the format HH:MM:SS 
, it has to be in this format in order to function correctly 
:3:00:00
['2', '00', '00']
['3', '00', '00']
2 hours for clock 1
3 hours for clock 2
0 minutes for clock 1
0 minutes for clock 2
0 seconds for clock 1
0 seconds for clock 2
sum of clock hours= 5
sum of clock minutes= 0
sum of clock seconds= 0
Sum of Times=5:0:0
Community
  • 1
  • 1

1 Answers1

1

I think python time module can help

import time

a = "00:00:00 1971"
a = time.mktime(time.strptime(a,"%H:%M:%S %Y"))
ClockTime1 = input('Enter clock two timie (in military time) in the format HH:MM:SS , 
it has to be in this format in order to function correctly :')
ClockTime2= input('Enter clock one time (in military time) in the format HH:MM:SS , 
it has to be in this format in order to function correctly :')
ClockTime1+=" 1971"
ClockTime2+=" 1971"
ClockTime1 = time.mktime(time.strptime(ClockTime1,"%H:%M:%S %Y"))
ClockTime2 = time.mktime(time.strptime(ClockTime2,"%H:%M:%S %Y"))
print(time.strftime("%H:%M:%S", time.localtime(ClockTime1-a+ClockTime2-a+a)))
Waffle
  • 43
  • 5
  • Thank You for your help. However, this is unfortunately not working for me. Also, I do not need the year included. I would greatly appreciate your help with this. The code that you suggested is not working for me. This is what comes back after I input that code: `SyntaxError: EOL while scanning string literal1. For some reason my program is not liking this code. Please help me to get the time in the right format. Best, --JAP –  Sep 13 '18 at 13:43
  • That error might be due to the line """ClockTime1 = input('Enter clock two timie (in military time) in the format HH:MM:SS , it has to be in this format in order to function correctly :')""" make sure it remains in one line. And the year is just to convert it to milliseconds which is needed to function normally. You only need to input HH:MM:SS. – Waffle Sep 13 '18 at 19:23