-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtidbtb.go
79 lines (66 loc) · 1.4 KB
/
tidbtb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"flag"
"fmt"
"io"
"os"
"strconv"
"time"
"github.com/pingcap/tidb/pkg/parser"
"github.com/tikv/client-go/v2/oracle"
"github.com/zncoder/check"
"github.com/zncoder/mygo"
)
type OpList struct{}
func (OpList) Digest() {
digest()
}
func digest() {
mygo.ParseFlag("[sql]")
var sql string
if flag.NArg() == 0 {
b := check.V(io.ReadAll(os.Stdin)).F("read from stdin")
sql = string(b)
} else {
sql = flag.Arg(0)
}
normSQL, digest := parser.NormalizeDigest(sql)
fmt.Printf("digest: %s\nnormalized: %s", digest, normSQL)
}
func (OpList) TSO() {
tso()
}
func isDigit(s string) bool {
for _, c := range s {
if c < '0' || c > '9' {
return false
}
}
return true
}
const timestampFormat = "2006-01-02 15:04:05 -0700 MST"
func tso() {
mygo.ParseFlag("[tso|timestamp...]")
if flag.NArg() == 0 {
tso := oracle.GoTimeToTS(time.Now())
fmt.Println(tso)
} else {
for i, s := range flag.Args() {
if isDigit(s) {
tso := check.V(strconv.ParseUint(s, 10, 64)).F("parse tso")
t := oracle.GetTimeFromTS(tso)
if i != 0 {
fmt.Println("----")
}
fmt.Printf("%d\n%d.%d\n%v\n%v\n", tso, t.Unix(), t.Nanosecond()/1e3, t.UTC(), t.Local())
} else {
t := check.V(time.Parse(timestampFormat, s)).F("parse time", "format", timestampFormat, "value", s)
tso := oracle.GoTimeToTS(t)
fmt.Println(tso)
}
}
}
}
func main() {
mygo.RunOpMapCmd[OpList]()
}