Java Code Examples for org.joda.time.Instant#getMillis()

The following examples show how to use org.joda.time.Instant#getMillis() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Examples.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void runInstant() {
    System.out.println("Instant");
    System.out.println("=======");
    System.out.println("Instant stores a point in the datetime continuum as millisecs from 1970-01-01T00:00:00Z");
    System.out.println("Instant is immutable and thread-safe");
    System.out.println("                      in = new Instant()");
    Instant in = new Instant();
    System.out.println("Millisecond time:     in.getMillis():           " + in.getMillis());
    System.out.println("ISO string version:   in.toString():            " + in.toString());
    System.out.println("ISO chronology:       in.getChronology():       " + in.getChronology());
    System.out.println("UTC time zone:        in.getDateTimeZone():     " + in.getZone());
    System.out.println("Change millis:        in.withMillis(0):         " + in.withMillis(0L));
    System.out.println("");
    System.out.println("Convert to Instant:   in.toInstant():           " + in.toInstant());
    System.out.println("Convert to DateTime:  in.toDateTime():          " + in.toDateTime());
    System.out.println("Convert to MutableDT: in.toMutableDateTime():   " + in.toMutableDateTime());
    System.out.println("Convert to Date:      in.toDate():              " + in.toDate());
    System.out.println("");
    System.out.println("                      in2 = new Instant(in.getMillis() + 10)");
    Instant in2 = new Instant(in.getMillis() + 10);
    System.out.println("Equals ms and chrono: in.equals(in2):           " + in.equals(in2));
    System.out.println("Compare millisecond:  in.compareTo(in2):        " + in.compareTo(in2));
    System.out.println("Compare millisecond:  in.isEqual(in2):          " + in.isEqual(in2));
    System.out.println("Compare millisecond:  in.isAfter(in2):          " + in.isAfter(in2));
    System.out.println("Compare millisecond:  in.isBefore(in2):         " + in.isBefore(in2));
}
 
Example 2
Source File: FixedWindows.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public IntervalWindow assignWindow(Instant timestamp) {
  Instant start =
      new Instant(
          timestamp.getMillis()
              - timestamp.plus(size).minus(offset).getMillis() % size.getMillis());

  // The global window is inclusive of max timestamp, while interval window excludes its
  // upper bound
  Instant endOfGlobalWindow = GlobalWindow.INSTANCE.maxTimestamp().plus(1);

  // The end of the window is either start + size if that is within the allowable range, otherwise
  // the end of the global window. Truncating the window drives many other
  // areas of this system in the appropriate way automatically.
  //
  // Though it is curious that the very last representable fixed window is shorter than the rest,
  // when we are processing data in the year 294247, we'll probably have technology that can
  // account for this.
  Instant end =
      start.isAfter(endOfGlobalWindow.minus(size)) ? endOfGlobalWindow : start.plus(size);

  return new IntervalWindow(start, end);
}
 
Example 3
Source File: SyntheticWatermark.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Calculates new watermark value and returns it if it's greater than the previous one. */
Instant calculateNew(long currentOffset, Instant processingTime) {

  // the source has seen all elements so the watermark is "+infinity"
  if (currentOffset >= endOffset) {
    watermark = BoundedWindow.TIMESTAMP_MAX_VALUE;
    return watermark;
  }

  Instant newWatermark =
      findLowestEventTimeInAdvance(currentOffset, processingTime)
          .minus(Duration.millis(options.watermarkDriftMillis));

  if (newWatermark.getMillis() > watermark.getMillis()) {
    watermark = newWatermark;
  }

  return watermark;
}
 
Example 4
Source File: FlinkStateInternals.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public void add(Instant value) {
  try {
    Instant current = watermarkHoldsState.get(namespaceString);
    if (current == null) {
      addWatermarkHoldUsage(value);
      watermarkHoldsState.put(namespaceString, value);
    } else {
      Instant combined = timestampCombiner.combine(current, value);
      if (combined.getMillis() != current.getMillis()) {
        removeWatermarkHoldUsage(current);
        addWatermarkHoldUsage(combined);
        watermarkHoldsState.put(namespaceString, combined);
      }
    }
  } catch (Exception e) {
    throw new RuntimeException("Error updating state.", e);
  }
}
 
