Example usage for org.joda.time DateTimeField roundCeiling

List of usage examples for org.joda.time DateTimeField roundCeiling

Introduction

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

Prototype

public abstract long roundCeiling(long instant);

Source Link

Document

Round to the highest whole unit of this field.

Usage

From source file:org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder.java

License:Apache License

DateTimeZone rewriteTimeZone(QueryShardContext context) throws IOException {
    final DateTimeZone tz = timeZone();
    if (field() != null && tz != null && tz.isFixed() == false && field() != null && script() == null) {
        final MappedFieldType ft = context.fieldMapper(field());
        final IndexReader reader = context.getIndexReader();
        if (ft != null && reader != null) {
            Long anyInstant = null;
            final IndexNumericFieldData fieldData = context.getForField(ft);
            for (LeafReaderContext ctx : reader.leaves()) {
                AtomicNumericFieldData leafFD = fieldData.load(ctx);
                SortedNumericDocValues values = leafFD.getLongValues();
                if (values.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
                    anyInstant = values.nextValue();
                    break;
                }/*from w  ww  .ja  va  2  s . c o m*/
            }

            if (anyInstant != null) {
                final long prevTransition = tz.previousTransition(anyInstant);
                final long nextTransition = tz.nextTransition(anyInstant);

                // We need all not only values but also rounded values to be within
                // [prevTransition, nextTransition].
                final long low;
                DateTimeUnit intervalAsUnit = getIntervalAsDateTimeUnit();
                if (intervalAsUnit != null) {
                    final DateTimeField dateTimeField = intervalAsUnit.field(tz);
                    low = dateTimeField.roundCeiling(prevTransition);
                } else {
                    final TimeValue intervalAsMillis = getIntervalAsTimeValue();
                    low = Math.addExact(prevTransition, intervalAsMillis.millis());
                }
                // rounding rounds down, so 'nextTransition' is a good upper bound
                final long high = nextTransition;

                if (ft.isFieldWithinQuery(reader, low, high, true, false, DateTimeZone.UTC, EPOCH_MILLIS_PARSER,
                        context) == Relation.WITHIN) {
                    // All values in this reader have the same offset despite daylight saving times.
                    // This is very common for location-based timezones such as Europe/Paris in
                    // combination with time-based indices.
                    return DateTimeZone.forOffsetMillis(tz.getOffset(anyInstant));
                }
            }
        }
    }
    return tz;
}