Source file 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  //go: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  	"os"
    14  	"strconv"
    15  )
    16  
    17  // header is put at the start of the generated file.
    18  // The string addition avoids this file (generate_zipdata.go) from
    19  // matching the "generated file" regexp.
    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.html
    34  // https://datatracker.ietf.org/doc/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 := os.ReadFile("../../../lib/time/zoneinfo.zip")
    43  	if err != nil {
    44  		die("cannot find zoneinfo.zip file: %v", err)
    45  	}
    46  
    47  	of, err := os.Create("zipdata.go")
    48  	if err != nil {
    49  		die("%v", err)
    50  	}
    51  
    52  	buf := bufio.NewWriter(of)
    53  	buf.WriteString(header)
    54  
    55  	ds := string(data)
    56  	i := 0
    57  	const chunk = 60
    58  	for ; i+chunk < len(data); i += chunk {
    59  		if i > 0 {
    60  			buf.WriteRune('\t')
    61  		}
    62  		fmt.Fprintf(buf, "%s +\n", strconv.Quote(ds[i:i+chunk]))
    63  	}
    64  	fmt.Fprintf(buf, "\t%s\n", strconv.Quote(ds[i:]))
    65  
    66  	if err := buf.Flush(); err != nil {
    67  		die("error writing to zipdata.go: %v", err)
    68  	}
    69  	if err := of.Close(); err != nil {
    70  		die("error closing zipdata.go: %v", err)
    71  	}
    72  }
    73  
    74  func die(format string, args ...any) {
    75  	fmt.Fprintf(os.Stderr, format+"\n", args...)
    76  	os.Exit(1)
    77  }
    78  

View as plain text