Skip to content

Add implicit conversions from java.time classes to plotly LocalDateTime #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

tmccarthy
Copy link
Contributor

This PR adds implicit conversions from the java.time classes to plotly.element.LocalDateTime. These conversions are similar to those provided for Joda time in plotly.Joda. They are included only in the JVM artefact.

Comment on lines 21 to 26
implicit def fromJavaOffsetDateTime(javaOffsetDateTime: OffsetDateTime): PlotlyLocalDateTime =
fromJavaLocalDateTime(javaOffsetDateTime.toLocalDateTime)

implicit def fromJavaZonedDateTime(javaZonedDateTime: ZonedDateTime): PlotlyLocalDateTime =
fromJavaLocalDateTime(javaZonedDateTime.toLocalDateTime)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth noting that there are some potential gotchas here around converting zoned datetimes caused by dropping the zone component. For example, converting a list of datetimes with differing zones/offsets may lead to unexpected results. I'm happy to add a Scaladoc warning if needed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to mention it in a scaladoc comment if you wish, yes.

Copy link
Owner

@alexarchambault alexarchambault left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tmccarthy!

Maybe the non-ambiguous conversions (from LocalDateTime, Instant, and LocalDate, I guess) could be added directly in the companion of plotly.element.LocalDateTime, so that users don't need the plotly.JavaTime._ import.

Comment on lines 21 to 26
implicit def fromJavaOffsetDateTime(javaOffsetDateTime: OffsetDateTime): PlotlyLocalDateTime =
fromJavaLocalDateTime(javaOffsetDateTime.toLocalDateTime)

implicit def fromJavaZonedDateTime(javaZonedDateTime: ZonedDateTime): PlotlyLocalDateTime =
fromJavaLocalDateTime(javaZonedDateTime.toLocalDateTime)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to mention it in a scaladoc comment if you wish, yes.

@tmccarthy
Copy link
Contributor Author

Thanks @tmccarthy!

Maybe the non-ambiguous conversions (from LocalDateTime, Instant, and LocalDate, I guess) could be added directly in the companion of plotly.element.LocalDateTime, so that users don't need the plotly.JavaTime._ import.

Sounds good. I avoided this because it takes a bit of work to get around the lack of java.time classes in ScalaJS, but I will find a better solution.

@alexarchambault
Copy link
Owner

alexarchambault commented Apr 19, 2020

Oh, I had missed that JavaTime.scala was under jvm/. It can be done by adding a PlatformLocalDateTimeCompanion trait for example, which would be empty under js/, and have some conversion under jvm/, and mixed into the companion of plotly.element.LocalDateTime under shared/. I can do it if you wish.

@tmccarthy
Copy link
Contributor Author

Nah I can do it!

… companion, and split out potentially-unsafe conversions so that they require an explicit import.
package plotly.element

trait PlotlyJavaTimeConversions {
// Empty since the java.time classes are not available in ScalaJS
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth noting that another solution here would be to add a dependency on scala-java-time from the core-js project. Let me know if you'd like to do this.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's fine this way, let's not add an extra dependency…

* It will generally be safer to convert your data to `java.time.LocalDateTime` in the appropriate timezone/offset,
* and then use the `PlotlyJavaTimeConversions.fromJavaLocalDateTime` implicit conversion.
*/
object UnsafeImplicitConversions {
Copy link
Contributor Author

@tmccarthy tmccarthy Apr 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These implicit conversions now need to be explicitly imported using something like:

import plotly.element.LocalDateTime.UnsafeImplicitConversions._

I like this approach, although I am open to feedback on the UnsafeImplicitConversions name.

@alexarchambault
Copy link
Owner

That looks great, merging!

@alexarchambault alexarchambault merged commit 8c4727e into alexarchambault:master Apr 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants