blob: ccdba4481922c369ea5ade4f7387bb00498fb055 [file] [log] [blame]
Serge Bazanskicc25bdf2018-10-25 14:02:58 +02001// 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
15package strfmt
16
17import (
18 "database/sql/driver"
19 "errors"
20 "fmt"
21 "time"
22
23 "github.com/globalsign/mgo/bson"
24 "github.com/mailru/easyjson/jlexer"
25 "github.com/mailru/easyjson/jwriter"
26)
27
28func init() {
29 d := Date{}
30 // register this format in the default registry
31 Default.Add("date", &d, IsDate)
32}
33
34// IsDate returns true when the string is a valid date
35func IsDate(str string) bool {
36 _, err := time.Parse(RFC3339FullDate, str)
37 return err == nil
38}
39
40const (
41 // RFC3339FullDate represents a full-date as specified by RFC3339
42 // See: http://goo.gl/xXOvVd
43 RFC3339FullDate = "2006-01-02"
44)
45
46// Date represents a date from the API
47//
48// swagger:strfmt date
49type Date time.Time
50
51// String converts this date into a string
52func (d Date) String() string {
53 return time.Time(d).Format(RFC3339FullDate)
54}
55
56// UnmarshalText parses a text representation into a date type
57func (d *Date) UnmarshalText(text []byte) error {
58 if len(text) == 0 {
59 return nil
60 }
61 dd, err := time.Parse(RFC3339FullDate, string(text))
62 if err != nil {
63 return err
64 }
65 *d = Date(dd)
66 return nil
67}
68
69// MarshalText serializes this date type to string
70func (d Date) MarshalText() ([]byte, error) {
71 return []byte(d.String()), nil
72}
73
74// Scan scans a Date value from database driver type.
75func (d *Date) Scan(raw interface{}) error {
76 switch v := raw.(type) {
77 case []byte:
78 return d.UnmarshalText(v)
79 case string:
80 return d.UnmarshalText([]byte(v))
81 case time.Time:
82 *d = Date(v)
83 return nil
84 case nil:
85 *d = Date{}
86 return nil
87 default:
88 return fmt.Errorf("cannot sql.Scan() strfmt.Date from: %#v", v)
89 }
90}
91
92// Value converts Date to a primitive value ready to written to a database.
93func (d Date) Value() (driver.Value, error) {
94 return driver.Value(d.String()), nil
95}
96
97// MarshalJSON returns the Date as JSON
98func (d Date) MarshalJSON() ([]byte, error) {
99 var w jwriter.Writer
100 d.MarshalEasyJSON(&w)
101 return w.BuildBytes()
102}
103
104// MarshalEasyJSON writes the Date to a easyjson.Writer
105func (d Date) MarshalEasyJSON(w *jwriter.Writer) {
106 w.String(time.Time(d).Format(RFC3339FullDate))
107}
108
109// UnmarshalJSON sets the Date from JSON
110func (d *Date) UnmarshalJSON(data []byte) error {
111 if string(data) == jsonNull {
112 return nil
113 }
114 l := jlexer.Lexer{Data: data}
115 d.UnmarshalEasyJSON(&l)
116 return l.Error()
117}
118
119// UnmarshalEasyJSON sets the Date from a easyjson.Lexer
120func (d *Date) UnmarshalEasyJSON(in *jlexer.Lexer) {
121 if data := in.String(); in.Ok() {
122 tt, err := time.Parse(RFC3339FullDate, data)
123 if err != nil {
124 in.AddError(err)
125 return
126 }
127 *d = Date(tt)
128 }
129}
130
131// GetBSON returns the Date as a bson.M{} map.
132func (d *Date) GetBSON() (interface{}, error) {
133 return bson.M{"data": d.String()}, nil
134}
135
136// SetBSON sets the Date from raw bson data
137func (d *Date) SetBSON(raw bson.Raw) error {
138 var m bson.M
139 if err := raw.Unmarshal(&m); err != nil {
140 return err
141 }
142
143 if data, ok := m["data"].(string); ok {
144 rd, err := time.Parse(RFC3339FullDate, data)
145 *d = Date(rd)
146 return err
147 }
148
149 return errors.New("couldn't unmarshal bson raw value as Date")
150}