3 // Copyright 2017 Microsoft Corporation
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
23 rfc1123JSON = `"` + time.RFC1123 + `"`
24 rfc1123 = time.RFC1123
27 // TimeRFC1123 defines a type similar to time.Time but assumes a layout of RFC1123 date-time (i.e.,
28 // Mon, 02 Jan 2006 15:04:05 MST).
29 type TimeRFC1123 struct {
33 // UnmarshalJSON reconstitutes the Time from a JSON string conforming to RFC1123 date-time
34 // (i.e., Mon, 02 Jan 2006 15:04:05 MST).
35 func (t *TimeRFC1123) UnmarshalJSON(data []byte) (err error) {
36 t.Time, err = ParseTime(rfc1123JSON, string(data))
43 // MarshalJSON preserves the Time as a JSON string conforming to RFC1123 date-time (i.e.,
44 // Mon, 02 Jan 2006 15:04:05 MST).
45 func (t TimeRFC1123) MarshalJSON() ([]byte, error) {
46 if y := t.Year(); y < 0 || y >= 10000 {
47 return nil, errors.New("Time.MarshalJSON: year outside of range [0,9999]")
49 b := []byte(t.Format(rfc1123JSON))
53 // MarshalText preserves the Time as a byte array conforming to RFC1123 date-time (i.e.,
54 // Mon, 02 Jan 2006 15:04:05 MST).
55 func (t TimeRFC1123) MarshalText() ([]byte, error) {
56 if y := t.Year(); y < 0 || y >= 10000 {
57 return nil, errors.New("Time.MarshalText: year outside of range [0,9999]")
60 b := []byte(t.Format(rfc1123))
64 // UnmarshalText reconstitutes a Time saved as a byte array conforming to RFC1123 date-time
65 // (i.e., Mon, 02 Jan 2006 15:04:05 MST).
66 func (t *TimeRFC1123) UnmarshalText(data []byte) (err error) {
67 t.Time, err = ParseTime(rfc1123, string(data))
74 // MarshalBinary preserves the Time as a byte array conforming to RFC1123 date-time (i.e.,
75 // Mon, 02 Jan 2006 15:04:05 MST).
76 func (t TimeRFC1123) MarshalBinary() ([]byte, error) {
77 return t.MarshalText()
80 // UnmarshalBinary reconstitutes a Time saved as a byte array conforming to RFC1123 date-time
81 // (i.e., Mon, 02 Jan 2006 15:04:05 MST).
82 func (t *TimeRFC1123) UnmarshalBinary(data []byte) error {
83 return t.UnmarshalText(data)
86 // ToTime returns a Time as a time.Time
87 func (t TimeRFC1123) ToTime() time.Time {
91 // String returns the Time formatted as an RFC1123 date-time string (i.e.,
92 // Mon, 02 Jan 2006 15:04:05 MST).
93 func (t TimeRFC1123) String() string {
94 // Note: time.Time.String does not return an RFC1123 compliant string, time.Time.MarshalText does.
95 b, err := t.MarshalText()