| 1 |
package 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 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"fmt" |
| 20 |
"reflect" |
| 21 |
"testing" |
| 22 |
"time" |
| 23 |
) |
| 24 |
|
| 25 |
func ExampleParseDate() { |
| 26 |
d, err := ParseDate("2001-02-03") |
| 27 |
if err != nil { |
| 28 |
fmt.Println(err) |
| 29 |
} |
| 30 |
fmt.Println(d) |
| 31 |
// Output: 2001-02-03 |
| 32 |
} |
| 33 |
|
| 34 |
func ExampleDate() { |
| 35 |
d, err := ParseDate("2001-02-03") |
| 36 |
if err != nil { |
| 37 |
fmt.Println(err) |
| 38 |
} |
| 39 |
|
| 40 |
t, err := time.Parse(time.RFC3339, "2001-02-04T00:00:00Z") |
| 41 |
if err != nil { |
| 42 |
fmt.Println(err) |
| 43 |
} |
| 44 |
|
| 45 |
// Date acts as time.Time when the receiver |
| 46 |
if d.Before(t) { |
| 47 |
fmt.Printf("Before ") |
| 48 |
} else { |
| 49 |
fmt.Printf("After ") |
| 50 |
} |
| 51 |
|
| 52 |
// Convert Date when needing a time.Time |
| 53 |
if t.After(d.ToTime()) { |
| 54 |
fmt.Printf("After") |
| 55 |
} else { |
| 56 |
fmt.Printf("Before") |
| 57 |
} |
| 58 |
// Output: Before After |
| 59 |
} |
| 60 |
|
| 61 |
func ExampleDate_MarshalBinary() { |
| 62 |
d, err := ParseDate("2001-02-03") |
| 63 |
if err != nil { |
| 64 |
fmt.Println(err) |
| 65 |
} |
| 66 |
t, err := d.MarshalBinary() |
| 67 |
if err != nil { |
| 68 |
fmt.Println(err) |
| 69 |
} |
| 70 |
fmt.Println(string(t)) |
| 71 |
// Output: 2001-02-03 |
| 72 |
} |
| 73 |
|
| 74 |
func ExampleDate_UnmarshalBinary() { |
| 75 |
d := Date{} |
| 76 |
t := "2001-02-03" |
| 77 |
|
| 78 |
if err := d.UnmarshalBinary([]byte(t)); err != nil { |
| 79 |
fmt.Println(err) |
| 80 |
} |
| 81 |
fmt.Println(d) |
| 82 |
// Output: 2001-02-03 |
| 83 |
} |
| 84 |
|
| 85 |
func ExampleDate_MarshalJSON() { |
| 86 |
d, err := ParseDate("2001-02-03") |
| 87 |
if err != nil { |
| 88 |
fmt.Println(err) |
| 89 |
} |
| 90 |
j, err := json.Marshal(d) |
| 91 |
if err != nil { |
| 92 |
fmt.Println(err) |
| 93 |
} |
| 94 |
fmt.Println(string(j)) |
| 95 |
// Output: "2001-02-03" |
| 96 |
} |
| 97 |
|
| 98 |
func ExampleDate_UnmarshalJSON() { |
| 99 |
var d struct { |
| 100 |
Date Date `json:"date"` |
| 101 |
} |
| 102 |
j := `{"date" : "2001-02-03"}` |
| 103 |
|
| 104 |
if err := json.Unmarshal([]byte(j), &d); err != nil { |
| 105 |
fmt.Println(err) |
| 106 |
} |
| 107 |
fmt.Println(d.Date) |
| 108 |
// Output: 2001-02-03 |
| 109 |
} |
| 110 |
|
| 111 |
func ExampleDate_MarshalText() { |
| 112 |
d, err := ParseDate("2001-02-03") |
| 113 |
if err != nil { |
| 114 |
fmt.Println(err) |
| 115 |
} |
| 116 |
t, err := d.MarshalText() |
| 117 |
if err != nil { |
| 118 |
fmt.Println(err) |
| 119 |
} |
| 120 |
fmt.Println(string(t)) |
| 121 |
// Output: 2001-02-03 |
| 122 |
} |
| 123 |
|
| 124 |
func ExampleDate_UnmarshalText() { |
| 125 |
d := Date{} |
| 126 |
t := "2001-02-03" |
| 127 |
|
| 128 |
if err := d.UnmarshalText([]byte(t)); err != nil { |
| 129 |
fmt.Println(err) |
| 130 |
} |
| 131 |
fmt.Println(d) |
| 132 |
// Output: 2001-02-03 |
| 133 |
} |
| 134 |
|
| 135 |
func TestDateString(t *testing.T) { |
| 136 |
d, err := ParseDate("2001-02-03") |
| 137 |
if err != nil { |
| 138 |
t.Fatalf("date: String failed (%v)", err) |
| 139 |
} |
| 140 |
if d.String() != "2001-02-03" { |
| 141 |
t.Fatalf("date: String failed (%v)", d.String()) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func TestDateBinaryRoundTrip(t *testing.T) { |
| 146 |
d1, err := ParseDate("2001-02-03") |
| 147 |
if err != nil { |
| 148 |
t.Fatalf("date: ParseDate failed (%v)", err) |
| 149 |
} |
| 150 |
t1, err := d1.MarshalBinary() |
| 151 |
if err != nil { |
| 152 |
t.Fatalf("date: MarshalBinary failed (%v)", err) |
| 153 |
} |
| 154 |
|
| 155 |
d2 := Date{} |
| 156 |
if err = d2.UnmarshalBinary(t1); err != nil { |
| 157 |
t.Fatalf("date: UnmarshalBinary failed (%v)", err) |
| 158 |
} |
| 159 |
|
| 160 |
if !reflect.DeepEqual(d1, d2) { |
| 161 |
t.Fatalf("date: Round-trip Binary failed (%v, %v)", d1, d2) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func TestDateJSONRoundTrip(t *testing.T) { |
| 166 |
type s struct { |
| 167 |
Date Date `json:"date"` |
| 168 |
} |
| 169 |
var err error |
| 170 |
d1 := s{} |
| 171 |
d1.Date, err = ParseDate("2001-02-03") |
| 172 |
if err != nil { |
| 173 |
t.Fatalf("date: ParseDate failed (%v)", err) |
| 174 |
} |
| 175 |
|
| 176 |
j, err := json.Marshal(d1) |
| 177 |
if err != nil { |
| 178 |
t.Fatalf("date: MarshalJSON failed (%v)", err) |
| 179 |
} |
| 180 |
|
| 181 |
d2 := s{} |
| 182 |
if err = json.Unmarshal(j, &d2); err != nil { |
| 183 |
t.Fatalf("date: UnmarshalJSON failed (%v)", err) |
| 184 |
} |
| 185 |
|
| 186 |
if !reflect.DeepEqual(d1, d2) { |
| 187 |
t.Fatalf("date: Round-trip JSON failed (%v, %v)", d1, d2) |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
func TestDateTextRoundTrip(t *testing.T) { |
| 192 |
d1, err := ParseDate("2001-02-03") |
| 193 |
if err != nil { |
| 194 |
t.Fatalf("date: ParseDate failed (%v)", err) |
| 195 |
} |
| 196 |
t1, err := d1.MarshalText() |
| 197 |
if err != nil { |
| 198 |
t.Fatalf("date: MarshalText failed (%v)", err) |
| 199 |
} |
| 200 |
d2 := Date{} |
| 201 |
if err = d2.UnmarshalText(t1); err != nil { |
| 202 |
t.Fatalf("date: 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 |
|
| 210 |
func TestDateToTime(t *testing.T) { |
| 211 |
var d Date |
| 212 |
d, err := ParseDate("2001-02-03") |
| 213 |
if err != nil { |
| 214 |
t.Fatalf("date: ParseDate failed (%v)", err) |
| 215 |
} |
| 216 |
var _ time.Time = d.ToTime() |
| 217 |
} |
| 218 |
|
| 219 |
func TestDateUnmarshalJSONReturnsError(t *testing.T) { |
| 220 |
var d struct { |
| 221 |
Date Date `json:"date"` |
| 222 |
} |
| 223 |
j := `{"date" : "February 3, 2001"}` |
| 224 |
|
| 225 |
if err := json.Unmarshal([]byte(j), &d); err == nil { |
| 226 |
t.Fatal("date: Date failed to return error for malformed JSON date") |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
func TestDateUnmarshalTextReturnsError(t *testing.T) { |
| 231 |
d := Date{} |
| 232 |
txt := "February 3, 2001" |
| 233 |
|
| 234 |
if err := d.UnmarshalText([]byte(txt)); err == nil { |
| 235 |
t.Fatal("date: Date failed to return error for malformed Text date") |
| 236 |
} |
| 237 |
} |