timestamp.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package 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 otherwise assume unixtimestamp
  78. if strings.Contains(value, "-") {
  79. return "", err // was probably an RFC3339 like timestamp but the parser failed with an error
  80. }
  81. return value, nil // unixtimestamp in and out case (meaning: the value passed at the command line is already in the right format for passing to the server)
  82. }
  83. return fmt.Sprintf("%d.%09d", t.Unix(), int64(t.Nanosecond())), nil
  84. }
  85. // ParseTimestamps returns seconds and nanoseconds from a timestamp that has the
  86. // format "%d.%09d", time.Unix(), int64(time.Nanosecond()))
  87. // if the incoming nanosecond portion is longer or shorter than 9 digits it is
  88. // converted to nanoseconds. The expectation is that the seconds and
  89. // seconds will be used to create a time variable. For example:
  90. // seconds, nanoseconds, err := ParseTimestamp("1136073600.000000001",0)
  91. // if err == nil since := time.Unix(seconds, nanoseconds)
  92. // returns seconds as def(aultSeconds) if value == ""
  93. func ParseTimestamps(value string, def int64) (int64, int64, error) {
  94. if value == "" {
  95. return def, 0, nil
  96. }
  97. sa := strings.SplitN(value, ".", 2)
  98. s, err := strconv.ParseInt(sa[0], 10, 64)
  99. if err != nil {
  100. return s, 0, err
  101. }
  102. if len(sa) != 2 {
  103. return s, 0, nil
  104. }
  105. n, err := strconv.ParseInt(sa[1], 10, 64)
  106. if err != nil {
  107. return s, n, err
  108. }
  109. // should already be in nanoseconds but just in case convert n to nanoseconds
  110. n = int64(float64(n) * math.Pow(float64(10), float64(9-len(sa[1]))))
  111. return s, n, nil
  112. }