podman
187 строк · 4.3 Кб
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
15package strfmt16
17import (18"database/sql/driver"19"encoding/json"20"errors"21"fmt"22"time"23
24"go.mongodb.org/mongo-driver/bson"25)
26
27func init() {28d := Date{}29// register this format in the default registry30Default.Add("date", &d, IsDate)31}
32
33// IsDate returns true when the string is a valid date
34func IsDate(str string) bool {35_, err := time.Parse(RFC3339FullDate, str)36return err == nil37}
38
39const (40// RFC3339FullDate represents a full-date as specified by RFC333941// See: http://goo.gl/xXOvVd42RFC3339FullDate = "2006-01-02"43)
44
45// Date represents a date from the API
46//
47// swagger:strfmt date
48type Date time.Time49
50// String converts this date into a string
51func (d Date) String() string {52return time.Time(d).Format(RFC3339FullDate)53}
54
55// UnmarshalText parses a text representation into a date type
56func (d *Date) UnmarshalText(text []byte) error {57if len(text) == 0 {58return nil59}60dd, err := time.ParseInLocation(RFC3339FullDate, string(text), DefaultTimeLocation)61if err != nil {62return err63}64*d = Date(dd)65return nil66}
67
68// MarshalText serializes this date type to string
69func (d Date) MarshalText() ([]byte, error) {70return []byte(d.String()), nil71}
72
73// Scan scans a Date value from database driver type.
74func (d *Date) Scan(raw interface{}) error {75switch v := raw.(type) {76case []byte:77return d.UnmarshalText(v)78case string:79return d.UnmarshalText([]byte(v))80case time.Time:81*d = Date(v)82return nil83case nil:84*d = Date{}85return nil86default:87return fmt.Errorf("cannot sql.Scan() strfmt.Date from: %#v", v)88}89}
90
91// Value converts Date to a primitive value ready to written to a database.
92func (d Date) Value() (driver.Value, error) {93return driver.Value(d.String()), nil94}
95
96// MarshalJSON returns the Date as JSON
97func (d Date) MarshalJSON() ([]byte, error) {98return json.Marshal(time.Time(d).Format(RFC3339FullDate))99}
100
101// UnmarshalJSON sets the Date from JSON
102func (d *Date) UnmarshalJSON(data []byte) error {103if string(data) == jsonNull {104return nil105}106var strdate string107if err := json.Unmarshal(data, &strdate); err != nil {108return err109}110tt, err := time.ParseInLocation(RFC3339FullDate, strdate, DefaultTimeLocation)111if err != nil {112return err113}114*d = Date(tt)115return nil116}
117
118func (d Date) MarshalBSON() ([]byte, error) {119return bson.Marshal(bson.M{"data": d.String()})120}
121
122func (d *Date) UnmarshalBSON(data []byte) error {123var m bson.M124if err := bson.Unmarshal(data, &m); err != nil {125return err126}127
128if data, ok := m["data"].(string); ok {129rd, err := time.ParseInLocation(RFC3339FullDate, data, DefaultTimeLocation)130if err != nil {131return err132}133*d = Date(rd)134return nil135}136
137return errors.New("couldn't unmarshal bson bytes value as Date")138}
139
140// DeepCopyInto copies the receiver and writes its value into out.
141func (d *Date) DeepCopyInto(out *Date) {142*out = *d143}
144
145// DeepCopy copies the receiver into a new Date.
146func (d *Date) DeepCopy() *Date {147if d == nil {148return nil149}150out := new(Date)151d.DeepCopyInto(out)152return out153}
154
155// GobEncode implements the gob.GobEncoder interface.
156func (d Date) GobEncode() ([]byte, error) {157return d.MarshalBinary()158}
159
160// GobDecode implements the gob.GobDecoder interface.
161func (d *Date) GobDecode(data []byte) error {162return d.UnmarshalBinary(data)163}
164
165// MarshalBinary implements the encoding.BinaryMarshaler interface.
166func (d Date) MarshalBinary() ([]byte, error) {167return time.Time(d).MarshalBinary()168}
169
170// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
171func (d *Date) UnmarshalBinary(data []byte) error {172var original time.Time173
174err := original.UnmarshalBinary(data)175if err != nil {176return err177}178
179*d = Date(original)180
181return nil182}
183
184// Equal checks if two Date instances are equal
185func (d Date) Equal(d2 Date) bool {186return time.Time(d).Equal(time.Time(d2))187}
188