timestamp.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package time // import "github.com/docker/docker/api/types/time"
  2. import (
  3. "fmt"
  4. "math"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. // These are additional predefined layouts for use in Time.Format and Time.Parse
  10. // with --since and --until parameters for `docker logs` and `docker events`
  11. const (
  12. rFC3339Local = "2006-01-02T15:04:05" // RFC3339 with local timezone
  13. rFC3339NanoLocal = "2006-01-02T15:04:05.999999999" // RFC3339Nano with local timezone
  14. dateWithZone = "2006-01-02Z07:00" // RFC3339 with time at 00:00:00
  15. dateLocal = "2006-01-02" // RFC3339 with local timezone and time at 00:00:00
  16. )
  17. // GetTimestamp tries to parse given string as golang duration,
  18. // then RFC3339 time and finally as a Unix timestamp. If
  19. // any of these were successful, it returns a Unix timestamp
  20. // as string otherwise returns the given value back.
  21. // In case of duration input, the returned timestamp is computed
  22. // as the given reference time minus the amount of the duration.
  23. func GetTimestamp(value string, reference time.Time) (string, error) {
  24. if d, err := time.ParseDuration(value); value != "0" && err == nil {
  25. return strconv.FormatInt(reference.Add(-d).Unix(), 10), nil
  26. }
  27. var format string
  28. // if the string has a Z or a + or three dashes use parse otherwise use parseinlocation
  29. parseInLocation := !(strings.ContainsAny(value, "zZ+") || strings.Count(value, "-") == 3)
  30. if strings.Contains(value, ".") {
  31. if parseInLocation {
  32. format = rFC3339NanoLocal
  33. } else {
  34. format = time.RFC3339Nano
  35. }
  36. } else if strings.Contains(value, "T") {
  37. // we want the number of colons in the T portion of the timestamp
  38. tcolons := strings.Count(value, ":")
  39. // if parseInLocation is off and we have a +/- zone offset (not Z) then
  40. // there will be an extra colon in the input for the tz offset subtract that
  41. // colon from the tcolons count
  42. if !parseInLocation && !strings.ContainsAny(value, "zZ") && tcolons > 0 {
  43. tcolons--
  44. }
  45. if parseInLocation {
  46. switch tcolons {
  47. case 0:
  48. format = "2006-01-02T15"
  49. case 1:
  50. format = "2006-01-02T15:04"
  51. default:
  52. format = rFC3339Local
  53. }
  54. } else {
  55. switch tcolons {
  56. case 0:
  57. format = "2006-01-02T15Z07:00"
  58. case 1:
  59. format = "2006-01-02T15:04Z07:00"
  60. default:
  61. format = time.RFC3339
  62. }
  63. }
  64. } else if parseInLocation {
  65. format = dateLocal
  66. } else {
  67. format = dateWithZone
  68. }
  69. var t time.Time
  70. var err error
  71. if parseInLocation {
  72. t, err = time.ParseInLocation(format, value, time.FixedZone(reference.Zone()))
  73. } else {
  74. t, err = time.Parse(format, value)
  75. }
  76. if err != nil {
  77. // if there is a `-` then it's an RFC3339 like timestamp
  78. if strings.Contains(value, "-") {
  79. return "", err // was probably an RFC3339 like timestamp but the parser failed with an error
  80. }
  81. if _, _, err := parseTimestamp(value); err != nil {
  82. return "", fmt.Errorf("failed to parse value as time or duration: %q", value)
  83. }
  84. return value, nil // unix timestamp in and out case (meaning: the value passed at the command line is already in the right format for passing to the server)
  85. }
  86. return fmt.Sprintf("%d.%09d", t.Unix(), int64(t.Nanosecond())), nil
  87. }
  88. // ParseTimestamps returns seconds and nanoseconds from a timestamp that has the
  89. // format "%d.%09d", time.Unix(), int64(time.Nanosecond()))
  90. // if the incoming nanosecond portion is longer or shorter than 9 digits it is
  91. // converted to nanoseconds. The expectation is that the seconds and
  92. // seconds will be used to create a time variable. For example:
  93. // seconds, nanoseconds, err := ParseTimestamp("1136073600.000000001",0)
  94. // if err == nil since := time.Unix(seconds, nanoseconds)
  95. // returns seconds as def(aultSeconds) if value == ""
  96. func ParseTimestamps(value string, def int64) (int64, int64, error) {
  97. if value == "" {
  98. return def, 0, nil
  99. }
  100. return parseTimestamp(value)
  101. }
  102. func parseTimestamp(value string) (int64, int64, error) {
  103. sa := strings.SplitN(value, ".", 2)
  104. s, err := strconv.ParseInt(sa[0], 10, 64)
  105. if err != nil {
  106. return s, 0, err
  107. }
  108. if len(sa) != 2 {
  109. return s, 0, nil
  110. }
  111. n, err := strconv.ParseInt(sa[1], 10, 64)
  112. if err != nil {
  113. return s, n, err
  114. }
  115. // should already be in nanoseconds but just in case convert n to nanoseconds
  116. n = int64(float64(n) * math.Pow(float64(10), float64(9-len(sa[1]))))
  117. return s, n, nil
  118. }