~ubuntu-branches/ubuntu/utopic/gccgo-go/utopic

« back to all changes in this revision

Viewing changes to src/pkg/time/genzabbrs.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-27 09:18:55 UTC
  • Revision ID: package-import@ubuntu.com-20140127091855-zxfshmykfsyyw4b2
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
5
// +build ignore
 
6
 
 
7
//
 
8
// usage:
 
9
//
 
10
// go run genzabbrs.go | gofmt > $GOROOT/src/pkg/time/zoneinfo_abbrs_windows.go
 
11
//
 
12
 
 
13
package main
 
14
 
 
15
import (
 
16
        "encoding/xml"
 
17
        "io/ioutil"
 
18
        "log"
 
19
        "net/http"
 
20
        "os"
 
21
        "sort"
 
22
        "text/template"
 
23
        "time"
 
24
)
 
25
 
 
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
 
37
                        }
 
38
                        return abbr1, abbr2
 
39
                }
 
40
        }
 
41
        return abbr1, abbr1
 
42
}
 
43
 
 
44
type zone struct {
 
45
        WinName  string
 
46
        UnixName string
 
47
        StTime   string
 
48
        DSTime   string
 
49
}
 
50
 
 
51
type zones []*zone
 
52
 
 
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 }
 
56
 
 
57
const wzURL = "http://unicode.org/cldr/data/common/supplemental/windowsZones.xml"
 
58
 
 
59
type MapZone struct {
 
60
        Other     string `xml:"other,attr"`
 
61
        Territory string `xml:"territory,attr"`
 
62
        Type      string `xml:"type,attr"`
 
63
}
 
64
 
 
65
type SupplementalData struct {
 
66
        Zones []MapZone `xml:"windowsZones>mapTimezones>mapZone"`
 
67
}
 
68
 
 
69
func readWindowsZones() (zones, error) {
 
70
        r, err := http.Get(wzURL)
 
71
        if err != nil {
 
72
                return nil, err
 
73
        }
 
74
        defer r.Body.Close()
 
75
 
 
76
        data, err := ioutil.ReadAll(r.Body)
 
77
        if err != nil {
 
78
                return nil, err
 
79
        }
 
80
 
 
81
        var sd SupplementalData
 
82
        err = xml.Unmarshal(data, &sd)
 
83
        if err != nil {
 
84
                return nil, err
 
85
        }
 
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
 
91
                }
 
92
                l, err := time.LoadLocation(z.Type)
 
93
                if err != nil {
 
94
                        return nil, err
 
95
                }
 
96
                st, dt := getAbbrs(l)
 
97
                zs = append(zs, &zone{
 
98
                        WinName:  z.Other,
 
99
                        UnixName: z.Type,
 
100
                        StTime:   st,
 
101
                        DSTime:   dt,
 
102
                })
 
103
        }
 
104
        return zs, nil
 
105
}
 
106
 
 
107
func main() {
 
108
        zs, err := readWindowsZones()
 
109
        if err != nil {
 
110
                log.Fatal(err)
 
111
        }
 
112
        sort.Sort(zs)
 
113
        var v = struct {
 
114
                URL string
 
115
                Zs  zones
 
116
        }{
 
117
                wzURL,
 
118
                zs,
 
119
        }
 
120
        err = template.Must(template.New("prog").Parse(prog)).Execute(os.Stdout, v)
 
121
        if err != nil {
 
122
                log.Fatal(err)
 
123
        }
 
124
}
 
125
 
 
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.
 
130
 
 
131
// generated by genzabbrs.go from
 
132
// {{.URL}}
 
133
 
 
134
package time
 
135
 
 
136
type abbr struct {
 
137
        std string
 
138
        dst string
 
139
}
 
140
 
 
141
var abbrs = map[string]abbr{
 
142
{{range .Zs}}   "{{.WinName}}": {"{{.StTime}}", "{{.DSTime}}"}, // {{.UnixName}}
 
143
{{end}}}
 
144
 
 
145
`