@VisibleForTesting static int parsePeriodToSeconds(String periodStr) { try { return Period.parse(periodStr.toUpperCase(), PERIOD_FORMATTER).toStandardSeconds().getSeconds(); } catch(ArithmeticException ae) { throw new RuntimeException(String.format("Reporting interval is too long. Max: %d seconds.", Integer.MAX_VALUE)); } }
private static String getSecondsCron(Period p) { Integer sec = p.getSeconds(); Seconds s = p.toStandardSeconds(); Integer seconds = s.getSeconds(); String str = "0" + (sec > 0 ? "/" + sec : ""); if (seconds > 60) { str = sec + ""; } return str; }
Period period = new Period(60, 40, 20, 500); System.out.println(period.toStandardSeconds().getSeconds() / 60.0); // outputs 3640.3333333333335
/** * Translates the given (Joda) Period to duration in seconds. * * @param period the period * @return the duration representing the same amount of time in seconds */ public static long toDuration(Period period) { return period.toStandardSeconds().getSeconds(); }
public CallDetailRecord setEndDate(DateTime endDate) { this.endDate = endDate; duration = new Period(startDate, endDate).toStandardSeconds().getSeconds(); return this; }
@VisibleForTesting static int parsePeriodToSeconds(String periodStr) { try { return Period.parse(periodStr.toUpperCase(), PERIOD_FORMATTER).toStandardSeconds().getSeconds(); } catch(ArithmeticException ae) { throw new RuntimeException(String.format("Reporting interval is too long. Max: %d seconds.", Integer.MAX_VALUE)); } }
@VisibleForTesting static int parsePeriodToSeconds(String periodStr) { try { return Period.parse(periodStr.toUpperCase(), PERIOD_FORMATTER).toStandardSeconds().getSeconds(); } catch(ArithmeticException ae) { throw new RuntimeException(String.format("Reporting interval is too long. Max: %d seconds.", Integer.MAX_VALUE)); } }
public Sessionize(String timeSpec) { Period p = new Period("PT" + timeSpec.toUpperCase()); this.millis = p.toStandardSeconds().getSeconds() * 1000; cleanup(); }
Period p = new Period(dt1, dt2); System.out.println("Days: " + p.toStandardDays().getDays()); System.out.println("Hours: " + p.toStandardHours().getHours()); System.out.println("Minutes: " + p.toStandardMinutes().getMinutes()); System.out.println("Seconds: " + p.toStandardSeconds().getSeconds());
public SessionCount(String timeSpec) { Period p = new Period("PT" + timeSpec.toUpperCase()); this.millis = p.toStandardSeconds().getSeconds() * 1000; cleanup(); }
@Override public void setDedupIntervalMin(String newDedupIntervalMin) { if (newDedupIntervalMin == null || newDedupIntervalMin.isEmpty()) { dedupIntervalSec = 0; return; } try { Period period = Period.parse(newDedupIntervalMin); this.dedupIntervalSec = period.toStandardSeconds().getSeconds(); } catch (Exception e) { LOG.warn("Fail to pares deDupIntervalMin, will disable deduplication instead", e); this.dedupIntervalSec = 0; } }
PeriodFormatter pf = new PeriodFormatterBuilder(). appendHours().appendSeparator(":"). appendMinutes().appendSeparator(":"). appendSeconds().toFormatter(); Period period = pf.parsePeriod("31:58:00"); System.out.println(period.toStandardSeconds().getSeconds());
private void logCacheLoadTime(long startTime) { Period cacheLoadPeriod = new Period(startTime, System.currentTimeMillis()); LOGGER.info("Cache load finished in {} ({} seconds)", PeriodFormat.getDefault().print(cacheLoadPeriod.normalizedStandard()), cacheLoadPeriod.toStandardSeconds()); }
private void logCacheLoadTime(long startTime) { Period cacheLoadPeriod = new Period(startTime, System.currentTimeMillis()); LOGGER.info("Cache load finished in {} ({} seconds)", PeriodFormat.getDefault().print(cacheLoadPeriod.normalizedStandard()), cacheLoadPeriod.toStandardSeconds()); }
@Override public Duration deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectCodec oc = parser.getCodec(); JsonNode node = oc.readTree(parser); final Period period = FORMATTER.parsePeriod(node.asText()); return new Duration(0, (long) period.toStandardSeconds().getSeconds()); } }
@Override public Duration deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectCodec oc = parser.getCodec(); JsonNode node = oc.readTree(parser); final Period period = FORMATTER.parsePeriod(node.asText()); return new Duration(0, (long) period.toStandardSeconds().getSeconds()); } }
private static String getSecondsCron(Period p) { Integer sec = p.getSeconds(); Seconds s = p.toStandardSeconds(); Integer seconds = s.getSeconds(); String str = "0" + (sec > 0 ? "/" + sec : ""); if (seconds > 60) { str = sec + ""; } return str; }
String dateText = "1h50m22s"; PeriodFormatterBuilder formatterBuilder = new PeriodFormatterBuilder(); if(dateText.contains("h")){ formatterBuilder.appendHours().appendLiteral("h"); } if(dateText.contains("m")){ formatterBuilder.appendMinutes().appendLiteral("m"); } if(dateText.contains("s")){ formatterBuilder.appendSeconds().appendLiteral("s"); } Period period = formatterBuilder.toFormatter().parsePeriod(dateText); int totalSeconds = period.toStandardSeconds().getSeconds();
@Test public void testJodaTimePeriodBySeconds() throws ParseException { String periodText = "PT10s"; Period period = new Period(periodText); int seconds = period.toStandardSeconds().getSeconds(); Assert.assertEquals(10, seconds); }
@Test public void testJodaTimePeriod() throws ParseException { String periodText = "PT10m"; Period period = new Period(periodText); int seconds = period.toStandardSeconds().getSeconds(); Assert.assertEquals(600, seconds); Assert.assertEquals(60, period.toStandardSeconds().dividedBy(10).getSeconds()); }