コード例 #1
0
ファイル: ADate.java プロジェクト: jefferya/basex
 @Override
 public int diff(final Item it, final Collation coll, final InputInfo ii) throws QueryException {
   final ADate d = (ADate) (it instanceof ADate ? it : type.cast(it, null, null, ii));
   final BigDecimal d1 = seconds().add(days().multiply(DAYSECONDS));
   final BigDecimal d2 = d.seconds().add(d.days().multiply(DAYSECONDS));
   return d1.compareTo(d2);
 }
コード例 #2
0
ファイル: ADate.java プロジェクト: jefferya/basex
 /**
  * Returns a day count for the specified years, months and days. All values must be specified in
  * their internal representation (undefined values are supported, too). Algorithm is derived from
  * J R Stockton (http://www.merlyn.demon.co.uk/daycount.htm).
  *
  * @param year year
  * @param month month
  * @param day days
  * @return days
  */
 private static BigDecimal days(final long year, final int month, final int day) {
   final long y = year - (month < 2 ? 1 : 0);
   final int m = month + (month < 2 ? 13 : 1);
   final int d = day + 1;
   return BD365
       .multiply(BigDecimal.valueOf(y))
       .add(BigDecimal.valueOf(y / 4 - y / 100 + y / 400 - 92 + d + (153 * m - 2) / 5));
 }
コード例 #3
0
ファイル: Dur.java プロジェクト: james-jw/basex
 @Override
 public final boolean eq(
     final Item it, final Collation coll, final StaticContext sc, final InputInfo ii)
     throws QueryException {
   final Dur d = (Dur) (it instanceof Dur ? it : type.cast(it, null, null, ii));
   final BigDecimal s1 = sec == null ? BigDecimal.ZERO : sec;
   final BigDecimal s2 = d.sec == null ? BigDecimal.ZERO : d.sec;
   return mon == d.mon && s1.compareTo(s2) == 0;
 }
コード例 #4
0
ファイル: Dur.java プロジェクト: james-jw/basex
  /**
   * Initializes the dayTime component.
   *
   * @param vl value
   * @param mt matcher
   * @param p first matching position
   * @param ii input info
   * @throws QueryException query exception
   */
  void dayTime(final byte[] vl, final Matcher mt, final int p, final InputInfo ii)
      throws QueryException {

    final long d = mt.group(p) != null ? toLong(mt.group(p + 1), true, ii) : 0;
    final long h = mt.group(p + 3) != null ? toLong(mt.group(p + 4), true, ii) : 0;
    final long m = mt.group(p + 5) != null ? toLong(mt.group(p + 6), true, ii) : 0;
    final BigDecimal s =
        mt.group(p + 7) != null ? toDecimal(mt.group(p + 8), true, ii) : BigDecimal.ZERO;
    sec =
        s.add(BigDecimal.valueOf(d).multiply(DAYSECONDS))
            .add(BigDecimal.valueOf(h).multiply(BD3600))
            .add(BigDecimal.valueOf(m).multiply(BD60));
    if (!mt.group(1).isEmpty()) sec = sec.negate();
    final double v = sec.doubleValue();
    if (v <= Long.MIN_VALUE || v >= Long.MAX_VALUE) throw DURRANGE_X_X.get(ii, type, vl);
  }
コード例 #5
0
ファイル: Dur.java プロジェクト: james-jw/basex
 /**
  * Adds the time to the specified token builder.
  *
  * @param tb token builder
  */
 final void time(final TokenBuilder tb) {
   if (sec.remainder(DAYSECONDS).signum() == 0) return;
   tb.add('T');
   final long h = hou();
   if (h != 0) {
     tb.addLong(Math.abs(h));
     tb.add('H');
   }
   final long m = min();
   if (m != 0) {
     tb.addLong(Math.abs(m));
     tb.add('M');
   }
   final BigDecimal sc = sec();
   if (sc.signum() == 0) return;
   tb.add(Token.chopNumber(Token.token(sc.abs().toPlainString()))).add('S');
 }
コード例 #6
0
ファイル: ADate.java プロジェクト: jefferya/basex
 /**
  * Returns the date in seconds.
  *
  * @return seconds
  */
 final BigDecimal seconds() {
   int z = tz;
   if (z == Short.MAX_VALUE) {
     // [CG] XQuery, DateTime: may be removed
     final long n = System.currentTimeMillis();
     z = Calendar.getInstance().getTimeZone().getOffset(n) / 60000;
   }
   return (sec == null ? BigDecimal.ZERO : sec)
       .add(BigDecimal.valueOf(Math.max(0, hou) * 3600 + Math.max(0, min) * 60 - z * 60));
 }
