timestamp.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package protocol
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math"
  6. "strconv"
  7. "time"
  8. "github.com/aws/aws-sdk-go/internal/sdkmath"
  9. )
  10. // Names of time formats supported by the SDK
  11. const (
  12. RFC822TimeFormatName = "rfc822"
  13. ISO8601TimeFormatName = "iso8601"
  14. UnixTimeFormatName = "unixTimestamp"
  15. )
  16. // Time formats supported by the SDK
  17. // Output time is intended to not contain decimals
  18. const (
  19. // RFC 7231#section-7.1.1.1 timetamp format. e.g Tue, 29 Apr 2014 18:30:38 GMT
  20. RFC822TimeFormat = "Mon, 2 Jan 2006 15:04:05 GMT"
  21. rfc822TimeFormatSingleDigitDay = "Mon, _2 Jan 2006 15:04:05 GMT"
  22. rfc822TimeFormatSingleDigitDayTwoDigitYear = "Mon, _2 Jan 06 15:04:05 GMT"
  23. // This format is used for output time without seconds precision
  24. RFC822OutputTimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
  25. // RFC3339 a subset of the ISO8601 timestamp format. e.g 2014-04-29T18:30:38Z
  26. ISO8601TimeFormat = "2006-01-02T15:04:05.999999999Z"
  27. iso8601TimeFormatNoZ = "2006-01-02T15:04:05.999999999"
  28. // This format is used for output time with fractional second precision up to milliseconds
  29. ISO8601OutputTimeFormat = "2006-01-02T15:04:05.999999999Z"
  30. )
  31. // IsKnownTimestampFormat returns if the timestamp format name
  32. // is know to the SDK's protocols.
  33. func IsKnownTimestampFormat(name string) bool {
  34. switch name {
  35. case RFC822TimeFormatName:
  36. fallthrough
  37. case ISO8601TimeFormatName:
  38. fallthrough
  39. case UnixTimeFormatName:
  40. return true
  41. default:
  42. return false
  43. }
  44. }
  45. // FormatTime returns a string value of the time.
  46. func FormatTime(name string, t time.Time) string {
  47. t = t.UTC().Truncate(time.Millisecond)
  48. switch name {
  49. case RFC822TimeFormatName:
  50. return t.Format(RFC822OutputTimeFormat)
  51. case ISO8601TimeFormatName:
  52. return t.Format(ISO8601OutputTimeFormat)
  53. case UnixTimeFormatName:
  54. ms := t.UnixNano() / int64(time.Millisecond)
  55. return strconv.FormatFloat(float64(ms)/1e3, 'f', -1, 64)
  56. default:
  57. panic("unknown timestamp format name, " + name)
  58. }
  59. }
  60. // ParseTime attempts to parse the time given the format. Returns
  61. // the time if it was able to be parsed, and fails otherwise.
  62. func ParseTime(formatName, value string) (time.Time, error) {
  63. switch formatName {
  64. case RFC822TimeFormatName: // Smithy HTTPDate format
  65. return tryParse(value,
  66. RFC822TimeFormat,
  67. rfc822TimeFormatSingleDigitDay,
  68. rfc822TimeFormatSingleDigitDayTwoDigitYear,
  69. time.RFC850,
  70. time.ANSIC,
  71. )
  72. case ISO8601TimeFormatName: // Smithy DateTime format
  73. return tryParse(value,
  74. ISO8601TimeFormat,
  75. iso8601TimeFormatNoZ,
  76. time.RFC3339Nano,
  77. time.RFC3339,
  78. )
  79. case UnixTimeFormatName:
  80. v, err := strconv.ParseFloat(value, 64)
  81. _, dec := math.Modf(v)
  82. dec = sdkmath.Round(dec*1e3) / 1e3 //Rounds 0.1229999 to 0.123
  83. if err != nil {
  84. return time.Time{}, err
  85. }
  86. return time.Unix(int64(v), int64(dec*(1e9))), nil
  87. default:
  88. panic("unknown timestamp format name, " + formatName)
  89. }
  90. }
  91. func tryParse(v string, formats ...string) (time.Time, error) {
  92. var errs parseErrors
  93. for _, f := range formats {
  94. t, err := time.Parse(f, v)
  95. if err != nil {
  96. errs = append(errs, parseError{
  97. Format: f,
  98. Err: err,
  99. })
  100. continue
  101. }
  102. return t, nil
  103. }
  104. return time.Time{}, fmt.Errorf("unable to parse time string, %v", errs)
  105. }
  106. type parseErrors []parseError
  107. func (es parseErrors) Error() string {
  108. var s bytes.Buffer
  109. for _, e := range es {
  110. fmt.Fprintf(&s, "\n * %q: %v", e.Format, e.Err)
  111. }
  112. return "parse errors:" + s.String()
  113. }
  114. type parseError struct {
  115. Format string
  116. Err error
  117. }