-
Notifications
You must be signed in to change notification settings - Fork 1
/
timestamp_test.go
59 lines (54 loc) · 1.23 KB
/
timestamp_test.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
package timestamp
import (
"encoding/json"
"strconv"
"testing"
"time"
)
func TestNewTimestamp(t *testing.T) {
ts := time.Now()
times := ToTimestamp(ts)
ta := times.ToTime()
var resolution = time.Millisecond * 1
diff := ts.Sub(ta)
if diff < 0 {
diff *= -1
}
if diff > resolution {
t.Logf("%s != %s", ts.String(), ta.String())
t.Fail()
}
t.Logf("%d", times.GetTimeUnixMs())
}
func TestTimestampJSON(t *testing.T) {
exampleVal := "2009-11-10T23:00:00Z"
ts, _ := time.Parse(time.RFC3339, exampleVal)
times := ToTimestamp(ts)
dat, err := json.Marshal(×)
if err != nil {
t.Fatal(err.Error())
}
datStr := string(dat)
if datStr != strconv.Quote(exampleVal) {
t.FailNow()
}
// test parsing the unix ms wrapped in a string
ms := times.GetTimeUnixMs()
msStr := strconv.FormatUint(ms, 10)
datStr = strconv.Quote(msStr)
parseTimestamp := &Timestamp{}
if err := json.Unmarshal([]byte(datStr), parseTimestamp); err != nil {
t.Fatal(err.Error())
}
if !parseTimestamp.EqualVT(times) {
t.FailNow()
}
// test parsing the unix ms as a number
parseTimestamp = &Timestamp{}
if err := json.Unmarshal([]byte(msStr), parseTimestamp); err != nil {
t.Fatal(err.Error())
}
if !parseTimestamp.EqualVT(times) {
t.FailNow()
}
}