1 package ber
2
3 import (
4 "testing"
5 "time"
6 )
7
8 func TestParseGeneralizedTime(t *testing.T) {
9 for _, test := range []struct {
10 str string
11 wanted time.Time
12 err error
13 }{
14 {
15 "20170222190527Z",
16 time.Date(2017, time.Month(2), 22, 19, 5, 27, 0, time.UTC),
17 nil,
18 },
19 {
20 "201702221905Z",
21 time.Date(2017, time.Month(2), 22, 19, 5, 0, 0, time.UTC),
22 nil,
23 },
24 {
25 "2017022219Z",
26 time.Date(2017, time.Month(2), 22, 19, 0, 0, 0, time.UTC),
27 nil,
28 },
29 {
30 "2017022219.25Z",
31 time.Date(2017, time.Month(2), 22, 19, 15, 0, 0, time.UTC),
32 nil,
33 },
34 {
35 "201702221905.25Z",
36 time.Date(2017, time.Month(2), 22, 19, 5, 15, 0, time.UTC),
37 nil,
38 },
39 {
40 "20170222190525-0100",
41 time.Date(2017, time.Month(2), 22, 19, 5, 25, 0, time.FixedZone("", -3600)),
42 nil,
43 },
44 {
45 "20170222190525+0100",
46 time.Date(2017, time.Month(2), 22, 19, 5, 25, 0, time.FixedZone("", 3600)),
47 nil,
48 },
49 {
50 "20170222190525+01",
51 time.Date(2017, time.Month(2), 22, 19, 5, 25, 0, time.FixedZone("", 3600)),
52 nil,
53 },
54 {
55 "20170222190527.123Z",
56 time.Date(2017, time.Month(2), 22, 19, 5, 27, 123*1000*1000, time.UTC),
57 nil,
58 },
59 {
60 "20170222190527,123Z",
61 time.Date(2017, time.Month(2), 22, 19, 5, 27, 123*1000*1000, time.UTC),
62 nil,
63 },
64 {
65 "2017022219-0100",
66 time.Date(2017, time.Month(2), 22, 19, 0, 0, 0, time.FixedZone("", -3600)),
67 nil,
68 },
69 } {
70 genTime, err := ParseGeneralizedTime([]byte(test.str))
71 if err != nil {
72 if test.err != nil {
73 if err != test.err {
74 t.Errorf("unexpected error in %s: %s", test.str, err)
75 }
76 } else {
77 t.Errorf("test %s failed with error: %s", test.str, err)
78 }
79 } else {
80 if !genTime.Equal(test.wanted) {
81 t.Errorf("test got unexpected result: wanted=%s, got=%s", test.wanted, genTime)
82 }
83 }
84 }
85 }
86
View as plain text