timestamp.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package protocol
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/aws/aws-sdk-go/internal/sdkmath"
  7. )
  8. // Names of time formats supported by the SDK
  9. const (
  10. RFC822TimeFormatName = "rfc822"
  11. ISO8601TimeFormatName = "iso8601"
  12. UnixTimeFormatName = "unixTimestamp"
  13. )
  14. // Time formats supported by the SDK
  15. // Output time is intended to not contain decimals
  16. const (
  17. // RFC 7231#section-7.1.1.1 timetamp format. e.g Tue, 29 Apr 2014 18:30:38 GMT
  18. RFC822TimeFormat = "Mon, 2 Jan 2006 15:04:05 GMT"
  19. // This format is used for output time without seconds precision
  20. RFC822OutputTimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
  21. // RFC3339 a subset of the ISO8601 timestamp format. e.g 2014-04-29T18:30:38Z
  22. ISO8601TimeFormat = "2006-01-02T15:04:05.999999999Z"
  23. // This format is used for output time with fractional second precision up to milliseconds
  24. ISO8601OutputTimeFormat = "2006-01-02T15:04:05.999999999Z"
  25. )
  26. // IsKnownTimestampFormat returns if the timestamp format name
  27. // is know to the SDK's protocols.
  28. func IsKnownTimestampFormat(name string) bool {
  29. switch name {
  30. case RFC822TimeFormatName:
  31. fallthrough
  32. case ISO8601TimeFormatName:
  33. fallthrough
  34. case UnixTimeFormatName:
  35. return true
  36. default:
  37. return false
  38. }
  39. }
  40. // FormatTime returns a string value of the time.
  41. func FormatTime(name string, t time.Time) string {
  42. t = t.UTC().Truncate(time.Millisecond)
  43. switch name {
  44. case RFC822TimeFormatName:
  45. return t.Format(RFC822OutputTimeFormat)
  46. case ISO8601TimeFormatName:
  47. return t.Format(ISO8601OutputTimeFormat)
  48. case UnixTimeFormatName:
  49. ms := t.UnixNano() / int64(time.Millisecond)
  50. return strconv.FormatFloat(float64(ms)/1e3, 'f', -1, 64)
  51. default:
  52. panic("unknown timestamp format name, " + name)
  53. }
  54. }
  55. // ParseTime attempts to parse the time given the format. Returns
  56. // the time if it was able to be parsed, and fails otherwise.
  57. func ParseTime(formatName, value string) (time.Time, error) {
  58. switch formatName {
  59. case RFC822TimeFormatName:
  60. return time.Parse(RFC822TimeFormat, value)
  61. case ISO8601TimeFormatName:
  62. return time.Parse(ISO8601TimeFormat, value)
  63. case UnixTimeFormatName:
  64. v, err := strconv.ParseFloat(value, 64)
  65. _, dec := math.Modf(v)
  66. dec = sdkmath.Round(dec*1e3) / 1e3 //Rounds 0.1229999 to 0.123
  67. if err != nil {
  68. return time.Time{}, err
  69. }
  70. return time.Unix(int64(v), int64(dec*(1e9))), nil
  71. default:
  72. panic("unknown timestamp format name, " + formatName)
  73. }
  74. }