| 1 | // Copyright 2015 go-swagger maintainers |
| 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 | |
| 15 | package strfmt |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "time" |
| 20 | ) |
| 21 | |
| 22 | func init() { |
| 23 | d := Date{} |
| 24 | // register this format in the default registry |
| 25 | Default.Add("date", &d, IsDate) |
| 26 | } |
| 27 | |
| 28 | // IsDate returns true when the string is a valid date |
| 29 | func IsDate(str string) bool { |
| 30 | _, err := time.Parse(RFC3339FullDate, str) |
| 31 | return err == nil |
| 32 | } |
| 33 | |
| 34 | const ( |
| 35 | // RFC3339FullDate represents a full-date as specified by RFC3339 |
| 36 | // See: http://goo.gl/xXOvVd |
| 37 | RFC3339FullDate = "2006-01-02" |
| 38 | ) |
| 39 | |
| 40 | // Date represents a date from the API |
| 41 | // |
| 42 | // swagger:strfmt date |
| 43 | type Date time.Time |
| 44 | |
| 45 | // String converts this date into a string |
| 46 | func (d Date) String() string { |
| 47 | return time.Time(d).Format(RFC3339FullDate) |
| 48 | } |
| 49 | |
| 50 | // UnmarshalText parses a text representation into a date type |
| 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 | // MarshalText serializes this date type to string |
| 64 | func (d Date) MarshalText() ([]byte, error) { |
| 65 | return []byte(d.String()), nil |
| 66 | } |
| 67 | |
| 68 | // MarshalJSON returns the Date as JSON |
| 69 | func (d Date) MarshalJSON() ([]byte, error) { |
| 70 | return json.Marshal(time.Time(d).Format(RFC3339FullDate)) |
| 71 | } |
| 72 | |
| 73 | // UnmarshalJSON sets the Date from JSON |
| 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 | // DeepCopyInto copies the receiver and writes its value into out. |
| 91 | func (d *Date) DeepCopyInto(out *Date) { |
| 92 | *out = *d |
| 93 | } |
| 94 | |
| 95 | // DeepCopy copies the receiver into a new Date. |
| 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 | } |