コード例 #7
0
ファイル: ADate.java プロジェクト: jefferya/basex
  /**
   * Adjusts the timezone.
   *
   * @param zone timezone
   * @param spec indicates if zone has been specified (may be {@code null})
   * @param ii input info
   * @throws QueryException query exception
   */
  void tz(final DTDur zone, final boolean spec, final InputInfo ii) throws QueryException {
    final short t;
    if (spec && zone == null) {
      t = Short.MAX_VALUE;
    } else {
      if (zone == null) {
        final Calendar c = Calendar.getInstance();
        t = (short) ((c.get(Calendar.ZONE_OFFSET) + c.get(Calendar.DST_OFFSET)) / 60000);
      } else {
        t = (short) (zone.min() + zone.hou() * 60);
        if (zone.sec().signum() != 0) throw ZONESEC_X.get(ii, zone);
        if (Math.abs(t) > 60 * 14 || zone.day() != 0) throw INVALZONE_X.get(ii, zone);
      }

      // change time if two competing time zones exist
      if (tz != Short.MAX_VALUE) add(BigDecimal.valueOf(60L * (t - tz)));
    }
    tz = t;
  }
コード例 #8
0
ファイル: ADate.java プロジェクト: jefferya/basex
  /**
   * Adds the specified dayTime duration.
   *
   * @param add value to be added
   */
  private void add(final BigDecimal add) {
    // normalized modulo: sc % 60  vs.  (-sc + sc % 60 + 60 + sc) % 60
    final BigDecimal sc = sec().add(add);
    sec =
        sc.signum() >= 0
            ? sc.remainder(BD60)
            : sc.negate().add(sc.remainder(BD60)).add(BD60).add(sc).remainder(BD60);

    final long mn = Math.max(min(), 0) + div(sc.longValue(), 60);
    min = (byte) mod(mn, 60);
    final long ho = Math.max(hou, 0) + div(mn, 60);
    hou = (byte) mod(ho, 24);
    final long da = div(ho, 24);

    final long[] ymd = ymd(days().add(BigDecimal.valueOf(da)));
    yea = ymd[0];
    mon = (byte) ymd[1];
    day = (byte) ymd[2];
  }
コード例 #9
0
ファイル: ADate.java プロジェクト: jefferya/basex
 /**
  * Converts a day count into year, month and day components. Algorithm is derived from J R
  * Stockton (http://www.merlyn.demon.co.uk/daycount.htm).
  *
  * @param days day count
  * @return result array
  */
 private static long[] ymd(final BigDecimal days) {
   BigDecimal d = days;
   BigDecimal t =
       d.add(BD36525).multiply(BD4).divideToIntegralValue(BD146097).subtract(BigDecimal.ONE);
   BigDecimal y = BD100.multiply(t);
   d = d.subtract(BD36524.multiply(t).add(t.divideToIntegralValue(BD4)));
   t = d.add(BD366).multiply(BD4).divideToIntegralValue(BD1461).subtract(BigDecimal.ONE);
   y = y.add(t);
   d = d.subtract(BD365.multiply(t).add(t.divideToIntegralValue(BD4)));
   final BigDecimal m = BD5.multiply(d).add(BD2).divideToIntegralValue(BD153);
   d = d.subtract(BD153.multiply(m).add(BD2).divideToIntegralValue(BD5));
   long mm = m.longValue();
   if (mm > 9) {
     mm -= 12;
     y = y.add(BigDecimal.ONE);
   }
   return new long[] {y.subtract(BigDecimal.valueOf(ADD_NEG)).longValue(), mm + 2, d.longValue()};
 }
