ion-java icon indicating copy to clipboard operation
ion-java copied to clipboard

[Feature Request] Please add support for java.time to com.amazon.ion.Timestamp

Open cameronsstone opened this issue 6 years ago • 5 comments

Specifically, I would like to be able to get a java.time.Instant or java.time.OffsetDateTime out of an IonTimestamp. Based on #33, I suspect you'll want to add these to Timestamp instead of IonTimestamp, but either works for me. I also wonder if you could move towards making the Timestamp class internal in favor of OffsetDateTime as your primary mode for going to or from IonTimestamp.

[edit: corrected DateTime to ZonedDateTime - twice] [edit: changed ZonedDateTime to OffsetDateTime, as it's a better fit]

cameronsstone avatar Nov 01 '19 17:11 cameronsstone

Unfortunately ion-java is locked into being java 1.5 compatible for the foreseeable future so we can't add support for java.time at this time. When we are able to drop this requirement we'll revisit this and adopt java.time

In the meantime you need to manually convert the Timestamp into a java.time object, example:

int offset = -8
Timestamp timestamp = Timestamp.forMinute(2019, 11, 1, 12, 10, offset)
Instant instant = timestamp.dateValue().toInstant // Instant is in UTC
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(offset))

raganhan avatar Nov 01 '19 19:11 raganhan

Thanks for a great answer! Sadly, I understand java's extended backwards compatibility approach.

What about an ion-java-lts fork of ion-java? :-)

cameronsstone avatar Nov 04 '19 19:11 cameronsstone

A fork is a way to get around this, but I suggest trying to add necessary extensions as an external package.

Using java.time as an example again you could have a util class or decorator to add the additional methods to Timestamp. The benefit of this approach is future interoperability with other packages that use ion-java such as ion-schema, jackson-dataformat-ion, etc.

raganhan avatar Nov 04 '19 21:11 raganhan

That's a good idea, though the natural endpoint of that approach is to make it a kotlin extension method, perhaps in an ion-kotlin package that wraps ion-java and adds functionality like this. :-)

Feel free to close this issue.

cameronsstone avatar Dec 05 '19 01:12 cameronsstone

I think it is worth to keep the issue open as we want to provide java.time apis when we can get rid of the jdk 1.5 requirement

raganhan avatar Dec 05 '19 03:12 raganhan