-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.go
More file actions
66 lines (55 loc) · 1.51 KB
/
time.go
File metadata and controls
66 lines (55 loc) · 1.51 KB
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
package main
import (
"fmt"
"time"
"os"
"bufio"
)
func main() {
// const t1Str = "2016-01-13T17:00:00.000000-05:00"
// t1, err := time.Parse(time.RFC3339, t1Str)
// See this to understand why using EST below we
// actually get UTC time:
// https://code.google.com/p/go/issues/detail?id=3604
const t1Str = "Tue Jan 13 17:00:00 EST 2016"
t1, err := time.Parse(time.UnixDate, t1Str)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("t1", t1, t1.Location().String())
fmt.Println("t1", t1.Local())
const t2Str = "2016-01-13T10:30:00.000000-08:00"
// t2, err := time.Parse(time.RFC3339, t2Str)
t2 := time.Now()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("t2", t2)
fmt.Println("t2-t1", t2.Sub(t1))
const xStreamTimeStr = "Tue Jan 12 10:30:32 EST 2016"
tXstream, err := time.Parse(time.UnixDate, xStreamTimeStr)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("xStream time:", tXstream)
tNow := time.Now()
fmt.Println("Current time:", tNow)
delta := tNow.Sub(tXstream)
fmt.Println("Now-xStreamTime =", delta)
fmt.Println("syslog time mapped to real time:", tNow.Add(delta))
var text string
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter syslog time: ")
text, _ = reader.ReadString('\n')
text = text[:len(text)-1] // chop off \n
t, err := time.Parse(time.RFC3339, text)
t = t.Local()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("syslog time mapped to real time:", t.Add(delta))
}