-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse_test.go
49 lines (43 loc) · 1.41 KB
/
parse_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
package timeutils
import (
"os"
"testing"
"time"
)
var testData = []struct{ in, out string }{
{"2014-12-05 09:51:20.939152 -0500", "2014-12-05 14:51:20.939152 +0000 UTC"},
{"2014-12-05 09:51:20.939152 -0500 EST", "2014-12-05 14:51:20.939152 +0000 UTC"},
{"2014-12-05 09:51:20.939152", "2014-12-05 09:51:20.939152 +0000 UTC"},
{"2014/12/05 09:51:20.939152", "2014-12-05 09:51:20.939152 +0000 UTC"},
{"2014.12.05 09:51:20.939152", "2014-12-05 09:51:20.939152 +0000 UTC"},
{"09:51:20.939152 2014-31-12", "2014-12-31 09:51:20.939152 +0000 UTC"},
{"09:51:20.939152am 2014-31-12", "2014-12-31 09:51:20.939152 +0000 UTC"},
{"09:51:20.939152pm 2014-31-12", "2014-12-31 21:51:20.939152 +0000 UTC"},
}
func TestParseDateString(t *testing.T) {
defer os.Setenv("TZ", os.Getenv("TZ"))
os.Setenv("TZ", "UTC")
for i, elem := range testData {
if tt, err := ParseDateString(elem.in); err != nil {
t.Error(err)
} else if elem.out != tt.String() {
t.Errorf("[%d] Unexpected parsed time.\nExpect:\t%s\nGot:\t%s\n", i, elem.out, tt)
}
}
}
func BenchmarkParseDateString(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err := ParseDateString("2014-12-05 09:51:20.939152 -0500")
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkGoTimeParse(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err := time.Parse("2006-01-02 15:04:05.999999 -0700", "2014-12-05 09:51:20.939152 -0500")
if err != nil {
b.Fatal(err)
}
}
}