timestamp.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. var parseInLocation bool
  29. // if the string has a Z or a + or three dashes use parse otherwise use parseinlocation
  30. parseInLocation = !(strings.ContainsAny(value, "zZ+") || strings.Count(value, "-") == 3)
  31. if strings.Contains(value, ".") {
  32. if parseInLocation {
  33. format = rFC3339NanoLocal
  34. } else {
  35. format = time.RFC3339Nano
  36. }
  37. } else if strings.Contains(value, "T") {
  38. // we want the number of colons in the T portion of the timestamp
  39. tcolons := strings.Count(value, ":")
  40. // if parseInLocation is off and we have a +/- zone offset (not Z) then
  41. // there will be an extra colon in the input for the tz offset subtract that
  42. // colon from the tcolons count
  43. if !parseInLocation && !strings.ContainsAny(value, "zZ") && tcolons > 0 {
  44. tcolons--
  45. }
  46. if parseInLocation {
  47. switch tcolons {
  48. case 0:
  49. format = "2006-01-02T15"
  50. case 1:
  51. format = "2006-01-02T15:04"
  52. default:
  53. format = rFC3339Local
  54. }
  55. } else {
  56. switch tcolons {
  57. case 0:
  58. format = "2006-01-02T15Z07:00"
  59. case 1:
  60. format = "2006-01-02T15:04Z07:00"
  61. default:
  62. format = time.RFC3339
  63. }
  64. }
  65. } else if parseInLocation {
  66. format = dateLocal
  67. } else {
  68. format = dateWithZone
  69. }
  70. var t time.Time
  71. var err error
  72. if parseInLocation {
  73. t, err = time.ParseInLocation(format, value, time.FixedZone(reference.Zone()))
  74. } else {
  75. t, err = time.Parse(format, value)
  76. }
  77. if err != nil {
  78. // if there is a `-` then its an RFC3339 like timestamp otherwise assume unixtimestamp
  79. if strings.Contains(value, "-") {
  80. return "", err // was probably an RFC3339 like timestamp but the parser failed with an error
  81. }
  82. 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)
  83. }
  84. return fmt.Sprintf("%d.%09d", t.Unix(), int64(t.Nanosecond())), nil
  85. }
  86. // ParseTimestamps returns seconds and nanoseconds from a timestamp that has the
  87. // format "%d.%09d", time.Unix(), int64(time.Nanosecond()))
  88. // if the incoming nanosecond portion is longer or shorter than 9 digits it is
  89. // converted to nanoseconds. The expectation is that the seconds and
  90. // seconds will be used to create a time variable. For example:
  91. // seconds, nanoseconds, err := ParseTimestamp("1136073600.000000001",0)
  92. // if err == nil since := time.Unix(seconds, nanoseconds)
  93. // returns seconds as def(aultSeconds) if value == ""
  94. func ParseTimestamps(value string, def int64) (int64, int64, error) {
  95. if value == "" {
  96. return def, 0, nil
  97. }
  98. sa := strings.SplitN(value, ".", 2)
  99. s, err := strconv.ParseInt(sa[0], 10, 64)
  100. if err != nil {
  101. return s, 0, err
  102. }
  103. if len(sa) != 2 {
  104. return s, 0, nil
  105. }
  106. n, err := strconv.ParseInt(sa[1], 10, 64)
  107. if err != nil {
  108. return s, n, err
  109. }
  110. // should already be in nanoseconds but just in case convert n to nanoseonds
  111. n = int64(float64(n) * math.Pow(float64(10), float64(9-len(sa[1]))))
  112. return s, n, nil
  113. }