Skip to content

Commit

Permalink
Merge branch 'master' into drs-readme-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
aklish authored Jul 7, 2020
2 parents ad6b2b4 + 3d22939 commit 2752fe2
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2020, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.utils.coerce.converters;

import java.time.Instant;
import java.time.format.DateTimeFormatter;

/**
* Convert an Instant to/from an ISO-8601 string representation.
*
* Uses the semantics of {@link java.time.format.DateTimeFormatter#ISO_INSTANT}
*/
@ElideTypeConverter(type = Instant.class, name = "Instant")
public class InstantSerde implements Serde<String, Instant> {
@Override
public Instant deserialize(final String value) {
return Instant.from(
// NB. ideally we would use ISO_INSTANT here but there is a bug in JDK-8 that
// means that parsing an ISO offset time doesn't work :-(
// https://bugs.openjdk.java.net/browse/JDK-8166138
DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(value)
);
}

@Override
public String serialize(final Instant value) {
return DateTimeFormatter.ISO_INSTANT.format(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2018, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.utils.coerce.converters;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import java.time.Instant;

public class InstantSerdeTest {
private final Serde<String, Instant> serde = new InstantSerde();

@Test
public void canDeserializeUtcIsoString() {
final Instant instant = serde.deserialize("2019-06-01T09:42:55Z");

assertEquals(1559382175, instant.getEpochSecond());
assertEquals(0, instant.getNano());
}

@Test
public void canDeserializeOffsetIsoString() {
final Instant instant = serde.deserialize("2019-06-01T10:42:55+01:00");

assertEquals(1559382175, instant.getEpochSecond());
assertEquals(0, instant.getNano());
}

@Test
public void canDeserializeSubSecondPrecisionUtcIsoString() {
final Instant instant = serde.deserialize("2019-06-01T09:42:55.123Z");

assertEquals(1559382175, instant.getEpochSecond());
assertEquals(123000000, instant.getNano());
}

@Test
public void canSerialize() {
assertEquals(
"2019-06-01T09:42:55Z",
serde.serialize(Instant.ofEpochSecond(1559382175))
);
}

@Test
public void canSerializeSubSecondPrecision() {
assertEquals(
"2019-06-01T09:42:55.123Z",
serde.serialize(Instant.ofEpochMilli(1559382175123L))
);
}
}

0 comments on commit 2752fe2

Please sign in to comment.