Black Lives Matter. Support the Equal Justice Initiative.

Source file src/time/zoneinfo_js.go

Documentation: time

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

View as plain text