genzabbrs.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2013 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. // +build ignore
  5. //
  6. // usage:
  7. //
  8. // go run genzabbrs.go -output zoneinfo_abbrs_windows.go
  9. //
  10. package main
  11. import (
  12. "bytes"
  13. "encoding/xml"
  14. "flag"
  15. "go/format"
  16. "io/ioutil"
  17. "log"
  18. "net/http"
  19. "sort"
  20. "text/template"
  21. "time"
  22. )
  23. var filename = flag.String("output", "zoneinfo_abbrs_windows.go", "output file name")
  24. // getAbbrs finds timezone abbreviations (standard and daylight saving time)
  25. // for location l.
  26. func getAbbrs(l *time.Location) (st, dt string) {
  27. t := time.Date(time.Now().Year(), 0, 0, 0, 0, 0, 0, l)
  28. abbr1, off1 := t.Zone()
  29. for i := 0; i < 12; i++ {
  30. t = t.AddDate(0, 1, 0)
  31. abbr2, off2 := t.Zone()
  32. if abbr1 != abbr2 {
  33. if off2-off1 < 0 { // southern hemisphere
  34. abbr1, abbr2 = abbr2, abbr1
  35. }
  36. return abbr1, abbr2
  37. }
  38. }
  39. return abbr1, abbr1
  40. }
  41. type zone struct {
  42. WinName string
  43. UnixName string
  44. StTime string
  45. DSTime string
  46. }
  47. type zones []*zone
  48. func (zs zones) Len() int { return len(zs) }
  49. func (zs zones) Swap(i, j int) { zs[i], zs[j] = zs[j], zs[i] }
  50. func (zs zones) Less(i, j int) bool { return zs[i].UnixName < zs[j].UnixName }
  51. const wzURL = "http://unicode.org/cldr/data/common/supplemental/windowsZones.xml"
  52. type MapZone struct {
  53. Other string `xml:"other,attr"`
  54. Territory string `xml:"territory,attr"`
  55. Type string `xml:"type,attr"`
  56. }
  57. type SupplementalData struct {
  58. Zones []MapZone `xml:"windowsZones>mapTimezones>mapZone"`
  59. }
  60. func readWindowsZones() (zones, error) {
  61. r, err := http.Get(wzURL)
  62. if err != nil {
  63. return nil, err
  64. }
  65. defer r.Body.Close()
  66. data, err := ioutil.ReadAll(r.Body)
  67. if err != nil {
  68. return nil, err
  69. }
  70. var sd SupplementalData
  71. err = xml.Unmarshal(data, &sd)
  72. if err != nil {
  73. return nil, err
  74. }
  75. zs := make(zones, 0)
  76. for _, z := range sd.Zones {
  77. if z.Territory != "001" {
  78. // to avoid dups. I don't know why.
  79. continue
  80. }
  81. l, err := time.LoadLocation(z.Type)
  82. if err != nil {
  83. return nil, err
  84. }
  85. st, dt := getAbbrs(l)
  86. zs = append(zs, &zone{
  87. WinName: z.Other,
  88. UnixName: z.Type,
  89. StTime: st,
  90. DSTime: dt,
  91. })
  92. }
  93. return zs, nil
  94. }
  95. func main() {
  96. flag.Parse()
  97. zs, err := readWindowsZones()
  98. if err != nil {
  99. log.Fatal(err)
  100. }
  101. sort.Sort(zs)
  102. var v = struct {
  103. URL string
  104. Zs zones
  105. }{
  106. wzURL,
  107. zs,
  108. }
  109. var buf bytes.Buffer
  110. err = template.Must(template.New("prog").Parse(prog)).Execute(&buf, v)
  111. if err != nil {
  112. log.Fatal(err)
  113. }
  114. data, err := format.Source(buf.Bytes())
  115. if err != nil {
  116. log.Fatal(err)
  117. }
  118. err = ioutil.WriteFile(*filename, data, 0644)
  119. if err != nil {
  120. log.Fatal(err)
  121. }
  122. }
  123. const prog = `
  124. // Copyright 2013 The Go Authors. All rights reserved.
  125. // Use of this source code is governed by a BSD-style
  126. // license that can be found in the LICENSE file.
  127. // generated by genzabbrs.go from
  128. // {{.URL}}
  129. package time
  130. type abbr struct {
  131. std string
  132. dst string
  133. }
  134. var abbrs = map[string]abbr{
  135. {{range .Zs}} "{{.WinName}}": {"{{.StTime}}", "{{.DSTime}}"}, // {{.UnixName}}
  136. {{end}}}
  137. `