1// Copyright 2019 CUE Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package time
16
17import (
18 "encoding/json"
19 "strconv"
20 "testing"
21 "time"
22)
23
24func TestTimestamp(t *testing.T) {
25 // Valid go times (for JSON marshaling) are represented as is
26 validTimes := []string{
27 // valid Go times
28 "null",
29 `"2019-01-02T15:04:05Z"`,
30 `"2019-01-02T15:04:05-08:00"`,
31 `"2019-01-02T15:04:05.0-08:00"`,
32 `"2019-01-02T15:04:05.01-08:00"`,
33 `"2019-01-02T15:04:05.012345678-08:00"`,
34 `"2019-02-28T15:04:59Z"`,
35
36 // TODO: allow leap seconds? This is allowed by the RFC 3339 spec.
37 // `"2019-06-30T23:59:60Z"`, // leap seconds
38
39 // NOTE: Go 1.17 rejected the extra digits,
40 // and Go 1.18 started accepting them while discarding them.
41 // We want CUE to be consistent across Go versions,
42 // so we should probably fork Go's time package to behave exactly the
43 // way we want and in a consistent way across Go versions.
44 `"2019-01-02T15:04:05.01234567890-08:00"`,
45 }
46
47 for _, tc := range validTimes {
48 t.Run(tc, func(t *testing.T) {
49 // Test JSON unmarshaling
50 var tm time.Time
51
52 if err := json.Unmarshal([]byte(tc), &tm); err != nil {
53 t.Errorf("unmarshal JSON failed unexpectedly: %v", err)
54 }
55
56 if tc == "null" {
57 return
58 }
59 str, _ := strconv.Unquote(tc)
60
61 if b, err := Time(str); !b || err != nil {
62 t.Errorf("Time failed unexpectedly: %v", err)
63 }
64 if _, err := Parse(RFC3339Nano, str); err != nil {
65 t.Errorf("Parse failed unexpectedly")
66 }
67 })
68 }
69
70 invalidTimes := []string{
71 `"2019-01-02T15:04:05"`, // missing time zone
72 `"2019-01-02T15:04:61Z"`, // seconds out of range
73 `"2019-01-02T15:60:00Z"`, // minute out of range
74 `"2019-01-02T24:00:00Z"`, // hour out of range
75 `"2019-01-32T23:00:00Z"`, // day out of range
76 `"2019-01-00T23:00:00Z"`, // day out of range
77 `"2019-00-15T23:00:00Z"`, // month out of range
78 `"2019-13-15T23:00:00Z"`, // month out of range
79 `"2019-01-02T15:04:05Z+08:00"`, // double time zone
80 `"2019-01-02T15:04:05+08"`, // partial time zone
81 }
82
83 for _, tc := range invalidTimes {
84 t.Run(tc, func(t *testing.T) {
85 // Test JSON unmarshaling
86 var tm time.Time
87
88 if err := json.Unmarshal([]byte(tc), &tm); err == nil {
89 t.Errorf("unmarshal JSON succeeded unexpectedly: %v", err)
90 }
91
92 str, _ := strconv.Unquote(tc)
93
94 if _, err := Time(str); err == nil {
95 t.Errorf("CUE eval succeeded unexpectedly")
96 }
97
98 if _, err := Parse(RFC3339Nano, str); err == nil {
99 t.Errorf("CUE eval succeeded unexpectedly")
100 }
101 })
102 }
103}
104
105func TestUnix(t *testing.T) {
106 valid := []struct {
107 sec int64
108 nano int64
109 want string
110 }{
111 {0, 0, "1970-01-01T00:00:00Z"},
112 {1500000000, 123456, "2017-07-14T02:40:00.000123456Z"},
113 }
114
115 for _, tc := range valid {
116 t.Run(tc.want, func(t *testing.T) {
117 got := Unix(tc.sec, tc.nano)
118 if got != tc.want {
119 t.Errorf("got %v; want %s", got, tc.want)
120 }
121 })
122 }
123}