Search in sources :

Example 66 with Duration

use of javax.xml.datatype.Duration in project structure-project by wudskq.

the class XMLGregorianCalendarImpl method normalizeToTimezone.

/**
 * <p>Normalize this instance to UTC.</p>
 *
 * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
 * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
 */
private XMLGregorianCalendar normalizeToTimezone(int timezone) {
    int minutes = timezone;
    XMLGregorianCalendar result = (XMLGregorianCalendar) this.clone();
    // normalizing to UTC time negates the timezone offset before
    // addition.
    minutes = -minutes;
    Duration d = new // isPositive
    DurationImpl(// isPositive
    minutes >= 0, // years
    0, // months
    0, // days
    0, // hours
    0, // absolute
    minutes < 0 ? -minutes : minutes, // seconds
    0);
    result.add(d);
    // set to zulu UTC time.
    result.setTimezone(0);
    return result;
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Duration(javax.xml.datatype.Duration)

Example 67 with Duration

use of javax.xml.datatype.Duration in project structure-project by wudskq.

the class DurationImpl method add.

/**
 * <p>Computes a new duration whose value is <code>this+rhs</code>.</p>
 *
 * <p>For example,</p>
 * <pre>
 * "1 day" + "-3 days" = "-2 days"
 * "1 year" + "1 day" = "1 year and 1 day"
 * "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
 * "15 hours" + "-3 days" = "-(2 days,9 hours)"
 * "1 year" + "-1 day" = IllegalStateException
 * </pre>
 *
 * <p>Since there's no way to meaningfully subtract 1 day from 1 month,
 * there are cases where the operation fails in
 * {@link IllegalStateException}.</p>
 *
 * <p>
 * Formally, the computation is defined as follows.</p>
 * <p>
 * Firstly, we can assume that two {@link Duration}s to be added
 * are both positive without losing generality (i.e.,
 * <code>(-X)+Y=Y-X</code>, <code>X+(-Y)=X-Y</code>,
 * <code>(-X)+(-Y)=-(X+Y)</code>)
 *
 * <p>
 * Addition of two positive {@link Duration}s are simply defined as
 * field by field addition where missing fields are treated as 0.
 * <p>
 * A field of the resulting {@link Duration} will be unset if and
 * only if respective fields of two input {@link Duration}s are unset.
 * <p>
 * Note that <code>lhs.add(rhs)</code> will be always successful if
 * <code>lhs.signum()*rhs.signum()!=-1</code> or both of them are
 * normalized.</p>
 *
 * @param rhs <code>Duration</code> to add to this <code>Duration</code>
 *
 * @return
 *      non-null valid Duration object.
 *
 * @throws NullPointerException
 *      If the rhs parameter is null.
 * @throws IllegalStateException
 *      If two durations cannot be meaningfully added. For
 *      example, adding negative one day to one month causes
 *      this exception.
 *
 * @see #subtract(Duration)
 */
public Duration add(final Duration rhs) {
    Duration lhs = this;
    BigDecimal[] buf = new BigDecimal[6];
    buf[0] = sanitize((BigInteger) lhs.getField(DatatypeConstants.YEARS), lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.YEARS), rhs.getSign()));
    buf[1] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MONTHS), lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MONTHS), rhs.getSign()));
    buf[2] = sanitize((BigInteger) lhs.getField(DatatypeConstants.DAYS), lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.DAYS), rhs.getSign()));
    buf[3] = sanitize((BigInteger) lhs.getField(DatatypeConstants.HOURS), lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.HOURS), rhs.getSign()));
    buf[4] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MINUTES), lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MINUTES), rhs.getSign()));
    buf[5] = sanitize((BigDecimal) lhs.getField(DatatypeConstants.SECONDS), lhs.getSign()).add(sanitize((BigDecimal) rhs.getField(DatatypeConstants.SECONDS), rhs.getSign()));
    // align sign
    // Y,M
    alignSigns(buf, 0, 2);
    // D,h,m,s
    alignSigns(buf, 2, 6);
    // make sure that the sign bit is consistent across all 6 fields.
    int s = 0;
    for (int i = 0; i < 6; i++) {
        if (s * buf[i].signum() < 0) {
            throw new IllegalStateException();
        }
        if (s == 0) {
            s = buf[i].signum();
        }
    }
    return new DurationImpl(s >= 0, toBigInteger(sanitize(buf[0], s), lhs.getField(DatatypeConstants.YEARS) == null && rhs.getField(DatatypeConstants.YEARS) == null), toBigInteger(sanitize(buf[1], s), lhs.getField(DatatypeConstants.MONTHS) == null && rhs.getField(DatatypeConstants.MONTHS) == null), toBigInteger(sanitize(buf[2], s), lhs.getField(DatatypeConstants.DAYS) == null && rhs.getField(DatatypeConstants.DAYS) == null), toBigInteger(sanitize(buf[3], s), lhs.getField(DatatypeConstants.HOURS) == null && rhs.getField(DatatypeConstants.HOURS) == null), toBigInteger(sanitize(buf[4], s), lhs.getField(DatatypeConstants.MINUTES) == null && rhs.getField(DatatypeConstants.MINUTES) == null), (buf[5].signum() == 0 && lhs.getField(DatatypeConstants.SECONDS) == null && rhs.getField(DatatypeConstants.SECONDS) == null) ? null : sanitize(buf[5], s));
}
Also used : Duration(javax.xml.datatype.Duration) BigDecimal(java.math.BigDecimal)

