Example usage for org.joda.time Duration toPeriod

List of usage examples for org.joda.time Duration toPeriod

Introduction

In this page you can find the example usage for org.joda.time Duration toPeriod.

Prototype

public Period toPeriod() 

Source Link

Document

Converts this duration to a Period instance using the standard period type and the ISO chronology.

Usage

From source file:com.funambol.common.pim.utility.TimeUtils.java

License:Open Source License

/**
 * Returns the given minutes in iso 8601 duration format
 * @param minutes String//www  .  j a v  a 2 s. co  m
 * @return String
 */
public static String getIso8601Duration(String minutes) {

    if (minutes == null || minutes.equals("")) {
        return minutes;
    }

    int min = Integer.parseInt(minutes);

    if (min == -1) {
        return null;
    }

    long mills = min * 60L * 1000L;
    Duration d = new Duration(mills);

    PeriodFormatter formatter = ISOPeriodFormat.standard();
    return formatter.print(d.toPeriod());
}

From source file:com.github.aneveux.euptime.EUptimeWidget.java

License:Open Source License

/**
 * This method allows to get the Eclipse's uptime in a pretty format using
 * Joda/*ww w.  ja va  2s  .c  o m*/
 * 
 * @return something like 1d4h3m2s describing the Eclipse's uptime
 */
protected String getPrettyDuration() {
    final Duration duration = new Duration(System.currentTimeMillis() - Activator.getDefault().getStartTime());
    final PeriodFormatter formatter = new PeriodFormatterBuilder().appendDays().appendSuffix("d").appendHours()
            .appendSuffix("h").appendMinutes().appendSuffix("m").appendSeconds().appendSuffix("s")
            .toFormatter();
    return formatter.print(duration.toPeriod());
}

From source file:com.github.rinde.gpem17.eval.Evaluate.java

License:Apache License

