コード例 #1
0
    private RubyTime buildTime(
        VirtualFrame frame,
        RubyClass timeClass,
        int sec,
        int min,
        int hour,
        int mday,
        int month,
        int year,
        int nsec,
        int isdst,
        boolean fromutc,
        Object utcoffset) {
      CompilerDirectives.transferToInterpreter();

      if (sec < 0
          || sec > 59
          || min < 0
          || min > 59
          || hour < 0
          || hour > 23
          || mday < 1
          || mday > 31
          || month < 1
          || month > 12) {
        throw new RaiseException(getContext().getCoreLibrary().argumentErrorOutOfRange(this));
      }

      final DateTimeZone zone;
      if (fromutc) {
        zone = DateTimeZone.UTC;
      } else if (utcoffset == nil()) {
        String tz = readTimeZoneNode.executeRubyString(frame).toString();
        zone = org.jruby.RubyTime.getTimeZoneFromTZString(getContext().getRuntime(), tz);
      } else if (utcoffset instanceof Integer) {
        zone = DateTimeZone.forOffsetMillis(((int) utcoffset) * 1_000);
      } else if (utcoffset instanceof Long) {
        zone = DateTimeZone.forOffsetMillis((int) ((long) utcoffset) * 1_000);
      } else if (utcoffset instanceof RubyBasicObject) {
        final int millis = cast(ruby(frame, "(offset * 1000).to_i", "offset", utcoffset));
        zone = DateTimeZone.forOffsetMillis(millis);
      } else {
        throw new UnsupportedOperationException(
            String.format("%s %s %s %s", isdst, fromutc, utcoffset, utcoffset.getClass()));
      }

      if (isdst == -1) {
        final DateTime dateTime =
            new DateTime(year, month, mday, hour, min, sec, nsec / 1_000_000, zone);
        return new RubyTime(timeClass, dateTime, utcoffset);
      } else {
        throw new UnsupportedOperationException(
            String.format("%s %s %s %s", isdst, fromutc, utcoffset, utcoffset.getClass()));
      }
    }
コード例 #2
0
  /**
   * Parses a date-time from the given text, returning a new MutableDateTime.
   *
   * <p>The parse will use the zone and chronology specified on this formatter.
   *
   * <p>If the text contains a time zone string then that will be taken into account in adjusting
   * the time of day as follows. If the {@link #withOffsetParsed()} has been called, then the
   * resulting DateTime will have a fixed offset based on the parsed time zone. Otherwise the
   * resulting DateTime will have the zone of this formatter, but the parsed zone may have caused
   * the time to be adjusted.
   *
   * @param text the text to parse, not null
   * @return the parsed date-time, never null
   * @throws UnsupportedOperationException if parsing is not supported
   * @throws IllegalArgumentException if the text to parse is invalid
   */
  public MutableDateTime parseMutableDateTime(String text) {
    InternalParser parser = requireParser();

    Chronology chrono = selectChronology(null);
    DateTimeParserBucket bucket =
        new DateTimeParserBucket(0, chrono, iLocale, iPivotYear, iDefaultYear);
    int newPos = parser.parseInto(bucket, text, 0);
    if (newPos >= 0) {
      if (newPos >= text.length()) {
        long millis = bucket.computeMillis(true, text);
        if (iOffsetParsed && bucket.getOffsetInteger() != null) {
          int parsedOffset = bucket.getOffsetInteger();
          DateTimeZone parsedZone = DateTimeZone.forOffsetMillis(parsedOffset);
          chrono = chrono.withZone(parsedZone);
        } else if (bucket.getZone() != null) {
          chrono = chrono.withZone(bucket.getZone());
        }
        MutableDateTime dt = new MutableDateTime(millis, chrono);
        if (iZone != null) {
          dt.setZone(iZone);
        }
        return dt;
      }
    } else {
      newPos = ~newPos;
    }
    throw new IllegalArgumentException(FormatUtils.createErrorMessage(text, newPos));
  }
