summaryrefslogtreecommitdiffstats
path: root/src/time/tzdata/generate_zipdata.go
blob: 653577cafaf1403934ec72e538727df7515f1beb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build ignore

// This program generates zipdata.go from $GOROOT/lib/time/zoneinfo.zip.
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

// header is put at the start of the generated file.
// The string addition avoids this file (generate_zipdata.go) from
// matching the "generated file" regexp.
const header = `// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

` + `// Code generated by generate_zipdata. DO NOT EDIT.

// This file contains an embedded zip archive that contains time zone
// files compiled using the code and data maintained as part of the
// IANA Time Zone Database.
// The IANA asserts that the data is in the public domain.

// For more information, see
// https://www.iana.org/time-zones
// ftp://ftp.iana.org/tz/code/tz-link.html
// https://datatracker.ietf.org/doc/html/rfc6557

package tzdata

const zipdata = `

func main() {
	// We should be run in the $GOROOT/src/time/tzdata directory.
	data, err := os.ReadFile("../../../lib/time/zoneinfo.zip")
	if err != nil {
		die("cannot find zoneinfo.zip file: %v", err)
	}

	of, err := os.Create("zipdata.go")
	if err != nil {
		die("%v", err)
	}

	buf := bufio.NewWriter(of)
	buf.WriteString(header)

	ds := string(data)
	i := 0
	const chunk = 60
	for ; i+chunk < len(data); i += chunk {
		if i > 0 {
			buf.WriteRune('\t')
		}
		fmt.Fprintf(buf, "%s +\n", strconv.Quote(ds[i:i+chunk]))
	}
	fmt.Fprintf(buf, "\t%s\n", strconv.Quote(ds[i:]))

	if err := buf.Flush(); err != nil {
		die("error writing to zipdata.go: %v", err)
	}
	if err := of.Close(); err != nil {
		die("error closing zipdata.go: %v", err)
	}
}

func die(format string, args ...any) {
	fmt.Fprintf(os.Stderr, format+"\n", args...)
	os.Exit(1)
}