Java Code Examples for org.joda.time.Period#negated()

The following examples show how to use org.joda.time.Period#negated() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateTimeUtils.java    From presto with Apache License 2.0 6 votes vote down vote up
private static Period parsePeriod(PeriodFormatter periodFormatter, String value)
{
    boolean negative = value.startsWith("-");
    if (negative) {
        value = value.substring(1);
    }

    Period period = periodFormatter.parsePeriod(value);
    for (DurationFieldType type : period.getFieldTypes()) {
        checkArgument(period.get(type) >= 0, "Period field %s is negative", type);
    }

    if (negative) {
        period = period.negated();
    }
    return period;
}
 
Example 2
Source File: ExpressionAnalyzer.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Symbol visitIntervalLiteral(IntervalLiteral node, ExpressionAnalysisContext context) {
    String value = node.getValue();

    IntervalParser.Precision start = INTERVAL_FIELDS.get(node.getStartField());
    IntervalParser.Precision end = node.getEndField() == null ? null : INTERVAL_FIELDS.get(node.getEndField());

    Period period = IntervalParser.apply(value, start, end);

    if (node.getSign() == IntervalLiteral.Sign.MINUS) {
        period = period.negated();
    }
    return Literal.newInterval(period);
}
 
Example 3
Source File: PGIntervalParser.java    From crate with Apache License 2.0 4 votes vote down vote up
static Period apply(String value) {
    final boolean ISOFormat = !value.startsWith("@");

    // Just a simple '0'
    if (!ISOFormat && value.length() == 3 && value.charAt(2) == '0') {
        return new Period();
    }
    boolean dataParsed = false;
    int years = 0;
    int months = 0;
    int days = 0;
    int hours = 0;
    int minutes = 0;
    int seconds = 0;
    int milliSeconds = 0;

    try {
        String valueToken = null;

        value = value.replace('+', ' ').replace('@', ' ');
        final StringTokenizer st = new StringTokenizer(value);
        for (int i = 1; st.hasMoreTokens(); i++) {
            String token = st.nextToken();

            if ((i & 1) == 1) {
                int endHours = token.indexOf(':');
                if (endHours == -1) {
                    valueToken = token;
                    continue;
                }
                // This handles hours, minutes, seconds and microseconds for
                // ISO intervals
                int offset = (token.charAt(0) == '-') ? 1 : 0;

                hours = nullSafeIntGet(token.substring(offset, endHours));
                minutes = nullSafeIntGet(token.substring(endHours + 1, endHours + 3));

                int endMinutes = token.indexOf(':', endHours + 1);
                seconds = parseInteger(token.substring(endMinutes + 1));
                milliSeconds = parseMilliSeconds(token.substring(endMinutes + 1));

                if (offset == 1) {
                    hours = -hours;
                    minutes = -minutes;
                    seconds = -seconds;
                    milliSeconds = -milliSeconds;
                }
                valueToken = null;
            } else {
                // This handles years, months, days for both, ISO and
                // Non-ISO intervals. Hours, minutes, seconds and microseconds
                // are handled for Non-ISO intervals here.
                if (token.startsWith("year")) {
                    years = nullSafeIntGet(valueToken);
                } else if (token.startsWith("mon")) {
                    months = nullSafeIntGet(valueToken);
                } else if (token.startsWith("day")) {
                    days = nullSafeIntGet(valueToken);
                } else if (token.startsWith("week")) {
                    days = nullSafeIntGet(valueToken) * 7;
                } else if (token.startsWith("hour")) {
                    hours = nullSafeIntGet(valueToken);
                } else if (token.startsWith("min")) {
                    minutes = nullSafeIntGet(valueToken);
                } else if (token.startsWith("sec")) {
                    seconds = parseInteger(valueToken);
                    milliSeconds = parseMilliSeconds(valueToken);
                } else {
                    throw new IllegalArgumentException("Invalid interval format " + value);
                }
                dataParsed = true;
            }
        }
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException("Invalid interval format " + value);
    }

    if (!dataParsed) {
        throw new IllegalArgumentException("Invalid interval format " + value);
    }

    Period period = new Period(years, months, 0, days, hours, minutes, seconds, milliSeconds);

    if (!ISOFormat && value.endsWith("ago")) {
        // Inverse the leading sign
        period = period.negated();
    }
    return period;
}