@Override protected long doReadDateTime() { return currentValue.asDateTime().getValue(); }
@Override public void encode(final BsonWriter writer, final BsonDateTime value, final EncoderContext encoderContext) { writer.writeDateTime(value.getValue()); }
private static Date getLastWriteDate(final BsonDocument isMasterResult) { if (!isMasterResult.containsKey("lastWrite")) { return null; } return new Date(isMasterResult.getDocument("lastWrite").getDateTime("lastWriteDate").getValue()); }
colValue = keyvalueforStruct.getValue().asDateTime().getValue(); break;
@Override public GridFSFile decode(final BsonReader reader, final DecoderContext decoderContext) { BsonDocument bsonDocument = bsonDocumentCodec.decode(reader, decoderContext); BsonValue id = bsonDocument.get("_id"); String filename = bsonDocument.get("filename", new BsonString("")).asString().getValue(); long length = bsonDocument.getNumber("length").longValue(); int chunkSize = bsonDocument.getNumber("chunkSize").intValue(); Date uploadDate = new Date(bsonDocument.getDateTime("uploadDate").getValue()); String md5 = bsonDocument.containsKey("md5") ? bsonDocument.getString("md5").getValue() : null; BsonDocument metadataBsonDocument = bsonDocument.getDocument("metadata", new BsonDocument()); Document optionalMetadata = asDocumentOrNull(metadataBsonDocument); for (String key : VALID_FIELDS) { bsonDocument.remove(key); } Document deprecatedExtraElements = asDocumentOrNull(bsonDocument); return new GridFSFile(id, filename, length, chunkSize, uploadDate, md5, optionalMetadata, deprecatedExtraElements); }
@Test public void withDate() { final Date date = new Date(); final ObjectId id = ObjectId.get(); final Jackson expected = ImmutableJackson.builder() .id(id) .prop1("prop1") .prop2("22") .date(new Date(date.getTime())) .build(); repository.insert(expected).getUnchecked(); check(collection.count()).is(1L); final Jackson actual = repository.findAll().fetchAll().getUnchecked().get(0); check(expected).is(actual); final BsonDocument doc = collection.find().first(); check(doc.keySet()).hasContentInAnyOrder("_id", "prop1", "prop2", "date", "uuid"); check(doc.get("date").asDateTime().getValue()).is(date.getTime()); check(doc.get("_id").asObjectId().getValue()).is(id); }
@Test public void dateCodec() throws IOException { TypeAdapter<Date> adapter = GsonCodecs.typeAdapterFromCodec(new DateCodec()); Date date = new Date(); BsonDocument doc = new BsonDocument(); BsonDocumentWriter writer = new BsonDocumentWriter(doc); writer.writeStartDocument(); writer.writeName("$date"); adapter.write(new BsonWriter(writer), date); writer.writeEndDocument(); check(doc.keySet()).hasSize(1); check(doc.get("$date").getBsonType()).is(BsonType.DATE_TIME); check(doc.get("$date").asDateTime().getValue()).is(date.getTime()); } }
return value.asBinary().getData(); case DATE_TIME: return new Date(value.asDateTime().getValue()); case SYMBOL: return value.asSymbol().getSymbol();
break; case DATE_TIME: writeDateTime(value.asDateTime().getValue()); break; case NULL:
private static Date localTimeFrom(ServerHeartbeatSucceededEvent event) { BsonDocument reply = event.getReply(); return new Date(reply.getDateTime("localTime").getValue()); }
private static Date localTimeFrom(ServerHeartbeatSucceededEvent event) { BsonDocument reply = event.getReply(); return new Date(reply.getDateTime("localTime").getValue()); }
return value.asBinary().getData(); case DATE_TIME: return new Date(value.asDateTime().getValue()); case SYMBOL: return value.asSymbol().getSymbol();
private static Date getLastWriteDate(final BsonDocument isMasterResult) { if (!isMasterResult.containsKey("lastWrite")) { return null; } return new Date(isMasterResult.getDocument("lastWrite").getDateTime("lastWriteDate").getValue()); }
private static Date lastWriteFrom(ServerHeartbeatSucceededEvent event) { BsonDocument reply = event.getReply(); if (!reply.containsKey("lastWrite")) { return null; } return new Date(reply.getDocument("lastWrite") .getDateTime("lastWriteDate").getValue()); } }
private static Date lastWriteFrom(ServerHeartbeatSucceededEvent event) { BsonDocument reply = event.getReply(); if (!reply.containsKey("lastWrite")) { return null; } return new Date(reply.getDocument("lastWrite") .getDateTime("lastWriteDate").getValue()); } }
@TestFactory @DisplayName("tests for logical type timestamp field conversions") public List<DynamicTest> testTimestampFieldConverter() { SinkFieldConverter converter = new TimestampFieldConverter(); List<DynamicTest> tests = new ArrayList<>(); new ArrayList<>(Arrays.asList( java.util.Date.from(ZonedDateTime.of(LocalDate.of(1970,1,1), LocalTime.MIDNIGHT, ZoneOffset.systemDefault()).toInstant()), java.util.Date.from(ZonedDateTime.of(LocalDate.of(1983,7,31), LocalTime.MIDNIGHT, ZoneOffset.systemDefault()).toInstant()), java.util.Date.from(ZonedDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT, ZoneOffset.systemDefault()).toInstant()) )).forEach( el -> tests.add(dynamicTest("conversion with " + converter.getClass().getSimpleName() + " for "+el, () -> assertEquals(el.toInstant().getEpochSecond()*1000, ((BsonDateTime)converter.toBson(el)).getValue()) )) ); tests.add(dynamicTest("optional type conversions", () -> { Schema valueOptionalDefault = Timestamp.builder().optional().defaultValue( java.util.Date.from(ZonedDateTime.of(LocalDate.of(1970,1,1), LocalTime.MIDNIGHT, ZoneOffset.systemDefault()).toInstant()) ); assertAll("checks", () -> assertThrows(DataException.class, () -> converter.toBson(null, Timestamp.SCHEMA)), () -> assertEquals(new BsonNull(), converter.toBson(null, Timestamp.builder().optional())), () -> assertEquals(((java.util.Date)valueOptionalDefault.defaultValue()).toInstant().getEpochSecond()*1000, ((BsonDateTime)converter.toBson(null, valueOptionalDefault)).getValue()) ); })); return tests; }
@TestFactory @DisplayName("tests for logical type date field conversions") public List<DynamicTest> testDateFieldConverter() { SinkFieldConverter converter = new DateFieldConverter(); List<DynamicTest> tests = new ArrayList<>(); new ArrayList<>(Arrays.asList( java.util.Date.from(ZonedDateTime.of(LocalDate.of(1970,1,1), LocalTime.MIDNIGHT, ZoneOffset.systemDefault()).toInstant()), java.util.Date.from(ZonedDateTime.of(LocalDate.of(1983,7,31), LocalTime.MIDNIGHT, ZoneOffset.systemDefault()).toInstant()), java.util.Date.from(ZonedDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT, ZoneOffset.systemDefault()).toInstant()) )).forEach( el -> tests.add(dynamicTest("conversion with " + converter.getClass().getSimpleName() + " for "+el, () -> assertEquals(el.toInstant().getEpochSecond()*1000, ((BsonDateTime)converter.toBson(el)).getValue()) )) ); tests.add(dynamicTest("optional type conversions", () -> { Schema valueOptionalDefault = Date.builder().optional().defaultValue( java.util.Date.from(ZonedDateTime.of(LocalDate.of(1970,1,1), LocalTime.MIDNIGHT, ZoneOffset.systemDefault()).toInstant()) ); assertAll("checks", () -> assertThrows(DataException.class, () -> converter.toBson(null, Date.SCHEMA)), () -> assertEquals(new BsonNull(), converter.toBson(null, Date.builder().optional())), () -> assertEquals(((java.util.Date)valueOptionalDefault.defaultValue()).toInstant().getEpochSecond()*1000, ((BsonDateTime)converter.toBson(null, valueOptionalDefault)).getValue()) ); })); return tests; }
@TestFactory @DisplayName("tests for logical type time field conversions") public List<DynamicTest> testTimeFieldConverter() { SinkFieldConverter converter = new TimeFieldConverter(); List<DynamicTest> tests = new ArrayList<>(); new ArrayList<>(Arrays.asList( java.util.Date.from(ZonedDateTime.of(LocalDate.of(1970,1,1), LocalTime.MIDNIGHT, ZoneOffset.systemDefault()).toInstant()), java.util.Date.from(ZonedDateTime.of(LocalDate.of(1970,1,1), LocalTime.NOON, ZoneOffset.systemDefault()).toInstant()) )).forEach( el -> tests.add(dynamicTest("conversion with " + converter.getClass().getSimpleName() + " for "+el, () -> assertEquals(el.toInstant().getEpochSecond()*1000, ((BsonDateTime)converter.toBson(el)).getValue()) )) ); tests.add(dynamicTest("optional type conversions", () -> { Schema valueOptionalDefault = Time.builder().optional().defaultValue( java.util.Date.from(ZonedDateTime.of(LocalDate.of(1970,1,1), LocalTime.MIDNIGHT, ZoneOffset.systemDefault()).toInstant()) ); assertAll("checks", () -> assertThrows(DataException.class, () -> converter.toBson(null, Time.SCHEMA)), () -> assertEquals(new BsonNull(), converter.toBson(null, Time.builder().optional())), () -> assertEquals(((java.util.Date)valueOptionalDefault.defaultValue()).toInstant().getEpochSecond()*1000, ((BsonDateTime)converter.toBson(null, valueOptionalDefault)).getValue()) ); })); return tests; }
@Override public GridFSFile decode(final BsonReader reader, final DecoderContext decoderContext) { BsonDocument bsonDocument = bsonDocumentCodec.decode(reader, decoderContext); BsonValue id = bsonDocument.get("_id"); String filename = bsonDocument.get("filename", new BsonString("")).asString().getValue(); long length = bsonDocument.getNumber("length").longValue(); int chunkSize = bsonDocument.getNumber("chunkSize").intValue(); Date uploadDate = new Date(bsonDocument.getDateTime("uploadDate").getValue()); String md5 = bsonDocument.containsKey("md5") ? bsonDocument.getString("md5").getValue() : null; BsonDocument metadataBsonDocument = bsonDocument.getDocument("metadata", new BsonDocument()); Document optionalMetadata = asDocumentOrNull(metadataBsonDocument); for (String key : VALID_FIELDS) { bsonDocument.remove(key); } Document deprecatedExtraElements = asDocumentOrNull(bsonDocument); return new GridFSFile(id, filename, length, chunkSize, uploadDate, md5, optionalMetadata, deprecatedExtraElements); }
Instant instant = Instant.ofEpochMilli(value.asDateTime().getValue()); LocalDateTime dateTime = LocalDateTime.ofInstant( instant,