Example 68 with Duration

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

the class XSDFuncOp method adjustDatetimeToTimezone.

public static NodeValue adjustDatetimeToTimezone(NodeValue nv1, NodeValue nv2) {
    if (nv1 == null)
        return null;
    if (!nv1.isDateTime() && !nv1.isDate() && !nv1.isTime())
        throw new ExprEvalException("Not a valid date, datetime or time:" + nv1);
    XMLGregorianCalendar calValue = nv1.getDateTime();
    Boolean hasTz = calValue.getTimezone() != DatatypeConstants.FIELD_UNDEFINED;
    int inputOffset = 0;
    if (hasTz) {
        inputOffset = calValue.getTimezone();
    }
    int tzOffset = 0;
    if (nv2 != null) {
        if (!nv2.isDuration()) {
            String nv2StrValue = nv2.getString();
            if (nv2StrValue.equals("")) {
                calValue.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
                if (nv1.isDateTime())
                    return NodeValue.makeDateTime(calValue);
                else if (nv1.isTime())
                    return NodeValue.makeNode(calValue.toXMLFormat(), XSDDatatype.XSDtime);
                else
                    return NodeValue.makeDate(calValue);
            }
            throw new ExprEvalException("Not a valid duration:" + nv2);
        }
        Duration tzDuration = nv2.getDuration();
        tzOffset = tzDuration.getSign() * (tzDuration.getMinutes() + 60 * tzDuration.getHours());
        if (tzDuration.getSeconds() > 0)
            throw new ExprEvalException("The timezone duration should be an integral number of minutes");
        int absTzOffset = java.lang.Math.abs(tzOffset);
        if (absTzOffset > 14 * 60)
            throw new ExprEvalException("The timezone should be a duration between -PT14H and PT14H.");
    } else {
        tzOffset = TimeZone.getDefault().getOffset(new Date().getTime()) / (1000 * 60);
    }
    Duration durToAdd = NodeValue.xmlDatatypeFactory.newDurationDayTime((tzOffset - inputOffset) > 0, 0, 0, java.lang.Math.abs(tzOffset - inputOffset), 0);
    if (hasTz)
        calValue.add(durToAdd);
    calValue.setTimezone(tzOffset);
    if (nv1.isDateTime())
        return NodeValue.makeDateTime(calValue);
    else if (nv1.isTime())
        return NodeValue.makeNode(calValue.toXMLFormat(), XSDDatatype.XSDtime);
    else
        return NodeValue.makeDate(calValue);
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Duration(javax.xml.datatype.Duration)

Example 69 with Duration

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

the class XSDFuncOp method localTimezoneDuration.

/**
 * Local (query engine) timezone including DST
 */
private static Duration localTimezoneDuration() {
    ZonedDateTime zdt = ZonedDateTime.now();
    int tzDurationInSeconds = zdt.getOffset().getTotalSeconds();
    Duration dur = NodeFunctions.duration(tzDurationInSeconds);
    return dur;
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Duration(javax.xml.datatype.Duration)

Example 70 with Duration

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

the class XSDFuncOp method accessDuration.

private static NodeValue accessDuration(NodeValue nv, Field field) {
    Duration dur = nv.getDuration();
    // if ( ! nv.isDuration() )
    // throw new ExprEvalException("Not a duration: "+nv) ;
    Number x = dur.getField(field);
    if (x == null) {
        x = field.equals(DatatypeConstants.SECONDS) ? BigDecimal.ZERO : BigInteger.ZERO;
    }
    if (field.equals(DatatypeConstants.SECONDS))
        return NodeValue.makeDecimal((BigDecimal) x);
    return NodeValue.makeInteger((BigInteger) x);
}
Also used : Duration(javax.xml.datatype.Duration) BigDecimal(java.math.BigDecimal)

Aggregations

Duration (javax.xml.datatype.Duration)137 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)57 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)14 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)12 Date (java.util.Date)12 BigDecimal (java.math.BigDecimal)9 Calendar (java.util.Calendar)9 GregorianCalendar (java.util.GregorianCalendar)9 ObjectDeltaType (com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType)8 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)7 CleanupPolicyType (com.evolveum.midpoint.xml.ns._public.common.common_3.CleanupPolicyType)6 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)5 IOException (java.io.IOException)5 XSDayTimeDuration (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration)5 FileNotFoundException (java.io.FileNotFoundException)4 Collection (java.util.Collection)4 DatatypeFactory (javax.xml.datatype.DatatypeFactory)4 NotNull (org.jetbrains.annotations.NotNull)4 PrismObject (com.evolveum.midpoint.prism.PrismObject)3