Statistics

Problem Statement for "Uptime"

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

  1. "7 Jun 2004 at 04:41:32 PM"

    "8 Jun 2004 at 07:16:28 PM"

    Returns: "1d 2h 34m 56s"

  2. "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 "".

  3. "28 Feb 2004 at 01:23:45 PM"

    "1 Mar 2004 at 12:34:56 AM"

    Returns: "1d 11h 11m 11s"

    Remember leap years.

  4. "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.

  5. "1 Jan 1900 at 12:00:00 AM"

    "31 Dec 2199 at 11:59:59 PM"

    Returns: "109572d 23h 59m 59s"

  6. "1 Jan 1900 at 12:00:00 AM"

    "31 Dec 1900 at 11:59:59 PM"

    Returns: "364d 23h 59m 59s"

  7. "1 Jan 1900 at 12:00:00 AM"

    "28 Feb 1900 at 11:59:59 PM"

    Returns: "58d 23h 59m 59s"

  8. "1 Jan 2000 at 12:00:00 AM"

    "28 Feb 2000 at 11:59:59 PM"

    Returns: "58d 23h 59m 59s"

  9. "1 Jan 2000 at 12:00:00 AM"

    "29 Feb 2000 at 11:59:59 PM"

    Returns: "59d 23h 59m 59s"

  10. "1 Mar 2000 at 12:00:00 AM"

    "28 Feb 2004 at 11:59:59 PM"

    Returns: "1459d 23h 59m 59s"

  11. "1 Jan 2000 at 12:00:00 AM"

    "1 Jan 2000 at 12:00:01 AM"

    Returns: "1s"

  12. "1 Jan 2000 at 12:00:00 AM"

    "1 Jan 2000 at 12:01:00 AM"

    Returns: "1m"

  13. "1 Jan 2000 at 12:00:00 AM"

    "1 Jan 2000 at 01:00:00 PM"

    Returns: "13h"

  14. "1 Apr 2000 at 12:00:00 AM"

    "1 May 2000 at 12:00:01 AM"

    Returns: "30d 1s"

  15. "1 Jun 2000 at 12:00:00 AM"

    "1 Jul 2000 at 12:01:01 AM"

    Returns: "30d 1m 1s"

  16. "1 Aug 2000 at 12:00:00 AM"

    "1 Sep 2000 at 12:01:01 AM"

    Returns: "31d 1m 1s"

  17. "1 Oct 2000 at 01:00:00 AM"

    "1 Nov 2000 at 12:00:01 AM"

    Returns: "30d 23h 1s"

  18. "1 Nov 2000 at 12:00:00 PM"

    "1 Dec 2000 at 12:00:01 AM"

    Returns: "29d 12h 1s"

  19. "31 Dec 2000 at 12:00:00 AM"

    "1 Jan 2001 at 12:00:01 AM"

    Returns: "1d 1s"

  20. "1 Jan 1900 at 02:21:47 AM"

    "2 Feb 1963 at 05:52:38 AM"

    Returns: "23042d 3h 30m 51s"

  21. "3 Mar 2000 at 09:27:40 AM"

    "4 Apr 2100 at 02:25:06 AM"

    Returns: "36555d 16h 57m 26s"

  22. "5 May 2101 at 01:27:14 AM"

    "6 Jun 2199 at 05:10:57 PM"

    Returns: "35826d 15h 43m 43s"

  23. "7 Jul 1979 at 05:31:10 PM"

    "8 Aug 2007 at 07:20:32 AM"

    Returns: "10258d 13h 49m 22s"

  24. "9 Sep 1922 at 06:21:56 PM"

    "10 Oct 2032 at 04:33:20 AM"

    Returns: "40208d 10h 11m 24s"

  25. "11 Nov 2047 at 08:00:28 PM"

    "12 Dec 2082 at 12:26:48 PM"

    Returns: "12814d 16h 26m 20s"

  26. "13 Jan 1923 at 02:54:06 PM"

    "14 Feb 1937 at 08:19:44 AM"

    Returns: "5145d 17h 25m 38s"

  27. "15 Mar 2004 at 03:32:54 AM"

    "16 Apr 2049 at 10:21:49 AM"

    Returns: "16468d 6h 48m 55s"

  28. "17 May 1966 at 03:10:24 PM"

    "18 Jun 2138 at 05:13:35 PM"

    Returns: "62854d 2h 3m 11s"

  29. "19 Jul 2098 at 08:15:11 PM"

    "20 Aug 2159 at 09:20:42 AM"

    Returns: "22310d 13h 5m 31s"

  30. "21 Sep 2056 at 01:30:24 PM"

    "22 Oct 2084 at 05:35:28 PM"

    Returns: "10258d 4h 5m 4s"

  31. "23 Nov 1966 at 09:19:29 AM"

    "24 Dec 1995 at 02:18:25 PM"

    Returns: "10623d 4h 58m 56s"

  32. "25 Jan 1922 at 05:58:52 AM"

    "26 Feb 2190 at 11:53:14 AM"

    Returns: "97918d 5h 54m 22s"

  33. "27 Mar 2067 at 02:45:10 PM"

    "28 Apr 2067 at 10:24:30 AM"

    Returns: "31d 19h 39m 20s"

  34. "29 May 1914 at 03:39:06 AM"

    "30 Jun 2123 at 07:51:09 PM"

    Returns: "76368d 16h 12m 3s"

  35. "1 Jan 1900 at 12:00:00 AM"

    "1 Jan 1901 at 12:00:00 AM"

    Returns: "365d"

  36. "1 Jan 1904 at 12:00:00 AM"

    "1 Jan 1905 at 12:00:00 AM"

    Returns: "366d"

  37. "1 Jan 1900 at 12:00:00 AM"

    "1 Jan 1900 at 12:00:00 AM"

    Returns: ""

  38. "1 Jun 2000 at 12:00:00 AM"

    "1 Jul 2000 at 12:00:00 AM"

    Returns: "30d"

  39. "1 Jun 2000 at 12:00:00 AM"

    "1 Aug 2000 at 12:00:00 AM"

    Returns: "61d"

  40. "1 Jun 2000 at 12:00:00 AM"

    "1 Sep 2000 at 12:00:00 AM"

    Returns: "92d"

  41. "1 Jun 2000 at 12:00:00 AM"

    "1 Oct 2000 at 12:00:00 AM"

    Returns: "122d"

  42. "1 Jun 2000 at 12:00:00 AM"

    "1 Nov 2000 at 12:00:00 AM"

    Returns: "153d"

  43. "1 Jun 2000 at 12:00:00 AM"

    "1 Dec 2000 at 12:00:00 AM"

    Returns: "183d"

  44. "7 Jun 2004 at 04:41:32 PM"

    "8 Jun 2004 at 07:16:28 PM"

    Returns: "1d 2h 34m 56s"

  45. "4 Apr 2004 at 01:59:00 AM"

    "4 Apr 2004 at 03:59:00 AM"

    Returns: "2h"

  46. "7 Jun 1901 at 04:41:32 PM"

    "7 Jun 2004 at 04:41:32 PM"

    Returns: "37621d"

  47. "7 Jun 2004 at 04:41:28 PM"

    "8 Jun 2004 at 07:16:28 PM"

    Returns: "1d 2h 35m"

  48. "7 Jun 2004 at 04:41:32 PM"

    "9 Jun 2004 at 12:41:32 PM"

    Returns: "1d 20h"

  49. "1 Jan 2004 at 01:02:02 AM"

    "2 Jan 2004 at 01:02:02 AM"

    Returns: "1d"

  50. "25 Jan 1922 at 05:58:52 AM"

    "26 Feb 2190 at 11:53:14 AM"

    Returns: "97918d 5h 54m 22s"

  51. "7 Jun 2004 at 04:41:32 PM"

    "7 Jun 2004 at 04:41:32 PM"

    Returns: ""

  52. "7 Jun 2004 at 04:41:32 PM"

    "7 Jun 2004 at 06:41:32 PM"

    Returns: "2h"

  53. "7 Jun 2004 at 04:41:32 PM"

    "8 Jun 2004 at 04:41:33 PM"

    Returns: "1d 1s"

  54. "7 Jun 2004 at 04:41:32 PM"

    "8 Jun 2004 at 04:41:32 PM"

    Returns: "1d"

  55. "8 May 1900 at 07:16:28 PM"

    "8 May 2004 at 07:16:28 PM"

    Returns: "37986d"

  56. "7 Jun 2004 at 04:41:32 PM"

    "7 Jun 2004 at 04:41:33 PM"

    Returns: "1s"

  57. "15 Jan 2004 at 01:00:00 PM"

    "15 Jun 2004 at 01:00:00 AM"

    Returns: "151d 12h"

  58. "7 Jun 1900 at 04:41:32 PM"

    "7 Jun 2199 at 04:41:32 PM"

    Returns: "109208d"

  59. "31 Jan 1900 at 12:58:52 AM"

    "1 Mar 2199 at 12:58:51 AM"

    Returns: "109236d 23h 59m 59s"

  60. "7 Jun 2004 at 04:41:32 PM"

    "7 Jun 2004 at 06:43:32 PM"

    Returns: "2h 2m"

  61. "7 Jun 2004 at 12:41:32 PM"

    "8 Jun 2004 at 07:16:28 PM"

    Returns: "1d 6h 34m 56s"

  62. "27 Feb 2004 at 01:23:45 PM"

    "28 Feb 2004 at 01:23:45 PM"

    Returns: "1d"

  63. "7 Jun 2004 at 04:41:32 PM"

    "8 Jun 2004 at 04:43:32 PM"

    Returns: "1d 2m"

  64. "1 Jan 1999 at 12:00:00 AM"

    "2 Jan 2004 at 01:00:00 AM"

    Returns: "1827d 1h"

  65. "1 Jan 1900 at 12:00:00 AM"

    "31 Dec 2199 at 11:00:59 PM"

    Returns: "109572d 23h 59s"

  66. "7 Jun 2004 at 04:41:32 PM"

    "7 Jun 2004 at 05:41:32 PM"

    Returns: "1h"

  67. "7 Jun 2004 at 12:00:00 AM"

    "8 Jun 2004 at 12:00:00 AM"

    Returns: "1d"

  68. "7 Jun 2004 at 04:41:32 PM"

    "7 Jun 2004 at 05:56:32 PM"

    Returns: "1h 15m"

  69. "7 Jun 2004 at 04:41:32 PM"

    "8 Jun 2004 at 05:16:28 PM"

    Returns: "1d 34m 56s"

  70. "7 Jun 2004 at 04:09:32 PM"

    "8 Jun 2004 at 07:16:28 PM"

    Returns: "1d 3h 6m 56s"

  71. "27 Feb 2004 at 01:23:45 PM"

    "28 Feb 2004 at 01:24:45 PM"

    Returns: "1d 1m"

  72. "1 Jan 2000 at 12:00:00 AM"

    "31 Dec 2000 at 11:59:59 PM"

    Returns: "365d 23h 59m 59s"

  73. "27 Feb 2004 at 01:01:01 PM"

    "1 Mar 2004 at 01:01:01 PM"

    Returns: "3d"

  74. "7 Jun 2004 at 12:41:32 PM"

    "7 Jun 2004 at 04:41:32 PM"

    Returns: "4h"

  75. "12 Mar 2004 at 12:12:12 PM"

    "13 Apr 2004 at 12:12:12 AM"

    Returns: "31d 12h"

  76. "7 Jun 2004 at 11:59:19 AM"

    "7 Jun 2004 at 12:01:07 PM"

    Returns: "1m 48s"

  77. "1 Feb 2004 at 12:58:52 AM"

    "1 Mar 2004 at 12:59:52 AM"

    Returns: "29d 1m"

  78. "7 Jun 2004 at 04:41:32 PM"

    "8 Dec 2004 at 07:16:28 PM"

    Returns: "184d 2h 34m 56s"

  79. "2 Jan 1900 at 12:00:00 AM"

    "2 Jan 1900 at 12:00:00 PM"

    Returns: "12h"


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: