Debian Codegraph @

pkg/validation/strfmt/time.go

raw
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 "regexp"
20 "strings"
21 "time"
22 )
23
24 func init() {
25 dt := DateTime{}
26 Default.Add("datetime", &dt, IsDateTime)
27 }
28
29 // IsDateTime returns true when the string is a valid date-time
30 func IsDateTime(str string) bool {
31 if len(str) < 4 {
32 return false
33 }
34 s := strings.Split(strings.ToLower(str), "t")
35 if len(s) < 2 || !IsDate(s[0]) {
36 return false
37 }
38
39 matches := rxDateTime.FindAllStringSubmatch(s[1], -1)
40 if len(matches) == 0 || len(matches[0]) == 0 {
41 return false
42 }
43 m := matches[0]
44 res := m[1] <= "23" && m[2] <= "59" && m[3] <= "59"
45 return res
46 }
47
48 const (
49 // RFC3339Millis represents a ISO8601 format to millis instead of to nanos
50 RFC3339Millis = "2006-01-02T15:04:05.000Z07:00"
51 // RFC3339Micro represents a ISO8601 format to micro instead of to nano
52 RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00"
53 // ISO8601LocalTime represents a ISO8601 format to ISO8601 in local time (no timezone)
54 ISO8601LocalTime = "2006-01-02T15:04:05"
55 // DateTimePattern pattern to match for the date-time format from http://tools.ietf.org/html/rfc3339#section-5.6
56 DateTimePattern = `^([0-9]{2}):([0-9]{2}):([0-9]{2})(.[0-9]+)?(z|([+-][0-9]{2}:[0-9]{2}))$`
57 )
58
59 var (
60 dateTimeFormats = []string{RFC3339Micro, RFC3339Millis, time.RFC3339, time.RFC3339Nano, ISO8601LocalTime}
61 rxDateTime = regexp.MustCompile(DateTimePattern)
62 // MarshalFormat sets the time resolution format used for marshaling time (set to milliseconds)
63 MarshalFormat = RFC3339Millis
64 )
65
66 // ParseDateTime parses a string that represents an ISO8601 time or a unix epoch
67 func ParseDateTime(data string) (DateTime, error) {
68 if data == "" {
69 return NewDateTime(), nil
70 }
71 var lastError error
72 for _, layout := range dateTimeFormats {
73 dd, err := time.Parse(layout, data)
74 if err != nil {
75 lastError = err
76 continue
77 }
78 return DateTime(dd), nil
79 }
80 return DateTime{}, lastError
81 }
82
83 // DateTime is a time but it serializes to ISO8601 format with millis
84 // It knows how to read 3 different variations of a RFC3339 date time.
85 // Most APIs we encounter want either millisecond or second precision times.
86 // This just tries to make it worry-free.
87 //
88 // swagger:strfmt date-time
89 type DateTime time.Time
90
91 // NewDateTime is a representation of zero value for DateTime type
92 func NewDateTime() DateTime {
93 return DateTime(time.Unix(0, 0).UTC())
94 }
95
96 // String converts this time to a string
97 func (t DateTime) String() string {
98 return time.Time(t).Format(MarshalFormat)
99 }
100
101 // MarshalText implements the text marshaller interface
102 func (t DateTime) MarshalText() ([]byte, error) {
103 return []byte(t.String()), nil
104 }
105
106 // UnmarshalText implements the text unmarshaller interface
107 func (t *DateTime) UnmarshalText(text []byte) error {
108 tt, err := ParseDateTime(string(text))
109 if err != nil {
110 return err
111 }
112 *t = tt
113 return nil
114 }
115
116 // MarshalJSON returns the DateTime as JSON
117 func (t DateTime) MarshalJSON() ([]byte, error) {
118 return json.Marshal(time.Time(t).Format(MarshalFormat))
119 }
120
121 // UnmarshalJSON sets the DateTime from JSON
122 func (t *DateTime) UnmarshalJSON(data []byte) error {
123 if string(data) == jsonNull {
124 return nil
125 }
126
127 var tstr string
128 if err := json.Unmarshal(data, &tstr); err != nil {
129 return err
130 }
131 tt, err := ParseDateTime(tstr)
132 if err != nil {
133 return err
134 }
135 *t = tt
136 return nil
137 }
138
139 // DeepCopyInto copies the receiver and writes its value into out.
140 func (t *DateTime) DeepCopyInto(out *DateTime) {
141 *out = *t
142 }
143
144 // DeepCopy copies the receiver into a new DateTime.
145 func (t *DateTime) DeepCopy() *DateTime {
146 if t == nil {
147 return nil
148 }
149 out := new(DateTime)
150 t.DeepCopyInto(out)
151 return out
152 }