| 1 |
// Copyright 2018 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build js && wasm |
| 6 |
|
| 7 |
package time |
| 8 |
|
| 9 |
import ( |
| 10 |
"runtime" |
| 11 |
"syscall/js" |
| 12 |
) |
| 13 |
|
| 14 |
var zoneSources = []string{ |
| 15 |
"/usr/share/zoneinfo/", |
| 16 |
"/usr/share/lib/zoneinfo/", |
| 17 |
"/usr/lib/locale/TZ/", |
| 18 |
runtime.GOROOT() + "/lib/time/zoneinfo.zip", |
| 19 |
} |
| 20 |
|
| 21 |
func initLocal() { |
| 22 |
localLoc.name = "Local" |
| 23 |
|
| 24 |
z := zone{} |
| 25 |
d := js.Global().Get("Date").New() |
| 26 |
offset := d.Call("getTimezoneOffset").Int() * -1 |
| 27 |
z.offset = offset * 60 |
| 28 |
// According to https://tc39.github.io/ecma262/#sec-timezoneestring, |
| 29 |
// the timezone name from (new Date()).toTimeString() is an implementation-dependent |
| 30 |
// result, and in Google Chrome, it gives the fully expanded name rather than |
| 31 |
// the abbreviation. |
| 32 |
// Hence, we construct the name from the offset. |
| 33 |
z.name = "UTC" |
| 34 |
if offset < 0 { |
| 35 |
z.name += "-" |
| 36 |
offset *= -1 |
| 37 |
} else { |
| 38 |
z.name += "+" |
| 39 |
} |
| 40 |
z.name += itoa(offset / 60) |
| 41 |
min := offset % 60 |
| 42 |
if min != 0 { |
| 43 |
z.name += ":" + itoa(min) |
| 44 |
} |
| 45 |
localLoc.zone = []zone{z} |
| 46 |
} |
| 47 |
|
| 48 |
// itoa is like strconv.Itoa but only works for values of i in range [0,99]. |
| 49 |
// It panics if i is out of range. |
| 50 |
func itoa(i int) string { |
| 51 |
if i < 10 { |
| 52 |
return digits[i : i+1] |
| 53 |
} |
| 54 |
return smallsString[i*2 : i*2+2] |
| 55 |
} |
| 56 |
|
| 57 |
const smallsString = "00010203040506070809" + |
| 58 |
"10111213141516171819" + |
| 59 |
"20212223242526272829" + |
| 60 |
"30313233343536373839" + |
| 61 |
"40414243444546474849" + |
| 62 |
"50515253545556575859" + |
| 63 |
"60616263646566676869" + |
| 64 |
"70717273747576777879" + |
| 65 |
"80818283848586878889" + |
| 66 |
"90919293949596979899" |
| 67 |
const digits = "0123456789" |