-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into drs-readme-fix
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
elide-core/src/main/java/com/yahoo/elide/utils/coerce/converters/InstantSerde.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
elide-core/src/test/java/com/yahoo/elide/utils/coerce/converters/InstantSerdeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
); | ||
} | ||
} |