-
Notifications
You must be signed in to change notification settings - Fork 1
/
tstodate.go
50 lines (44 loc) · 880 Bytes
/
tstodate.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"time"
)
const (
TimeFormat = "2006-01-02T15:04:05.999999999 MST (-0700)"
)
func main() {
for _, arg := range os.Args[1:] {
var t time.Time
if strings.Index(arg, ".") >= 0 {
v, err := strconv.ParseFloat(arg, 64)
if err != nil {
panic(err)
}
nanos := int64(v * float64(time.Second/time.Nanosecond))
t = time.Unix(0, nanos)
} else {
v, err := strconv.ParseInt(arg, 10, 64)
if err != nil {
panic(err)
}
switch len(arg) {
case 13:
// must be milliseconds
t = time.Unix(0, v*1e6)
case 16:
// must be microseconds
t = time.Unix(0, v*1e3)
case 19:
// must be nanoseconds
t = time.Unix(0, v)
default:
// assume it is seconds
t = time.Unix(v, 0)
}
}
fmt.Println(t.Format(TimeFormat), " | ", t.In(time.UTC).Format(TimeFormat))
}
}