github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/compiler/natives/src/time/zoneinfo_js.go (about)

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