2018-07-31 00:07:29 +02:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2019-10-11 18:50:12 +02:00
|
|
|
"math"
|
2018-07-31 00:07:29 +02:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2019-10-11 18:50:12 +02:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/internal/sdkmath"
|
2018-07-31 00:07:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Names of time formats supported by the SDK
|
|
|
|
const (
|
|
|
|
RFC822TimeFormatName = "rfc822"
|
|
|
|
ISO8601TimeFormatName = "iso8601"
|
|
|
|
UnixTimeFormatName = "unixTimestamp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Time formats supported by the SDK
|
2019-10-11 18:50:12 +02:00
|
|
|
// Output time is intended to not contain decimals
|
2018-07-31 00:07:29 +02:00
|
|
|
const (
|
|
|
|
// RFC 7231#section-7.1.1.1 timetamp format. e.g Tue, 29 Apr 2014 18:30:38 GMT
|
|
|
|
RFC822TimeFormat = "Mon, 2 Jan 2006 15:04:05 GMT"
|
|
|
|
|
2019-10-11 18:50:12 +02:00
|
|
|
// This format is used for output time without seconds precision
|
|
|
|
RFC822OutputTimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
|
|
|
|
|
2018-07-31 00:07:29 +02:00
|
|
|
// RFC3339 a subset of the ISO8601 timestamp format. e.g 2014-04-29T18:30:38Z
|
2019-10-11 18:50:12 +02:00
|
|
|
ISO8601TimeFormat = "2006-01-02T15:04:05.999999999Z"
|
|
|
|
|
|
|
|
// This format is used for output time without seconds precision
|
|
|
|
ISO8601OutputTimeFormat = "2006-01-02T15:04:05Z"
|
2018-07-31 00:07:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// IsKnownTimestampFormat returns if the timestamp format name
|
|
|
|
// is know to the SDK's protocols.
|
|
|
|
func IsKnownTimestampFormat(name string) bool {
|
|
|
|
switch name {
|
|
|
|
case RFC822TimeFormatName:
|
|
|
|
fallthrough
|
|
|
|
case ISO8601TimeFormatName:
|
|
|
|
fallthrough
|
|
|
|
case UnixTimeFormatName:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FormatTime returns a string value of the time.
|
|
|
|
func FormatTime(name string, t time.Time) string {
|
|
|
|
t = t.UTC()
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case RFC822TimeFormatName:
|
2019-10-11 18:50:12 +02:00
|
|
|
return t.Format(RFC822OutputTimeFormat)
|
2018-07-31 00:07:29 +02:00
|
|
|
case ISO8601TimeFormatName:
|
2019-10-11 18:50:12 +02:00
|
|
|
return t.Format(ISO8601OutputTimeFormat)
|
2018-07-31 00:07:29 +02:00
|
|
|
case UnixTimeFormatName:
|
|
|
|
return strconv.FormatInt(t.Unix(), 10)
|
|
|
|
default:
|
|
|
|
panic("unknown timestamp format name, " + name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseTime attempts to parse the time given the format. Returns
|
|
|
|
// the time if it was able to be parsed, and fails otherwise.
|
|
|
|
func ParseTime(formatName, value string) (time.Time, error) {
|
|
|
|
switch formatName {
|
|
|
|
case RFC822TimeFormatName:
|
|
|
|
return time.Parse(RFC822TimeFormat, value)
|
|
|
|
case ISO8601TimeFormatName:
|
|
|
|
return time.Parse(ISO8601TimeFormat, value)
|
|
|
|
case UnixTimeFormatName:
|
|
|
|
v, err := strconv.ParseFloat(value, 64)
|
2019-10-11 18:50:12 +02:00
|
|
|
_, dec := math.Modf(v)
|
|
|
|
dec = sdkmath.Round(dec*1e3) / 1e3 //Rounds 0.1229999 to 0.123
|
2018-07-31 00:07:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
2019-10-11 18:50:12 +02:00
|
|
|
return time.Unix(int64(v), int64(dec*(1e9))), nil
|
2018-07-31 00:07:29 +02:00
|
|
|
default:
|
|
|
|
panic("unknown timestamp format name, " + formatName)
|
|
|
|
}
|
|
|
|
}
|