-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetime_test.go
66 lines (58 loc) · 1.92 KB
/
datetime_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
package datetime
import (
"testing"
"time"
)
func TestPrecision(t *testing.T) {
type test struct {
input string
want Precision
}
tests := []test{
{input: "1997", want: PrecisionYear},
{input: "1997-07", want: PrecisionMonth},
{input: "1997-07-16", want: PrecisionDay},
{input: "1997-07-16T19:20+01:00", want: PrecisionHour},
{input: "1997-07-16T19:20:30+01:00", want: PrecisionSecond},
{input: "1997-07-16T19:20:30.45+01:00", want: PrecisionNanosecond},
{input: "invalid", want: PrecisionUnknown},
}
for _, tc := range tests {
have := ParsePrecision(tc.input)
if have != tc.want {
t.Fatalf("for input %q, expected: %s, got: %s", tc.input, tc.want, have)
}
}
}
func TestTime(t *testing.T) {
type test struct {
input string
want time.Time
}
loc := time.FixedZone("+0100", 60*60)
tests := []test{
{input: "1997", want: time.Date(1997, 1, 1, 0, 0, 0, 0, time.UTC)},
{input: "1997-07", want: time.Date(1997, 7, 1, 0, 0, 0, 0, time.UTC)},
{input: "1997-07-16", want: time.Date(1997, 7, 16, 0, 0, 0, 0, time.UTC)},
{input: "1997-07-16T19:20+01:00", want: time.Date(1997, 7, 16, 19, 20, 0, 0, loc)},
{input: "1997-07-16T19:20:30+01:00", want: time.Date(1997, 7, 16, 19, 20, 30, 0, loc)},
{input: "1997-07-16T19:20:30.45+01:00", want: time.Date(1997, 7, 16, 19, 20, 30, 450000000, loc)},
{input: "1994-11-05T08:15:30Z", want: time.Date(1994, 11, 5, 8, 15, 30, 0, time.UTC)},
}
for _, tc := range tests {
var dt Time
if err := dt.UnmarshalText([]byte(tc.input)); err != nil {
t.Fatalf("(datetime.Time).UnmarshalText failed for %q: %s", tc.input, err)
}
if !tc.want.Equal(dt.Time) {
t.Fatalf("for input %q, expected: %s, got: %s", tc.input, tc.want, dt.Time)
}
text, err := dt.MarshalText()
if err != nil {
t.Fatalf("(datetime.Time).MarshalText failed for %q: %s", tc.input, err)
}
if tc.input != string(text) {
t.Fatalf("expected: %s, got: %s", tc.input, string(text))
}
}
}