github.com/goplusjs/gopherjs@v1.2.6-0.20211206034512-f187917453b8/compiler/natives/src/time/zoneinfo.go (about)

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