Search in sources :

Example 11 with BsonDateTime

use of org.bson.BsonDateTime in project immutables by immutables.

the class JavaTimeTest method localDate.

@Test
public void localDate() throws IOException {
    long epoch = System.currentTimeMillis();
    LocalDate now = Instant.ofEpochMilli(epoch).atOffset(ZoneOffset.UTC).toLocalDate();
    TypeAdapter<LocalDate> adapter = gson.getAdapter(LocalDate.class);
    // read
    LocalDate date = adapter.read(Jsons.readerAt(new BsonDateTime(epoch)));
    check(date).is(now);
    // write
    BsonValue bson = writeAndReadBson(now);
    check(bson.getBsonType()).is(BsonType.DATE_TIME);
    check(Instant.ofEpochMilli(bson.asDateTime().getValue()).atOffset(ZoneOffset.UTC).toLocalDate()).is(now);
}
Also used : BsonDateTime(org.bson.BsonDateTime) LocalDate(java.time.LocalDate) BsonValue(org.bson.BsonValue) Test(org.junit.Test)

Example 12 with BsonDateTime

use of org.bson.BsonDateTime in project immutables by immutables.

the class TypeConversionTest method dateTime.

@Test
void dateTime() throws IOException {
    final long epoch = System.currentTimeMillis();
    final BsonDateTime value = new BsonDateTime(epoch);
    check(Parsers.parserAt(value).getIntValue()).is((int) epoch);
    check(Parsers.parserAt(value).getLongValue()).is(epoch);
    check(Parsers.parserAt(value).getDoubleValue()).is((double) epoch);
    check(Parsers.parserAt(value).getDecimalValue()).is(BigDecimal.valueOf(epoch));
    check(Parsers.parserAt(value).getBigIntegerValue()).is(BigInteger.valueOf(epoch));
    check(Parsers.parserAt(value).getText()).is(Long.toString(epoch));
}
Also used : BsonDateTime(org.bson.BsonDateTime) Test(org.junit.jupiter.api.Test)

Example 13 with BsonDateTime

use of org.bson.BsonDateTime in project immutables by immutables.

the class TupleCodecProviderTest method localDate.

@Test
void localDate() {
    LocalDateHolderCriteria criteria = LocalDateHolderCriteria.localDateHolder;
    Query query = Query.of(TypeHolder.LocalDateHolder.class).addProjections(Matchers.toExpression(criteria.value), Matchers.toExpression(criteria.nullable), Matchers.toExpression(criteria.optional));
    Path idPath = Visitors.toPath(KeyExtractor.defaultFactory().create(TypeHolder.LocalDateHolder.class).metadata().keys().get(0));
    TupleCodecProvider provider = new TupleCodecProvider(query, new MongoPathNaming(idPath, PathNaming.defaultNaming()).toExpression());
    Codec<ProjectedTuple> codec = provider.get(ProjectedTuple.class, registry);
    LocalDate now = LocalDate.now();
    final long millisEpoch = now.atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli();
    BsonDocument doc = new BsonDocument().append("id", new BsonString("id1")).append("value", new BsonDateTime(millisEpoch)).append("nullable", BsonNull.VALUE).append("optional", BsonNull.VALUE).append("array", new BsonArray()).append("list", new BsonArray());
    ProjectedTuple tuple = codec.decode(new BsonDocumentReader(doc), DecoderContext.builder().build());
    check(tuple.get(Matchers.toExpression(criteria.value))).is(now);
    check(tuple.get(Matchers.toExpression(criteria.nullable))).isNull();
    check(tuple.get(Matchers.toExpression(criteria.optional))).is(Optional.empty());
}
Also used : Path(org.immutables.criteria.expression.Path) Query(org.immutables.criteria.expression.Query) BsonDocumentReader(org.bson.BsonDocumentReader) LocalDate(java.time.LocalDate) TypeHolder(org.immutables.criteria.typemodel.TypeHolder) LocalDateHolderCriteria(org.immutables.criteria.typemodel.LocalDateHolderCriteria) BsonDocument(org.bson.BsonDocument) BsonDateTime(org.bson.BsonDateTime) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ProjectedTuple(org.immutables.criteria.backend.ProjectedTuple) Test(org.junit.jupiter.api.Test)

Example 14 with BsonDateTime

use of org.bson.BsonDateTime in project immutables by immutables.

the class JavaTimeTypeTest method localDate.

@Test
void localDate() {
    LocalDateHolderRepository repository = new LocalDateHolderRepository(backend);
    LocalDate value = LocalDate.now();
    ImmutableLocalDateHolder holder = TypeHolder.LocalDateHolder.generator().get().withValue(value).withOptional(value).withNullable(null);
    repository.insert(holder);
    BsonDocument doc = fetch();
    BsonDateTime expected = new BsonDateTime(value.atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli());
    check(doc.get("value")).is(expected);
    check(doc.get("optional")).is(expected);
    if (doc.containsKey("nullable")) {
        check(doc.get("nullable")).is(BsonNull.VALUE);
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDateTime(org.bson.BsonDateTime) ImmutableLocalDateHolder(org.immutables.criteria.typemodel.ImmutableLocalDateHolder) LocalDate(java.time.LocalDate) LocalDateHolderRepository(org.immutables.criteria.typemodel.LocalDateHolderRepository) Test(org.junit.jupiter.api.Test)

Example 15 with BsonDateTime

use of org.bson.BsonDateTime in project mongo-java-driver by mongodb.

the class IdHoldingBsonWriter method writeDateTime.

@Override
public void writeDateTime(final long value) {
    addBsonValue(() -> new BsonDateTime(value), () -> getIdBsonWriter().writeDateTime(value));
    super.writeDateTime(value);
}
Also used : BsonDateTime(org.bson.BsonDateTime)

Aggregations

BsonDateTime (org.bson.BsonDateTime)17 BsonDocument (org.bson.BsonDocument)9 Test (org.junit.Test)8 BsonValue (org.bson.BsonValue)6 Test (org.junit.jupiter.api.Test)5 LocalDate (java.time.LocalDate)4 BsonString (org.bson.BsonString)4 Date (java.util.Date)3 BsonArray (org.bson.BsonArray)3 BsonDocumentReader (org.bson.BsonDocumentReader)3 BsonInt32 (org.bson.BsonInt32)3 BsonInt64 (org.bson.BsonInt64)3 Instant (java.time.Instant)2 LocalDateTime (java.time.LocalDateTime)2 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)2 BsonBinary (org.bson.BsonBinary)2 BsonBoolean (org.bson.BsonBoolean)2 BsonDbPointer (org.bson.BsonDbPointer)2 BsonDouble (org.bson.BsonDouble)2 BsonJavaScript (org.bson.BsonJavaScript)2