コード例 #3
0
  /**
   * Parses a datetime from the given text, at the given position, saving the result into the fields
   * of the given ReadWritableInstant. If the parse succeeds, the return value is the new text
   * position. Note that the parse may succeed without fully reading the text and in this case those
   * fields that were read will be set.
   *
   * <p>Only those fields present in the string will be changed in the specified instant. All other
   * fields will remain unaltered. Thus if the string only contains a year and a month, then the day
   * and time will be retained from the input instant. If this is not the behaviour you want, then
   * reset the fields before calling this method, or use {@link #parseDateTime(String)} or {@link
   * #parseMutableDateTime(String)}.
   *
   * <p>If it fails, the return value is negative, but the instant may still be modified. To
   * determine the position where the parse failed, apply the one's complement operator (~) on the
   * return value.
   *
   * <p>This parse method ignores the {@link #getDefaultYear() default year} and parses using the
   * year from the supplied instant based on the chronology and time-zone of the supplied instant.
   *
   * <p>The parse will use the chronology of the instant.
   *
   * @param instant an instant that will be modified, not null
   * @param text the text to parse
   * @param position position to start parsing from
   * @return new position, negative value means parse failed - apply complement operator (~) to get
   *     position of failure
   * @throws UnsupportedOperationException if parsing is not supported
   * @throws IllegalArgumentException if the instant is null
   * @throws IllegalArgumentException if any field is out of range
   */
  public int parseInto(ReadWritableInstant instant, String text, int position) {
    InternalParser parser = requireParser();
    if (instant == null) {
      throw new IllegalArgumentException("Instant must not be null");
    }

    long instantMillis = instant.getMillis();
    Chronology chrono = instant.getChronology();
    int defaultYear = DateTimeUtils.getChronology(chrono).year().get(instantMillis);
    long instantLocal = instantMillis + chrono.getZone().getOffset(instantMillis);
    chrono = selectChronology(chrono);

    DateTimeParserBucket bucket =
        new DateTimeParserBucket(instantLocal, chrono, iLocale, iPivotYear, defaultYear);
    int newPos = parser.parseInto(bucket, text, position);
    instant.setMillis(bucket.computeMillis(false, text));
    if (iOffsetParsed && bucket.getOffsetInteger() != null) {
      int parsedOffset = bucket.getOffsetInteger();
      DateTimeZone parsedZone = DateTimeZone.forOffsetMillis(parsedOffset);
      chrono = chrono.withZone(parsedZone);
    } else if (bucket.getZone() != null) {
      chrono = chrono.withZone(bucket.getZone());
    }
    instant.setChronology(chrono);
    if (iZone != null) {
      instant.setZone(iZone);
    }
    return newPos;
  }
コード例 #4
0
 private DateTimeZone getTimeZone(HttpServletRequest request) {
   Integer millisOffset = getMillisOffset(request);
   if (millisOffset != null) {
     try {
       return DateTimeZone.forOffsetMillis(millisOffset);
     } catch (IllegalArgumentException e) {
       return DateTimeZone.getDefault();
     }
   } else {
     return DateTimeZone.getDefault();
   }
 }
コード例 #5
0
  @Override
  protected Object[] toConvertedColumns(DateTime value) {

    final DateTime myValue;
    if (databaseZone == null) {
      myValue = value;
    } else {
      myValue = value.withZone(databaseZone);
    }
    return new Object[] {
      myValue.toLocalDateTime(),
      new DateTimeZoneWithOffset(
          value.getZone(),
          value.getZone().isFixed()
              ? null
              : DateTimeZone.forOffsetMillis(value.getZone().getOffset(value)))
    };
  }
コード例 #6
0
  static {
    registerKeyword(OP_NAME, AgeSearch.class);

    sGerritFormat =
        DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss Z")
            .withLocale(Locale.US)
            .withZone(DateTimeZone.forOffsetMillis(TimeZone.getDefault().getRawOffset()));

    // Format for serializing and deserialising this keyword
    sLocalFormat =
        new DateTimeFormatterBuilder()
            .appendPattern("yyyy-MM-dd")
            .appendLiteral('T')
            .appendPattern("HH:mm:ss")
            .toFormatter();

    // For pretty printing the datetime as the selected value for refine search
    prettyFormatter =
        DateTimeFormat.forPattern("MMM dd, YYYY 'at' HH:mm").withLocale(Locale.getDefault());
  }
コード例 #7
0
  public DateTimeZone getZone(String id) {
    if (id == null) {
      return DateTimeZone.UTC;
    }

    TimeZone tz = TimeZone.getTimeZone(id);
    if (tz == null) {
      return DateTimeZone.UTC;
    }

    int rawOffset = tz.getRawOffset();

    // sub-optimal. could be improved to only create a new Date every few
    // minutes
    if (tz.inDaylightTime(new Date())) {
      rawOffset += tz.getDSTSavings();
    }

    return DateTimeZone.forOffsetMillis(rawOffset);
  }
