diff --git a/offsets.go b/offsets.go index 0004f58..d9901c7 100644 --- a/offsets.go +++ b/offsets.go @@ -6,7 +6,7 @@ package tai64 -// http://maia.usno.navy.mil/ser7/tai-utc.dat +// https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat // http://www.stjarnhimlen.se/comp/time.html var tia64nDifferences = []struct { // TAI time diff --git a/tools/generate.go b/tools/generate.go index 4ee6a78..bfea995 100644 --- a/tools/generate.go +++ b/tools/generate.go @@ -6,8 +6,10 @@ package main import ( "bufio" + "bytes" "flag" "fmt" + "go/format" "html/template" "log" "net/http" @@ -27,7 +29,7 @@ const tplText = ` package {{.Pkg}} -// http://maia.usno.navy.mil/ser7/tai-utc.dat +// https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat // http://www.stjarnhimlen.se/comp/time.html var tia64nDifferences = []struct { // TAI time @@ -57,10 +59,11 @@ type entry struct { Tts int64 } +const datURL = "https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat" + func main() { - var output, input, pkg string + var output, pkg string flag.StringVar(&output, "output", "", "output file") - flag.StringVar(&input, "input", "", "input file") flag.StringVar(&pkg, "pkg", "", "package name") flag.Parse() @@ -73,24 +76,14 @@ func main() { } var br *bufio.Reader - if input == "" { - input = "http://maia.usno.navy.mil/ser7/tai-utc.dat" - resp, err := http.Get(input) - if err != nil { - log.Fatalf("Error fetching tai-utc.dat: %s", err) - } - defer resp.Body.Close() - br = bufio.NewReader(resp.Body) - } else { - f, err := os.Open(input) - if err != nil { - log.Fatalf("Cant open file: %s", err) - } - defer f.Close() - br = bufio.NewReader(f) + resp, err := http.Get(datURL) + if err != nil { + log.Fatalf("Error fetching '%s': %s", datURL, err) } + defer resp.Body.Close() + br = bufio.NewReader(resp.Body) - fmt.Printf("Generating %s based on %s\n", path.Base(output), path.Base(input)) + fmt.Printf("Generating '%s' based on '%s'\n", path.Base(output), datURL) t, err := template.New("fileTemplate").Parse(strings.TrimLeft(tplText, "\n")) if err != nil { @@ -106,12 +99,13 @@ func main() { } line = strings.TrimSpace(line) - if line == "" || strings.HasPrefix(line, ";") { + if line == "" || strings.HasPrefix(line, ";") || strings.HasPrefix(line, "#") { continue } parts := strings.Fields(line) - t, err := time.Parse("2006-1-2", strings.Title(strings.Join(parts[0:3], "-"))) + // parse date of leap second + t, err := time.Parse("2-1-2006", strings.Join(parts[1:4], "-")) if err != nil { fmt.Println(err) continue @@ -121,7 +115,8 @@ func main() { continue } - s, err := strconv.ParseFloat(parts[3], 32) + // parse TAI-UTC(s) + s, err := strconv.ParseFloat(parts[4], 32) if err != nil { fmt.Println(err) continue @@ -133,19 +128,21 @@ func main() { entries = append(entries, e) } - w, err := os.Create(output) + bufr := &bytes.Buffer{} + + srcfile := &srcFile{pkg, entries} + err = t.Execute(bufr, srcfile) if err != nil { log.Fatal(err) } - defer w.Close() - - writer := bufio.NewWriter(w) - defer writer.Flush() - srcfile := &srcFile{pkg, entries} - err = t.Execute(writer, srcfile) + outBytes, err := format.Source(bufr.Bytes()) if err != nil { log.Fatal(err) } + err = os.WriteFile(output, outBytes, 0644) + if err != nil { + log.Fatal(err) + } } diff --git a/tools/update-leaps.sh b/tools/update-leaps.sh deleted file mode 100755 index c6f1d66..0000000 --- a/tools/update-leaps.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash - -# sometimes sites go offline, so list a few options. -# just use one. -{ - #curl -s 'http://maia.usno.navy.mil/ser7/tai-utc.dat' - #curl -s 'https://cdf.gsfc.nasa.gov/html/CDFLeapSeconds.txt' - #curl -s 'https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat'| grep -v '^#'| awk '{print $4" "$3" "$2" "$5}' -} > leaps.dat - -go run ./tools/generate.go -input leaps.dat -output offsets.go -pkg tai64 -go fmt offsets.go