ClockPanel extends JPanel { static long stringToSeconds(String time) throws NumberFormatException, DateTimeParseException { long duration = 0; if (time.matches(INPUT_HMS_REGEX)) { String textWithoutWhitespaces = time.replaceAll(WHITESPACE_REGEX, ""); duration = Duration.parse("PT" + textWithoutWhitespaces).toMillis() / 1000; } else { String[] parts = time.split(":"); for (int i = parts.length - 1, multiplier = 1; i >= 0 && multiplier <= 3600; i--, multiplier *= 60) { duration += Integer.parseInt(parts[i].trim()) * multiplier; } } return duration; } ClockPanel(ClockManager clockManager, Clock clock, String clockType, boolean editable);   }
@Test public void properColonSeparatedTimeStringShouldReturnCorrectSeconds() { assertEquals(5, ClockPanel.stringToSeconds("5")); assertEquals(50, ClockPanel.stringToSeconds("50")); assertEquals(120, ClockPanel.stringToSeconds("2:00")); assertEquals(120, ClockPanel.stringToSeconds("0:120")); assertEquals(120, ClockPanel.stringToSeconds("0:0:120")); assertEquals(1200, ClockPanel.stringToSeconds("20:00")); assertEquals(50, ClockPanel.stringToSeconds("00:00:50")); assertEquals(121, ClockPanel.stringToSeconds("00:02:01")); assertEquals(3660, ClockPanel.stringToSeconds("01:01:00")); assertEquals(9000, ClockPanel.stringToSeconds("2:30:00")); assertEquals(9033, ClockPanel.stringToSeconds("02:30:33")); assertEquals(82800, ClockPanel.stringToSeconds("23:00:00")); assertEquals(400271, ClockPanel.stringToSeconds("111:11:11")); }
@Test public void properIntuitiveTimeStringShouldReturnCorrectSeconds() { assertEquals(5, ClockPanel.stringToSeconds("5s")); assertEquals(50, ClockPanel.stringToSeconds("50s")); assertEquals(120, ClockPanel.stringToSeconds("2m")); assertEquals(120, ClockPanel.stringToSeconds("120s")); assertEquals(1200, ClockPanel.stringToSeconds("20m")); assertEquals(121, ClockPanel.stringToSeconds("2m1s")); assertEquals(121, ClockPanel.stringToSeconds("2m 1s")); assertEquals(3660, ClockPanel.stringToSeconds("1h 1m")); assertEquals(3660, ClockPanel.stringToSeconds("61m")); assertEquals(3660, ClockPanel.stringToSeconds("3660s")); assertEquals(9000, ClockPanel.stringToSeconds("2h 30m")); assertEquals(9033, ClockPanel.stringToSeconds("2h 30m 33s")); assertEquals(82800, ClockPanel.stringToSeconds("23h")); assertEquals(400271, ClockPanel.stringToSeconds("111h 11m 11s")); }