2014-04-11 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / libgo / go / time / genzabbrs.go
blob7c637cb43a7faa6b5ce118c1f9a352f4842430fa
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.
5 // +build ignore
7 //
8 // usage:
9 //
10 // go run genzabbrs.go | gofmt > $GOROOT/src/pkg/time/zoneinfo_abbrs_windows.go
13 package main
15 import (
16 "encoding/xml"
17 "io/ioutil"
18 "log"
19 "net/http"
20 "os"
21 "sort"
22 "text/template"
23 "time"
26 // getAbbrs finds timezone abbreviations (standard and daylight saving time)
27 // for location l.
28 func getAbbrs(l *time.Location) (st, dt string) {
29 t := time.Date(time.Now().Year(), 0, 0, 0, 0, 0, 0, l)
30 abbr1, off1 := t.Zone()
31 for i := 0; i < 12; i++ {
32 t = t.AddDate(0, 1, 0)
33 abbr2, off2 := t.Zone()
34 if abbr1 != abbr2 {
35 if off2-off1 < 0 { // southern hemisphere
36 abbr1, abbr2 = abbr2, abbr1
38 return abbr1, abbr2
41 return abbr1, abbr1
44 type zone struct {
45 WinName string
46 UnixName string
47 StTime string
48 DSTime string
51 type zones []*zone
53 func (zs zones) Len() int { return len(zs) }
54 func (zs zones) Swap(i, j int) { zs[i], zs[j] = zs[j], zs[i] }
55 func (zs zones) Less(i, j int) bool { return zs[i].UnixName < zs[j].UnixName }
57 const wzURL = "http://unicode.org/cldr/data/common/supplemental/windowsZones.xml"
59 type MapZone struct {
60 Other string `xml:"other,attr"`
61 Territory string `xml:"territory,attr"`
62 Type string `xml:"type,attr"`
65 type SupplementalData struct {
66 Zones []MapZone `xml:"windowsZones>mapTimezones>mapZone"`
69 func readWindowsZones() (zones, error) {
70 r, err := http.Get(wzURL)
71 if err != nil {
72 return nil, err
74 defer r.Body.Close()
76 data, err := ioutil.ReadAll(r.Body)
77 if err != nil {
78 return nil, err
81 var sd SupplementalData
82 err = xml.Unmarshal(data, &sd)
83 if err != nil {
84 return nil, err
86 zs := make(zones, 0)
87 for _, z := range sd.Zones {
88 if z.Territory != "001" {
89 // to avoid dups. I don't know why.
90 continue
92 l, err := time.LoadLocation(z.Type)
93 if err != nil {
94 return nil, err
96 st, dt := getAbbrs(l)
97 zs = append(zs, &zone{
98 WinName: z.Other,
99 UnixName: z.Type,
100 StTime: st,
101 DSTime: dt,
104 return zs, nil
107 func main() {
108 zs, err := readWindowsZones()
109 if err != nil {
110 log.Fatal(err)
112 sort.Sort(zs)
113 var v = struct {
114 URL string
115 Zs zones
117 wzURL,
120 err = template.Must(template.New("prog").Parse(prog)).Execute(os.Stdout, v)
121 if err != nil {
122 log.Fatal(err)
126 const prog = `
127 // Copyright 2013 The Go Authors. All rights reserved.
128 // Use of this source code is governed by a BSD-style
129 // license that can be found in the LICENSE file.
131 // generated by genzabbrs.go from
132 // {{.URL}}
134 package time
136 type abbr struct {
137 std string
138 dst string
141 var abbrs = map[string]abbr{
142 {{range .Zs}} "{{.WinName}}": {"{{.StTime}}", "{{.DSTime}}"}, // {{.UnixName}}
143 {{end}}}