...

Source file src/github.com/Azure/go-autorest/autorest/date/timerfc1123.go

Documentation: github.com/Azure/go-autorest/autorest/date

     1  package date
     2  
     3  // Copyright 2017 Microsoft Corporation
     4  //
     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
     8  //
     9  //      http://www.apache.org/licenses/LICENSE-2.0
    10  //
    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.
    16  
    17  import (
    18  	"errors"
    19  	"time"
    20  )
    21  
    22  const (
    23  	rfc1123JSON = `"` + time.RFC1123 + `"`
    24  	rfc1123     = time.RFC1123
    25  )
    26  
    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 {
    30  	time.Time
    31  }
    32  
    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))
    37  	if err != nil {
    38  		return err
    39  	}
    40  	return nil
    41  }
    42  
    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]")
    48  	}
    49  	b := []byte(t.Format(rfc1123JSON))
    50  	return b, nil
    51  }
    52  
    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]")
    58  	}
    59  
    60  	b := []byte(t.Format(rfc1123))
    61  	return b, nil
    62  }
    63  
    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))
    68  	if err != nil {
    69  		return err
    70  	}
    71  	return nil
    72  }
    73  
    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()
    78  }
    79  
    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)
    84  }
    85  
    86  // ToTime returns a Time as a time.Time
    87  func (t TimeRFC1123) ToTime() time.Time {
    88  	return t.Time
    89  }
    90  
    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()
    96  	if err != nil {
    97  		return ""
    98  	}
    99  	return string(b)
   100  }
   101  

View as plain text