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;
}
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;
}
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;
}
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);
}
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);
}
Aggregations