TopCoder problem "Uptime" used in SRM 213 (Division I Level Two , Division II Level Three)



Problem Statement

    

When sending automated emails, it is useful if the system sends the time the server was started, and how long it has been up.

Write a method, calcUptime which is given two Strings which describe the time the server was started and the current time, and calculates the amount time the system has been "up". The time the system was started and the current time (now) will be given in the following format:

	day month year at hh:mm:ss AM/PM

The day of the month will be between 1 and 31, inclusive, (or 30, 29, or 28, depending on the month and year). Month will be a 3-letter abbreviation (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec.) hh, mm, and ss will always be two digits, with leading zeros if less than 10. Year will be between 1900 and 2199, inclusive. The word 'at' will always be in lower case, and either AM or PM (always upper case) will always be present. (Note that 12:00 PM is considered noon and 12:00 AM is considered midnight of the day that is just starting.)

For example:

	7 Jun 2004 at 04:41:32 PM

Your method should return a String in the following format:

	#d #h #m #s
where each number is immediately adjacent to the units that it represents. Numbers should not have a leading zero. If any of the numbers are zero, omit that section. The maximum number of seconds returned is 59, the maximum number of minutes returned is 59, the maximum number of hours returned is 23, and there is no maximum number of days.

For example, an uptime period of zero days, 16 hours 36 minutes and zero seconds would be represented as:

	16h 36m
 

Definition

    
Class:Uptime
Method:calcUptime
Parameters:String, String
Returns:String
Method signature:String calcUptime(String started, String now)
(be sure your method is public)
    
 

Notes

-Note that 12:00 PM is considered noon and 12:00 AM is considered midnight of the day that is just starting.
-You must take leap years into account. Leap years are divisible by 4, except for years divisible by 100, unless they are also divisible by 400. Therefore, 1996 and 2000 were leap years, but 1900 was not, and the year 2100 will not be.
-The 3-letter month abbreviations are Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec. The number of days in each of the corresponding months in a non-leap year is: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31. In a leap year the number of days in each month is: 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31.
-Do NOT consider "Daylight savings" when calculating uptime.
 

Constraints

-started and now will be between "1 Jan 1900 at 12:00:00 AM" and "31 Dec 2199 at 11:59:59 PM", inclusive.
-started and now will both be in the format described in the problem statement.
-now will never be before started
 

Examples

0)
    
"7 Jun 2004 at 04:41:32 PM"
"8 Jun 2004 at 07:16:28 PM"
Returns: "1d 2h 34m 56s"
1)
    
"7 Jun 2004 at 04:41:32 PM"
"7 Jun 2004 at 04:41:32 PM"
Returns: ""
Zero days, zero hours, zero minutes, zero seconds should return "".
2)
    
"28 Feb 2004 at 01:23:45 PM"
"1 Mar 2004 at 12:34:56 AM"
Returns: "1d 11h 11m 11s"
Remember leap years.
3)
    
"28 Feb 2005 at 01:23:45 PM"
"1 Jan 2015 at 12:34:56 AM"
Returns: "3593d 11h 11m 11s"
Don't show years.
4)
    
"25 Jan 1922 at 05:58:52 AM"
"26 Feb 2190 at 11:53:14 AM"
Returns: "97918d 5h 54m 22s"

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=2893

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=5859&pm=2893

Writer:

dplass

Testers:

PabloGilberto , lbackstrom , brett1479

Problem categories:

Math, String Parsing