timestamp.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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
  89. // the format ("%d.%09d", time.Unix(), int64(time.Nanosecond())).
  90. // If the incoming nanosecond portion is longer than 9 digits it is truncated.
  91. // The expectation is that the seconds and nanoseconds will be used to create a
  92. // time variable. For example:
  93. //
  94. // seconds, nanoseconds, _ := ParseTimestamp("1136073600.000000001",0)
  95. // since := time.Unix(seconds, nanoseconds)
  96. //
  97. // returns seconds as defaultSeconds if value == ""
  98. func ParseTimestamps(value string, defaultSeconds int64) (seconds int64, nanoseconds int64, err error) {
  99. if value == "" {
  100. return defaultSeconds, 0, nil
  101. }
  102. return parseTimestamp(value)
  103. }
  104. func parseTimestamp(value string) (sec int64, nsec int64, err error) {
  105. s, n, ok := strings.Cut(value, ".")
  106. sec, err = strconv.ParseInt(s, 10, 64)
  107. if err != nil {
  108. return sec, 0, err
  109. }
  110. if !ok {
  111. return sec, 0, nil
  112. }
  113. nsec, err = strconv.ParseInt(n, 10, 64)
  114. if err != nil {
  115. return sec, nsec, err
  116. }
  117. // should already be in nanoseconds but just in case convert n to nanoseconds
  118. nsec = int64(float64(nsec) * math.Pow(float64(10), float64(9-len(n))))
  119. return sec, nsec, nil
  120. }