-
Notifications
You must be signed in to change notification settings - Fork 102
/
generate_tznames.go
63 lines (57 loc) · 1.23 KB
/
generate_tznames.go
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
//go:build never
// +build never
// Copyright 2021 The Godror Authors
package main
import (
"archive/zip"
"bytes"
"context"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"time"
)
func main() {
if err := Main(); err != nil {
log.Fatalf("%+v", err)
}
}
func Main() error {
flagPackage := flag.String("pkg", "godror", "package name to use")
flagOut := flag.String("o", "", "output file name")
flag.Parse()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
b, err := exec.CommandContext(ctx, "go", "env", "GOROOT").Output()
if err != nil {
return err
}
fn := filepath.Join(string(bytes.TrimSpace(b)), "lib", "time", "zoneinfo.zip")
log.Printf("go env GOROOT: %s -> fn=%q", b, fn)
zr, err := zip.OpenReader(fn)
if err != nil {
return err
}
defer zr.Close()
var buf bytes.Buffer
buf.WriteString(`// Generated by generate_tznames.go. DO NOT EDIT
package ` + *flagPackage + `
func init() {
tzNames = []string{
`)
for _, f := range zr.File {
if f.Mode().IsDir() {
continue
}
fmt.Fprintf(&buf, "\t\t%q,\n", f.Name)
}
buf.WriteString("\t}\n}\n")
if *flagOut == "" || *flagOut == "-" {
fmt.Println(buf.String())
return nil
}
return os.WriteFile(*flagOut, buf.Bytes(), 0640)
}