Search in sources :

Example 1 with DateTimeFieldType

use of org.joda.time.DateTimeFieldType in project CoreNLP by stanfordnlp.

the class JodaTimeUtils method getJodaTimePeriod.

protected static Period getJodaTimePeriod(Partial p) {
    if (p.size() > 0) {
        DateTimeFieldType dtType = p.getFieldType(p.size() - 1);
        DurationFieldType dType = dtType.getDurationType();
        Period period = new Period();
        if (period.isSupported(dType)) {
            return period.withField(dType, 1);
        } else {
            DurationField df = dType.getField(p.getChronology());
            if (df instanceof ScaledDurationField) {
                ScaledDurationField sdf = (ScaledDurationField) df;
                return period.withField(sdf.getWrappedField().getType(), sdf.getScalar());
            }
        // PeriodType.forFields(new DurationFieldType[]{dType});
        // return new Period(df.getUnitMillis(), PeriodType.forFields(new DurationFieldType[]{dType}));
        }
    }
    return null;
}
Also used : ScaledDurationField(org.joda.time.field.ScaledDurationField) DurationFieldType(org.joda.time.DurationFieldType) DateTimeFieldType(org.joda.time.DateTimeFieldType) ScaledDurationField(org.joda.time.field.ScaledDurationField)

Example 2 with DateTimeFieldType

use of org.joda.time.DateTimeFieldType in project CoreNLP by stanfordnlp.

the class JodaTimeUtils method padMoreSpecificFields.

public static Partial padMoreSpecificFields(Partial p, Period granularity) {
    DateTimeFieldType msf = getMostSpecific(p);
    if (isMoreGeneral(msf, DateTimeFieldType.year(), p.getChronology()) || isMoreGeneral(msf, DateTimeFieldType.yearOfCentury(), p.getChronology())) {
        if (p.isSupported(DateTimeFieldType.yearOfCentury())) {
        // OKAY
        } else {
            if (p.isSupported(JodaTimeUtils.DecadeOfCentury)) {
                if (p.isSupported(DateTimeFieldType.centuryOfEra())) {
                    int year = p.get(DateTimeFieldType.centuryOfEra()) * 100 + p.get(JodaTimeUtils.DecadeOfCentury) * 10;
                    p = p.without(JodaTimeUtils.DecadeOfCentury);
                    p = p.without(DateTimeFieldType.centuryOfEra());
                    p = p.with(DateTimeFieldType.year(), year);
                } else {
                    int year = p.get(JodaTimeUtils.DecadeOfCentury) * 10;
                    p = p.without(JodaTimeUtils.DecadeOfCentury);
                    p = p.with(DateTimeFieldType.yearOfCentury(), year);
                }
            } else {
                if (p.isSupported(DateTimeFieldType.centuryOfEra())) {
                    int year = p.get(DateTimeFieldType.centuryOfEra()) * 100;
                    p = p.without(DateTimeFieldType.centuryOfEra());
                    p = p.with(DateTimeFieldType.year(), year);
                }
            }
        }
    }
    boolean useWeek = false;
    if (p.isSupported(DateTimeFieldType.weekOfWeekyear())) {
        if (!p.isSupported(DateTimeFieldType.dayOfMonth()) && !p.isSupported(DateTimeFieldType.dayOfWeek())) {
            p = p.with(DateTimeFieldType.dayOfWeek(), 1);
            if (p.isSupported(DateTimeFieldType.monthOfYear())) {
                p = p.without(DateTimeFieldType.monthOfYear());
            }
        }
        useWeek = true;
    }
    Partial p2 = useWeek ? EMPTY_ISO_WEEK_PARTIAL : EMPTY_ISO_PARTIAL;
    for (int i = 0; i < p2.size(); i++) {
        DateTimeFieldType fieldType = p2.getFieldType(i);
        if (msf == null || isMoreSpecific(fieldType, msf, p.getChronology())) {
            if (!p.isSupported(fieldType)) {
                if (fieldType == DateTimeFieldType.monthOfYear()) {
                    if (p.isSupported(QuarterOfYear)) {
                        p = p.with(DateTimeFieldType.monthOfYear(), (p.get(QuarterOfYear) - 1) * 3 + 1);
                        continue;
                    } else if (p.isSupported(HalfYearOfYear)) {
                        p = p.with(DateTimeFieldType.monthOfYear(), (p.get(HalfYearOfYear) - 1) * 6 + 1);
                        continue;
                    }
                }
                p = p.with(fieldType, p2.getValue(i));
            }
        }
    }
    if (granularity != null) {
        DurationFieldType mostSpecific = getMostSpecific(granularity);
        p = discardMoreSpecificFields(p, mostSpecific);
    }
    return p;
}
Also used : DurationFieldType(org.joda.time.DurationFieldType) DateTimeFieldType(org.joda.time.DateTimeFieldType)

