tzdata_embedded.mx raw

   1  // The embedded timezone database is opt-in: importing time/tzdata runs its
   2  // init, which registers its loader with package time through a linkname
   3  // (time.initRegisterLoadFromEmbeddedTZData). Nothing in the tree imported it,
   4  // so the registration never ran in any build and the embedded data was dead
   5  // weight.
   6  //
   7  // This asserts a zone name and offset from the loaded database. On a host with
   8  // /usr/share/zoneinfo the NATIVE run is answered by the system database, so a
   9  // native-only test would pass even with the registration broken. In the browser
  10  // there is no zoneinfo at all and _platformZoneSources() points at paths that do
  11  // not exist, so the wasm run is answered ONLY by the embedded loader - which is
  12  // why this fixture is compared native-against-wasm by phase 5.
  13  package main
  14  
  15  import (
  16  	"time"
  17  
  18  	_ "time/tzdata"
  19  )
  20  
  21  func main() {
  22  	// Termination: a batch program must still be stoppable, so the entry
  23  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  24  	select {
  25  	case <-shutdown:
  26  		return
  27  	default:
  28  	}
  29  
  30  	loc, err := time.LoadLocation("Asia/Tokyo")
  31  	if err != nil {
  32  		panic("LoadLocation(Asia/Tokyo): " | err.Error())
  33  	}
  34  	// 2024-01-01T00:00:00Z is 09:00 in Tokyo: JST, UTC+9, no DST.
  35  	t := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC())
  36  	name, off := t.In(loc).Zone()
  37  	if name != "JST" || off != 9*60*60 {
  38  		panic("Asia/Tokyo zone is wrong: " | name)
  39  	}
  40  	// A second zone, and one with a negative offset, so the loader runs twice.
  41  	ny, err := time.LoadLocation("America/New_York")
  42  	if err != nil {
  43  		panic("LoadLocation(America/New_York): " | err.Error())
  44  	}
  45  	n2, o2 := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC()).In(ny).Zone()
  46  	if n2 != "EST" || o2 != -5*60*60 {
  47  		panic("America/New_York zone is wrong: " | n2)
  48  	}
  49  	// A southern-hemisphere zone that is in DST on this date, so the offset is
  50  	// not the zone's standard one.
  51  	syd, err := time.LoadLocation("Australia/Sydney")
  52  	if err != nil {
  53  		panic("LoadLocation(Australia/Sydney): " | err.Error())
  54  	}
  55  	n3, o3 := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC()).In(syd).Zone()
  56  	if n3 != "AEDT" || o3 != 11*60*60 {
  57  		panic("Australia/Sydney zone is wrong: " | n3)
  58  	}
  59  	// Print, so the native/wasm comparison in phase 5 has something to compare.
  60  	// A registration that did not run would panic above in the browser, where
  61  	// there is no zoneinfo to fall back to. The builtin Stringers are behind the
  62  	// moxie.stringer build tag, which the wasm target does not set, so the zone
  63  	// names are printed rather than the numeric offsets.
  64  	println("tokyo=" | name)
  65  	println("newyork=" | n2)
  66  	println("sydney=" | n3)
  67  	println("tzdata registration ok")
  68  }
  69