-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_test.go
76 lines (60 loc) · 1.62 KB
/
time_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"regexp"
"testing"
)
func TestObtainSystemTime(t *testing.T) {
availableFormats := map[string]*regexp.Regexp{
"ansic": regexp.MustCompile(
`\w{2,3} \w{3} \_?\d{1,2} \d{2}:\d{2}:\d{2} \d{4}`,
),
"unix_date": regexp.MustCompile(
`\w{2,3} \w{3} \_?\d{1,2} \d{2}:\d{2}:\d{2} [\-|\+]?\d{2} \d{4}`,
),
"ruby_date": regexp.MustCompile(
`\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2} [\-|\+]?\d{1,4} \d{4}`,
),
"rfc822": regexp.MustCompile(
`\d{2} \w{3} \d{2} \d{2}:\d{2} [\-|\+]?\d{2}`,
),
"rfc822z": regexp.MustCompile(
`\d{2} \w{3} \d{2} \d{2}:\d{2} [\-|\+]?\d{1,4}`,
),
"rfc850": regexp.MustCompile(
`\w{6,9}, \d{2}-\w{3}-\d{2} \d{2}:\d{2}:\d{2} [\-|\+]?\d{1,4}`,
),
"rfc1123": regexp.MustCompile(
`\w{3}, \d{2} \w{3} \d{4} \d{2}:\d{2}:\d{2} [\-|\+]?\d{2}`,
),
"rfc1123z": regexp.MustCompile(
`\w{3}, \d{2} \w{3} \d{4} \d{2}:\d{2}:\d{2} [\-|\+]?\d{1,4}`,
),
"rfc3339": regexp.MustCompile(
`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[\-|\+]?\d{2}:\d{2}`,
),
"rfc3339nano": regexp.MustCompile(
`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}[\-|\+]?\d{2}:\d{2}`,
),
"empty": regexp.MustCompile(
`\d{10}`,
),
}
for format, matchPattern := range availableFormats {
t.Logf("testing format %s\n", format)
systemTime, err := obtainSystemTime(format)
if err != nil {
t.Errorf(
"unexpected error: %s\n", err.Error(),
)
}
matchedString := matchPattern.FindString(systemTime)
if matchedString != systemTime {
t.Errorf(
"testing failed for format %s: got value: %s, expected: %s\n",
format,
matchedString,
systemTime,
)
}
}
}