Example 3 with DateTimeFieldType

use of org.joda.time.DateTimeFieldType in project gephi by gephi.

the class DateTick method create.

public static DateTick create(double min, double max, int width) {
    DateTime minDate = new DateTime((long) min);
    DateTime maxDate = new DateTime((long) max);
    Period period = new Period(minDate, maxDate, PeriodType.yearMonthDayTime());
    int years = period.getYears();
    int months = period.getMonths();
    int days = period.getDays();
    int hours = period.getHours();
    int minutes = period.getMinutes();
    int seconds = period.getSeconds();
    //Top type
    DateTimeFieldType topType;
    if (years > 0) {
        topType = DateTimeFieldType.year();
    } else if (months > 0) {
        topType = DateTimeFieldType.monthOfYear();
    } else if (days > 0) {
        topType = DateTimeFieldType.dayOfMonth();
    } else if (hours > 0) {
        topType = DateTimeFieldType.hourOfDay();
    } else if (minutes > 0) {
        topType = DateTimeFieldType.minuteOfHour();
    } else if (seconds > 0) {
        topType = DateTimeFieldType.secondOfMinute();
    } else {
        topType = DateTimeFieldType.millisOfSecond();
    }
    //Bottom type
    if (topType != DateTimeFieldType.millisOfSecond()) {
        DateTimeFieldType bottomType;
        if (topType.equals(DateTimeFieldType.year())) {
            bottomType = DateTimeFieldType.monthOfYear();
        } else if (topType.equals(DateTimeFieldType.monthOfYear())) {
            bottomType = DateTimeFieldType.dayOfMonth();
        } else if (topType.equals(DateTimeFieldType.dayOfMonth())) {
            bottomType = DateTimeFieldType.hourOfDay();
        } else if (topType.equals(DateTimeFieldType.hourOfDay())) {
            bottomType = DateTimeFieldType.minuteOfHour();
        } else if (topType.equals(DateTimeFieldType.minuteOfHour())) {
            bottomType = DateTimeFieldType.secondOfMinute();
        } else {
            bottomType = DateTimeFieldType.millisOfSecond();
        }
        //Number of ticks
        Period p = new Period(minDate, maxDate, PeriodType.forFields(new DurationFieldType[] { bottomType.getDurationType() }));
        int intervals = p.get(bottomType.getDurationType());
        if (intervals > 0) {
            int intervalSize = width / intervals;
            if (intervalSize >= MIN_PIXELS) {
                return new DateTick(minDate, maxDate, new DateTimeFieldType[] { topType, bottomType });
            }
        }
    }
    return new DateTick(minDate, maxDate, new DateTimeFieldType[] { topType });
}
Also used : DurationFieldType(org.joda.time.DurationFieldType) DateTimeFieldType(org.joda.time.DateTimeFieldType) Period(org.joda.time.Period) DateTime(org.joda.time.DateTime)

Example 4 with DateTimeFieldType

use of org.joda.time.DateTimeFieldType in project graylog2-server by Graylog2.

the class TimeBasedRotationStrategy method determineRotationPeriodAnchor.

/**
     * Determines the starting point ("anchor") for a period.
     *
     * To produce repeatable rotation points in time, the period is "snapped" to a "grid" of time.
     * For example, an hourly index rotation would be anchored to the last full hour, instead of happening at whatever minute
     * the first rotation was started.
     *
     * This "snapping" is done accordingly with the other parts of a period.
     *
     * For highly irregular periods (those that do not have a small zero component)
     *
     * @param period the rotation period
     * @return the anchor DateTime to calculate rotation periods from
     */
