Search in sources :

Example 6 with BsonDateTime

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

the class JavaTimeTypeTest method localDateTime.

@Test
void localDateTime() {
    LocalDateTimeHolderRepository repository = new LocalDateTimeHolderRepository(backend);
    LocalDateTime value = LocalDateTime.now();
    ImmutableLocalDateTimeHolder holder = TypeHolder.LocalDateTimeHolder.generator().get().withValue(value).withOptional(value).withNullable(null);
    repository.insert(holder);
    BsonDocument doc = fetch();
    BsonDateTime expected = new BsonDateTime(value.toInstant(ZoneOffset.UTC).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 : LocalDateTime(java.time.LocalDateTime) BsonDocument(org.bson.BsonDocument) BsonDateTime(org.bson.BsonDateTime) LocalDateTimeHolderRepository(org.immutables.criteria.typemodel.LocalDateTimeHolderRepository) ImmutableLocalDateTimeHolder(org.immutables.criteria.typemodel.ImmutableLocalDateTimeHolder) Test(org.junit.jupiter.api.Test)

Example 7 with BsonDateTime

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

the class JavaTimeTest method javaUtilDate.

@Test
public void javaUtilDate() throws IOException {
    Date now = new Date();
    long epoch = now.getTime();
    TypeAdapter<Date> adapter = GsonCodecs.typeAdapterFromCodec(new DateCodec());
    // read
    Date 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(new Date(bson.asDateTime().getValue())).is(now);
}
Also used : BsonDateTime(org.bson.BsonDateTime) DateCodec(org.bson.codecs.DateCodec) Date(java.util.Date) LocalDate(java.time.LocalDate) BsonValue(org.bson.BsonValue) Test(org.junit.Test)

Example 8 with BsonDateTime

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

the class JavaTimeTest method localDateTime.

@Test
public void localDateTime() throws IOException {
    LocalDateTime now = LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS);
    long epoch = now.toInstant(ZoneOffset.UTC).toEpochMilli();
    TypeAdapter<LocalDateTime> adapter = gson.getAdapter(LocalDateTime.class);
    // read
    LocalDateTime date = adapter.read(Jsons.readerAt(new BsonDateTime(epoch)));
    LocalDateTime valueRead = Instant.ofEpochMilli(epoch).atOffset(ZoneOffset.UTC).toLocalDateTime();
    check(date).is(now);
    // write
    BsonValue bson = writeAndReadBson(valueRead);
    check(bson.getBsonType()).is(BsonType.DATE_TIME);
    check(Instant.ofEpochMilli(bson.asDateTime().getValue()).atOffset(ZoneOffset.UTC).toLocalDateTime()).is(valueRead);
}
Also used : LocalDateTime(java.time.LocalDateTime) BsonDateTime(org.bson.BsonDateTime) BsonValue(org.bson.BsonValue) Test(org.junit.Test)

Example 9 with BsonDateTime

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

the class TypeConversionTest method dateTime.

@Test
public void dateTime() throws IOException {
    final long epoch = System.currentTimeMillis();
    BsonDateTime value = new BsonDateTime(epoch);
    check(Jsons.readerAt(value).peek()).is(JsonToken.NUMBER);
    check(Jsons.readerAt(value).nextInt()).is((int) epoch);
    check(Jsons.readerAt(value).nextLong()).is(epoch);
    check(Jsons.readerAt(value).nextDouble()).is((double) epoch);
}
Also used : BsonDateTime(org.bson.BsonDateTime) Test(org.junit.Test)

Example 10 with BsonDateTime

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

the class JavaTimeTest method instant.

@Test
public void instant() throws IOException {
    Instant now = Instant.now().truncatedTo(ChronoUnit.MILLIS);
    long epoch = now.toEpochMilli();
    TypeAdapter<Instant> adapter = gson.getAdapter(Instant.class);
    // read
    Instant 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())).is(now);
}
Also used : BsonDateTime(org.bson.BsonDateTime) Instant(java.time.Instant) BsonValue(org.bson.BsonValue) Test(org.junit.Test)

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