public static ExperimentResults execute(Iterable<GPProgram<GpGlobal>> programs, boolean realtime,
        FileProvider.Builder scenarioFiles, File resDir, boolean createTimeStampedResDir,
        @Nullable Function<Scenario, Scenario> scenarioConverter, boolean createTmpFiles, ReauctOpt reauctOpt,
        Gendreau06ObjectiveFunction objFuncUsedAtRuntime, RpOpt routePlanner, boolean enableTimeMeasurements,
        boolean addOptaPlannerMAS, long heuristicComputationDelay, String... expArgs) {
    checkArgument(realtime ^ scenarioConverter != null);
    final long startTime = System.currentTimeMillis();

    if (createTimeStampedResDir) {
        resDir = createExperimentDir(resDir);
    } else {/*from  w  ww . j  a v a2s .com*/
        resDir.mkdirs();
    }

    boolean evolution = scenarioConverter != null;

    ResultWriter rw = new VanLonHolvoetResultWriter(resDir, GPEM17.OBJ_FUNC,
            scenarioFiles.build().get().iterator().next().getParent().toString(), realtime, true,
            createTmpFiles, evolution);
    Experiment.Builder exp = Experiment.builder().addScenarios(scenarioFiles).showGui(GPEM17.gui())
            .showGui(false)
            .usePostProcessor(new GpemPostProcessor(GPEM17.OBJ_FUNC,
                    evolution ? FailureStrategy.INCLUDE : FailureStrategy.RETRY, false))
            .computeLocal().withRandomSeed(123).repeat(3)
            // SEED_REPS,REPS,SCENARIO,CONFIG
            .withOrdering(SimulationProperty.SEED_REPS, SimulationProperty.REPS, SimulationProperty.SCENARIO,
                    SimulationProperty.CONFIG)

            .addResultListener(rw);

    if (!realtime) {
        exp.addResultListener(new SimRuntimeLogger(resDir));
    }
    if (realtime) {
        exp.setScenarioReader(ScenarioIO.readerAdapter(ScenarioConverter.TO_ONLINE_REALTIME_250))
                .withWarmup(30000).addResultListener(new CommandLineProgress(System.out))
                .withThreads((int) Math.floor((Runtime.getRuntime().availableProcessors() - 1) / 2d));
    } else if (scenarioConverter == null) {
        exp.setScenarioReader(ScenarioIO.readerAdapter(ScenarioConverter.TO_ONLINE_SIMULATED_250))
                .addResultListener(new CommandLineProgress(System.out));
    } else {
        exp.setScenarioReader(ScenarioIO.readerAdapter(scenarioConverter));
    }

    if (addOptaPlannerMAS) {
        exp.addConfiguration(createOptaPlanner(enableTimeMeasurements));
    }

    int counter = 0;
    StringBuilder sb = new StringBuilder();
    for (GPProgram<GpGlobal> prog : programs) {
        String progId = "c" + counter++;
        sb.append(progId).append(" = ").append(prog.getId()).append(System.lineSeparator());

        if (realtime) {
            exp.addConfiguration(GPEM17.createRtConfig(prog, progId, reauctOpt, objFuncUsedAtRuntime,
                    routePlanner, enableTimeMeasurements, heuristicComputationDelay));
        } else {
            exp.addConfiguration(GPEM17.createStConfig(prog, progId, reauctOpt, objFuncUsedAtRuntime,
                    enableTimeMeasurements));
        }
    }

    try {
        Files.write(sb, new File(rw.getExperimentDirectory(), "configs.txt"), Charsets.UTF_8);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    final Optional<ExperimentResults> results = exp.perform(System.out, expArgs);
    final long duration = System.currentTimeMillis() - startTime;
    if (!results.isPresent()) {
        return null;
    }

    final Duration dur = new Duration(startTime, System.currentTimeMillis());
    System.out.println("Done, computed " + results.get().getResults().size() + " simulations in "
            + duration / 1000d + "s (" + PeriodFormat.getDefault().print(dur.toPeriod()) + ")");
    return results.get();
}

From source file:com.github.rinde.gpem17.evo.StatsLogger.java

License:Apache License

public void finalStatistics(final EvolutionState state, final int result) {
    super.finalStatistics(state, result);

    // convert to ms duration
    final Duration dur = new Duration(startTime, System.nanoTime()).dividedBy(1000000L);

    System.out.println("End of evolutionary run.");
    System.out.println("Total runtime: " + PeriodFormat.getDefault().print(dur.toPeriod()));

    File firstGenDir = new File(experimentDirectory, "/generation0");
    File lastGenDir = new File(experimentDirectory, "/generation" + state.generation);

    while (lastGenDir.listFiles().length != firstGenDir.listFiles().length) {
        System.out.println("Waiting for all results to be written to disk.");
        try {//from  w w  w. j  a v  a 2s  .  c  om
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
    }
    System.out.println("Done.");

}

From source file:com.github.rinde.rinsim.experiment.CommandLineProgress.java

License:Apache License

@Override
public void receive(SimulationResult result) {
    if (result.getResultObject() instanceof FailureStrategy) {
        failures++;//w ww . j  a v  a  2 s .com
    } else {
        received++;
    }
    final Duration dur = new Duration(startTime, System.currentTimeMillis());
    printStream.println(Joiner.on("").join(received, SLASH, total, " (failures: ", failures, ", duration: ",
            PeriodFormat.getDefault().print(dur.toPeriod()), ", memory free/total/max (M): ", memorySummary(),
            ")"));
}

From source file:com.github.rinde.rinsim.experiment.CommandLineProgress.java

License:Apache License

@Override
public void doneComputing(ExperimentResults results) {
    final Duration dur = new Duration(startTime, System.currentTimeMillis());
    printStream.println("Computing done, duration: " + PeriodFormat.getDefault().print(dur.toPeriod()) + ".");
    printMemorySummary(printStream);//from ww  w. ja  v  a  2s .com
}

From source file:com.github.vase4kin.teamcityapp.utils.DateUtils.java

License:Apache License

/**
 * Format date to Overview screen to format "20 Apr 15 16:14:28 - 16:14:46 (18s)"
 *//*from  ww  w . j  a  v  a  2  s. c o m*/
public String formatDateToOverview() {
    String formattedStartDate = formatStartDateToBuildTitle();
    String formattedFinishedDate = formatFinishedDate();

    Duration duration = new Duration(new DateTime(finishedDate).minus(startDate.getTime()).getMillis());
    PeriodFormatter formatter = new PeriodFormatterBuilder().appendDays().appendSuffix("d:").appendHours()
            .appendSuffix("h:").appendMinutes().appendSuffix("m:").appendSeconds().appendSuffix("s")
            .toFormatter();
    String formatted = formatter.print(duration.toPeriod());
    return String.format("%s - %s (%s)", formattedStartDate, formattedFinishedDate, formatted);
}

From source file:com.google.android.apps.forscience.whistlepunk.review.TimestampPickerController.java

License:Open Source License

public String getTimeString() {
    Duration duration;
    String prefix = "";
    if (mSelectedTime.isAfter(mZeroTime) || mSelectedTime.isEqual(mZeroTime)) {
        duration = new Duration(mZeroTime, mSelectedTime);
    } else {/*from w  ww  .j  a  v a2s . c  o m*/
        duration = new Duration(mSelectedTime, mZeroTime);
        prefix = mNegativePrefix;
    }
    return prefix + mPeriodFormatter.print(duration.toPeriod());
}

From source file:com.helger.html.hc.html5.HCTime.java

License:Apache License

@Nonnull
public HCTime setAsDuration(@Nonnull final Duration aDuration) {
    return setAsDuration(aDuration.toPeriod());
}

From source file:com.igormaznitsa.jute.Utils.java

License:Apache License

public static String printTimeDelay(final long timeInMilliseconds) {
    final Duration duration = new Duration(timeInMilliseconds);
    final Period period = duration.toPeriod().normalizedStandard(PeriodType.time());
    return TIME_FORMATTER.print(period);
}