| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package strfmt |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"time" |
| 20 |
) |
| 21 |
|
| 22 |
func init() { |
| 23 |
d := Date{} |
| 24 |
|
| 25 |
Default.Add("date", &d, IsDate) |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
func IsDate(str string) bool { |
| 30 |
_, err := time.Parse(RFC3339FullDate, str) |
| 31 |
return err == nil |
| 32 |
} |
| 33 |
|
| 34 |
const ( |
| 35 |
|
| 36 |
|
| 37 |
RFC3339FullDate = "2006-01-02" |
| 38 |
) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
type Date time.Time |
| 44 |
|
| 45 |
|
| 46 |
func (d Date) String() string { |
| 47 |
return time.Time(d).Format(RFC3339FullDate) |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
func (d *Date) UnmarshalText(text []byte) error { |
| 52 |
if len(text) == 0 { |
| 53 |
return nil |
| 54 |
} |
| 55 |
dd, err := time.Parse(RFC3339FullDate, string(text)) |
| 56 |
if err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
*d = Date(dd) |
| 60 |
return nil |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
func (d Date) MarshalText() ([]byte, error) { |
| 65 |
return []byte(d.String()), nil |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
func (d Date) MarshalJSON() ([]byte, error) { |
| 70 |
return json.Marshal(time.Time(d).Format(RFC3339FullDate)) |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
func (d *Date) UnmarshalJSON(data []byte) error { |
| 75 |
if string(data) == jsonNull { |
| 76 |
return nil |
| 77 |
} |
| 78 |
var strdate string |
| 79 |
if err := json.Unmarshal(data, &strdate); err != nil { |
| 80 |
return err |
| 81 |
} |
| 82 |
tt, err := time.Parse(RFC3339FullDate, strdate) |
| 83 |
if err != nil { |
| 84 |
return err |
| 85 |
} |
| 86 |
*d = Date(tt) |
| 87 |
return nil |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func (d *Date) DeepCopyInto(out *Date) { |
| 92 |
*out = *d |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
func (d *Date) DeepCopy() *Date { |
| 97 |
if d == nil { |
| 98 |
return nil |
| 99 |
} |
| 100 |
out := new(Date) |
| 101 |
d.DeepCopyInto(out) |
| 102 |
return out |
| 103 |
} |