Search in sources :

Example 21 with DateTimeFieldType

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

the class TestISODateTimeFormat_Fields method testForFields_weekBased_YD.

//-----------------------------------------------------------------------
public void testForFields_weekBased_YD() {
    DateTimeFieldType[] fields = new DateTimeFieldType[] { DateTimeFieldType.weekyear(), DateTimeFieldType.dayOfWeek() };
    int[] values = new int[] { 2005, 5 };
    List types = new ArrayList(Arrays.asList(fields));
    DateTimeFormatter f = ISODateTimeFormat.forFields(types, true, false);
    assertEquals("2005-W-5", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, false, false);
    assertEquals("2005W-5", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    try {
        ISODateTimeFormat.forFields(types, true, true);
        fail();
    } catch (IllegalArgumentException ex) {
    }
    types = new ArrayList(Arrays.asList(fields));
    try {
        ISODateTimeFormat.forFields(types, false, true);
        fail();
    } catch (IllegalArgumentException ex) {
    }
}
Also used : Partial(org.joda.time.Partial) DateTimeFieldType(org.joda.time.DateTimeFieldType) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 22 with DateTimeFieldType

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

the class JodaTimeUtils method discardMoreSpecificFields.

public static Partial discardMoreSpecificFields(Partial p, DurationFieldType dft) {
    DurationField df = dft.getField(p.getChronology());
    Partial res = new Partial();
    for (int i = 0; i < p.size(); i++) {
        DateTimeFieldType fieldType = p.getFieldType(i);
        DurationField f = fieldType.getDurationType().getField(p.getChronology());
        int cmp = df.compareTo(f);
        if (cmp <= 0) {
            res = res.with(fieldType, p.getValue(i));
        }
    }
    return res;
}
Also used : DateTimeFieldType(org.joda.time.DateTimeFieldType) ScaledDurationField(org.joda.time.field.ScaledDurationField)

Example 23 with DateTimeFieldType

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

the class JodaTimeUtils method isCompatible.

public static boolean isCompatible(Partial p1, Partial p2) {
    if (p1 == null)
        return true;
    if (p2 == null)
        return true;
    for (int i = 0; i < p1.size(); i++) {
        DateTimeFieldType type = p1.getFieldType(i);
        int v = p1.getValue(i);
        if (JodaTimeUtils.hasField(p2, type)) {
            if (v != p2.get(type)) {
                return false;
            }
        }
    }
    return true;
}
Also used : DateTimeFieldType(org.joda.time.DateTimeFieldType)

Example 24 with DateTimeFieldType

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

the class JodaTimeUtils method combineMoreGeneralFields.

// Combines more general fields from p2 to p1
public static Partial combineMoreGeneralFields(Partial p1, Partial p2, DateTimeFieldType mgf) {
    Partial p = p1;
    Chronology c1 = p1.getChronology();
    Chronology c2 = p2.getChronology();
    if (!c1.equals(c2)) {
        throw new RuntimeException("Different chronology: c1=" + c1 + ", c2=" + c2);
    }
    DateTimeFieldType p1MostGeneralField = null;
    if (p1.size() > 0) {
        // Assume fields ordered from most general to least....
        p1MostGeneralField = p1.getFieldType(0);
    }
    if (mgf == null || (p1MostGeneralField != null && isMoreGeneral(p1MostGeneralField, mgf, c1))) {
        mgf = p1MostGeneralField;
    }
    for (int i = 0; i < p2.size(); i++) {
        DateTimeFieldType fieldType = p2.getFieldType(i);
        if (fieldType == DateTimeFieldType.year()) {
            if (p.isSupported(DateTimeFieldType.yearOfCentury())) {
                if (!p.isSupported(DateTimeFieldType.centuryOfEra())) {
                    int yoc = p.get(DateTimeFieldType.yearOfCentury());
                    int refYear = p2.getValue(i);
                    int century = refYear / 100;
                    int y2 = yoc + century * 100;
                    // TODO: Figure out which way to go
                    if (refYear < y2) {
                        y2 -= 100;
                    }
                    p = p.without(DateTimeFieldType.yearOfCentury());
                    p = p.with(DateTimeFieldType.year(), y2);
                }
                continue;
            } else if (p.isSupported(JodaTimeUtils.DecadeOfCentury)) {
                if (!p.isSupported(DateTimeFieldType.centuryOfEra())) {
                    int decade = p.get(JodaTimeUtils.DecadeOfCentury);
                    int refYear = p2.getValue(i);
                    int century = refYear / 100;
                    int y2 = decade * 10 + century * 100;
                    // TODO: Figure out which way to go
                    if (refYear < y2) {
                        century--;
                    }
                    p = p.with(DateTimeFieldType.centuryOfEra(), century);
                }
                continue;
            }
        }
        if (mgf == null || isMoreGeneral(fieldType, mgf, c1)) {
            if (!p.isSupported(fieldType)) {
                p = p.with(fieldType, p2.getValue(i));
            }
        } else {
            break;
        }
    }
    if (!p.isSupported(DateTimeFieldType.year())) {
        if (p.isSupported(DateTimeFieldType.yearOfCentury()) && p.isSupported(DateTimeFieldType.centuryOfEra())) {
            int year = p.get(DateTimeFieldType.yearOfCentury()) + p.get(DateTimeFieldType.centuryOfEra()) * 100;
            p = p.with(DateTimeFieldType.year(), year);
            p = p.without(DateTimeFieldType.yearOfCentury());
            p = p.without(DateTimeFieldType.centuryOfEra());
        }
    }
    return p;
}
Also used : DateTimeFieldType(org.joda.time.DateTimeFieldType) ISOChronology(org.joda.time.chrono.ISOChronology)

Example 25 with DateTimeFieldType

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

the class JodaTimeUtils method resolveDowToDay.

// Resolve dow for p1
public static Partial resolveDowToDay(Partial p) {
    if (p.isSupported(DateTimeFieldType.dayOfWeek())) {
        if (!p.isSupported(DateTimeFieldType.dayOfMonth())) {
            if (p.isSupported(DateTimeFieldType.weekOfWeekyear()) && (p.isSupported(DateTimeFieldType.year()))) {
                // Convert from year to weekyear (to avoid weirdness when the weekyear and year don't match at the beginning of the year)
                Partial pwy = withWeekYear(p);
                Instant t2 = getInstant(pwy);
                DateTime t1 = pwy.toDateTime(t2);
                Partial res = getPartial(t1.toInstant(), EMPTY_ISO_PARTIAL);
                DateTimeFieldType mostSpecific = getMostSpecific(p);
                res = discardMoreSpecificFields(res, mostSpecific.getDurationType());
                return res;
            }
        }
    }
    return p;
}
Also used : DateTimeFieldType(org.joda.time.DateTimeFieldType)

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