Example 5
Source File: Examples.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void runInstant() {
    System.out.println("Instant");
    System.out.println("=======");
    System.out.println("Instant stores a point in the datetime continuum as millisecs from 1970-01-01T00:00:00Z");
    System.out.println("Instant is immutable and thread-safe");
    System.out.println("                      in = new Instant()");
    Instant in = new Instant();
    System.out.println("Millisecond time:     in.getMillis():           " + in.getMillis());
    System.out.println("ISO string version:   in.toString():            " + in.toString());
    System.out.println("ISO chronology:       in.getChronology():       " + in.getChronology());
    System.out.println("UTC time zone:        in.getDateTimeZone():     " + in.getZone());
    System.out.println("Change millis:        in.withMillis(0):         " + in.withMillis(0L));
    System.out.println("");
    System.out.println("Convert to Instant:   in.toInstant():           " + in.toInstant());
    System.out.println("Convert to DateTime:  in.toDateTime():          " + in.toDateTime());
    System.out.println("Convert to MutableDT: in.toMutableDateTime():   " + in.toMutableDateTime());
    System.out.println("Convert to Date:      in.toDate():              " + in.toDate());
    System.out.println("");
    System.out.println("                      in2 = new Instant(in.getMillis() + 10)");
    Instant in2 = new Instant(in.getMillis() + 10);
    System.out.println("Equals ms and chrono: in.equals(in2):           " + in.equals(in2));
    System.out.println("Compare millisecond:  in.compareTo(in2):        " + in.compareTo(in2));
    System.out.println("Compare millisecond:  in.isEqual(in2):          " + in.isEqual(in2));
    System.out.println("Compare millisecond:  in.isAfter(in2):          " + in.isAfter(in2));
    System.out.println("Compare millisecond:  in.isBefore(in2):         " + in.isBefore(in2));
}
 
Example 6
Source File: SnippetsTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
@Category({NeedsRunner.class, UsesStatefulParDo.class})
public void testSlowlyUpdatingSideInputsWindowed() {
  Instant startAt = Instant.now().minus(Duration.standardMinutes(3));
  Duration duration = Duration.standardSeconds(10);
  Instant stopAt = startAt.plus(duration);
  Duration interval1 = Duration.standardSeconds(1);
  Duration interval2 = Duration.standardSeconds(1);

  File f = null;
  try {
    f = File.createTempFile("testSlowlyUpdatingSIWindowed", "txt");
    try (BufferedWriter fw = Files.newWriter(f, Charset.forName("UTF-8"))) {
      fw.append("testdata");
    }
  } catch (IOException e) {
    Assert.fail("failed to create temp file: " + e.toString());
    throw new RuntimeException("Should never reach here");
  }

  PCollection<Long> result =
      Snippets.PeriodicallyUpdatingSideInputs.main(
          p, startAt, stopAt, interval1, interval2, f.getPath());

  ArrayList<Long> expectedResults = new ArrayList<Long>();
  expectedResults.add(0L);
  for (Long i = startAt.getMillis(); i < stopAt.getMillis(); i = i + interval2.getMillis()) {
    expectedResults.add(1L);
  }

  PAssert.that(result).containsInAnyOrder(expectedResults);

  p.run().waitUntilFinish();
  f.deleteOnExit();
}
 
Example 7
Source File: NexmarkQueryModel.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Return the start of the most recent window of {@code size} and {@code period} which ends
 * strictly before {@code timestamp}.
 */
static Instant windowStart(Duration size, Duration period, Instant timestamp) {
  long ts = timestamp.getMillis();
  long p = period.getMillis();
  long lim = ts - ts % p;
  long s = size.getMillis();
  return new Instant(lim - s);
}
 
Example 8
Source File: AbstractSimulator.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Update window and counts. */
private void updateCounts(Instant timestamp) {
  long window = timestamp.getMillis() - timestamp.getMillis() % WINDOW_SIZE.getMillis();
  if (window > currentWindow) {
    if (currentWindow > BoundedWindow.TIMESTAMP_MIN_VALUE.getMillis()) {
      pendingCounts.add(currentCount);
    }
    currentCount = 0;
    currentWindow = window;
  }
  currentCount++;
}
 
Example 9
Source File: InstantCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(Instant value, OutputStream outStream) throws CoderException, IOException {
  if (value == null) {
    throw new CoderException("cannot encode a null Instant");
  }

  // Converts {@link Instant} to a {@code long} representing its millis-since-epoch,
  // but shifted so that the byte representation of negative values are lexicographically
  // ordered before the byte representation of positive values.
  //
  // This deliberately utilizes the well-defined underflow for {@code long} values.
  // See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.2
  long shiftedMillis = value.getMillis() - Long.MIN_VALUE;
  new DataOutputStream(outStream).writeLong(shiftedMillis);
}
 
