List of usage examples for org.joda.time Duration getStandardSeconds
public long getStandardSeconds()
From source file:org.opendatakit.tables.sms.MsgHandler.java
License:Apache License
private boolean respondToDrSlotQuery(String phoneNum, TableProperties tp, Query query, ColumnProperties drSlotColumn, int drSlotDuration) { Set<Constraint> constraints = new HashSet<Constraint>(); for (int i = query.getConstraintCount(); i >= 0; i--) { Constraint c = query.getConstraint(i); if (c.getColumnDbName().equals(drSlotColumn.getElementKey())) { constraints.add(c);//from w ww .j a v a2s. c om query.removeConstraint(i); } } query.setOrderBy(Query.SortOrder.ASCENDING, drSlotColumn); DbTable dbt = DbTable.getDbTable(dbh, tp); UserTable table = dbt.getRaw(query, new String[] { drSlotColumn.getElementKey() }); // TODO: range should not be slash-separated but stored as two columns OR json in db... List<String[]> rawRanges = new ArrayList<String[]>(); for (int i = 0; i < table.getNumberOfRows(); i++) { rawRanges.add(table.getData(i, 0).split("/")); } String earlyDate = null; String lateDate = null; for (int i = 0; i < query.getConstraintCount(); i++) { Constraint c = query.getConstraint(i); if (c.getComparisonCount() == 2) { String[] range; if (c.getComparator(0) == Query.Comparator.LESS_THAN) { range = new String[] { c.getValue(0), c.getValue(1) }; } else { range = new String[] { c.getValue(1), c.getValue(0) }; } boolean inserted = false; for (int j = 0; j < rawRanges.size(); j++) { if (range[0].compareTo(rawRanges.get(j)[0]) < 0) { rawRanges.add(j, range); inserted = true; break; } } if (!inserted) { rawRanges.add(range); } } else if (c.getComparator(0) == Query.Comparator.LESS_THAN) { if (lateDate == null) { lateDate = c.getValue(0); } else if (c.getValue(0).compareTo(lateDate) < 0) { lateDate = c.getValue(0); } } else if (c.getComparator(0) == Query.Comparator.GREATER_THAN) { if (earlyDate == null) { earlyDate = c.getValue(0); } else if (c.getValue(0).compareTo(earlyDate) > 0) { earlyDate = c.getValue(0); } } } if (earlyDate != null) { rawRanges.add(new String[] { null, earlyDate }); } if (lateDate != null) { for (int j = 0; j < rawRanges.size(); j++) { if (lateDate.compareTo(rawRanges.get(j)[0]) < 0) { rawRanges.add(j, new String[] { lateDate, null }); break; } } } if (rawRanges.isEmpty()) { String resp = "anytime"; smsSender.sendSMSWithCutoff(phoneNum, resp); return true; } List<String[]> ranges = new ArrayList<String[]>(); ranges.add(rawRanges.get(0)); for (int i = 1; i < rawRanges.size(); i++) { String[] lastRange = ranges.get(ranges.size() - 1); String[] nextRange = rawRanges.get(i); if (nextRange[1] == null) { ranges.add(nextRange); break; } if (nextRange[0].compareTo(lastRange[1]) > 0) { ranges.add(nextRange); } else if (nextRange[1].compareTo(lastRange[1]) > 0) { lastRange[1] = nextRange[1]; } } StringBuilder sb = new StringBuilder(); if (ranges.get(0)[0] != null) { DateTime dt = du.parseDateTimeFromDb(ranges.get(0)[0]); sb.append(";before" + du.formatShortDateTimeForUser(dt)); } for (int i = 1; i < ranges.size(); i++) { DateTime start = du.parseDateTimeFromDb(ranges.get(i - 1)[1]); DateTime end = du.parseDateTimeFromDb(ranges.get(i)[0]); Duration duration = new Duration(start, end); if (duration.getStandardSeconds() >= drSlotDuration) { sb.append(";" + du.formatShortDateTimeForUser(start) + "-" + du.formatShortDateTimeForUser(end)); } } if (ranges.get(ranges.size() - 1)[1] != null) { DateTime dt = du.parseDateTimeFromDb(ranges.get(ranges.size() - 1)[1]); sb.append(";after" + du.formatShortDateTimeForUser(dt)); } sb.deleteCharAt(0); String resp = sb.toString(); smsSender.sendSMSWithCutoff(phoneNum, resp); return true; }
From source file:org.tanrabad.survey.utils.time.DurationTimePrinter.java
License:Apache License
public static String print(DateTime startTime, DateTime endTime) { Duration duration = new Duration(startTime, endTime); if (duration.getStandardHours() > 0) return String.format(Locale.US, "%d:%02d:%02d", duration.getStandardHours(), duration.getStandardMinutes() % 60, duration.getStandardSeconds() % 60); else/*from ww w. ja va 2 s . c o m*/ return String.format(Locale.US, "%02d:%02d", duration.getStandardMinutes(), duration.getStandardSeconds() % 60); }
From source file:org.thelq.pircbotx.commands.Alarm.java
License:Open Source License
public void countdown() throws InterruptedException { DateTime startDate = DateTime.now(DateTimeZone.UTC); Duration duration = new Duration(startDate, alarmDate); long durationSeconds = duration.getStandardSeconds(); //Register times we want to notify the user List<Integer> notifySeconds = getNotifySeconds(durationSeconds); notifySeconds.add(0);/* w w w .j a va 2s . c o m*/ List<DateTime> notifyTimes = Lists.newArrayList(); for (Integer curSecond : notifySeconds) { //If the requested seconds is still in the period, add to list DateTime notifyTime = alarmDate.minusSeconds(curSecond); if (notifyTime.isAfter(startDate) || notifyTime.isEqual(startDate)) notifyTimes.add(notifyTime); } onStart(durationSeconds); DateTime lastDateTime = startDate; for (DateTime curDateTime : notifyTimes) { long waitPeriodMilli = curDateTime.getMillis() - lastDateTime.getMillis(); onNotifyBefore((int) waitPeriodMilli / 1000); Thread.sleep(waitPeriodMilli); long secondsRemaining = new Duration(curDateTime, alarmDate).getStandardSeconds(); if (secondsRemaining <= 0) //Done break; else onNotify(secondsRemaining); lastDateTime = curDateTime; } onEnd(); }
From source file:org.wisdom.cache.ehcache.EhCacheService.java
License:Apache License
/** * Adds an entry in the cache.//from w w w. j a v a 2 s .c o m * * @param key Item key. * @param value Item value. * @param expiration Expiration time. */ @Override public void set(String key, Object value, Duration expiration) { Element element = new Element(key, value); if (expiration == null) { element.setEternal(true); } else { element.setTimeToLive((int) expiration.getStandardSeconds()); } cache.put(element); }
From source file:play.modules.webdrive.WebDriverRunner.java
License:Apache License
private void runTestsWithDriver(Class<?> webDriverClass, List<String> tests) throws Exception { Logger.info("~ Starting tests with " + webDriverClass); long startTime = System.currentTimeMillis(); WebDriver webDriver = (WebDriver) webDriverClass.newInstance(); webDriver.get(appUrlBase + "/@tests/init"); boolean ok = true; for (String test : tests) { long start = System.currentTimeMillis(); String testName = test.replace(".class", "").replace(".test.html", "").replace(".", "/").replace("$", "/"); StringBuilder loggerOutput = new StringBuilder("~ " + testName + "... "); for (int i = 0; i < maxTestNameLength - testName.length(); i++) { loggerOutput.append(" "); }//from w ww . j av a 2 s . co m loggerOutput.append(" "); String url; if (test.endsWith(".class")) { url = appUrlBase + "/@tests/" + test; } else { url = appUrlBase + seleniumUrlPart + "?baseUrl=" + appUrlBase + "&test=/@tests/" + test + ".suite&auto=true&resultsUrl=/@tests/" + test; } webDriver.get(url); int retry = 0; while (retry < testTimeoutInSeconds) { if (new File(testResultRoot, test.replace("/", ".") + ".passed.html").exists()) { loggerOutput.append("PASSED "); break; } else if (new File(testResultRoot, test.replace("/", ".") + ".failed.html").exists()) { loggerOutput.append("FAILED ! "); ok = false; break; } else { retry++; if (retry == testTimeoutInSeconds) { loggerOutput.append("TIMEOUT ? "); ok = false; break; } Thread.sleep(1000); } } // int duration = (int) (System.currentTimeMillis() - start); int seconds = (duration / 1000) % 60; int minutes = (duration / (1000 * 60)) % 60; if (minutes > 0) { loggerOutput.append(minutes + " min " + seconds + "s"); } else { loggerOutput.append(seconds + "s"); } Logger.info(loggerOutput.toString()); } webDriver.get(appUrlBase + "/@tests/end?result=" + (ok ? "passed" : "failed")); webDriver.quit(); saveTestResults(webDriver.getClass().getSimpleName()); if (!ok) { failed = true; } long endTime = System.currentTimeMillis(); Duration testDuration = new Duration(startTime, endTime); String time = testDuration.getStandardSeconds() + " sec."; Logger.info(" ~ duration: " + webDriver.getClass().getSimpleName() + " " + time); }
From source file:predojo.vo.JogadorVO.java
public void assassinar(JogadorVO jogadorMorto, ArmaVO armaUtilizada, Date dataHoraAssassinato) { if (jogadorMorto == null) return;/* w ww . j a va 2s. co m*/ if (armaUtilizada == null) return; if (dataHoraAssassinato == null) return; jogadorMorto.morrer(); if (this.nome.equalsIgnoreCase("<WORLD>")) return; AssassinatoVO assassinato = new AssassinatoVO(jogadorMorto, armaUtilizada, dataHoraAssassinato); this.assassinatos.add(assassinato); this.quantidadeAssassinatosAteMorte++; // Contabilizar numero assassinatos ultimo minuto if (!possuiTrofeuCincoAssassinatosEmUmMinuto && this.assassinatos.size() >= 5) { // Repupera data e hora do quinto ultimo assassinato DateTime dataHoraQuintoUltimoAssassinato = new DateTime( this.assassinatos.get(this.assassinatos.size() - 5).getDataHoraAssassinato().getTime()); // Repupera data e hora do ultimo assassinato DateTime dataHoraUltimoAssassinato = new DateTime( this.assassinatos.get(this.assassinatos.size() - 1).getDataHoraAssassinato().getTime()); // Calcula o intervalo de tempo entre a ultima morte e a quinta ultima morte utilizando JodaTime Duration duracao = new Duration(dataHoraQuintoUltimoAssassinato, dataHoraUltimoAssassinato); Long diferencaEmSegundos = duracao.getStandardSeconds(); if (diferencaEmSegundos <= 60) possuiTrofeuCincoAssassinatosEmUmMinuto = true; } }
From source file:propel.core.utils.ConversionUtils.java
License:Open Source License
/** * Returns the value of the given timespan in a human-readable form, appending the suffix. Example: 10 seconds become * "less than a minute". Example: 1.1 minutes become "about a minute". Example: 50 minutes become "50 minutes". Example: 13 hours, 10 * minutes become "about 13 hours". The suffix can be used to describe the event's position in time, use e.g. " ago" or " from now" *///from w w w .j a v a 2 s.c o m @Validate public static String toHumanReadable(@NotNull final Duration ts, @NotNull final String suffix) { val values = new AvlHashtable<Double, String>(Double.class, String.class); long totalSeconds = Math.abs(ts.getStandardSeconds()); final double totalMinutes = Math.round(totalSeconds / 60); double totalHours = Math.round(totalMinutes / 60); double totalDays = Math.floor(totalHours / 24); double totalMonths = Math.floor(totalDays / 30); double totalYears = Math.floor(totalMonths / 12); values.add(0.75d, "less than a minute"); values.add(1.5d, "about a minute"); values.add(45d, String.format("%d minutes", (int) totalMinutes)); values.add(90d, "about an hour"); values.add(1440d, String.format("about %d hours", (int) totalHours)); // 60 * 24 values.add(2880d, "a day"); // 60 * 48 values.add(43200d, String.format("%d days", (int) totalDays)); // 60 * 24 * 30 values.add(86400d, "about a month"); // 60 * 24 * 60 values.add(525600d, String.format("%d months", (int) totalMonths)); // 60 * 24 * 365 values.add(1051200d, "about a year"); // 60 * 24 * 365 * 2 values.add(Double.MAX_VALUE, String.format("%d years", (int) totalYears)); Double key = Linq.first(values.getKeys(), keyGreaterThan(totalMinutes)); return values.get(key) + suffix; }
From source file:web.StreamMeetupComTask.java
License:Apache License
private String getUpfrontReservationTime(Long dateInMillis) { DateTime now = new DateTime(DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC"))); DateTime event = new DateTime(dateInMillis, DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC"))); Duration duration = new Duration(now, event); if (duration.getMillis() < 0) { return "-1"; } else if (duration.getStandardSeconds() < 86400) { return "1d"; } else if (duration.getStandardDays() < 4) { return "4d"; } else if (duration.getStandardDays() < 7) { return "1w"; } else if (duration.getStandardDays() < 14) { return "2w"; } else if (duration.getStandardDays() < 28) { return "4w"; } else if (duration.getStandardDays() < 56) { return "8w"; } else {//from w w w . jav a 2 s .c om return "-"; } }