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