@Test public void testParseDuration() throws Exception { Assert.assertEquals(Utils.parseDuration("5s"), 5); Assert.assertEquals(Utils.parseDuration("5w"), 5 * Utils.SECONDS_PER_WEEK); Assert.assertEquals(Utils.parseDuration("2y"), 2 * Utils.SECONDS_PER_YEAR); Assert.assertEquals(Utils.parseDuration("5s-ago"), 5); Assert.assertEquals(Utils.parseDuration("5w-ago"), 5 * Utils.SECONDS_PER_WEEK); Assert.assertEquals(Utils.parseDuration("2y-ago"), 2 * Utils.SECONDS_PER_YEAR); Assert.assertEquals(Utils.parseDuration("arglefoo-ago"), 0); Assert.assertEquals(Utils.parseDuration("27t-ago"), 0); }
public static long parseDuration(String v) { int idx; char last; if ((idx = v.indexOf('-')) > 0) { idx -= 1; last = v.charAt(idx); } else { idx = v.length() - 1; last = v.charAt(idx); } long period = 0; try { period = Long.parseLong(v.substring(0, idx)); } catch (NumberFormatException e) { return 0; } switch (last) { case 's': return period; case 'm': return period * SECONDS_PER_MINUTE; case 'h': return period * SECONDS_PER_HOUR; case 'd': return period * SECONDS_PER_DAY; case 'w': return period * SECONDS_PER_WEEK; case 'y': return period * SECONDS_PER_YEAR; } return 0; }
Utils { public static long parseDuration(String v) { int idx; char last; if ((idx = v.indexOf('-')) > 0) { idx -= 1; last = v.charAt(idx); } else { idx = v.length() - 1; last = v.charAt(idx); } long period = 0; try { period = Long.parseLong(v.substring(0, idx)); } catch (NumberFormatException e) { return 0; } switch (last) { case 's': return period; case 'm': return period * SECONDS_PER_MINUTE; case 'h': return period * SECONDS_PER_HOUR; case 'd': return period * SECONDS_PER_DAY; case 'w': return period * SECONDS_PER_WEEK; case 'y': return period * SECONDS_PER_YEAR; } return 0; } }
Utils { public static long parseDuration(String v) { int idx; char last; if ((idx = v.indexOf('-')) > 0) { idx -= 1; last = v.charAt(idx); } else { idx = v.length() - 1; last = v.charAt(idx); } long period = 0; try { period = Long.parseLong(v.substring(0, idx)); } catch (NumberFormatException e) { return 0; } switch (last) { case 's': return period; case 'm': return period * SECONDS_PER_MINUTE; case 'h': return period * SECONDS_PER_HOUR; case 'd': return period * SECONDS_PER_DAY; case 'w': return period * SECONDS_PER_WEEK; case 'y': return period * SECONDS_PER_YEAR; } return 0; } private  Utils(); }
Utils { public static long parseDuration(String v) { int idx; char last; if ((idx = v.indexOf('-')) > 0) { idx -= 1; last = v.charAt(idx); } else { idx = v.length() - 1; last = v.charAt(idx); } long period = 0; try { period = Long.parseLong(v.substring(0, idx)); } catch (NumberFormatException e) { return 0; } switch (last) { case 's': return period; case 'm': return period * SECONDS_PER_MINUTE; case 'h': return period * SECONDS_PER_HOUR; case 'd': return period * SECONDS_PER_DAY; case 'w': return period * SECONDS_PER_WEEK; case 'y': return period * SECONDS_PER_YEAR; } return 0; } private  Utils(); static Map<String, Object> makeError(String errorMessage, String errorCause, String errorPart); static Response getErrorResponse(String id, int status,
                                            String message, String context); static String createUuid(); static long parseDate(String value); static long parseDuration(String v); static String jsonStringFromObject(Object object); static ObjectMapper getObjectMapper(); }
Utils { public static long parseDuration(String v) { int idx; char last; if ((idx = v.indexOf('-')) > 0) { idx -= 1; last = v.charAt(idx); } else { idx = v.length() - 1; last = v.charAt(idx); } long period = 0; try { period = Long.parseLong(v.substring(0, idx)); } catch (NumberFormatException e) { return 0; } switch (last) { case 's': return period; case 'm': return period * SECONDS_PER_MINUTE; case 'h': return period * SECONDS_PER_HOUR; case 'd': return period * SECONDS_PER_DAY; case 'w': return period * SECONDS_PER_WEEK; case 'y': return period * SECONDS_PER_YEAR; } return 0; } private  Utils(); static Map<String, Object> makeError(String errorMessage, String errorCause, String errorPart); static Response getErrorResponse(String id, int status,
                                            String message, String context); static String createUuid(); static long parseDate(String value); static long parseDuration(String v); static String jsonStringFromObject(Object object); static ObjectMapper getObjectMapper(); static final String NOW; static final String DEFAULT_START_TIME; static final String DEFAULT_END_TIME; static final String NOT_SPECIFIED; static final String CLIENT_ID; static final String ERRORS; static final String ERROR_MESSAGE; static final String ERROR_CAUSE; static final String ERROR_PART; static final String START; static final String END; static final String COUNT; static final double DEFAULT_DOWNSAMPLE_MULTIPLIER; static final int DAYS_PER_YEAR; static final int DAYS_PER_WEEK; static final int HOURS_PER_DAY; static final int MINUTES_PER_HOUR; static final int SECONDS_PER_MINUTE; static final int SECONDS_PER_HOUR; static final int SECONDS_PER_DAY; static final int SECONDS_PER_WEEK; static final int SECONDS_PER_YEAR; }