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