github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/time/zoneinfo_js.go (about)

     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  // +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  	} else {
    37  		z.name += "+"
    38  	}
    39  	z.name += itoa(offset / 60)
    40  	min := offset % 60
    41  	if min != 0 {
    42  		z.name += ":" + itoa(min)
    43  	}
    44  	localLoc.zone = []zone{z}
    45  }
    46  
    47  // itoa is just the fast path implementation copied from strconv.Itoa.
    48  // No timezone hour can exceed 100, so the fast path will always satisfy.
    49  func itoa(i int) string {
    50  	if i < 10 {
    51  		return digits[i : i+1]
    52  	}
    53  	return smallsString[i*2 : i*2+2]
    54  }
    55  
    56  const smallsString = "00010203040506070809" +
    57  	"10111213141516171819" +
    58  	"20212223242526272829" +
    59  	"30313233343536373839" +
    60  	"40414243444546474849" +
    61  	"50515253545556575859" +
    62  	"60616263646566676869" +
    63  	"70717273747576777879" +
    64  	"80818283848586878889" +
    65  	"90919293949596979899"
    66  const digits = "0123456789"