Example 10
Source File: HL7v2IO.java    From beam with Apache License 2.0 5 votes vote down vote up
@GetInitialRestriction
public OffsetRange getEarliestToLatestRestriction(@Element String hl7v2Store)
    throws IOException {
  Instant from = this.client.getEarliestHL7v2SendTime(hl7v2Store, this.filter.get());
  // filters are [from, to) to match logic of OffsetRangeTracker but need latest element to be
  // included in results set to add an extra ms to the upper bound.
  Instant to = this.client.getLatestHL7v2SendTime(hl7v2Store, this.filter.get()).plus(1);
  return new OffsetRange(from.getMillis(), to.getMillis());
}
 
Example 11
Source File: HL7v2IO.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * List messages.
 *
 * @param hl7v2Store the HL7v2 store to list messages from
 * @throws IOException the io exception
 */
@ProcessElement
public void listMessages(
    @Element String hl7v2Store,
    RestrictionTracker<OffsetRange, Long> tracker,
    OutputReceiver<HL7v2Message> outputReceiver)
    throws IOException {
  OffsetRange currentRestriction = (OffsetRange) tracker.currentRestriction();
  Instant startRestriction = Instant.ofEpochMilli(currentRestriction.getFrom());
  Instant endRestriction = Instant.ofEpochMilli(currentRestriction.getTo());
  HttpHealthcareApiClient.HL7v2MessagePages pages =
      new HttpHealthcareApiClient.HL7v2MessagePages(
          client, hl7v2Store, startRestriction, endRestriction, filter.get(), "sendTime");
  Instant cursor;
  long lastClaimedMilliSecond = startRestriction.getMillis() - 1;
  for (HL7v2Message msg : FluentIterable.concat(pages)) {
    cursor = Instant.parse(msg.getSendTime());
    if (cursor.getMillis() > lastClaimedMilliSecond) {
      // Return early after the first claim failure preventing us from iterating
      // through the remaining messages.
      if (!tracker.tryClaim(cursor.getMillis())) {
        return;
      }
      lastClaimedMilliSecond = cursor.getMillis();
    }

    outputReceiver.output(msg);
  }

  // We've paginated through all messages for this restriction but the last message may be
  // before the end of the restriction
  tracker.tryClaim(currentRestriction.getTo());
}
 
Example 12
Source File: BeamUnboundedSourceVertex.java    From incubator-nemo with Apache License 2.0 5 votes vote down vote up
@Override
public long readWatermark() {
  final Instant watermark = reader.getWatermark();
  // Finish if the watermark == TIMESTAMP_MAX_VALUE
  isFinished = (watermark.getMillis() >= GlobalWindow.TIMESTAMP_MAX_VALUE.getMillis());
  return watermark.getMillis();
}
 
Example 13
Source File: WindmillTimeUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Convert a harness timestamp to a Windmill timestamp. */
public static long harnessToWindmillTimestamp(Instant timestamp) {
  if (!timestamp.isBefore(BoundedWindow.TIMESTAMP_MAX_VALUE)) {
    // End of time.
    return Long.MAX_VALUE;
  } else if (timestamp.getMillis() < Long.MIN_VALUE / 1000) {
    return Long.MIN_VALUE + 1;
  } else {
    return timestamp.getMillis() * 1000;
  }
}
 
Example 14
Source File: FlinkGroupAlsoByWindowWrapper.java    From flink-dataflow with Apache License 2.0 5 votes vote down vote up
@Override
public void processWatermark(Watermark mark) throws Exception {
	context.setCurrentInputWatermark(new Instant(mark.getTimestamp()));

	Multimap<K, TimerInternals.TimerData> timers = getTimersReadyToProcess(mark.getTimestamp());
	if (!timers.isEmpty()) {
		for (K key : timers.keySet()) {
			processKeyedWorkItem(KeyedWorkItems.<K, VIN>timersWorkItem(key, timers.get(key)));
		}
	}

	/**
	 * This is to take into account the different semantics of the Watermark in Flink and
	 * in Dataflow. To understand the reasoning behind the Dataflow semantics and its
	 * watermark holding logic, see the documentation of
	 * {@link WatermarkHold#addHold(ReduceFn.ProcessValueContext, boolean)}
	 * */
	long millis = Long.MAX_VALUE;
	for (FlinkStateInternals state : perKeyStateInternals.values()) {
		Instant watermarkHold = state.getWatermarkHold();
		if (watermarkHold != null && watermarkHold.getMillis() < millis) {
			millis = watermarkHold.getMillis();
		}
	}

	if (mark.getTimestamp() < millis) {
		millis = mark.getTimestamp();
	}

	context.setCurrentOutputWatermark(new Instant(millis));

	// Don't forget to re-emit the watermark for further operators down the line.
	// This is critical for jobs with multiple aggregation steps.
	// Imagine a job with a groupByKey() on key K1, followed by a map() that changes
	// the key K1 to K2, and another groupByKey() on K2. In this case, if the watermark
	// is not re-emitted, the second aggregation would never be triggered, and no result
	// will be produced.
	output.emitWatermark(new Watermark(millis));
}
 
