podman

Форк
0
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

15
package strfmt
16

17
import (
18
	"database/sql/driver"
19
	"encoding/json"
20
	"errors"
21
	"fmt"
22
	"time"
23

24
	"go.mongodb.org/mongo-driver/bson"
25
)
26

27
func init() {
28
	d := Date{}
29
	// register this format in the default registry
30
	Default.Add("date", &d, IsDate)
31
}
32

33
// IsDate returns true when the string is a valid date
34
func IsDate(str string) bool {
35
	_, err := time.Parse(RFC3339FullDate, str)
36
	return err == nil
37
}
38

39
const (
40
	// RFC3339FullDate represents a full-date as specified by RFC3339
41
	// See: http://goo.gl/xXOvVd
42
	RFC3339FullDate = "2006-01-02"
43
)
44

45
// Date represents a date from the API
46
//
47
// swagger:strfmt date
48
type Date time.Time
49

50
// String converts this date into a string
51
func (d Date) String() string {
52
	return time.Time(d).Format(RFC3339FullDate)
53
}
54

55
// UnmarshalText parses a text representation into a date type
56
func (d *Date) UnmarshalText(text []byte) error {
57
	if len(text) == 0 {
58
		return nil
59
	}
60
	dd, err := time.ParseInLocation(RFC3339FullDate, string(text), DefaultTimeLocation)
61
	if err != nil {
62
		return err
63
	}
64
	*d = Date(dd)
65
	return nil
66
}
67

68
// MarshalText serializes this date type to string
69
func (d Date) MarshalText() ([]byte, error) {
70
	return []byte(d.String()), nil
71
}
72

73
// Scan scans a Date value from database driver type.
74
func (d *Date) Scan(raw interface{}) error {
75
	switch v := raw.(type) {
76
	case []byte:
77
		return d.UnmarshalText(v)
78
	case string:
79
		return d.UnmarshalText([]byte(v))
80
	case time.Time:
81
		*d = Date(v)
82
		return nil
83
	case nil:
84
		*d = Date{}
85
		return nil
86
	default:
87
		return 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.
92
func (d Date) Value() (driver.Value, error) {
93
	return driver.Value(d.String()), nil
94
}
95

96
// MarshalJSON returns the Date as JSON
97
func (d Date) MarshalJSON() ([]byte, error) {
98
	return json.Marshal(time.Time(d).Format(RFC3339FullDate))
99
}
100

101
// UnmarshalJSON sets the Date from JSON
102
func (d *Date) UnmarshalJSON(data []byte) error {
103
	if string(data) == jsonNull {
104
		return nil
105
	}
106
	var strdate string
107
	if err := json.Unmarshal(data, &strdate); err != nil {
108
		return err
109
	}
110
	tt, err := time.ParseInLocation(RFC3339FullDate, strdate, DefaultTimeLocation)
111
	if err != nil {
112
		return err
113
	}
114
	*d = Date(tt)
115
	return nil
116
}
117

118
func (d Date) MarshalBSON() ([]byte, error) {
119
	return bson.Marshal(bson.M{"data": d.String()})
120
}
121

122
func (d *Date) UnmarshalBSON(data []byte) error {
123
	var m bson.M
124
	if err := bson.Unmarshal(data, &m); err != nil {
125
		return err
126
	}
127

128
	if data, ok := m["data"].(string); ok {
129
		rd, err := time.ParseInLocation(RFC3339FullDate, data, DefaultTimeLocation)
130
		if err != nil {
131
			return err
132
		}
133
		*d = Date(rd)
134
		return nil
135
	}
136

137
	return errors.New("couldn't unmarshal bson bytes value as Date")
138
}
139

140
// DeepCopyInto copies the receiver and writes its value into out.
141
func (d *Date) DeepCopyInto(out *Date) {
142
	*out = *d
143
}
144

145
// DeepCopy copies the receiver into a new Date.
146
func (d *Date) DeepCopy() *Date {
147
	if d == nil {
148
		return nil
149
	}
150
	out := new(Date)
151
	d.DeepCopyInto(out)
152
	return out
153
}
154

155
// GobEncode implements the gob.GobEncoder interface.
156
func (d Date) GobEncode() ([]byte, error) {
157
	return d.MarshalBinary()
158
}
159

160
// GobDecode implements the gob.GobDecoder interface.
161
func (d *Date) GobDecode(data []byte) error {
162
	return d.UnmarshalBinary(data)
163
}
164

165
// MarshalBinary implements the encoding.BinaryMarshaler interface.
166
func (d Date) MarshalBinary() ([]byte, error) {
167
	return time.Time(d).MarshalBinary()
168
}
169

170
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
171
func (d *Date) UnmarshalBinary(data []byte) error {
172
	var original time.Time
173

174
	err := original.UnmarshalBinary(data)
175
	if err != nil {
176
		return err
177
	}
178

179
	*d = Date(original)
180

181
	return nil
182
}
183

184
// Equal checks if two Date instances are equal
185
func (d Date) Equal(d2 Date) bool {
186
	return time.Time(d).Equal(time.Time(d2))
187
}
188

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.