| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package strfmt |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"fmt" |
| 20 |
"regexp" |
| 21 |
"strings" |
| 22 |
"time" |
| 23 |
) |
| 24 |
|
| 25 |
func init() { |
| 26 |
dt := DateTime{} |
| 27 |
Default.Add("datetime", &dt, IsDateTime) |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
func IsDateTime(str string) bool { |
| 32 |
if len(str) < 4 { |
| 33 |
return false |
| 34 |
} |
| 35 |
s := strings.Split(strings.ToLower(str), "t") |
| 36 |
if len(s) < 2 || !IsDate(s[0]) { |
| 37 |
return false |
| 38 |
} |
| 39 |
|
| 40 |
matches := rxDateTime.FindAllStringSubmatch(s[1], -1) |
| 41 |
if len(matches) == 0 || len(matches[0]) == 0 { |
| 42 |
return false |
| 43 |
} |
| 44 |
m := matches[0] |
| 45 |
res := m[1] <= "23" && m[2] <= "59" && m[3] <= "59" |
| 46 |
return res |
| 47 |
} |
| 48 |
|
| 49 |
const ( |
| 50 |
|
| 51 |
RFC3339Millis = "2006-01-02T15:04:05.000Z07:00" |
| 52 |
|
| 53 |
RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00" |
| 54 |
|
| 55 |
ISO8601LocalTime = "2006-01-02T15:04:05" |
| 56 |
|
| 57 |
DateTimePattern = `^([0-9]{2}):([0-9]{2}):([0-9]{2})(.[0-9]+)?(z|([+-][0-9]{2}:[0-9]{2}))$` |
| 58 |
) |
| 59 |
|
| 60 |
var ( |
| 61 |
dateTimeFormats = []string{RFC3339Micro, RFC3339Millis, time.RFC3339, time.RFC3339Nano, ISO8601LocalTime} |
| 62 |
rxDateTime = regexp.MustCompile(DateTimePattern) |
| 63 |
|
| 64 |
MarshalFormat = RFC3339Millis |
| 65 |
) |
| 66 |
|
| 67 |
|
| 68 |
func ParseDateTime(data string) (DateTime, error) { |
| 69 |
if data == "" { |
| 70 |
return NewDateTime(), nil |
| 71 |
} |
| 72 |
var lastError error |
| 73 |
for _, layout := range dateTimeFormats { |
| 74 |
dd, err := time.Parse(layout, data) |
| 75 |
if err != nil { |
| 76 |
lastError = err |
| 77 |
continue |
| 78 |
} |
| 79 |
return DateTime(dd), nil |
| 80 |
} |
| 81 |
return DateTime{}, lastError |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
type DateTime time.Time |
| 91 |
|
| 92 |
|
| 93 |
func NewDateTime() DateTime { |
| 94 |
return DateTime(time.Unix(0, 0).UTC()) |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
func (t DateTime) String() string { |
| 99 |
return time.Time(t).Format(MarshalFormat) |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
func (t DateTime) MarshalText() ([]byte, error) { |
| 104 |
return []byte(t.String()), nil |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
func (t *DateTime) UnmarshalText(text []byte) error { |
| 109 |
tt, err := ParseDateTime(string(text)) |
| 110 |
if err != nil { |
| 111 |
return err |
| 112 |
} |
| 113 |
*t = tt |
| 114 |
return nil |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
func (t *DateTime) Scan(raw interface{}) error { |
| 119 |
|
| 120 |
switch v := raw.(type) { |
| 121 |
case []byte: |
| 122 |
return t.UnmarshalText(v) |
| 123 |
case string: |
| 124 |
return t.UnmarshalText([]byte(v)) |
| 125 |
case time.Time: |
| 126 |
*t = DateTime(v) |
| 127 |
case nil: |
| 128 |
*t = DateTime{} |
| 129 |
default: |
| 130 |
return fmt.Errorf("cannot sql.Scan() strfmt.DateTime from: %#v", v) |
| 131 |
} |
| 132 |
|
| 133 |
return nil |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
func (t DateTime) MarshalJSON() ([]byte, error) { |
| 138 |
return json.Marshal(time.Time(t).Format(MarshalFormat)) |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
func (t *DateTime) UnmarshalJSON(data []byte) error { |
| 143 |
if string(data) == jsonNull { |
| 144 |
return nil |
| 145 |
} |
| 146 |
|
| 147 |
var tstr string |
| 148 |
if err := json.Unmarshal(data, &tstr); err != nil { |
| 149 |
return err |
| 150 |
} |
| 151 |
tt, err := ParseDateTime(tstr) |
| 152 |
if err != nil { |
| 153 |
return err |
| 154 |
} |
| 155 |
*t = tt |
| 156 |
return nil |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
func (t *DateTime) DeepCopyInto(out *DateTime) { |
| 161 |
*out = *t |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
func (t *DateTime) DeepCopy() *DateTime { |
| 166 |
if t == nil { |
| 167 |
return nil |
| 168 |
} |
| 169 |
out := new(DateTime) |
| 170 |
t.DeepCopyInto(out) |
| 171 |
return out |
| 172 |
} |