Example usage for org.joda.time MutableDateTime MutableDateTime

List of usage examples for org.joda.time MutableDateTime MutableDateTime

Introduction

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

Prototype

public MutableDateTime(Object instant, Chronology chronology) 

Source Link

Document

Constructs an instance from an Object that represents a datetime, using the specified chronology.

Usage

From source file:app.decoder.data.wrap.WrapEntry.java

License:BSD License

public TimeValue getTradeDate() {
    if (MessageRules.hasTradeDate(getMessage(), entry)) {
        final int days = MessageRules.getTradeDate(getMessage(), entry);
        final MutableDateTime date = new MutableDateTime(0, DateTimeZone.UTC);
        date.addDays(days);//from w ww  . j  a  v a 2s.  c o m
        return ValueBuilder.newTime(date.getMillis());
    } else {
        return ValueConst.NULL_TIME;
    }
}

From source file:com.foundationdb.server.types.mcompat.mfuncs.MFromUnixtimeTwoArgs.java

License:Open Source License

private static Object[] computeResult(long unix, String format, String tz) {
    String st = null;// w  w  w . jav a 2  s  . co m
    InvalidOperationException error = null;

    try {
        st = DateTimeField.getFormatted(new MutableDateTime(unix * 1000L, DateTimeZone.forID(tz)), format);
    } catch (InvalidParameterValueException e) {
        st = null;
        error = e;
    }

    return new Object[] { st, error };
}

From source file:com.lithium.yoda.IsoStartDateOfWeek.java

License:Apache License

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    Preconditions.checkPositionIndex(0, arguments.length);
    if (!(arguments[0] instanceof LongObjectInspector)) {
        throw new IllegalArgumentException(
                "Input to  IsoStartDateOfWeek UDF must be a bigint. Given " + arguments[0].getTypeName());
    }/*from  ww  w  .j  a v a2  s. c om*/
    timestampOi = (LongObjectInspector) arguments[0];

    YYYYMMDD = org.joda.time.format.DateTimeFormat.forPattern("YYYY-MM-dd");
    mdt = new MutableDateTime(0, DateTimeZone.UTC);

    return PrimitiveObjectInspectorFactory.javaIntObjectInspector;
}

From source file:net.danlew.android.joda.ZoneInfoCompiler.java

License:Apache License

static int parseTime(String str) {
    DateTimeFormatter p = ISODateTimeFormat.hourMinuteSecondFraction();
    MutableDateTime mdt = new MutableDateTime(0, getLenientISOChronology());
    int pos = 0;//from  w  w  w  .  java2s  .  c o  m
    if (str.startsWith("-")) {
        pos = 1;
    }
    int newPos = p.parseInto(mdt, str, pos);
    if (newPos == ~pos) {
        throw new IllegalArgumentException(str);
    }
    int millis = (int) mdt.getMillis();
    if (pos == 1) {
        millis = -millis;
    }
    return millis;
}

From source file:org.calrissian.accumulorecipes.commons.support.TimeUnit.java

License:Apache License

public long normalize(long timestamp) {
    MutableDateTime ts = new MutableDateTime(timestamp, DateTimeZone.UTC);

    /**/*from  w ww.ja  v  a  2s. com*/
     * NOTE: order of switch matters.
     *
     * This switch is designed to fall through from most to least significant. Zeroes all non significant
     * portions of the time before finally breaking at the end.
     */
    switch (this) {
    case MONTHS:
        ts.setDayOfMonth(1);
    case DAYS:
        ts.setHourOfDay(0);
    case HOURS:
        ts.setMinuteOfHour(0);
    case MINUTES:
        ts.setSecondOfMinute(0);
        ts.setMillisOfSecond(0);
        break;
    default:
        throw new IllegalArgumentException("Unsupported time unit");
    }

    return ts.getMillis();
}

From source file:org.codelibs.elasticsearch.common.joda.DateMathParser.java

License:Apache License

