Skip to content

Default ZoneId #41

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

Merged
merged 2 commits into from
Apr 8, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions js/src/main/scala/java/util/TimeZone.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,49 @@ import org.threeten.bp.ZoneId

import scala.util.Try

import scala.scalajs.js
import js.annotation._

@js.native
trait DateTimeFormatOptions extends js.Object {
val timeZone: js.UndefOr[String]
}

@js.native
@JSGlobal("Intl.DateTimeFormat")
class DateTimeFormat() extends js.Object {
def resolvedOptions(): DateTimeFormatOptions = js.native
}

object TimeZone {
final val SHORT = 0
final val LONG = 1

private var default: TimeZone = {
Try {
// This is supported since EcmaScript 1
def offsetInMillis: Int = {
val browserDate = new scalajs.js.Date()
val offsetInMillis = browserDate.getTimezoneOffset() * 60 * 1000
val id = browserDate.toTimeString().split(' ')(1).takeWhile(e => e != '+' && e != '-')
browserDate.getTimezoneOffset() * 60 * 1000
}

new SimpleTimeZone(offsetInMillis, id)
} getOrElse {
new SimpleTimeZone(0, "UTC")
def timeZone: String = {
def browserTZ: Try[String] = {
Try {
val browserDate = new scalajs.js.Date()
browserDate.toTimeString().split(' ')(1).takeWhile(e => e != ' ')
}
}

Try {
// First try with the intl API
new DateTimeFormat().resolvedOptions().timeZone.getOrElse(browserTZ.getOrElse("UTC"))
}.orElse {
// If it fails try to parse it from the date string
browserTZ
}.getOrElse("UTC") // Fallback to UTC
}

new SimpleTimeZone(offsetInMillis, timeZone)
}

def getDefault: TimeZone = default
Expand Down