]> Cypherpunks.ru repositories - gotai64n.git/blob - cmd/tai64nlocal/main.go
Unify copyright comment format
[gotai64n.git] / cmd / tai64nlocal / main.go
1 // go.cypherpunks.ru/tai64n -- Pure Go TAI64/TAI64N implementation
2 // Copyright (C) 2020-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "bufio"
20         "flag"
21         "fmt"
22         "log"
23         "os"
24         "strings"
25         "time"
26
27         "go.cypherpunks.ru/tai64n/v2"
28 )
29
30 func main() {
31         log.SetFlags(0)
32         flag.Usage = func() {
33                 fmt.Fprintf(
34                         flag.CommandLine.Output(),
35                         "Replace \"@HEX(TAI64)\"-prefixed line with human readable UTC.\n",
36                 )
37                 flag.PrintDefaults()
38         }
39         leapsecs := flag.Bool("leapsecs", false, "Take leap seconds into account: honest TAI->UTC")
40         db := flag.String("leapsecsdb", "", "Use that leapsecs.dat leap seconds database")
41         flag.Parse()
42
43         if *db != "" {
44                 buf, err := os.ReadFile(*db)
45                 if err != nil {
46                         log.Fatalln(err)
47                 }
48                 tai64n.LeapsecsDBLoad(buf)
49         }
50
51         scanner := bufio.NewScanner(os.Stdin)
52         var err error
53         var s string
54         var sep int
55         var t time.Time
56         for {
57                 if !scanner.Scan() {
58                         if err = scanner.Err(); err != nil {
59                                 log.Fatalln(err)
60                         }
61                         break
62                 }
63                 s = scanner.Text()
64                 if s[0] != '@' {
65                         os.Stdout.WriteString(s + "\n")
66                         continue
67                 }
68                 sep = strings.IndexByte(s, byte(' '))
69                 if sep == -1 {
70                         os.Stdout.WriteString(s + "\n")
71                         continue
72                 }
73                 t, err = tai64n.Decode(s[1:sep])
74                 if err != nil {
75                         log.Fatalln(err)
76                 }
77                 if *leapsecs {
78                         t = tai64n.LeapsecsSub(t)
79                 }
80                 os.Stdout.WriteString(t.Format(tai64n.LocalFmt) + s[sep:] + "\n")
81         }
82 }