private long parseMath(String mathString, long time, boolean roundUp, DateTimeZone timeZone)
        throws ElasticsearchParseException {
    if (timeZone == null) {
        timeZone = DateTimeZone.UTC;/*from  w  ww  .ja va 2s  . com*/
    }
    MutableDateTime dateTime = new MutableDateTime(time, timeZone);
    for (int i = 0; i < mathString.length();) {
        char c = mathString.charAt(i++);
        final boolean round;
        final int sign;
        if (c == '/') {
            round = true;
            sign = 1;
        } else {
            round = false;
            if (c == '+') {
                sign = 1;
            } else if (c == '-') {
                sign = -1;
            } else {
                throw new ElasticsearchParseException("operator not supported for date math [{}]", mathString);
            }
        }

        if (i >= mathString.length()) {
            throw new ElasticsearchParseException("truncated date math [{}]", mathString);
        }

        final int num;
        if (!Character.isDigit(mathString.charAt(i))) {
            num = 1;
        } else {
            int numFrom = i;
            while (i < mathString.length() && Character.isDigit(mathString.charAt(i))) {
                i++;
            }
            if (i >= mathString.length()) {
                throw new ElasticsearchParseException("truncated date math [{}]", mathString);
            }
            num = Integer.parseInt(mathString.substring(numFrom, i));
        }
        if (round) {
            if (num != 1) {
                throw new ElasticsearchParseException("rounding `/` can only be used on single unit types [{}]",
                        mathString);
            }
        }
        char unit = mathString.charAt(i++);
        MutableDateTime.Property propertyToRound = null;
        switch (unit) {
        case 'y':
            if (round) {
                propertyToRound = dateTime.yearOfCentury();
            } else {
                dateTime.addYears(sign * num);
            }
            break;
        case 'M':
            if (round) {
                propertyToRound = dateTime.monthOfYear();
            } else {
                dateTime.addMonths(sign * num);
            }
            break;
        case 'w':
            if (round) {
                propertyToRound = dateTime.weekOfWeekyear();
            } else {
                dateTime.addWeeks(sign * num);
            }
            break;
        case 'd':
            if (round) {
                propertyToRound = dateTime.dayOfMonth();
            } else {
                dateTime.addDays(sign * num);
            }
            break;
        case 'h':
        case 'H':
            if (round) {
                propertyToRound = dateTime.hourOfDay();
            } else {
                dateTime.addHours(sign * num);
            }
            break;
        case 'm':
            if (round) {
                propertyToRound = dateTime.minuteOfHour();
            } else {
                dateTime.addMinutes(sign * num);
            }
            break;
        case 's':
            if (round) {
                propertyToRound = dateTime.secondOfMinute();
            } else {
                dateTime.addSeconds(sign * num);
            }
            break;
        default:
            throw new ElasticsearchParseException("unit [{}] not supported for date math [{}]", unit,
                    mathString);
        }
        if (propertyToRound != null) {
            if (roundUp) {
                // we want to go up to the next whole value, even if we are already on a rounded value
                propertyToRound.add(1);
                propertyToRound.roundFloor();
                dateTime.addMillis(-1); // subtract 1 millisecond to get the largest inclusive value
            } else {
                propertyToRound.roundFloor();
            }
        }
    }
    return dateTime.getMillis();
}

From source file:org.elasticsearch.common.joda.DateMathParser.java

License:Apache License

