1package date 2 3// Copyright 2017 Microsoft Corporation 4// 5// Licensed under the Apache License, Version 2.0 (the "License"); 6// you may not use this file except in compliance with the License. 7// You may obtain a copy of the License at 8// 9// http://www.apache.org/licenses/LICENSE-2.0 10// 11// Unless required by applicable law or agreed to in writing, software 12// distributed under the License is distributed on an "AS IS" BASIS, 13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14// See the License for the specific language governing permissions and 15// limitations under the License. 16 17import ( 18 "encoding/json" 19 "fmt" 20 "reflect" 21 "testing" 22 "time" 23) 24 25func ExampleParseTime() { 26 d, _ := ParseTime(rfc3339, "2001-02-03T04:05:06Z") 27 fmt.Println(d) 28 // Output: 2001-02-03 04:05:06 +0000 UTC 29} 30 31func ExampleTime_MarshalBinary() { 32 ti, err := ParseTime(rfc3339, "2001-02-03T04:05:06Z") 33 if err != nil { 34 fmt.Println(err) 35 } 36 d := Time{ti} 37 t, err := d.MarshalBinary() 38 if err != nil { 39 fmt.Println(err) 40 } 41 fmt.Println(string(t)) 42 // Output: 2001-02-03T04:05:06Z 43} 44 45func ExampleTime_UnmarshalBinary() { 46 d := Time{} 47 t := "2001-02-03T04:05:06Z" 48 49 if err := d.UnmarshalBinary([]byte(t)); err != nil { 50 fmt.Println(err) 51 } 52 fmt.Println(d) 53 // Output: 2001-02-03T04:05:06Z 54} 55 56func ExampleTime_MarshalJSON() { 57 d, err := ParseTime(rfc3339, "2001-02-03T04:05:06Z") 58 if err != nil { 59 fmt.Println(err) 60 } 61 j, err := json.Marshal(d) 62 if err != nil { 63 fmt.Println(err) 64 } 65 fmt.Println(string(j)) 66 // Output: "2001-02-03T04:05:06Z" 67} 68 69func ExampleTime_UnmarshalJSON() { 70 var d struct { 71 Time Time `json:"datetime"` 72 } 73 j := `{"datetime" : "2001-02-03T04:05:06Z"}` 74 75 if err := json.Unmarshal([]byte(j), &d); err != nil { 76 fmt.Println(err) 77 } 78 fmt.Println(d.Time) 79 // Output: 2001-02-03T04:05:06Z 80} 81 82func ExampleTime_MarshalText() { 83 d, err := ParseTime(rfc3339, "2001-02-03T04:05:06Z") 84 if err != nil { 85 fmt.Println(err) 86 } 87 t, err := d.MarshalText() 88 if err != nil { 89 fmt.Println(err) 90 } 91 fmt.Println(string(t)) 92 // Output: 2001-02-03T04:05:06Z 93} 94 95func ExampleTime_UnmarshalText() { 96 d := Time{} 97 t := "2001-02-03T04:05:06Z" 98 99 if err := d.UnmarshalText([]byte(t)); err != nil { 100 fmt.Println(err) 101 } 102 fmt.Println(d) 103 // Output: 2001-02-03T04:05:06Z 104} 105 106func TestUnmarshalTextforInvalidDate(t *testing.T) { 107 d := Time{} 108 dt := "2001-02-03T04:05:06AAA" 109 110 if err := d.UnmarshalText([]byte(dt)); err == nil { 111 t.Fatalf("date: Time#Unmarshal was expecting error for invalid date") 112 } 113} 114 115func TestUnmarshalJSONforInvalidDate(t *testing.T) { 116 d := Time{} 117 dt := `"2001-02-03T04:05:06AAA"` 118 119 if err := d.UnmarshalJSON([]byte(dt)); err == nil { 120 t.Fatalf("date: Time#Unmarshal was expecting error for invalid date") 121 } 122} 123 124func TestTimeString(t *testing.T) { 125 ti, err := ParseTime(rfc3339, "2001-02-03T04:05:06Z") 126 if err != nil { 127 fmt.Println(err) 128 } 129 d := Time{ti} 130 if d.String() != "2001-02-03T04:05:06Z" { 131 t.Fatalf("date: Time#String failed (%v)", d.String()) 132 } 133} 134 135func TestTimeStringReturnsEmptyStringForError(t *testing.T) { 136 d := Time{Time: time.Date(20000, 01, 01, 01, 01, 01, 01, time.UTC)} 137 if d.String() != "" { 138 t.Fatalf("date: Time#String failed empty string for an error") 139 } 140} 141 142func TestTimeBinaryRoundTrip(t *testing.T) { 143 ti, err := ParseTime(rfc3339, "2001-02-03T04:05:06Z") 144 if err != nil { 145 t.Fatalf("date: Time#ParseTime failed (%v)", err) 146 } 147 d1 := Time{ti} 148 t1, err := d1.MarshalBinary() 149 if err != nil { 150 t.Fatalf("date: Time#MarshalBinary failed (%v)", err) 151 } 152 153 d2 := Time{} 154 if err = d2.UnmarshalBinary(t1); err != nil { 155 t.Fatalf("date: Time#UnmarshalBinary failed (%v)", err) 156 } 157 158 if !reflect.DeepEqual(d1, d2) { 159 t.Fatalf("date:Round-trip Binary failed (%v, %v)", d1, d2) 160 } 161} 162 163func TestTimeJSONRoundTrip(t *testing.T) { 164 type s struct { 165 Time Time `json:"datetime"` 166 } 167 168 ti, err := ParseTime(rfc3339, "2001-02-03T04:05:06Z") 169 if err != nil { 170 t.Fatalf("date: Time#ParseTime failed (%v)", err) 171 } 172 173 d1 := s{Time: Time{ti}} 174 j, err := json.Marshal(d1) 175 if err != nil { 176 t.Fatalf("date: Time#MarshalJSON failed (%v)", err) 177 } 178 179 d2 := s{} 180 if err = json.Unmarshal(j, &d2); err != nil { 181 t.Fatalf("date: Time#UnmarshalJSON failed (%v)", err) 182 } 183 184 if !reflect.DeepEqual(d1, d2) { 185 t.Fatalf("date: Round-trip JSON failed (%v, %v)", d1, d2) 186 } 187} 188 189func TestTimeTextRoundTrip(t *testing.T) { 190 ti, err := ParseTime(rfc3339, "2001-02-03T04:05:06Z") 191 if err != nil { 192 t.Fatalf("date: Time#ParseTime failed (%v)", err) 193 } 194 d1 := Time{Time: ti} 195 t1, err := d1.MarshalText() 196 if err != nil { 197 t.Fatalf("date: Time#MarshalText failed (%v)", err) 198 } 199 200 d2 := Time{} 201 if err = d2.UnmarshalText(t1); err != nil { 202 t.Fatalf("date: Time#UnmarshalText failed (%v)", err) 203 } 204 205 if !reflect.DeepEqual(d1, d2) { 206 t.Fatalf("date: Round-trip Text failed (%v, %v)", d1, d2) 207 } 208} 209 210func TestTimeToTime(t *testing.T) { 211 ti, err := ParseTime(rfc3339, "2001-02-03T04:05:06Z") 212 d := Time{ti} 213 if err != nil { 214 t.Fatalf("date: Time#ParseTime failed (%v)", err) 215 } 216 var _ time.Time = d.ToTime() 217} 218 219func TestUnmarshalJSONNoOffset(t *testing.T) { 220 var d struct { 221 Time Time `json:"datetime"` 222 } 223 j := `{"datetime" : "2001-02-03T04:05:06.789"}` 224 225 if err := json.Unmarshal([]byte(j), &d); err != nil { 226 t.Fatalf("date: Time#Unmarshal failed (%v)", err) 227 } 228} 229 230func TestUnmarshalJSONPosOffset(t *testing.T) { 231 var d struct { 232 Time Time `json:"datetime"` 233 } 234 j := `{"datetime" : "1980-01-02T00:11:35.01+01:00"}` 235 236 if err := json.Unmarshal([]byte(j), &d); err != nil { 237 t.Fatalf("date: Time#Unmarshal failed (%v)", err) 238 } 239} 240 241func TestUnmarshalJSONNegOffset(t *testing.T) { 242 var d struct { 243 Time Time `json:"datetime"` 244 } 245 j := `{"datetime" : "1492-10-12T10:15:01.789-08:00"}` 246 247 if err := json.Unmarshal([]byte(j), &d); err != nil { 248 t.Fatalf("date: Time#Unmarshal failed (%v)", err) 249 } 250} 251 252func TestUnmarshalTextNoOffset(t *testing.T) { 253 d := Time{} 254 t1 := "2001-02-03T04:05:06" 255 256 if err := d.UnmarshalText([]byte(t1)); err != nil { 257 t.Fatalf("date: Time#UnmarshalText failed (%v)", err) 258 } 259} 260 261func TestUnmarshalTextPosOffset(t *testing.T) { 262 d := Time{} 263 t1 := "2001-02-03T04:05:06+00:30" 264 265 if err := d.UnmarshalText([]byte(t1)); err != nil { 266 t.Fatalf("date: Time#UnmarshalText failed (%v)", err) 267 } 268} 269 270func TestUnmarshalTextNegOffset(t *testing.T) { 271 d := Time{} 272 t1 := "2001-02-03T04:05:06-11:00" 273 274 if err := d.UnmarshalText([]byte(t1)); err != nil { 275 t.Fatalf("date: Time#UnmarshalText failed (%v)", err) 276 } 277} 278