timestamp.go 2.2 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 without seconds precision
  24. ISO8601OutputTimeFormat = "2006-01-02T15:04:05Z"
  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()
  43. switch name {
  44. case RFC822TimeFormatName:
  45. return t.Format(RFC822OutputTimeFormat)
  46. case ISO8601TimeFormatName:
  47. return t.Format(ISO8601OutputTimeFormat)
  48. case UnixTimeFormatName:
  49. return strconv.FormatInt(t.Unix(), 10)
  50. default:
  51. panic("unknown timestamp format name, " + name)
  52. }
  53. }
  54. // ParseTime attempts to parse the time given the format. Returns
  55. // the time if it was able to be parsed, and fails otherwise.
  56. func ParseTime(formatName, value string) (time.Time, error) {
  57. switch formatName {
  58. case RFC822TimeFormatName:
  59. return time.Parse(RFC822TimeFormat, value)
  60. case ISO8601TimeFormatName:
  61. return time.Parse(ISO8601TimeFormat, value)
  62. case UnixTimeFormatName:
  63. v, err := strconv.ParseFloat(value, 64)
  64. _, dec := math.Modf(v)
  65. dec = sdkmath.Round(dec*1e3) / 1e3 //Rounds 0.1229999 to 0.123
  66. if err != nil {
  67. return time.Time{}, err
  68. }
  69. return time.Unix(int64(v), int64(dec*(1e9))), nil
  70. default:
  71. panic("unknown timestamp format name, " + formatName)
  72. }
  73. }