Example usage for org.joda.time ReadWritablePeriod toPeriod

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

Introduction

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

Prototype

Period toPeriod();

Source Link

Document

Get this period as an immutable Period object.

Usage

From source file:io.warp10.script.functions.DURATION.java

License:Apache License

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

    Object o = stack.pop();//  ww  w.j av  a2  s .  c  o  m

    if (!(o instanceof String)) {
        throw new WarpScriptException(getName()
                + " expects an ISO8601 duration (a string) on top of the stack. See http://en.wikipedia.org/wiki/ISO_8601#Durations");
    }

    ReadWritablePeriod period = new MutablePeriod();

    ISOPeriodFormat.standard().getParser().parseInto(period, o.toString(), 0, Locale.US);

    Period p = period.toPeriod();

    if (p.getMonths() != 0 || p.getYears() != 0) {
        throw new WarpScriptException(getName()
                + " doesn't support ambiguous durations containing years or months, please convert those to days.");
    }

    Duration duration = p.toDurationFrom(new Instant());

    stack.push(duration.getMillis() * Constants.TIME_UNITS_PER_MS);

    return stack;
}