time.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package util
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. )
  7. const (
  8. // SecsPerMin expresses the amount of seconds in a minute
  9. SecsPerMin = 60.0
  10. // SecsPerHour expresses the amount of seconds in a minute
  11. SecsPerHour = 3600.0
  12. // SecsPerDay expressed the amount of seconds in a day
  13. SecsPerDay = 86400.0
  14. // MinsPerHour expresses the amount of minutes in an hour
  15. MinsPerHour = 60.0
  16. // MinsPerDay expresses the amount of minutes in a day
  17. MinsPerDay = 1440.0
  18. // HoursPerDay expresses the amount of hours in a day
  19. HoursPerDay = 24.0
  20. // HoursPerMonth expresses the amount of hours in a month
  21. HoursPerMonth = 730.0
  22. // DaysPerMonth expresses the amount of days in a month
  23. DaysPerMonth = 30.42
  24. )
  25. // DurationString converts a duration to a Prometheus-compatible string in
  26. // terms of days, hours, minutes, or seconds.
  27. func DurationString(duration time.Duration) string {
  28. durSecs := int64(duration.Seconds())
  29. durStr := ""
  30. if durSecs > 0 {
  31. if durSecs%SecsPerDay == 0 {
  32. // convert to days
  33. durStr = fmt.Sprintf("%dd", durSecs/SecsPerDay)
  34. } else if durSecs%SecsPerHour == 0 {
  35. // convert to hours
  36. durStr = fmt.Sprintf("%dh", durSecs/SecsPerHour)
  37. } else if durSecs%SecsPerMin == 0 {
  38. // convert to mins
  39. durStr = fmt.Sprintf("%dm", durSecs/SecsPerMin)
  40. } else if durSecs > 0 {
  41. // default to mins, as long as duration is positive
  42. durStr = fmt.Sprintf("%ds", durSecs)
  43. }
  44. }
  45. return durStr
  46. }
  47. // DurationOffsetStrings converts a (duration, offset) pair to Prometheus-
  48. // compatible strings in terms of days, hours, minutes, or seconds.
  49. func DurationOffsetStrings(duration, offset time.Duration) (string, string) {
  50. return DurationString(duration), DurationString(offset)
  51. }
  52. // ParseDuration converts a Prometheus-style duration string into a Duration
  53. func ParseDuration(duration string) (*time.Duration, error) {
  54. unitStr := duration[len(duration)-1:]
  55. var unit time.Duration
  56. switch unitStr {
  57. case "s":
  58. unit = time.Second
  59. case "m":
  60. unit = time.Minute
  61. case "h":
  62. unit = time.Hour
  63. case "d":
  64. unit = 24.0 * time.Hour
  65. default:
  66. return nil, fmt.Errorf("error parsing duration: %s did not match expected format [0-9+](s|m|d|h)", duration)
  67. }
  68. amountStr := duration[:len(duration)-1]
  69. amount, err := strconv.ParseInt(amountStr, 10, 64)
  70. if err != nil {
  71. return nil, fmt.Errorf("error parsing duration: %s did not match expected format [0-9+](s|m|d|h)", duration)
  72. }
  73. dur := time.Duration(amount) * unit
  74. return &dur, nil
  75. }
  76. // ParseTimeRange returns a start and end time, respectively, which are converted from
  77. // a duration and offset, defined as strings with Prometheus-style syntax.
  78. func ParseTimeRange(duration, offset string) (*time.Time, *time.Time, error) {
  79. // endTime defaults to the current time, unless an offset is explicity declared,
  80. // in which case it shifts endTime back by given duration
  81. endTime := time.Now()
  82. if offset != "" {
  83. o, err := ParseDuration(offset)
  84. if err != nil {
  85. return nil, nil, fmt.Errorf("error parsing offset (%s): %s", offset, err)
  86. }
  87. endTime = endTime.Add(-1 * *o)
  88. }
  89. // if duration is defined in terms of days, convert to hours
  90. // e.g. convert "2d" to "48h"
  91. durationNorm, err := normalizeTimeParam(duration)
  92. if err != nil {
  93. return nil, nil, fmt.Errorf("error parsing duration (%s): %s", duration, err)
  94. }
  95. // convert time duration into start and end times, formatted
  96. // as ISO datetime strings
  97. dur, err := time.ParseDuration(durationNorm)
  98. if err != nil {
  99. return nil, nil, fmt.Errorf("errorf parsing duration (%s): %s", durationNorm, err)
  100. }
  101. startTime := endTime.Add(-1 * dur)
  102. return &startTime, &endTime, nil
  103. }
  104. func normalizeTimeParam(param string) (string, error) {
  105. // convert days to hours
  106. if param[len(param)-1:] == "d" {
  107. count := param[:len(param)-1]
  108. val, err := strconv.ParseInt(count, 10, 64)
  109. if err != nil {
  110. return "", err
  111. }
  112. val = val * 24
  113. param = fmt.Sprintf("%dh", val)
  114. }
  115. return param, nil
  116. }