| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"bufio" |
| 9 |
"bytes" |
| 10 |
"flag" |
| 11 |
"fmt" |
| 12 |
"go/format" |
| 13 |
"html/template" |
| 14 |
"log" |
| 15 |
"net/http" |
| 16 |
"os" |
| 17 |
"path" |
| 18 |
"strconv" |
| 19 |
"strings" |
| 20 |
"time" |
| 21 |
) |
| 22 |
|
| 23 |
const tplText = ` |
| 24 |
// Copyright (c) 2012-2016 Eli Janssen |
| 25 |
// Use of this source code is governed by an MIT-style |
| 26 |
// license that can be found in the LICENSE file. |
| 27 |
|
| 28 |
// THIS FILE IS AUTOGENERATED. DO NOT EDIT! |
| 29 |
|
| 30 |
package {{.Pkg}} |
| 31 |
|
| 32 |
// https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat |
| 33 |
// http://www.stjarnhimlen.se/comp/time.html |
| 34 |
var tia64nDifferences = []struct { |
| 35 |
// TAI time |
| 36 |
ttime int64 |
| 37 |
// TAI-UTC offset |
| 38 |
offset int64 |
| 39 |
// unix UTC time |
| 40 |
utime int64 |
| 41 |
}{ |
| 42 |
{{- range .Entries}} |
| 43 |
{{"{"}}0x{{.Tts|printf "%x"}}, {{.Drift}}, {{.Ts}}{{"}"}}, // {{.TsD}} |
| 44 |
{{- end}} |
| 45 |
} |
| 46 |
|
| 47 |
var tia64nSize = len(tia64nDifferences) |
| 48 |
` |
| 49 |
|
| 50 |
type srcFile struct { |
| 51 |
Pkg string |
| 52 |
Entries []entry |
| 53 |
} |
| 54 |
|
| 55 |
type entry struct { |
| 56 |
Ts int64 |
| 57 |
TsD string |
| 58 |
Drift int64 |
| 59 |
Tts int64 |
| 60 |
} |
| 61 |
|
| 62 |
const datURL = "https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat" |
| 63 |
|
| 64 |
func main() { |
| 65 |
var output, pkg string |
| 66 |
flag.StringVar(&output, "output", "", "output file") |
| 67 |
flag.StringVar(&pkg, "pkg", "", "package name") |
| 68 |
flag.Parse() |
| 69 |
|
| 70 |
if output == "" { |
| 71 |
log.Fatal("Output option is required") |
| 72 |
} |
| 73 |
|
| 74 |
if pkg == "" { |
| 75 |
log.Fatal("Package option is required") |
| 76 |
} |
| 77 |
|
| 78 |
var br *bufio.Reader |
| 79 |
resp, err := http.Get(datURL) |
| 80 |
if err != nil { |
| 81 |
log.Fatalf("Error fetching '%s': %s", datURL, err) |
| 82 |
} |
| 83 |
defer resp.Body.Close() |
| 84 |
br = bufio.NewReader(resp.Body) |
| 85 |
|
| 86 |
fmt.Printf("Generating '%s' based on '%s'\n", path.Base(output), datURL) |
| 87 |
|
| 88 |
t, err := template.New("fileTemplate").Parse(strings.TrimLeft(tplText, "\n")) |
| 89 |
if err != nil { |
| 90 |
log.Fatal(err) |
| 91 |
} |
| 92 |
|
| 93 |
entries := make([]entry, 0) |
| 94 |
|
| 95 |
for { |
| 96 |
line, err := br.ReadString('\n') |
| 97 |
if err != nil { |
| 98 |
break |
| 99 |
} |
| 100 |
|
| 101 |
line = strings.TrimSpace(line) |
| 102 |
if line == "" || strings.HasPrefix(line, ";") || strings.HasPrefix(line, "#") { |
| 103 |
continue |
| 104 |
} |
| 105 |
|
| 106 |
parts := strings.Fields(line) |
| 107 |
|
| 108 |
t, err := time.Parse("2-1-2006", strings.Join(parts[1:4], "-")) |
| 109 |
if err != nil { |
| 110 |
fmt.Println(err) |
| 111 |
continue |
| 112 |
} |
| 113 |
|
| 114 |
if t.Before(time.Date(1972, 1, 1, 0, 0, 0, 0, time.UTC)) { |
| 115 |
continue |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
s, err := strconv.ParseFloat(parts[4], 32) |
| 120 |
if err != nil { |
| 121 |
fmt.Println(err) |
| 122 |
continue |
| 123 |
} |
| 124 |
|
| 125 |
unixT := t.Unix() |
| 126 |
taiT := (2 << 61) + unixT + int64(s) |
| 127 |
e := entry{unixT, t.Format(time.RFC3339), int64(s), taiT} |
| 128 |
entries = append(entries, e) |
| 129 |
} |
| 130 |
|
| 131 |
bufr := &bytes.Buffer{} |
| 132 |
|
| 133 |
srcfile := &srcFile{pkg, entries} |
| 134 |
err = t.Execute(bufr, srcfile) |
| 135 |
if err != nil { |
| 136 |
log.Fatal(err) |
| 137 |
} |
| 138 |
|
| 139 |
outBytes, err := format.Source(bufr.Bytes()) |
| 140 |
if err != nil { |
| 141 |
log.Fatal(err) |
| 142 |
} |
| 143 |
|
| 144 |
err = os.WriteFile(output, outBytes, 0644) |
| 145 |
if err != nil { |
| 146 |
log.Fatal(err) |
| 147 |
} |
| 148 |
} |