示例#1
0
  /* Result is the datetime value of subtracting datetime2 from datetime1.
   */
  public static SchemaDuration datetimeDiff(SchemaDateTime datetime1, SchemaDateTime datetime2) {
    long resultticks = datetime1.getTimeValue() - datetime2.getTimeValue();
    SchemaDuration dur = new SchemaDuration("PT0S");

    if (resultticks < 0) {
      dur.setNegative(true);
      resultticks = -resultticks;
    }

    dur.setTimeFromTimeValue(resultticks);
    dur.setDay((int) (resultticks / (86400 * 1000)));
    return dur;
  }
示例#2
0
  /* Returns the addition of two durations.
   */
  public static SchemaDuration durationAdd(SchemaDuration duration1, SchemaDuration duration2) {
    SchemaDuration dur = new SchemaDuration();
    dur.setNegative(false); // sets isempty to false
    int ym = duration1.getYearMonthValue() + duration2.getYearMonthValue();
    long dt = duration1.getDayTimeValue() + duration2.getDayTimeValue();

    if ((ym < 0 && dt > 0) || (ym > 0 && dt < 0))
      throw new NotANumberException(
          "Invalid duration result, yearmonth part differs in sign from daytime part.");

    if (ym != 0) dur.setYearMonthValue(ym);
    if (dt != 0) dur.setDayTimeValue(dt);
    return dur;
  }