Search in sources :

Example 6 with Duration

use of javax.xml.datatype.Duration in project graphhopper by graphhopper.

the class OSMTagParser method parseDuration.

/**
 * Parser according to http://wiki.openstreetmap.org/wiki/Key:duration The value consists of a
 * string ala 'hh:mm', format for hours and minutes 'mm', 'hh:mm' or 'hh:mm:ss', or
 * alternatively ISO_8601 duration
 * <p>
 *
 * @return duration value in seconds
 */
public static long parseDuration(String str) throws IllegalArgumentException {
    long seconds = 0;
    if (str == null)
        return 0;
    // Check for ISO_8601 format
    if (str.startsWith("P")) {
        // A common mistake is when the minutes format is intended but the month format is specified
        // e.g. one month "P1M" is set, but on minute "PT1M" is meant.
        Duration dur;
        try {
            dur = DatatypeFactory.newInstance().newDuration(str);
            seconds = dur.getTimeInMillis(STATIC_DATE) / 1000;
        } catch (Exception ex) {
            throw new IllegalArgumentException("Cannot parse duration tag value: " + str, ex);
        }
        return seconds;
    }
    try {
        int index = str.indexOf(":");
        if (index > 0) {
            String hourStr = str.substring(0, index);
            String minStr = str.substring(index + 1);
            String secondsStr = "0";
            index = minStr.indexOf(":");
            if (index > 0) {
                secondsStr = minStr.substring(index + 1, index + 3);
                minStr = minStr.substring(0, index);
            }
            seconds += Integer.parseInt(hourStr) * 60L * 60;
            seconds += Integer.parseInt(minStr) * 60L;
            seconds += Integer.parseInt(secondsStr);
            return seconds;
        } else {
            // value contains minutes
            seconds = Integer.parseInt(str) * 60;
        }
    } catch (Exception ex) {
        throw new IllegalArgumentException("Cannot parse duration tag value: " + str, ex);
    }
    return seconds;
}
Also used : Duration(javax.xml.datatype.Duration)

Example 7 with Duration

use of javax.xml.datatype.Duration in project cxf by apache.

the class DurationAndDateUtil method toExpirationTypeContainingDuration.

public static ExpirationType toExpirationTypeContainingDuration(XMLGregorianCalendar date) {
    ExpirationType et = new ExpirationType();
    XMLGregorianCalendar now = factory.newXMLGregorianCalendar(new GregorianCalendar());
    XMLGregorianCalendar then = factory.newXMLGregorianCalendar(date.toGregorianCalendar());
    long durationMillis = then.toGregorianCalendar().getTimeInMillis() - now.toGregorianCalendar().getTimeInMillis();
    Duration duration = factory.newDuration(durationMillis);
    et.setValue(duration.toString());
    return et;
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) ExpirationType(org.apache.cxf.ws.eventing.ExpirationType) GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Duration(javax.xml.datatype.Duration)

Example 8 with Duration

use of javax.xml.datatype.Duration in project cxf by apache.

the class AbstractSubscription method parseTerminationTime.

protected XMLGregorianCalendar parseTerminationTime(String value) {
    try {
        Duration d = datatypeFactory.newDuration(value);
        XMLGregorianCalendar c = getCurrentTime();
        c.add(d);
        return c;
    } catch (Exception e) {
    // Ignore
    }
    try {
        Duration d = datatypeFactory.newDurationDayTime(value);
        XMLGregorianCalendar c = getCurrentTime();
        c.add(d);
        return c;
    } catch (Exception e) {
    // Ignore
    }
    try {
        Duration d = datatypeFactory.newDurationYearMonth(value);
        XMLGregorianCalendar c = getCurrentTime();
        c.add(d);
        return c;
    } catch (Exception e) {
    // Ignore
    }
    try {
        return datatypeFactory.newXMLGregorianCalendar(value);
    } catch (Exception e) {
    // Ignore
    }
    return null;
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Duration(javax.xml.datatype.Duration) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException)

Example 9 with Duration

use of javax.xml.datatype.Duration in project cxf by apache.

the class FiqlSearchConditionBuilderTest method testGreaterThanDuration.

@Test
public void testGreaterThanDuration() throws DatatypeConfigurationException {
    Duration d = DatatypeFactory.newInstance().newDuration(false, 0, 0, 1, 12, 0, 0);
    String ret = b.is("foo").after(d).query();
    assertEquals("foo=gt=-P0Y0M1DT12H0M0S", ret);
}
Also used : Duration(javax.xml.datatype.Duration) Test(org.junit.Test)

Example 10 with Duration

use of javax.xml.datatype.Duration in project cxf by apache.

the class FiqlSearchConditionBuilderTest method testLessThanDuration.

@Test
public void testLessThanDuration() throws DatatypeConfigurationException {
    Duration d = DatatypeFactory.newInstance().newDuration(false, 0, 0, 1, 12, 0, 0);
    String ret = b.is("foo").before(d).query();
    assertEquals("foo=lt=-P0Y0M1DT12H0M0S", ret);
}
Also used : Duration(javax.xml.datatype.Duration) Test(org.junit.Test)

Aggregations

Duration (javax.xml.datatype.Duration)110 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)50 Test (org.junit.Test)17 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)12 ArrayList (java.util.ArrayList)11 ObjectDeltaType (com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType)8 GregorianCalendar (java.util.GregorianCalendar)8 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)7 Date (java.util.Date)7 CleanupPolicyType (com.evolveum.midpoint.xml.ns._public.common.common_3.CleanupPolicyType)6 Calendar (java.util.Calendar)6 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)5 XSDayTimeDuration (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration)5 Collection (java.util.Collection)4 NodeValue (org.apache.jena.sparql.expr.NodeValue)4 NotNull (org.jetbrains.annotations.NotNull)4 PrismObject (com.evolveum.midpoint.prism.PrismObject)3 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)3 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)3 Task (com.evolveum.midpoint.task.api.Task)3