private long parseMath(String mathString, long time, boolean roundUp) throws ElasticsearchParseException {
    MutableDateTime dateTime = new MutableDateTime(time, DateTimeZone.UTC);
    try {/* w w w  . j a v  a2  s .  c  o  m*/
        for (int i = 0; i < mathString.length();) {
            char c = mathString.charAt(i++);
            int type;
            if (c == '/') {
                type = 0;
            } else if (c == '+') {
                type = 1;
            } else if (c == '-') {
                type = 2;
            } else {
                throw new ElasticsearchParseException(
                        "operator not supported for date math [" + mathString + "]");
            }

            int num;
            if (!Character.isDigit(mathString.charAt(i))) {
                num = 1;
            } else {
                int numFrom = i;
                while (Character.isDigit(mathString.charAt(i))) {
                    i++;
                }
                num = Integer.parseInt(mathString.substring(numFrom, i));
            }
            if (type == 0) {
                // rounding is only allowed on whole numbers
                if (num != 1) {
                    throw new ElasticsearchParseException(
                            "rounding `/` can only be used on single unit types [" + mathString + "]");
                }
            }
            char unit = mathString.charAt(i++);
            switch (unit) {
            case 'y':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.yearOfCentury().roundCeiling();
                    } else {
                        dateTime.yearOfCentury().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addYears(num);
                } else if (type == 2) {
                    dateTime.addYears(-num);
                }
                break;
            case 'M':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.monthOfYear().roundCeiling();
                    } else {
                        dateTime.monthOfYear().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addMonths(num);
                } else if (type == 2) {
                    dateTime.addMonths(-num);
                }
                break;
            case 'w':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.weekOfWeekyear().roundCeiling();
                    } else {
                        dateTime.weekOfWeekyear().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addWeeks(num);
                } else if (type == 2) {
                    dateTime.addWeeks(-num);
                }
                break;
            case 'd':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.dayOfMonth().roundCeiling();
                    } else {
                        dateTime.dayOfMonth().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addDays(num);
                } else if (type == 2) {
                    dateTime.addDays(-num);
                }
                break;
            case 'h':
            case 'H':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.hourOfDay().roundCeiling();
                    } else {
                        dateTime.hourOfDay().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addHours(num);
                } else if (type == 2) {
                    dateTime.addHours(-num);
                }
                break;
            case 'm':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.minuteOfHour().roundCeiling();
                    } else {
                        dateTime.minuteOfHour().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addMinutes(num);
                } else if (type == 2) {
                    dateTime.addMinutes(-num);
                }
                break;
            case 's':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.secondOfMinute().roundCeiling();
                    } else {
                        dateTime.secondOfMinute().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addSeconds(num);
                } else if (type == 2) {
                    dateTime.addSeconds(-num);
                }
                break;
            default:
                throw new ElasticsearchParseException(
                        "unit [" + unit + "] not supported for date math [" + mathString + "]");
            }
        }
    } catch (Exception e) {
        if (e instanceof ElasticsearchParseException) {
            throw (ElasticsearchParseException) e;
        }
        throw new ElasticsearchParseException("failed to parse date math [" + mathString + "]");
    }
    return dateTime.getMillis();
}

From source file:org.elasticsearch.script.expression.DateObjectValueSource.java

License:Apache License

@Override
@SuppressWarnings("rawtypes") // ValueSource uses a rawtype
public FunctionValues getValues(Map context, LeafReaderContext leaf) throws IOException {
    AtomicNumericFieldData leafData = (AtomicNumericFieldData) fieldData.load(leaf);
    MutableDateTime joda = new MutableDateTime(0, DateTimeZone.UTC);
    NumericDoubleValues docValues = multiValueMode.select(leafData.getDoubleValues(), 0d);
    return new DoubleDocValues(this) {
        @Override/* w  ww.j  a  va2 s .c o  m*/
        public double doubleVal(int docId) {
            long millis = (long) docValues.get(docId);
            joda.setMillis(millis);
            return function.applyAsInt(joda);
        }
    };
}

From source file:org.graylog2.indexer.results.FieldHistogramResult.java

License:Open Source License

public Map<Long, Map<String, Number>> getResults() {
    if (result.getBuckets().isEmpty()) {
        return Collections.emptyMap();
    }// w  w w.  ja  v  a  2  s .  c o m

    final Map<Long, Map<String, Number>> results = Maps.newTreeMap();
    for (DateHistogram.Bucket b : result.getBuckets()) {
        final ImmutableMap.Builder<String, Number> resultMap = ImmutableMap.builder();
        resultMap.put("total_count", b.getDocCount());

        final Stats stats = b.getAggregations().get(Searches.AGG_STATS);
        resultMap.put("count", stats.getCount());
        resultMap.put("min", stats.getMin());
        resultMap.put("max", stats.getMax());
        resultMap.put("total", stats.getSum());
        resultMap.put("mean", stats.getAvg());

        // cardinality is only calculated if it was explicitly requested, so this might be null
        final Cardinality cardinality = b.getAggregations().get(Searches.AGG_CARDINALITY);
        resultMap.put("cardinality", cardinality == null ? 0 : cardinality.getValue());

        final long timestamp = b.getKeyAsDate().getMillis() / 1000L;
        results.put(timestamp, resultMap.build());
    }

    final long minTimestamp = Collections.min(results.keySet());
    final long maxTimestamp = Collections.max(results.keySet());
    final MutableDateTime currentTime = new MutableDateTime(minTimestamp, DateTimeZone.UTC);

    while (currentTime.getMillis() < maxTimestamp) {
        final Map<String, Number> entry = results.get(currentTime.getMillis());

        // advance timestamp by the interval's seconds value
        currentTime.add(interval.getPeriod());

        if (entry == null) {
            // synthesize a 0 value for this timestamp
            results.put(currentTime.getMillis(), EMPTY_RESULT);
        }
    }
    return results;
}