示例#1
0
  /**
   * Extract time-zone information from a zoneinfo (Olson database) ID and saves them in the
   * TimeZoneHelper fields.
   *
   * @param id the time zone ID according to the zoneinfo (Olson) database
   * @param from the start of the relevant time interval for the generation of transitions (an
   *     istant expressed as a long)
   * @param to the end of the relevant time interval for the generation of transitions (an istant
   *     expressed as a long)
   */
  protected void processID(String id, long from, long to) {

    DateTimeZone tz = DateTimeZone.forID(id);
    if (name == null) { // The name could have been set already using TZID
      // and in this case it is important not to change it
      name = id; // The Olson ID is perfect as a unique name
    }
    basicOffset = tz.getStandardOffset(from);
    transitions.clear();

    if (!tz.isFixed()) {

      long oldFrom = from;
      from = fixFrom(tz, basicOffset, oldFrom);

      // @todo Consider case when to go beyond last transition (cycle
      // could become endless)
      while (tz.getStandardOffset(to) != tz.getOffset(to)) {
        to = tz.nextTransition(to);
      }

      while ((from <= to) && (oldFrom != from)) {
        transitions.add(new TimeZoneTransition(tz.getOffset(from), from, id));
        oldFrom = from;
        from = tz.nextTransition(oldFrom);
      }
    }
  }
 /**
  * To be even more nasty, go to a transition in the selected time zone. In one third of the cases
  * stay there, otherwise go half a unit back or forth
  */
 private static long nastyDate(long initialDate, DateTimeZone timezone, long unitMillis) {
   long date = timezone.nextTransition(initialDate);
   if (randomBoolean()) {
     return date + (randomLong() % unitMillis); // positive and negative offset possible
   } else {
     return date;
   }
 }
示例#3
0
  protected long fixFrom(DateTimeZone tz, int standardOffset, long from) {

    if (standardOffset != tz.getOffset(from)) { // NB: this must NOT be
      // a call to getBasicOffset(), because that method may be
      // overriden by the cached implementation(s) of this class.
      do {
        from = tz.previousTransition(from) + 1;
      } while ((from != 0) && (standardOffset == tz.getOffset(from)));
    } else {
      from = tz.nextTransition(from);
    }
    return from;
  }
示例#4
0
  private boolean matchesID(String idToCheck) {

    DateTimeZone tz;

    try {
      tz = DateTimeZone.forID(idToCheck);
    } catch (IllegalArgumentException e) { // the ID is not recognized
      return false;
    }

    if (getTransitions().size() == 0) { // No transitions
      if (tz.getStandardOffset(REFERENCE_TIME) != basicOffset) {
        return false; // Offsets don't match: wrong guess
      }
      if (tz.isFixed() || (REFERENCE_TIME == tz.nextTransition(REFERENCE_TIME))) {
        return true; // A right fixed or currently-fixed time zone
        // has been found
      }
      return false; // Wrong guess
    }

    long t = getTransitions().get(0).getTime() - 1;
    if (tz.getStandardOffset(t) != basicOffset) {
      return false; // Wrong guess
    }

    for (TimeZoneTransition transition : getTransitions()) {
      t = tz.nextTransition(t);
      if (!isClose(t, transition.getTime())) {
        return false; // Wrong guess
      }
      if (tz.getOffset(t) != transition.getOffset()) {
        return false; // Wrong guess
      }
    }
    return true; // A right non-fixed time zone has been found
  }
  /** @return false if error. */
  static boolean test(String id, DateTimeZone tz) {
    if (!id.equals(tz.getID())) {
      return true;
    }

    // Test to ensure that reported transitions are not duplicated.

    long millis = ISOChronology.getInstanceUTC().year().set(0, 1850);
    long end = ISOChronology.getInstanceUTC().year().set(0, 2050);

    int offset = tz.getOffset(millis);
    String key = tz.getNameKey(millis);

    List<Long> transitions = new ArrayList<Long>();

    while (true) {
      long next = tz.nextTransition(millis);
      if (next == millis || next > end) {
        break;
      }

      millis = next;

      int nextOffset = tz.getOffset(millis);
      String nextKey = tz.getNameKey(millis);

      if (offset == nextOffset && key.equals(nextKey)) {
        System.out.println(
            "*d* Error in "
                + tz.getID()
                + " "
                + new DateTime(millis, ISOChronology.getInstanceUTC()));
        return false;
      }

      if (nextKey == null || (nextKey.length() < 3 && !"??".equals(nextKey))) {
        System.out.println(
            "*s* Error in "
                + tz.getID()
                + " "
                + new DateTime(millis, ISOChronology.getInstanceUTC())
                + ", nameKey="
                + nextKey);
        return false;
      }

      transitions.add(Long.valueOf(millis));

      offset = nextOffset;
      key = nextKey;
    }

    // Now verify that reverse transitions match up.

    millis = ISOChronology.getInstanceUTC().year().set(0, 2050);
    end = ISOChronology.getInstanceUTC().year().set(0, 1850);

    for (int i = transitions.size(); --i >= 0; ) {
      long prev = tz.previousTransition(millis);
      if (prev == millis || prev < end) {
        break;
      }

      millis = prev;

      long trans = transitions.get(i).longValue();

      if (trans - 1 != millis) {
        System.out.println(
            "*r* Error in "
                + tz.getID()
                + " "
                + new DateTime(millis, ISOChronology.getInstanceUTC())
                + " != "
                + new DateTime(trans - 1, ISOChronology.getInstanceUTC()));

        return false;
      }
    }

    return true;
  }