Java Code Examples for java.time.Instant#getNano()
The following examples show how to use
java.time.Instant#getNano() .
These examples are extracted from open source projects.
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 Project: presto File: CurrentTimestamp.java License: Apache License 2.0 | 8 votes |
@LiteralParameters("p") @SqlType("timestamp(p) with time zone") public static long shortTimestamp( @LiteralParameter("p") long precision, ConnectorSession session, @SqlNullable @SqlType("timestamp(p) with time zone") Long dummy) // need a dummy value since the type inferencer can't bind type arguments exclusively from return type { Instant start = session.getStart(); long epochMillis = start.toEpochMilli(); if (precision < MAX_SHORT_PRECISION) { epochMillis = round(epochMillis, (int) (MAX_SHORT_PRECISION - precision)); } else { long nanosOfMilli = start.getNano() % NANOSECONDS_PER_MILLISECOND; if (roundToNearest(nanosOfMilli, NANOSECONDS_PER_MILLISECOND) == NANOSECONDS_PER_MILLISECOND) { epochMillis++; } } return packDateTimeWithZone(epochMillis, session.getTimeZoneKey()); }
Example 2
Source Project: phoebus File: TimeScreenTransform.java License: Eclipse Public License 1.0 | 6 votes |
/** {@inheritDoc} */ @Override public synchronized void config(final Instant i1, final Instant i2, final double y1, final double y2) { final double x1 = i1.getEpochSecond() + 1e-9*i1.getNano(); final double x2 = i2.getEpochSecond() + 1e-9*i2.getNano(); final double d = x2 - x1; if (d != 0.0) { a = (y2 - y1) / d; b = (x2*y1 - x1*y2) / d; if (a != 0.0) return; } a = 1.0; b = 0.0; }
Example 3
Source Project: armeria File: HttpTimestampSupplier.java License: Apache License 2.0 | 6 votes |
@VisibleForTesting String currentTimestamp() { final long currentTimeNanos = nanoTimeSupplier.getAsLong(); if (nextUpdateNanos - currentTimeNanos > 0) { return timestamp; } final Instant now = instantSupplier.get(); // The next time we need to update our formatted timestamp is the next time System.nanoTime() // equals the following second. We can determine this by adding one second to our current // nanoTime minus the nanos part of the current system time (i.e., the number of nanos since the // last second). nextUpdateNanos = currentTimeNanos - now.getNano() + NANOS_IN_SECOND; return timestamp = DateTimeFormatter.RFC_1123_DATE_TIME.format( ZonedDateTime.ofInstant(now, ZoneOffset.UTC)); }
Example 4
Source Project: vespa File: LogReader.java License: Apache License 2.0 | 5 votes |
void writeLogs(OutputStream out, Instant from, Instant to) { double fromSeconds = from.getEpochSecond() + from.getNano() / 1e9; double toSeconds = to.getEpochSecond() + to.getNano() / 1e9; BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out)); try { for (List<Path> logs : getMatchingFiles(from, to)) { List<LogLineIterator> logLineIterators = new ArrayList<>(); try { // Logs in each sub-list contain entries covering the same time interval, so do a merge sort while reading for (Path log : logs) logLineIterators.add(new LogLineIterator(log, fromSeconds, toSeconds)); Iterator<LineWithTimestamp> lines = Iterators.mergeSorted(logLineIterators, Comparator.comparingDouble(LineWithTimestamp::timestamp)); while (lines.hasNext()) { writer.write(lines.next().line()); writer.newLine(); } } catch (IOException e) { throw new UncheckedIOException(e); } finally { for (LogLineIterator ll : logLineIterators) { try { ll.close(); } catch (IOException ignored) { } } } } } finally { Exceptions.uncheck(writer::flush); } }
Example 5
Source Project: tcMenu File: FileBasedProjectPersistor.java License: Apache License 2.0 | 5 votes |
@Override public JsonElement serialize(Instant dt, Type type, JsonSerializationContext jsonSerializationContext) { var seconds = dt.getEpochSecond(); var nanos = dt.getNano(); JsonObject obj = new JsonObject(); obj.add("seconds", new JsonPrimitive(seconds)); obj.add("nanos", new JsonPrimitive(nanos)); return obj; }
Example 6
Source Project: hottub File: JavatimeTest.java License: GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); //System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); /////////// Timestamp //////////////////////////////// Timestamp ta = new Timestamp(millis); ta.setNanos(nanos); if (!isEqual(ta.toLocalDateTime(), ta)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ta.toLocalDateTime(), ta); throw new RuntimeException("FAILED: j.s.ts -> ldt"); } if (!isEqual(ldt, Timestamp.valueOf(ldt))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ldt, Timestamp.valueOf(ldt)); throw new RuntimeException("FAILED: ldt -> j.s.ts"); } Instant inst0 = ta.toInstant(); if (ta.getTime() != inst0.toEpochMilli() || ta.getNanos() != inst0.getNano() || !ta.equals(Timestamp.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts"); } inst = Instant.ofEpochSecond(secs, nanos); Timestamp ta0 = Timestamp.from(inst); if (ta0.getTime() != inst.toEpochMilli() || ta0.getNanos() != inst.getNano() || !inst.equals(ta0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> timestamp -> instant"); } ////////// java.sql.Date ///////////////////////////// // j.s.d/t uses j.u.d.equals() !!!!!!!! java.sql.Date jsd = new java.sql.Date(millis); if (!isEqual(jsd.toLocalDate(), jsd)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(jsd.toLocalDate(), jsd); throw new RuntimeException("FAILED: j.s.d -> ld"); } LocalDate ld = ldt.toLocalDate(); if (!isEqual(ld, java.sql.Date.valueOf(ld))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ld, java.sql.Date.valueOf(ld)); throw new RuntimeException("FAILED: ld -> j.s.d"); } ////////// java.sql.Time ///////////////////////////// java.sql.Time jst = new java.sql.Time(millis); if (!isEqual(jst.toLocalTime(), jst)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(jst.toLocalTime(), jst); throw new RuntimeException("FAILED: j.s.t -> lt"); } // millis precision LocalTime lt = ldt_ms.toLocalTime(); if (!isEqual(lt, java.sql.Time.valueOf(lt))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(lt, java.sql.Time.valueOf(lt)); throw new RuntimeException("FAILED: lt -> j.s.t"); } } System.out.println("Passed!"); }
Example 7
Source Project: openjdk-jdk9 File: ZoneRules.java License: GNU General Public License v2.0 | 4 votes |
/** * Gets the previous transition before the specified instant. * <p> * This returns details of the previous transition before the specified instant. * For example, if the instant represents a point where "summer" daylight saving time * applies, then the method will return the transition from the previous "winter" time. * * @param instant the instant to get the previous transition after, not null, but null * may be ignored if the rules have a single offset for all instants * @return the previous transition before the specified instant, null if this is before the first transition */ public ZoneOffsetTransition previousTransition(Instant instant) { if (savingsInstantTransitions.length == 0) { return null; } long epochSec = instant.getEpochSecond(); if (instant.getNano() > 0 && epochSec < Long.MAX_VALUE) { epochSec += 1; // allow rest of method to only use seconds } // check if using last rules long lastHistoric = savingsInstantTransitions[savingsInstantTransitions.length - 1]; if (lastRules.length > 0 && epochSec > lastHistoric) { // search year the instant is in ZoneOffset lastHistoricOffset = wallOffsets[wallOffsets.length - 1]; int year = findYear(epochSec, lastHistoricOffset); ZoneOffsetTransition[] transArray = findTransitionArray(year); for (int i = transArray.length - 1; i >= 0; i--) { if (epochSec > transArray[i].toEpochSecond()) { return transArray[i]; } } // use last from preceding year int lastHistoricYear = findYear(lastHistoric, lastHistoricOffset); if (--year > lastHistoricYear) { transArray = findTransitionArray(year); return transArray[transArray.length - 1]; } // drop through } // using historic rules int index = Arrays.binarySearch(savingsInstantTransitions, epochSec); if (index < 0) { index = -index - 1; } if (index <= 0) { return null; } return new ZoneOffsetTransition(savingsInstantTransitions[index - 1], wallOffsets[index - 1], wallOffsets[index]); }
Example 8
Source Project: openjdk-8-source File: JavatimeTest.java License: GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); //System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); /////////// Timestamp //////////////////////////////// Timestamp ta = new Timestamp(millis); ta.setNanos(nanos); if (!isEqual(ta.toLocalDateTime(), ta)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ta.toLocalDateTime(), ta); throw new RuntimeException("FAILED: j.s.ts -> ldt"); } if (!isEqual(ldt, Timestamp.valueOf(ldt))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ldt, Timestamp.valueOf(ldt)); throw new RuntimeException("FAILED: ldt -> j.s.ts"); } Instant inst0 = ta.toInstant(); if (ta.getTime() != inst0.toEpochMilli() || ta.getNanos() != inst0.getNano() || !ta.equals(Timestamp.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts"); } inst = Instant.ofEpochSecond(secs, nanos); Timestamp ta0 = Timestamp.from(inst); if (ta0.getTime() != inst.toEpochMilli() || ta0.getNanos() != inst.getNano() || !inst.equals(ta0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> timestamp -> instant"); } ////////// java.sql.Date ///////////////////////////// // j.s.d/t uses j.u.d.equals() !!!!!!!! java.sql.Date jsd = new java.sql.Date(millis); if (!isEqual(jsd.toLocalDate(), jsd)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(jsd.toLocalDate(), jsd); throw new RuntimeException("FAILED: j.s.d -> ld"); } LocalDate ld = ldt.toLocalDate(); if (!isEqual(ld, java.sql.Date.valueOf(ld))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ld, java.sql.Date.valueOf(ld)); throw new RuntimeException("FAILED: ld -> j.s.d"); } ////////// java.sql.Time ///////////////////////////// java.sql.Time jst = new java.sql.Time(millis); if (!isEqual(jst.toLocalTime(), jst)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(jst.toLocalTime(), jst); throw new RuntimeException("FAILED: j.s.t -> lt"); } // millis precision LocalTime lt = ldt_ms.toLocalTime(); if (!isEqual(lt, java.sql.Time.valueOf(lt))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(lt, java.sql.Time.valueOf(lt)); throw new RuntimeException("FAILED: lt -> j.s.t"); } } System.out.println("Passed!"); }
Example 9
Source Project: openjdk-8 File: JavatimeTest.java License: GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); //System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); /////////// Timestamp //////////////////////////////// Timestamp ta = new Timestamp(millis); ta.setNanos(nanos); if (!isEqual(ta.toLocalDateTime(), ta)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ta.toLocalDateTime(), ta); throw new RuntimeException("FAILED: j.s.ts -> ldt"); } if (!isEqual(ldt, Timestamp.valueOf(ldt))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ldt, Timestamp.valueOf(ldt)); throw new RuntimeException("FAILED: ldt -> j.s.ts"); } Instant inst0 = ta.toInstant(); if (ta.getTime() != inst0.toEpochMilli() || ta.getNanos() != inst0.getNano() || !ta.equals(Timestamp.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts"); } inst = Instant.ofEpochSecond(secs, nanos); Timestamp ta0 = Timestamp.from(inst); if (ta0.getTime() != inst.toEpochMilli() || ta0.getNanos() != inst.getNano() || !inst.equals(ta0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> timestamp -> instant"); } ////////// java.sql.Date ///////////////////////////// // j.s.d/t uses j.u.d.equals() !!!!!!!! java.sql.Date jsd = new java.sql.Date(millis); if (!isEqual(jsd.toLocalDate(), jsd)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(jsd.toLocalDate(), jsd); throw new RuntimeException("FAILED: j.s.d -> ld"); } LocalDate ld = ldt.toLocalDate(); if (!isEqual(ld, java.sql.Date.valueOf(ld))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ld, java.sql.Date.valueOf(ld)); throw new RuntimeException("FAILED: ld -> j.s.d"); } ////////// java.sql.Time ///////////////////////////// java.sql.Time jst = new java.sql.Time(millis); if (!isEqual(jst.toLocalTime(), jst)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(jst.toLocalTime(), jst); throw new RuntimeException("FAILED: j.s.t -> lt"); } // millis precision LocalTime lt = ldt_ms.toLocalTime(); if (!isEqual(lt, java.sql.Time.valueOf(lt))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(lt, java.sql.Time.valueOf(lt)); throw new RuntimeException("FAILED: lt -> j.s.t"); } } System.out.println("Passed!"); }
Example 10
Source Project: jdk8u-jdk File: ZoneRules.java License: GNU General Public License v2.0 | 4 votes |
/** * Gets the previous transition before the specified instant. * <p> * This returns details of the previous transition after the specified instant. * For example, if the instant represents a point where "summer" daylight saving time * applies, then the method will return the transition from the previous "winter" time. * * @param instant the instant to get the previous transition after, not null, but null * may be ignored if the rules have a single offset for all instants * @return the previous transition after the specified instant, null if this is before the first transition */ public ZoneOffsetTransition previousTransition(Instant instant) { if (savingsInstantTransitions.length == 0) { return null; } long epochSec = instant.getEpochSecond(); if (instant.getNano() > 0 && epochSec < Long.MAX_VALUE) { epochSec += 1; // allow rest of method to only use seconds } // check if using last rules long lastHistoric = savingsInstantTransitions[savingsInstantTransitions.length - 1]; if (lastRules.length > 0 && epochSec > lastHistoric) { // search year the instant is in ZoneOffset lastHistoricOffset = wallOffsets[wallOffsets.length - 1]; int year = findYear(epochSec, lastHistoricOffset); ZoneOffsetTransition[] transArray = findTransitionArray(year); for (int i = transArray.length - 1; i >= 0; i--) { if (epochSec > transArray[i].toEpochSecond()) { return transArray[i]; } } // use last from preceding year int lastHistoricYear = findYear(lastHistoric, lastHistoricOffset); if (--year > lastHistoricYear) { transArray = findTransitionArray(year); return transArray[transArray.length - 1]; } // drop through } // using historic rules int index = Arrays.binarySearch(savingsInstantTransitions, epochSec); if (index < 0) { index = -index - 1; } if (index <= 0) { return null; } return new ZoneOffsetTransition(savingsInstantTransitions[index - 1], wallOffsets[index - 1], wallOffsets[index]); }
Example 11
Source Project: jdk8u-jdk File: JavatimeTest.java License: GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); //System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); /////////// Timestamp //////////////////////////////// Timestamp ta = new Timestamp(millis); ta.setNanos(nanos); if (!isEqual(ta.toLocalDateTime(), ta)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ta.toLocalDateTime(), ta); throw new RuntimeException("FAILED: j.s.ts -> ldt"); } if (!isEqual(ldt, Timestamp.valueOf(ldt))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ldt, Timestamp.valueOf(ldt)); throw new RuntimeException("FAILED: ldt -> j.s.ts"); } Instant inst0 = ta.toInstant(); if (ta.getTime() != inst0.toEpochMilli() || ta.getNanos() != inst0.getNano() || !ta.equals(Timestamp.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts"); } inst = Instant.ofEpochSecond(secs, nanos); Timestamp ta0 = Timestamp.from(inst); if (ta0.getTime() != inst.toEpochMilli() || ta0.getNanos() != inst.getNano() || !inst.equals(ta0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> timestamp -> instant"); } ////////// java.sql.Date ///////////////////////////// // j.s.d/t uses j.u.d.equals() !!!!!!!! java.sql.Date jsd = new java.sql.Date(millis); if (!isEqual(jsd.toLocalDate(), jsd)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(jsd.toLocalDate(), jsd); throw new RuntimeException("FAILED: j.s.d -> ld"); } LocalDate ld = ldt.toLocalDate(); if (!isEqual(ld, java.sql.Date.valueOf(ld))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ld, java.sql.Date.valueOf(ld)); throw new RuntimeException("FAILED: ld -> j.s.d"); } ////////// java.sql.Time ///////////////////////////// java.sql.Time jst = new java.sql.Time(millis); if (!isEqual(jst.toLocalTime(), jst)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(jst.toLocalTime(), jst); throw new RuntimeException("FAILED: j.s.t -> lt"); } // millis precision LocalTime lt = ldt_ms.toLocalTime(); if (!isEqual(lt, java.sql.Time.valueOf(lt))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(lt, java.sql.Time.valueOf(lt)); throw new RuntimeException("FAILED: lt -> j.s.t"); } } System.out.println("Passed!"); }
Example 12
Source Project: trellis File: TriplestoreResourceServiceTest.java License: Apache License 2.0 | 4 votes |
static boolean isReallyLaterThan(final Instant time) { final Instant t = now(); return t.isAfter(time) && (t.toEpochMilli() > time.toEpochMilli() || t.getNano() > time.getNano()); }
Example 13
Source Project: titan1withtp3.1 File: ConsistentKeyLockerTest.java License: Apache License 2.0 | 4 votes |
@Override public long getTime(Instant timestamp) { return timestamp.getEpochSecond() * 1000000000L + timestamp.getNano(); }
Example 14
Source Project: jdk8u_jdk File: JavatimeTest.java License: GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); //System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); /////////// Timestamp //////////////////////////////// Timestamp ta = new Timestamp(millis); ta.setNanos(nanos); if (!isEqual(ta.toLocalDateTime(), ta)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ta.toLocalDateTime(), ta); throw new RuntimeException("FAILED: j.s.ts -> ldt"); } if (!isEqual(ldt, Timestamp.valueOf(ldt))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ldt, Timestamp.valueOf(ldt)); throw new RuntimeException("FAILED: ldt -> j.s.ts"); } Instant inst0 = ta.toInstant(); if (ta.getTime() != inst0.toEpochMilli() || ta.getNanos() != inst0.getNano() || !ta.equals(Timestamp.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts"); } inst = Instant.ofEpochSecond(secs, nanos); Timestamp ta0 = Timestamp.from(inst); if (ta0.getTime() != inst.toEpochMilli() || ta0.getNanos() != inst.getNano() || !inst.equals(ta0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> timestamp -> instant"); } ////////// java.sql.Date ///////////////////////////// // j.s.d/t uses j.u.d.equals() !!!!!!!! java.sql.Date jsd = new java.sql.Date(millis); if (!isEqual(jsd.toLocalDate(), jsd)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(jsd.toLocalDate(), jsd); throw new RuntimeException("FAILED: j.s.d -> ld"); } LocalDate ld = ldt.toLocalDate(); if (!isEqual(ld, java.sql.Date.valueOf(ld))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ld, java.sql.Date.valueOf(ld)); throw new RuntimeException("FAILED: ld -> j.s.d"); } ////////// java.sql.Time ///////////////////////////// java.sql.Time jst = new java.sql.Time(millis); if (!isEqual(jst.toLocalTime(), jst)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(jst.toLocalTime(), jst); throw new RuntimeException("FAILED: j.s.t -> lt"); } // millis precision LocalTime lt = ldt_ms.toLocalTime(); if (!isEqual(lt, java.sql.Time.valueOf(lt))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(lt, java.sql.Time.valueOf(lt)); throw new RuntimeException("FAILED: lt -> j.s.t"); } } System.out.println("Passed!"); }
Example 15
Source Project: TencentKona-8 File: JavatimeTest.java License: GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); //System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); /////////// Timestamp //////////////////////////////// Timestamp ta = new Timestamp(millis); ta.setNanos(nanos); if (!isEqual(ta.toLocalDateTime(), ta)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ta.toLocalDateTime(), ta); throw new RuntimeException("FAILED: j.s.ts -> ldt"); } if (!isEqual(ldt, Timestamp.valueOf(ldt))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ldt, Timestamp.valueOf(ldt)); throw new RuntimeException("FAILED: ldt -> j.s.ts"); } Instant inst0 = ta.toInstant(); if (ta.getTime() != inst0.toEpochMilli() || ta.getNanos() != inst0.getNano() || !ta.equals(Timestamp.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts"); } inst = Instant.ofEpochSecond(secs, nanos); Timestamp ta0 = Timestamp.from(inst); if (ta0.getTime() != inst.toEpochMilli() || ta0.getNanos() != inst.getNano() || !inst.equals(ta0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> timestamp -> instant"); } ////////// java.sql.Date ///////////////////////////// // j.s.d/t uses j.u.d.equals() !!!!!!!! java.sql.Date jsd = new java.sql.Date(millis); if (!isEqual(jsd.toLocalDate(), jsd)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(jsd.toLocalDate(), jsd); throw new RuntimeException("FAILED: j.s.d -> ld"); } LocalDate ld = ldt.toLocalDate(); if (!isEqual(ld, java.sql.Date.valueOf(ld))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ld, java.sql.Date.valueOf(ld)); throw new RuntimeException("FAILED: ld -> j.s.d"); } ////////// java.sql.Time ///////////////////////////// java.sql.Time jst = new java.sql.Time(millis); if (!isEqual(jst.toLocalTime(), jst)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(jst.toLocalTime(), jst); throw new RuntimeException("FAILED: j.s.t -> lt"); } // millis precision LocalTime lt = ldt_ms.toLocalTime(); if (!isEqual(lt, java.sql.Time.valueOf(lt))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(lt, java.sql.Time.valueOf(lt)); throw new RuntimeException("FAILED: lt -> j.s.t"); } } System.out.println("Passed!"); }
Example 16
Source Project: openjdk-jdk8u-backup File: JavatimeTest.java License: GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); //System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); /////////// Timestamp //////////////////////////////// Timestamp ta = new Timestamp(millis); ta.setNanos(nanos); if (!isEqual(ta.toLocalDateTime(), ta)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ta.toLocalDateTime(), ta); throw new RuntimeException("FAILED: j.s.ts -> ldt"); } if (!isEqual(ldt, Timestamp.valueOf(ldt))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ldt, Timestamp.valueOf(ldt)); throw new RuntimeException("FAILED: ldt -> j.s.ts"); } Instant inst0 = ta.toInstant(); if (ta.getTime() != inst0.toEpochMilli() || ta.getNanos() != inst0.getNano() || !ta.equals(Timestamp.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts"); } inst = Instant.ofEpochSecond(secs, nanos); Timestamp ta0 = Timestamp.from(inst); if (ta0.getTime() != inst.toEpochMilli() || ta0.getNanos() != inst.getNano() || !inst.equals(ta0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> timestamp -> instant"); } ////////// java.sql.Date ///////////////////////////// // j.s.d/t uses j.u.d.equals() !!!!!!!! java.sql.Date jsd = new java.sql.Date(millis); if (!isEqual(jsd.toLocalDate(), jsd)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(jsd.toLocalDate(), jsd); throw new RuntimeException("FAILED: j.s.d -> ld"); } LocalDate ld = ldt.toLocalDate(); if (!isEqual(ld, java.sql.Date.valueOf(ld))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(ld, java.sql.Date.valueOf(ld)); throw new RuntimeException("FAILED: ld -> j.s.d"); } ////////// java.sql.Time ///////////////////////////// java.sql.Time jst = new java.sql.Time(millis); if (!isEqual(jst.toLocalTime(), jst)) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(jst.toLocalTime(), jst); throw new RuntimeException("FAILED: j.s.t -> lt"); } // millis precision LocalTime lt = ldt_ms.toLocalTime(); if (!isEqual(lt, java.sql.Time.valueOf(lt))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); print(lt, java.sql.Time.valueOf(lt)); throw new RuntimeException("FAILED: lt -> j.s.t"); } } System.out.println("Passed!"); }
Example 17
Source Project: jdk8u-jdk File: Timestamp.java License: GNU General Public License v2.0 | 3 votes |
/** * Obtains an instance of {@code Timestamp} from an {@link Instant} object. * <p> * {@code Instant} can store points on the time-line further in the future * and further in the past than {@code Date}. In this scenario, this method * will throw an exception. * * @param instant the instant to convert * @return an {@code Timestamp} representing the same point on the time-line as * the provided instant * @exception NullPointerException if {@code instant} is null. * @exception IllegalArgumentException if the instant is too large to * represent as a {@code Timesamp} * @since 1.8 */ public static Timestamp from(Instant instant) { try { Timestamp stamp = new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND); stamp.nanos = instant.getNano(); return stamp; } catch (ArithmeticException ex) { throw new IllegalArgumentException(ex); } }
Example 18
Source Project: sis File: DefaultTemporalCRS.java License: Apache License 2.0 | 3 votes |
/** * Converts the given instant into a value in this axis unit. * If the given instant is {@code null}, then this method returns {@link Double#NaN}. * This method is the converse of {@link #toInstant(double)}. * * @param time the value as an instant, or {@code null}. * @return the value in this axis unit, or {@link Double#NaN} if the given instant is {@code null}. * Unit of measurement is given by {@link #getUnit()}. * * @since 1.0 */ public double toValue(final Instant time) { if (time != null) { double t = Math.subtractExact(time.getEpochSecond(), origin); t += time.getNano() / (double) NANOS_PER_SECOND; return toSeconds.inverse().convert(t); } else { return Double.NaN; } }
Example 19
Source Project: openjdk-8 File: Timestamp.java License: GNU General Public License v2.0 | 3 votes |
/** * Obtains an instance of {@code Timestamp} from an {@link Instant} object. * <p> * {@code Instant} can store points on the time-line further in the future * and further in the past than {@code Date}. In this scenario, this method * will throw an exception. * * @param instant the instant to convert * @return an {@code Timestamp} representing the same point on the time-line as * the provided instant * @exception NullPointerException if {@code instant} is null. * @exception IllegalArgumentException if the instant is too large to * represent as a {@code Timesamp} * @since 1.8 */ public static Timestamp from(Instant instant) { try { Timestamp stamp = new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND); stamp.nanos = instant.getNano(); return stamp; } catch (ArithmeticException ex) { throw new IllegalArgumentException(ex); } }
Example 20
Source Project: Java8CN File: Timestamp.java License: Apache License 2.0 | 3 votes |
/** * Obtains an instance of {@code Timestamp} from an {@link Instant} object. * <p> * {@code Instant} can store points on the time-line further in the future * and further in the past than {@code Date}. In this scenario, this method * will throw an exception. * * @param instant the instant to convert * @return an {@code Timestamp} representing the same point on the time-line as * the provided instant * @exception NullPointerException if {@code instant} is null. * @exception IllegalArgumentException if the instant is too large to * represent as a {@code Timesamp} * @since 1.8 */ public static Timestamp from(Instant instant) { try { Timestamp stamp = new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND); stamp.nanos = instant.getNano(); return stamp; } catch (ArithmeticException ex) { throw new IllegalArgumentException(ex); } }