static DateTime determineRotationPeriodAnchor(@Nullable DateTime lastAnchor, Period period) {
    final Period normalized = period.normalizedStandard();
    int years = normalized.getYears();
    int months = normalized.getMonths();
    int weeks = normalized.getWeeks();
    int days = normalized.getDays();
    int hours = normalized.getHours();
    int minutes = normalized.getMinutes();
    int seconds = normalized.getSeconds();
    if (years == 0 && months == 0 && weeks == 0 && days == 0 && hours == 0 && minutes == 0 && seconds == 0) {
        throw new IllegalArgumentException("Invalid rotation period specified");
    }
    // find the largest non-zero stride in the period. that's our anchor type. statement order matters here!
    DateTimeFieldType largestStrideType = null;
    if (seconds > 0)
        largestStrideType = secondOfMinute();
    if (minutes > 0)
        largestStrideType = minuteOfHour();
    if (hours > 0)
        largestStrideType = hourOfDay();
    if (days > 0)
        largestStrideType = dayOfMonth();
    if (weeks > 0)
        largestStrideType = weekOfWeekyear();
    if (months > 0)
        largestStrideType = monthOfYear();
    if (years > 0)
        largestStrideType = year();
    if (largestStrideType == null) {
        throw new IllegalArgumentException("Could not determine rotation stride length.");
    }
    final DateTime anchorTime = MoreObjects.firstNonNull(lastAnchor, Tools.nowUTC());
    final DateTimeField field = largestStrideType.getField(anchorTime.getChronology());
    // use normalized here to make sure we actually have the largestStride type available! see https://github.com/Graylog2/graylog2-server/issues/836
    int periodValue = normalized.get(largestStrideType.getDurationType());
    final long fieldValue = field.roundFloor(anchorTime.getMillis());
    final int fieldValueInUnit = field.get(fieldValue);
    if (periodValue == 0) {
        // https://github.com/Graylog2/graylog2-server/issues/836
        log.warn("Determining stride length failed because of a 0 period. Defaulting back to 1 period to avoid crashing, but this is a bug!");
        periodValue = 1;
    }
    final long difference = fieldValueInUnit % periodValue;
    final long newValue = field.add(fieldValue, -1 * difference);
    return new DateTime(newValue, DateTimeZone.UTC);
}
Also used : DateTimeFieldType(org.joda.time.DateTimeFieldType) Period(org.joda.time.Period) DateTimeField(org.joda.time.DateTimeField) DateTime(org.joda.time.DateTime)

Example 5 with DateTimeFieldType

use of org.joda.time.DateTimeFieldType in project joda-time by JodaOrg.

the class TestISODateTimeFormat_Fields method testForFields_calBased_YMD.

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
public void testForFields_calBased_YMD() {
    DateTimeFieldType[] fields = new DateTimeFieldType[] { DateTimeFieldType.year(), DateTimeFieldType.monthOfYear(), DateTimeFieldType.dayOfMonth() };
    int[] values = new int[] { 2005, 6, 25 };
    List types = new ArrayList(Arrays.asList(fields));
    DateTimeFormatter f = ISODateTimeFormat.forFields(types, true, true);
    assertEquals("2005-06-25", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, true, false);
    assertEquals("2005-06-25", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, false, true);
    assertEquals("20050625", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, false, false);
    assertEquals("20050625", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
}
Also used : Partial(org.joda.time.Partial) DateTimeFieldType(org.joda.time.DateTimeFieldType) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

DateTimeFieldType (org.joda.time.DateTimeFieldType)47 ArrayList (java.util.ArrayList)38 List (java.util.List)38 Partial (org.joda.time.Partial)38 DurationFieldType (org.joda.time.DurationFieldType)3 DateTime (org.joda.time.DateTime)2 Period (org.joda.time.Period)2 ScaledDurationField (org.joda.time.field.ScaledDurationField)2 DateTimeField (org.joda.time.DateTimeField)1 ISOChronology (org.joda.time.chrono.ISOChronology)1