Example 15
Source File: BigQueryUtilsTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public Long toInputType(Instant base) {
  return base.getMillis();
}
 
Example 16
Source File: GJChronology.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Factory method returns instances of the GJ cutover chronology. Any
 * cutover date may be specified.
 *
 * @param zone  the time zone to use, null is default
 * @param gregorianCutover  the cutover to use, null means default
 * @param minDaysInFirstWeek  minimum number of days in first week of the year; default is 4
 */
public static synchronized GJChronology getInstance(
        DateTimeZone zone,
        ReadableInstant gregorianCutover,
        int minDaysInFirstWeek) {
    
    zone = DateTimeUtils.getZone(zone);
    Instant cutoverInstant;
    if (gregorianCutover == null) {
        cutoverInstant = DEFAULT_CUTOVER;
    } else {
        cutoverInstant = gregorianCutover.toInstant();
        LocalDate cutoverDate = new LocalDate(cutoverInstant.getMillis(), GregorianChronology.getInstance(zone));
        if (cutoverDate.getYear() <= 0) {
            throw new IllegalArgumentException("Cutover too early. Must be on or after 0001-01-01.");
        }
    }

    GJChronology chrono;
    synchronized (cCache) {
        ArrayList<GJChronology> chronos = cCache.get(zone);
        if (chronos == null) {
            chronos = new ArrayList<GJChronology>(2);
            cCache.put(zone, chronos);
        } else {
            for (int i = chronos.size(); --i >= 0;) {
                chrono = chronos.get(i);
                if (minDaysInFirstWeek == chrono.getMinimumDaysInFirstWeek() &&
                    cutoverInstant.equals(chrono.getGregorianCutover())) {
                    
                    return chrono;
                }
            }
        }
        if (zone == DateTimeZone.UTC) {
            chrono = new GJChronology
                (JulianChronology.getInstance(zone, minDaysInFirstWeek),
                 GregorianChronology.getInstance(zone, minDaysInFirstWeek),
                 cutoverInstant);
        } else {
            chrono = getInstance(DateTimeZone.UTC, cutoverInstant, minDaysInFirstWeek);
            chrono = new GJChronology
                (ZonedChronology.getInstance(chrono, zone),
                 chrono.iJulianChronology,
                 chrono.iGregorianChronology,
                 chrono.iCutoverInstant);
        }
        chronos.add(chrono);
    }
    return chrono;
}
 
Example 17
Source File: Time_6_GJChronology_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Factory method returns instances of the GJ cutover chronology. Any
 * cutover date may be specified.
 *
 * @param zone  the time zone to use, null is default
 * @param gregorianCutover  the cutover to use, null means default
 * @param minDaysInFirstWeek  minimum number of days in first week of the year; default is 4
 */
public static synchronized GJChronology getInstance(
        DateTimeZone zone,
        ReadableInstant gregorianCutover,
        int minDaysInFirstWeek) {
    
    zone = DateTimeUtils.getZone(zone);
    Instant cutoverInstant;
    if (gregorianCutover == null) {
        cutoverInstant = DEFAULT_CUTOVER;
    } else {
        cutoverInstant = gregorianCutover.toInstant();
        LocalDate cutoverDate = new LocalDate(cutoverInstant.getMillis(), GregorianChronology.getInstance(zone));
        if (cutoverDate.getYear() <= 0) {
            throw new IllegalArgumentException("Cutover too early. Must be on or after 0001-01-01.");
        }
    }

    GJChronology chrono;
    synchronized (cCache) {
        ArrayList<GJChronology> chronos = cCache.get(zone);
        if (chronos == null) {
            chronos = new ArrayList<GJChronology>(2);
            cCache.put(zone, chronos);
        } else {
            for (int i = chronos.size(); --i >= 0;) {
                chrono = chronos.get(i);
                if (minDaysInFirstWeek == chrono.getMinimumDaysInFirstWeek() &&
                    cutoverInstant.equals(chrono.getGregorianCutover())) {
                    
                    return chrono;
                }
            }
        }
        if (zone == DateTimeZone.UTC) {
            chrono = new GJChronology
                (JulianChronology.getInstance(zone, minDaysInFirstWeek),
                 GregorianChronology.getInstance(zone, minDaysInFirstWeek),
                 cutoverInstant);
        } else {
            chrono = getInstance(DateTimeZone.UTC, cutoverInstant, minDaysInFirstWeek);
            chrono = new GJChronology
                (ZonedChronology.getInstance(chrono, zone),
                 chrono.iJulianChronology,
                 chrono.iGregorianChronology,
                 chrono.iCutoverInstant);
        }
        chronos.add(chrono);
    }
    return chrono;
}
 
