Problem 16: Later

Description

Working out what time is 3 hours and 40 minutes later than 11:45 a.m. is not very difficult, but is quite easy to get wrong when worked out in a hurry. For this problem you have to write a program that performs calculations of exactly that kind.

Your program will receive as input a time (in the usual hours and minutes form, using the 12 hour clock), a + or - sign to indicate whether it is to add or subtract an interval, and an interval expressed in hours and minutes. The output should simply be the new time.

Take care to keep the hours between 1 and 12. You do not need to be worried about the difference between a.m. and p.m.: 20 minutes earlier than 12:10 is 11:50; 12 hours later than 12:10 is 12:10.

Input Format

The input to your program will consist of a number of lines, each line containing the following:

  1. An integer between 1 and 12, signifying the 'hours' part of the base time,
  2. An integer between 0 and 59, signifying the 'minutes' part of the base time,
  3. Either a + or a - appearing as a single character,
  4. An integer between 1 and 12, signifying the hours to be added or subtracted, and
  5. An integer between 0 and 59, signifying the minutes to be added or subtracted.
Each of the five components will be separated by a single space.

If the sign is a -, the hours and minutes are both to be subtracted - be careful not to accidentally add the minutes. 2:20 minus 1:05 should be 1:15.

The end of the input will be signalled by a line containing "0 0 + 0 0". This is not an input to be processed, it is simply a marker for the end of the inputs.

Output Format

For each input case print a single line containing the resultant time in the traditional format: the hours in one or two digits, then a colon, then the minutes always in two digits. No spaces anywhere.

Take care when the minutes component is small. "five minutes past two" should be printed as 2:05, not 2:5.

Sample Input

12 10 - 0 20
5 22 - 7 31
4 13 + 0 0
9 30 + 6 0
0 0 + 0 0

Sample Output

11:50
9:51
4:13
3:30

Software Warning

If you are using the vital.h library, make sure you have downloaded a new version on or after Tuesday 12th February 2002, otherwise you will not be able to read two inputs from the same line.

Limits

All the inputs will provide proper base times: the hour component will be between 1 and 12, and the minutes component will be between 0 and 59. The time to be added or subtracted will never be more than 12 hours. In the time to be added, the hours will be between 0 and 12, and the minutes between 0 and 59.

Clue

When dealing with dollars and cents, it is clearly best to convert everything to just cents and work with ints using one consistent unit. So, when dealing with hours and minutes ...

End of Problem