zoneinfo_js.go 1.6 KB

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