]> Cypherpunks.ru repositories - gostls13.git/blob - src/time/tzdata/generate_zipdata.go
io: simplify Examples
[gostls13.git] / src / time / tzdata / generate_zipdata.go
1 // Copyright 2020 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 // This program generates zipdata.go from $GOROOT/lib/time/zoneinfo.zip.
8 package main
9
10 import (
11         "bufio"
12         "fmt"
13         "io/ioutil"
14         "os"
15         "path/filepath"
16         "runtime"
17         "strconv"
18 )
19
20 const header = `// Copyright 2020 The Go Authors. All rights reserved.
21 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
23
24 // Code generated by generate_zipdata. DO NOT EDIT.
25
26 // This file contains an embedded zip archive that contains time zone
27 // files compiled using the code and data maintained as part of the
28 // IANA Time Zone Database.
29 // The IANA asserts that the data is in the public domain.
30
31 // For more information, see
32 // https://www.iana.org/time-zones
33 // ftp://ftp.iana.org/tz/code/tz-link.htm
34 // http://tools.ietf.org/html/rfc6557
35
36 package tzdata
37
38 const zipdata = `
39
40 func main() {
41         // We should be run in the $GOROOT/src/time/tzdata directory.
42         data, err := ioutil.ReadFile("../../../lib/time/zoneinfo.zip")
43         if err != nil {
44                 if os.IsNotExist(err) {
45                         // For convenience try $GOROOT as a backup.
46                         data, err = ioutil.ReadFile(filepath.Join(runtime.GOROOT(), "lib", "time", "zoneinfo.zip"))
47                 }
48                 if err != nil {
49                         die("cannot find zoneinfo.zip file: %v", err)
50                 }
51         }
52
53         of, err := os.Create("zipdata.go")
54         if err != nil {
55                 die("%v", err)
56         }
57
58         buf := bufio.NewWriter(of)
59         buf.WriteString(header)
60
61         ds := string(data)
62         i := 0
63         const chunk = 60
64         for ; i+chunk < len(data); i += chunk {
65                 if i > 0 {
66                         buf.WriteRune('\t')
67                 }
68                 fmt.Fprintf(buf, "%s +\n", strconv.Quote(ds[i:i+chunk]))
69         }
70         fmt.Fprintf(buf, "\t%s\n", strconv.Quote(ds[i:]))
71
72         if err := buf.Flush(); err != nil {
73                 die("error writing to zipdata.go: %v", err)
74         }
75         if err := of.Close(); err != nil {
76                 die("error closing zipdata.go: %v", err)
77         }
78 }
79
80 func die(format string, args ...interface{}) {
81         fmt.Fprintf(os.Stderr, format+"\n", args...)
82         os.Exit(1)
83 }