mirror of https://github.com/status-im/consul.git
vendor: add github.com/Azure/go-autorest/autorest/date
This commit is contained in:
parent
20c149873b
commit
c28893f491
|
@ -0,0 +1,82 @@
|
||||||
|
/*
|
||||||
|
Package date provides time.Time derivatives that conform to the Swagger.io (https://swagger.io/)
|
||||||
|
defined date formats: Date and DateTime. Both types may, in most cases, be used in lieu of
|
||||||
|
time.Time types. And both convert to time.Time through a ToTime method.
|
||||||
|
*/
|
||||||
|
package date
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
fullDate = "2006-01-02"
|
||||||
|
fullDateJSON = `"2006-01-02"`
|
||||||
|
dateFormat = "%04d-%02d-%02d"
|
||||||
|
jsonFormat = `"%04d-%02d-%02d"`
|
||||||
|
)
|
||||||
|
|
||||||
|
// Date defines a type similar to time.Time but assumes a layout of RFC3339 full-date (i.e.,
|
||||||
|
// 2006-01-02).
|
||||||
|
type Date struct {
|
||||||
|
time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseDate create a new Date from the passed string.
|
||||||
|
func ParseDate(date string) (d Date, err error) {
|
||||||
|
return parseDate(date, fullDate)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseDate(date string, format string) (Date, error) {
|
||||||
|
d, err := time.Parse(format, date)
|
||||||
|
return Date{Time: d}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalBinary preserves the Date as a byte array conforming to RFC3339 full-date (i.e.,
|
||||||
|
// 2006-01-02).
|
||||||
|
func (d Date) MarshalBinary() ([]byte, error) {
|
||||||
|
return d.MarshalText()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalBinary reconstitutes a Date saved as a byte array conforming to RFC3339 full-date (i.e.,
|
||||||
|
// 2006-01-02).
|
||||||
|
func (d *Date) UnmarshalBinary(data []byte) error {
|
||||||
|
return d.UnmarshalText(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON preserves the Date as a JSON string conforming to RFC3339 full-date (i.e.,
|
||||||
|
// 2006-01-02).
|
||||||
|
func (d Date) MarshalJSON() (json []byte, err error) {
|
||||||
|
return []byte(fmt.Sprintf(jsonFormat, d.Year(), d.Month(), d.Day())), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON reconstitutes the Date from a JSON string conforming to RFC3339 full-date (i.e.,
|
||||||
|
// 2006-01-02).
|
||||||
|
func (d *Date) UnmarshalJSON(data []byte) (err error) {
|
||||||
|
d.Time, err = time.Parse(fullDateJSON, string(data))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalText preserves the Date as a byte array conforming to RFC3339 full-date (i.e.,
|
||||||
|
// 2006-01-02).
|
||||||
|
func (d Date) MarshalText() (text []byte, err error) {
|
||||||
|
return []byte(fmt.Sprintf(dateFormat, d.Year(), d.Month(), d.Day())), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalText reconstitutes a Date saved as a byte array conforming to RFC3339 full-date (i.e.,
|
||||||
|
// 2006-01-02).
|
||||||
|
func (d *Date) UnmarshalText(data []byte) (err error) {
|
||||||
|
d.Time, err = time.Parse(fullDate, string(data))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the Date formatted as an RFC3339 full-date string (i.e., 2006-01-02).
|
||||||
|
func (d Date) String() string {
|
||||||
|
return fmt.Sprintf(dateFormat, d.Year(), d.Month(), d.Day())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToTime returns a Date as a time.Time
|
||||||
|
func (d Date) ToTime() time.Time {
|
||||||
|
return d.Time
|
||||||
|
}
|
|
@ -0,0 +1,89 @@
|
||||||
|
package date
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Azure reports time in UTC but it doesn't include the 'Z' time zone suffix in some cases.
|
||||||
|
const (
|
||||||
|
azureUtcFormatJSON = `"2006-01-02T15:04:05.999999999"`
|
||||||
|
azureUtcFormat = "2006-01-02T15:04:05.999999999"
|
||||||
|
rfc3339JSON = `"` + time.RFC3339Nano + `"`
|
||||||
|
rfc3339 = time.RFC3339Nano
|
||||||
|
tzOffsetRegex = `(Z|z|\+|-)(\d+:\d+)*"*$`
|
||||||
|
)
|
||||||
|
|
||||||
|
// Time defines a type similar to time.Time but assumes a layout of RFC3339 date-time (i.e.,
|
||||||
|
// 2006-01-02T15:04:05Z).
|
||||||
|
type Time struct {
|
||||||
|
time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalBinary preserves the Time as a byte array conforming to RFC3339 date-time (i.e.,
|
||||||
|
// 2006-01-02T15:04:05Z).
|
||||||
|
func (t Time) MarshalBinary() ([]byte, error) {
|
||||||
|
return t.Time.MarshalText()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalBinary reconstitutes a Time saved as a byte array conforming to RFC3339 date-time
|
||||||
|
// (i.e., 2006-01-02T15:04:05Z).
|
||||||
|
func (t *Time) UnmarshalBinary(data []byte) error {
|
||||||
|
return t.UnmarshalText(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON preserves the Time as a JSON string conforming to RFC3339 date-time (i.e.,
|
||||||
|
// 2006-01-02T15:04:05Z).
|
||||||
|
func (t Time) MarshalJSON() (json []byte, err error) {
|
||||||
|
return t.Time.MarshalJSON()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON reconstitutes the Time from a JSON string conforming to RFC3339 date-time
|
||||||
|
// (i.e., 2006-01-02T15:04:05Z).
|
||||||
|
func (t *Time) UnmarshalJSON(data []byte) (err error) {
|
||||||
|
timeFormat := azureUtcFormatJSON
|
||||||
|
match, err := regexp.Match(tzOffsetRegex, data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else if match {
|
||||||
|
timeFormat = rfc3339JSON
|
||||||
|
}
|
||||||
|
t.Time, err = ParseTime(timeFormat, string(data))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalText preserves the Time as a byte array conforming to RFC3339 date-time (i.e.,
|
||||||
|
// 2006-01-02T15:04:05Z).
|
||||||
|
func (t Time) MarshalText() (text []byte, err error) {
|
||||||
|
return t.Time.MarshalText()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalText reconstitutes a Time saved as a byte array conforming to RFC3339 date-time
|
||||||
|
// (i.e., 2006-01-02T15:04:05Z).
|
||||||
|
func (t *Time) UnmarshalText(data []byte) (err error) {
|
||||||
|
timeFormat := azureUtcFormat
|
||||||
|
match, err := regexp.Match(tzOffsetRegex, data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else if match {
|
||||||
|
timeFormat = rfc3339
|
||||||
|
}
|
||||||
|
t.Time, err = ParseTime(timeFormat, string(data))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the Time formatted as an RFC3339 date-time string (i.e.,
|
||||||
|
// 2006-01-02T15:04:05Z).
|
||||||
|
func (t Time) String() string {
|
||||||
|
// Note: time.Time.String does not return an RFC3339 compliant string, time.Time.MarshalText does.
|
||||||
|
b, err := t.MarshalText()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToTime returns a Time as a time.Time
|
||||||
|
func (t Time) ToTime() time.Time {
|
||||||
|
return t.Time
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
package date
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
rfc1123JSON = `"` + time.RFC1123 + `"`
|
||||||
|
rfc1123 = time.RFC1123
|
||||||
|
)
|
||||||
|
|
||||||
|
// TimeRFC1123 defines a type similar to time.Time but assumes a layout of RFC1123 date-time (i.e.,
|
||||||
|
// Mon, 02 Jan 2006 15:04:05 MST).
|
||||||
|
type TimeRFC1123 struct {
|
||||||
|
time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON reconstitutes the Time from a JSON string conforming to RFC1123 date-time
|
||||||
|
// (i.e., Mon, 02 Jan 2006 15:04:05 MST).
|
||||||
|
func (t *TimeRFC1123) UnmarshalJSON(data []byte) (err error) {
|
||||||
|
t.Time, err = ParseTime(rfc1123JSON, string(data))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON preserves the Time as a JSON string conforming to RFC1123 date-time (i.e.,
|
||||||
|
// Mon, 02 Jan 2006 15:04:05 MST).
|
||||||
|
func (t TimeRFC1123) MarshalJSON() ([]byte, error) {
|
||||||
|
if y := t.Year(); y < 0 || y >= 10000 {
|
||||||
|
return nil, errors.New("Time.MarshalJSON: year outside of range [0,9999]")
|
||||||
|
}
|
||||||
|
b := []byte(t.Format(rfc1123JSON))
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalText preserves the Time as a byte array conforming to RFC1123 date-time (i.e.,
|
||||||
|
// Mon, 02 Jan 2006 15:04:05 MST).
|
||||||
|
func (t TimeRFC1123) MarshalText() ([]byte, error) {
|
||||||
|
if y := t.Year(); y < 0 || y >= 10000 {
|
||||||
|
return nil, errors.New("Time.MarshalText: year outside of range [0,9999]")
|
||||||
|
}
|
||||||
|
|
||||||
|
b := []byte(t.Format(rfc1123))
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalText reconstitutes a Time saved as a byte array conforming to RFC1123 date-time
|
||||||
|
// (i.e., Mon, 02 Jan 2006 15:04:05 MST).
|
||||||
|
func (t *TimeRFC1123) UnmarshalText(data []byte) (err error) {
|
||||||
|
t.Time, err = ParseTime(rfc1123, string(data))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalBinary preserves the Time as a byte array conforming to RFC1123 date-time (i.e.,
|
||||||
|
// Mon, 02 Jan 2006 15:04:05 MST).
|
||||||
|
func (t TimeRFC1123) MarshalBinary() ([]byte, error) {
|
||||||
|
return t.MarshalText()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalBinary reconstitutes a Time saved as a byte array conforming to RFC1123 date-time
|
||||||
|
// (i.e., Mon, 02 Jan 2006 15:04:05 MST).
|
||||||
|
func (t *TimeRFC1123) UnmarshalBinary(data []byte) error {
|
||||||
|
return t.UnmarshalText(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToTime returns a Time as a time.Time
|
||||||
|
func (t TimeRFC1123) ToTime() time.Time {
|
||||||
|
return t.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the Time formatted as an RFC1123 date-time string (i.e.,
|
||||||
|
// Mon, 02 Jan 2006 15:04:05 MST).
|
||||||
|
func (t TimeRFC1123) String() string {
|
||||||
|
// Note: time.Time.String does not return an RFC1123 compliant string, time.Time.MarshalText does.
|
||||||
|
b, err := t.MarshalText()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
|
@ -0,0 +1,106 @@
|
||||||
|
package date
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// unixEpoch is the moment in time that should be treated as timestamp 0.
|
||||||
|
var unixEpoch = time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
|
// UnixTime marshals and unmarshals a time that is represented as the number
|
||||||
|
// of seconds (ignoring skip-seconds) since the Unix Epoch.
|
||||||
|
type UnixTime time.Time
|
||||||
|
|
||||||
|
// Duration returns the time as a Duration since the UnixEpoch.
|
||||||
|
func (t UnixTime) Duration() time.Duration {
|
||||||
|
return time.Time(t).Sub(unixEpoch)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewUnixTimeFromSeconds creates a UnixTime as a number of seconds from the UnixEpoch.
|
||||||
|
func NewUnixTimeFromSeconds(seconds float64) UnixTime {
|
||||||
|
return NewUnixTimeFromDuration(time.Duration(seconds * float64(time.Second)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewUnixTimeFromNanoseconds creates a UnixTime as a number of nanoseconds from the UnixEpoch.
|
||||||
|
func NewUnixTimeFromNanoseconds(nanoseconds int64) UnixTime {
|
||||||
|
return NewUnixTimeFromDuration(time.Duration(nanoseconds))
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewUnixTimeFromDuration creates a UnixTime as a duration of time since the UnixEpoch.
|
||||||
|
func NewUnixTimeFromDuration(dur time.Duration) UnixTime {
|
||||||
|
return UnixTime(unixEpoch.Add(dur))
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnixEpoch retreives the moment considered the Unix Epoch. I.e. The time represented by '0'
|
||||||
|
func UnixEpoch() time.Time {
|
||||||
|
return unixEpoch
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON preserves the UnixTime as a JSON number conforming to Unix Timestamp requirements.
|
||||||
|
// (i.e. the number of seconds since midnight January 1st, 1970 not considering leap seconds.)
|
||||||
|
func (t UnixTime) MarshalJSON() ([]byte, error) {
|
||||||
|
buffer := &bytes.Buffer{}
|
||||||
|
enc := json.NewEncoder(buffer)
|
||||||
|
enc.Encode(float64(time.Time(t).UnixNano()) / 1e9)
|
||||||
|
return buffer.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON reconstitures a UnixTime saved as a JSON number of the number of seconds since
|
||||||
|
// midnight January 1st, 1970.
|
||||||
|
func (t *UnixTime) UnmarshalJSON(text []byte) error {
|
||||||
|
dec := json.NewDecoder(bytes.NewReader(text))
|
||||||
|
|
||||||
|
var secondsSinceEpoch float64
|
||||||
|
if err := dec.Decode(&secondsSinceEpoch); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
*t = NewUnixTimeFromSeconds(secondsSinceEpoch)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalText stores the number of seconds since the Unix Epoch as a textual floating point number.
|
||||||
|
func (t UnixTime) MarshalText() ([]byte, error) {
|
||||||
|
cast := time.Time(t)
|
||||||
|
return cast.MarshalText()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalText populates a UnixTime with a value stored textually as a floating point number of seconds since the Unix Epoch.
|
||||||
|
func (t *UnixTime) UnmarshalText(raw []byte) error {
|
||||||
|
var unmarshaled time.Time
|
||||||
|
|
||||||
|
if err := unmarshaled.UnmarshalText(raw); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
*t = UnixTime(unmarshaled)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalBinary converts a UnixTime into a binary.LittleEndian float64 of nanoseconds since the epoch.
|
||||||
|
func (t UnixTime) MarshalBinary() ([]byte, error) {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
|
payload := int64(t.Duration())
|
||||||
|
|
||||||
|
if err := binary.Write(buf, binary.LittleEndian, &payload); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalBinary converts a from a binary.LittleEndian float64 of nanoseconds since the epoch into a UnixTime.
|
||||||
|
func (t *UnixTime) UnmarshalBinary(raw []byte) error {
|
||||||
|
var nanosecondsSinceEpoch int64
|
||||||
|
|
||||||
|
if err := binary.Read(bytes.NewReader(raw), binary.LittleEndian, &nanosecondsSinceEpoch); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*t = NewUnixTimeFromNanoseconds(nanosecondsSinceEpoch)
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package date
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ParseTime to parse Time string to specified format.
|
||||||
|
func ParseTime(format string, t string) (d time.Time, err error) {
|
||||||
|
return time.Parse(format, strings.ToUpper(t))
|
||||||
|
}
|
|
@ -33,6 +33,13 @@
|
||||||
"revisionTime": "2017-04-20T22:41:48Z"
|
"revisionTime": "2017-04-20T22:41:48Z"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"checksumSHA1": "QapQRLZlewQkaHFgQ3ktePbyh6k=",
|
||||||
|
"path": "github.com/Azure/go-autorest/autorest/date",
|
||||||
|
"revision": "c2571aa487482a987546c123fef9334582786335",
|
||||||
|
"revisionTime": "2017-04-20T22:41:48Z"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"checksumSHA1": "Ev8qCsbFjDlMlX0N2tYAhYQFpUc=",
|
||||||
"checksumSHA1": "JhyS/zIicgtrSasHSZ6WtXGWJVk=",
|
"checksumSHA1": "JhyS/zIicgtrSasHSZ6WtXGWJVk=",
|
||||||
"path": "github.com/DataDog/datadog-go/statsd",
|
"path": "github.com/DataDog/datadog-go/statsd",
|
||||||
"revision": "cc2f4770f4d61871e19bfee967bc767fe730b0d9",
|
"revision": "cc2f4770f4d61871e19bfee967bc767fe730b0d9",
|
||||||
|
|
Loading…
Reference in New Issue