cquiroz / scala-java-time

Implementation of the `java.time` API in scala. Especially useful for scala.js
http://cquiroz.github.io/scala-java-time/
BSD 3-Clause "New" or "Revised" License
126 stars 33 forks source link

ZoneRulesException without timezone #257

Open marcadella opened 3 years ago

marcadella commented 3 years ago

The following code works in scala: LocalDateTime.now(). But in Scalajs it throws: java.time.zone.ZoneRulesException: Unknown time-zone ID: Europe/Oslo. I do not intend to use any timezone. Scalajs version: 1.5.0 scala-java-time version: 2.1.0

marcadella commented 3 years ago

I guess it is because, contrary to what the doc says, the default system timezone is used by default instead of UTC. If it is not what the doc mean, then it should be fixed to specify that one need to use UTC explicitly. Ex: LocalDateTime.now(ZoneId.of("UTC)) works in ScalaJs.

cquiroz commented 3 years ago

I think you're right. it will try to guess the local timezone, it is better to set a default one. PRs to the documentation are welcome

OndrejSpanel commented 1 year ago

I came to this issue because I started to get this exception after upgrading from udash-0.8.0 to udash-0.9.0.

Udash used https://github.com/zoepepper/scalajs-jsjoda in previous versions. I am not sure what its implementation of LocalDateTime.now was, but it did not throw any exceptions. Most likely it just calls ZoneId.systemDefault() from JS Joda.

OndrejSpanel commented 1 year ago

it is better to set a default one.

When I am using this in a frontend (browser), what way can I use to set the default timezone to something reasonable? I do not want users to have to tell me their timezone, there surely must by some simpler way.

cquiroz commented 1 year ago

The code tries to guess the timezone but it is not always successful https://github.com/cquiroz/scala-java-time/blob/master/core/js/src/main/scala/java/util/TimeZone.scala#L28

I'd suggest you try to verify what is being selected with that piece of code.