コード例 #8
0
  /**
   * Parses only the local date-time from the given text, returning a new LocalDateTime.
   *
   * <p>This will parse the text fully according to the formatter, using the UTC zone. Once parsed,
   * only the local date-time will be used. This means that any parsed time-zone or offset field is
   * completely ignored. It also means that the zone and offset-parsed settings are ignored.
   *
   * @param text the text to parse, not null
   * @return the parsed date-time, never null
   * @throws UnsupportedOperationException if parsing is not supported
   * @throws IllegalArgumentException if the text to parse is invalid
   * @since 2.0
   */
  public LocalDateTime parseLocalDateTime(String text) {
    InternalParser parser = requireParser();

    Chronology chrono = selectChronology(null).withUTC(); // always use UTC, avoiding DST gaps
    DateTimeParserBucket bucket =
        new DateTimeParserBucket(0, chrono, iLocale, iPivotYear, iDefaultYear);
    int newPos = parser.parseInto(bucket, text, 0);
    if (newPos >= 0) {
      if (newPos >= text.length()) {
        long millis = bucket.computeMillis(true, text);
        if (bucket.getOffsetInteger() != null) { // treat withOffsetParsed() as being true
          int parsedOffset = bucket.getOffsetInteger();
          DateTimeZone parsedZone = DateTimeZone.forOffsetMillis(parsedOffset);
          chrono = chrono.withZone(parsedZone);
        } else if (bucket.getZone() != null) {
          chrono = chrono.withZone(bucket.getZone());
        }
        return new LocalDateTime(millis, chrono);
      }
    } else {
      newPos = ~newPos;
    }
    throw new IllegalArgumentException(FormatUtils.createErrorMessage(text, newPos));
  }
コード例 #9
0
public class CommonGpsTestDataHelper {
  public static final DateTimeZone TIME_ZONE = DateTimeZone.forOffsetMillis(0);
  public static final TrackPoint POINT_1 =
      new TrackPoint(
          54.709699, 25.245331, 165.282010, new DateTime(2012, 11, 24, 6, 42, 35, 0, TIME_ZONE));
  public static final TrackPoint POINT_2 =
      new TrackPoint(
          54.709651, 25.245321, 165.064160, new DateTime(2012, 11, 24, 6, 42, 36, 0, TIME_ZONE));
  public static final TrackPoint POINT_3 =
      new TrackPoint(
          54.709612, 25.245299, 164.895780, new DateTime(2012, 11, 24, 6, 42, 37, 0, TIME_ZONE));
  public static final TrackPoint POINT_4 =
      new TrackPoint(
          54.709577, 25.245242, 164.769670, new DateTime(2012, 11, 24, 6, 42, 38, 0, TIME_ZONE));
  public static final TrackPoint POINT_5 =
      new TrackPoint(
          54.709552, 25.245176, 164.748660, new DateTime(2012, 11, 24, 6, 42, 39, 0, TIME_ZONE));
  public static final TrackPoint POINT_6 =
      new TrackPoint(
          54.709523, 25.245106, 164.775070, new DateTime(2012, 11, 24, 6, 42, 40, 0, TIME_ZONE));

  public static Trail prepareTrail(TrackPoint... points) {
    Track track = trackOf(points);
    return new Trail(ImmutableList.of(track));
  }

  public static Trail prepareTrailWithoutAltitude(TrackPoint... points) {
    return prepareTrail(prepareTrackPointsWithoutAltitude(points));
  }

  public static TrackPoint[] prepareTrackPointsWithoutAltitude(TrackPoint... points) {
    List<TrackPoint> withoutAltitude =
        Lists.transform(
            Arrays.asList(points),
            new Function<TrackPoint, TrackPoint>() {
              @Override
              public TrackPoint apply(TrackPoint input) {
                return new TrackPoint(
                    input.getLatitude(), input.getLongitude(), null, input.getTime());
              }
            });
    return withoutAltitude.toArray(new TrackPoint[withoutAltitude.size()]);
  }

  public static Trail prepareTrailWithoutTime(TrackPoint... tracksPoints) {
    return prepareTrail(prepareTrackPointsWithoutTime(tracksPoints));
  }

  public static TrackPoint[] prepareTrackPointsWithoutTime(TrackPoint... points) {
    List<TrackPoint> withoutTime =
        Lists.transform(
            Arrays.asList(points),
            new Function<TrackPoint, TrackPoint>() {
              @Override
              public TrackPoint apply(TrackPoint input) {
                return new TrackPoint(
                    input.getLatitude(), input.getLongitude(), input.getAltitude(), null);
              }
            });
    return withoutTime.toArray(new TrackPoint[withoutTime.size()]);
  }

  public static Track trackOf(TrackPoint... points) {
    return new Track(Arrays.asList(points));
  }

  public static Track trackWithoutTimeOf(TrackPoint... points) {
    return trackOf(prepareTrackPointsWithoutTime(points));
  }

  public static Track trackWithoutAltitudeOf(TrackPoint... points) {
    return trackOf(prepareTrackPointsWithoutAltitude(points));
  }
}