Example 18
Source File: CassandraPreparedStatementTest.java    From cassandra-jdbc-driver with Apache License 2.0 4 votes vote down vote up
@Test(groups = {"unit", "server"})
public void testTimestamp() {
    String insertCql = "insert into test_drive.basic_data_type(id_uuid, date_timestamp) values(?, ?)";
    String queryCql = "select date_timestamp from test_drive.basic_data_type where id_uuid = ?";
    UUID id = UUID.randomUUID();
    String timestamp = "2019-02-01T04:12:21.330Z";
    String timestamp1 = "2019-02-01 04:12:21.330";
    Instant instant = Instant.parse(timestamp);
    long ts = instant.getMillis();
    Timestamp t = new Timestamp(ts);

    try {
        // set time by string
        java.sql.PreparedStatement s = conn.prepareStatement(insertCql);
        s.setObject(1, id);
        s.setString(2, timestamp);
        s.execute();
        s.close();

        s = conn.prepareStatement(queryCql);
        s.setObject(1, id);
        ResultSet rs = s.executeQuery();
        rs.next();
        assertEquals(rs.getString(1), timestamp);
        assertEquals(rs.getObject(1), instant.toDate());
        assertEquals(rs.getTimestamp(1), t);
        rs.close();
        s.close();

        // set time by string in the other format
        s = conn.prepareStatement(insertCql);
        s.setObject(1, id);
        s.setString(2, timestamp1);
        s.execute();
        s.close();

        s = conn.prepareStatement(queryCql);
        s.setObject(1, id);
        rs = s.executeQuery();
        rs.next();
        assertEquals(rs.getString(1), timestamp);
        assertEquals(rs.getObject(1), instant.toDate());
        assertEquals(rs.getTimestamp(1), t);
        rs.close();
        s.close();

        // by long
        s = conn.prepareStatement(insertCql);
        s.setObject(1, id);
        s.setObject(2, ts);
        s.execute();
        s.close();

        s = conn.prepareStatement(queryCql);
        s.setObject(1, id);
        rs = s.executeQuery();
        rs.next();
        assertEquals(rs.getString(1), timestamp);
        assertEquals(rs.getObject(1), instant.toDate());
        assertEquals(rs.getTimestamp(1), t);
        rs.close();
        s.close();

        // by timestamp
        s = conn.prepareStatement(insertCql);
        s.setObject(1, id);
        s.setObject(2, t);
        s.execute();
        s.close();

        s = conn.prepareStatement(queryCql);
        s.setObject(1, id);
        rs = s.executeQuery();
        rs.next();
        assertEquals(rs.getString(1), timestamp);
        assertEquals(rs.getObject(1), instant.toDate());
        assertEquals(rs.getTimestamp(1), t);
        rs.close();
        s.close();

        // by Joda Instant
        s = conn.prepareStatement(insertCql);
        s.setObject(1, id);
        s.setObject(2, instant);
        s.execute();
        s.close();

        s = conn.prepareStatement(queryCql);
        s.setObject(1, id);
        rs = s.executeQuery();
        rs.next();
        assertEquals(rs.getString(1), timestamp);
        assertEquals(rs.getObject(1), instant.toDate());
        assertEquals(rs.getTimestamp(1), t);
        rs.close();
        s.close();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Error occurred during testing: " + e.getMessage());
    }
}
 
Example 19
Source File: SlidingWindows.java    From beam with Apache License 2.0 4 votes vote down vote up
/** Return the last start of a sliding window that contains the timestamp. */
private long lastStartFor(Instant timestamp) {
  return timestamp.getMillis()
      - timestamp.plus(period).minus(offset).getMillis() % period.getMillis();
}
 
Example 20
Source File: TimestampColumnInstantMapper.java    From jadira with Apache License 2.0 4 votes vote down vote up
@Override
public Timestamp toNonNullValue(Instant value) {
    
    final Timestamp timestamp = new Timestamp(value.getMillis());
    return timestamp;
}