コード例 #10
0
ファイル: DecFormatter.java プロジェクト: LeoWoerteler/basex
  /**
   * Formats the specified number and returns a string representation.
   *
   * @param item item
   * @param pics pictures
   * @param ii input info
   * @return picture variables
   * @throws QueryException query exception
   */
  private byte[] format(final ANum item, final Picture[] pics, final InputInfo ii)
      throws QueryException {

    // Rule 1: return results for NaN
    final double d = item.dbl(ii);
    if (Double.isNaN(d)) return nan;

    // Rule 2: check if value if negative (smaller than zero or -0)
    final boolean neg = d < 0 || d == 0 && Double.doubleToLongBits(d) == Long.MIN_VALUE;
    final Picture pic = pics[neg && pics.length == 2 ? 1 : 0];
    final IntList res = new IntList(), intgr = new IntList(), fract = new IntList();
    int exp = 0;

    // Rule 3: percent/permille
    ANum num = item;
    if (pic.pc) num = (ANum) Calc.MULT.ev(num, Int.get(100), ii);
    if (pic.pm) num = (ANum) Calc.MULT.ev(num, Int.get(1000), ii);

    if (Double.isInfinite(num.dbl(ii))) {
      // Rule 4: infinity
      intgr.add(new TokenParser(inf).toArray());
    } else {
      // Rule 5: exponent
      if (pic.minExp != 0 && d != 0) {
        BigDecimal dec = num.dec(ii).abs().stripTrailingZeros();
        int scl = 0;
        if (dec.compareTo(BigDecimal.ONE) >= 0) {
          scl = dec.setScale(0, RoundingMode.HALF_DOWN).precision();
        } else {
          while (dec.compareTo(BigDecimal.ONE) < 0) {
            dec = dec.multiply(BigDecimal.TEN);
            scl--;
          }
          scl++;
        }
        exp = scl - pic.min[0];
        if (exp != 0) {
          final BigDecimal n = BigDecimal.TEN.pow(Math.abs(exp));
          num = (ANum) Calc.MULT.ev(num, Dec.get(exp > 0 ? BigDecimal.ONE.divide(n) : n), ii);
        }
      }
      num = num.round(pic.maxFrac, true).abs();

      // convert positive number to string
      final String s =
          (num instanceof Dbl || num instanceof Flt
                  ? Dec.get(BigDecimal.valueOf(num.dbl(ii)))
                  : num)
              .toString();

      // integer/fractional separator
      final int sep = s.indexOf('.');

      // create integer part
      final int sl = s.length();
      final int il = sep == -1 ? sl : sep;
      for (int i = il; i < pic.min[0]; ++i) intgr.add(zero);
      // fractional number: skip leading 0
      if (!s.startsWith("0.") || pic.min[0] > 0) {
        for (int i = 0; i < il; i++) intgr.add(zero + s.charAt(i) - '0');
      }

      // squeeze in grouping separators
      if (pic.group[0].length == 1 && pic.group[0][0] > 0) {
        // regular pattern with repeating separators
        for (int p = intgr.size() - (neg ? 2 : 1); p > 0; --p) {
          if (p % pic.group[0][0] == 0) intgr.insert(intgr.size() - p, grouping);
        }
      } else {
        // irregular pattern, or no separators at all
        final int gl = pic.group[0].length;
        for (int g = 0; g < gl; ++g) {
          final int pos = intgr.size() - pic.group[0][g];
          if (pos > 0) intgr.insert(pos, grouping);
        }
      }

      // create fractional part
      final int fl = sep == -1 ? 0 : sl - il - 1;
      if (fl != 0) for (int i = sep + 1; i < sl; i++) fract.add(zero + s.charAt(i) - '0');
      for (int i = fl; i < pic.min[1]; ++i) fract.add(zero);

      // squeeze in grouping separators in a reverse manner
      final int ul = fract.size();
      for (int p = pic.group[1].length - 1; p >= 0; p--) {
        final int pos = pic.group[1][p];
        if (pos < ul) fract.insert(pos, grouping);
      }
    }

    // add minus sign
    if (neg && pics.length != 2) res.add(minus);
    // add prefix and integer part
    res.add(pic.prefSuf[0].toArray()).add(intgr.finish());
    // add fractional part
    if (!fract.isEmpty()) res.add(decimal).add(fract.finish());
    // add exponent
    if (pic.minExp != 0) {
      res.add(exponent);
      if (exp < 0) res.add(minus);
      final String s = Integer.toString(Math.abs(exp));
      final int sl = s.length();
      for (int i = sl; i < pic.minExp; i++) res.add(zero);
      for (int i = 0; i < sl; i++) res.add(zero + s.charAt(i) - '0');
    }
    // add suffix
    res.add(pic.prefSuf[1].toArray());
    return new TokenBuilder(res.finish()).finish();
  }