timeutil.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package timeutil
  2. import (
  3. "errors"
  4. "fmt"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "time"
  10. )
  11. const (
  12. // SecsPerMin expresses the amount of seconds in a minute
  13. SecsPerMin = 60.0
  14. // SecsPerHour expresses the amount of seconds in a minute
  15. SecsPerHour = 3600.0
  16. // SecsPerDay expressed the amount of seconds in a day
  17. SecsPerDay = 86400.0
  18. // MinsPerHour expresses the amount of minutes in an hour
  19. MinsPerHour = 60.0
  20. // MinsPerDay expresses the amount of minutes in a day
  21. MinsPerDay = 1440.0
  22. // HoursPerDay expresses the amount of hours in a day
  23. HoursPerDay = 24.0
  24. // HoursPerMonth expresses the amount of hours in a month
  25. HoursPerMonth = 730.0
  26. // DaysPerMonth expresses the amount of days in a month
  27. DaysPerMonth = 30.42
  28. )
  29. // DurationString converts a duration to a Prometheus-compatible string in
  30. // terms of days, hours, minutes, or seconds.
  31. func DurationString(duration time.Duration) string {
  32. durSecs := int64(duration.Seconds())
  33. durStr := ""
  34. if durSecs > 0 {
  35. if durSecs%SecsPerDay == 0 {
  36. // convert to days
  37. durStr = fmt.Sprintf("%dd", durSecs/SecsPerDay)
  38. } else if durSecs%SecsPerHour == 0 {
  39. // convert to hours
  40. durStr = fmt.Sprintf("%dh", durSecs/SecsPerHour)
  41. } else if durSecs%SecsPerMin == 0 {
  42. // convert to mins
  43. durStr = fmt.Sprintf("%dm", durSecs/SecsPerMin)
  44. } else if durSecs > 0 {
  45. // default to secs, as long as duration is positive
  46. durStr = fmt.Sprintf("%ds", durSecs)
  47. }
  48. }
  49. return durStr
  50. }
  51. // DurationToPromOffsetString returns a Prometheus formatted string with leading offset or empty string if given a negative duration
  52. func DurationToPromOffsetString(duration time.Duration) string {
  53. dirStr := DurationString(duration)
  54. if dirStr != "" {
  55. dirStr = fmt.Sprintf("offset %s", dirStr)
  56. }
  57. return dirStr
  58. }
  59. // DurationOffsetStrings converts a (duration, offset) pair to Prometheus-
  60. // compatible strings in terms of days, hours, minutes, or seconds.
  61. func DurationOffsetStrings(duration, offset time.Duration) (string, string) {
  62. return DurationString(duration), DurationString(offset)
  63. }
  64. // FormatStoreResolution provides a clean notation for ETL store resolutions.
  65. // e.g. daily => 1d; hourly => 1h
  66. func FormatStoreResolution(dur time.Duration) string {
  67. if dur >= 24*time.Hour {
  68. return fmt.Sprintf("%dd", int(dur.Hours()/24.0))
  69. } else if dur >= time.Hour {
  70. return fmt.Sprintf("%dh", int(dur.Hours()))
  71. }
  72. return fmt.Sprint(dur)
  73. }
  74. // ParseDuration parses a duration string.
  75. // A duration string is a possibly signed sequence of
  76. // decimal numbers, each with optional fraction and a unit suffix,
  77. // such as "300ms", "-1.5h" or "2h45m".
  78. // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h", "d"
  79. func ParseDuration(duration string) (time.Duration, error) {
  80. duration = CleanDurationString(duration)
  81. return goParseDuration(duration)
  82. }
  83. // unitMap contains a list of units that can be parsed by ParseDuration
  84. var unitMap = map[string]int64{
  85. "ns": int64(time.Nanosecond),
  86. "us": int64(time.Microsecond),
  87. "µs": int64(time.Microsecond), // U+00B5 = micro symbol
  88. "μs": int64(time.Microsecond), // U+03BC = Greek letter mu
  89. "ms": int64(time.Millisecond),
  90. "s": int64(time.Second),
  91. "m": int64(time.Minute),
  92. "h": int64(time.Hour),
  93. "d": int64(time.Hour * 24),
  94. }
  95. // goParseDuration is time.ParseDuration lifted from the go std library and enhanced with the ability to
  96. // handle the "d" (day) unit. The contents of the function itself are identical to the std library, it is
  97. // only the unitMap above that contains the added unit.
  98. func goParseDuration(s string) (time.Duration, error) {
  99. // [-+]?([0-9]*(\.[0-9]*)?[a-z]+)+
  100. orig := s
  101. var d int64
  102. neg := false
  103. // Consume [-+]?
  104. if s != "" {
  105. c := s[0]
  106. if c == '-' || c == '+' {
  107. neg = c == '-'
  108. s = s[1:]
  109. }
  110. }
  111. // Special case: if all that is left is "0", this is zero.
  112. if s == "0" {
  113. return 0, nil
  114. }
  115. if s == "" {
  116. return 0, errors.New("time: invalid duration " + quote(orig))
  117. }
  118. for s != "" {
  119. var (
  120. v, f int64 // integers before, after decimal point
  121. scale float64 = 1 // value = v + f/scale
  122. )
  123. var err error
  124. // The next character must be [0-9.]
  125. if !(s[0] == '.' || '0' <= s[0] && s[0] <= '9') {
  126. return 0, errors.New("time: invalid duration " + quote(orig))
  127. }
  128. // Consume [0-9]*
  129. pl := len(s)
  130. v, s, err = leadingInt(s)
  131. if err != nil {
  132. return 0, errors.New("time: invalid duration " + quote(orig))
  133. }
  134. pre := pl != len(s) // whether we consumed anything before a period
  135. // Consume (\.[0-9]*)?
  136. post := false
  137. if s != "" && s[0] == '.' {
  138. s = s[1:]
  139. pl := len(s)
  140. f, scale, s = leadingFraction(s)
  141. post = pl != len(s)
  142. }
  143. if !pre && !post {
  144. // no digits (e.g. ".s" or "-.s")
  145. return 0, errors.New("time: invalid duration " + quote(orig))
  146. }
  147. // Consume unit.
  148. i := 0
  149. for ; i < len(s); i++ {
  150. c := s[i]
  151. if c == '.' || '0' <= c && c <= '9' {
  152. break
  153. }
  154. }
  155. if i == 0 {
  156. return 0, errors.New("time: missing unit in duration " + quote(orig))
  157. }
  158. u := s[:i]
  159. s = s[i:]
  160. unit, ok := unitMap[u]
  161. if !ok {
  162. return 0, errors.New("time: unknown unit " + quote(u) + " in duration " + quote(orig))
  163. }
  164. if v > (1<<63-1)/unit {
  165. // overflow
  166. return 0, errors.New("time: invalid duration " + quote(orig))
  167. }
  168. v *= unit
  169. if f > 0 {
  170. // float64 is needed to be nanosecond accurate for fractions of hours.
  171. // v >= 0 && (f*unit/scale) <= 3.6e+12 (ns/h, h is the largest unit)
  172. v += int64(float64(f) * (float64(unit) / scale))
  173. if v < 0 {
  174. // overflow
  175. return 0, errors.New("time: invalid duration " + quote(orig))
  176. }
  177. }
  178. d += v
  179. if d < 0 {
  180. // overflow
  181. return 0, errors.New("time: invalid duration " + quote(orig))
  182. }
  183. }
  184. if neg {
  185. d = -d
  186. }
  187. return time.Duration(d), nil
  188. }
  189. // CleanDurationString removes prometheus formatted prefix "offset " allong with leading a trailing whitespace
  190. // from duration string, leaving behind a string with format [0-9+](s|m|d|h)
  191. func CleanDurationString(duration string) string {
  192. duration = strings.TrimSpace(duration)
  193. duration = strings.TrimPrefix(duration, "offset ")
  194. return duration
  195. }
  196. // ParseTimeRange returns a start and end time, respectively, which are converted from
  197. // a duration and offset, defined as strings with Prometheus-style syntax.
  198. func ParseTimeRange(duration, offset time.Duration) (time.Time, time.Time) {
  199. // endTime defaults to the current time, unless an offset is explicity declared,
  200. // in which case it shifts endTime back by given duration
  201. endTime := time.Now()
  202. if offset > 0 {
  203. endTime = endTime.Add(-1 * offset)
  204. }
  205. startTime := endTime.Add(-1 * duration)
  206. return startTime, endTime
  207. }
  208. // FormatDurationStringDaysToHours converts string from format [0-9+]d to [0-9+]h
  209. func FormatDurationStringDaysToHours(param string) (string, error) {
  210. //check that input matches format
  211. ok, err := regexp.MatchString("[0-9+]d", param)
  212. if !ok {
  213. return param, fmt.Errorf("FormatDurationStringDaysToHours: input string (%s) not formatted as [0-9+]d", param)
  214. }
  215. if err != nil {
  216. return "", err
  217. }
  218. // convert days to hours
  219. if param[len(param)-1:] == "d" {
  220. count := param[:len(param)-1]
  221. val, err := strconv.ParseInt(count, 10, 64)
  222. if err != nil {
  223. return "", err
  224. }
  225. val = val * 24
  226. param = fmt.Sprintf("%dh", val)
  227. }
  228. return param, nil
  229. }
  230. // JobTicker is a ticker used to synchronize the next run of a repeating
  231. // process. The designated use-case is for infinitely-looping selects,
  232. // where a timeout or an exit channel might cancel the process, but otherwise
  233. // the intent is to wait at the select for some amount of time until the
  234. // next run. This differs from a standard ticker, which ticks without
  235. // waiting and drops any missed ticks; rather, this ticker must be kicked
  236. // off manually for each tick, so that after the current run of the job
  237. // completes, the timer starts again.
  238. type JobTicker struct {
  239. Ch <-chan time.Time
  240. ch chan time.Time
  241. closed bool
  242. mx sync.Mutex
  243. }
  244. // NewJobTicker instantiates a new JobTicker.
  245. func NewJobTicker() *JobTicker {
  246. c := make(chan time.Time)
  247. return &JobTicker{
  248. Ch: c,
  249. ch: c,
  250. closed: false,
  251. }
  252. }
  253. // Close closes the JobTicker channels
  254. func (jt *JobTicker) Close() {
  255. jt.mx.Lock()
  256. defer jt.mx.Unlock()
  257. if jt.closed {
  258. return
  259. }
  260. jt.closed = true
  261. close(jt.ch)
  262. }
  263. // TickAt schedules the next tick of the ticker for the given time in the
  264. // future. If the time is not in the future, the ticker will tick immediately.
  265. func (jt *JobTicker) TickAt(t time.Time) {
  266. go func(t time.Time) {
  267. n := time.Now()
  268. if t.After(n) {
  269. time.Sleep(t.Sub(n))
  270. }
  271. jt.mx.Lock()
  272. defer jt.mx.Unlock()
  273. if !jt.closed {
  274. jt.ch <- time.Now()
  275. }
  276. }(t)
  277. }
  278. // TickIn schedules the next tick of the ticker for the given duration into
  279. // the future. If the duration is less than or equal to zero, the ticker will
  280. // tick immediately.
  281. func (jt *JobTicker) TickIn(d time.Duration) {
  282. go func(d time.Duration) {
  283. if d > 0 {
  284. time.Sleep(d)
  285. }
  286. jt.mx.Lock()
  287. defer jt.mx.Unlock()
  288. if !jt.closed {
  289. jt.ch <- time.Now()
  290. }
  291. }(d)
  292. }
  293. // NOTE: The following functions were lifted from the go std library to support the ParseDuration enhancement
  294. // NOTE: described above.
  295. const (
  296. lowerhex = "0123456789abcdef"
  297. runeSelf = 0x80
  298. runeError = '\uFFFD'
  299. )
  300. // quote is lifted from the go std library to support the custom ParseDuration enhancement
  301. func quote(s string) string {
  302. buf := make([]byte, 1, len(s)+2) // slice will be at least len(s) + quotes
  303. buf[0] = '"'
  304. for i, c := range s {
  305. if c >= runeSelf || c < ' ' {
  306. // This means you are asking us to parse a time.Duration or
  307. // time.Location with unprintable or non-ASCII characters in it.
  308. // We don't expect to hit this case very often. We could try to
  309. // reproduce strconv.Quote's behavior with full fidelity but
  310. // given how rarely we expect to hit these edge cases, speed and
  311. // conciseness are better.
  312. var width int
  313. if c == runeError {
  314. width = 1
  315. if i+2 < len(s) && s[i:i+3] == string(runeError) {
  316. width = 3
  317. }
  318. } else {
  319. width = len(string(c))
  320. }
  321. for j := 0; j < width; j++ {
  322. buf = append(buf, `\x`...)
  323. buf = append(buf, lowerhex[s[i+j]>>4])
  324. buf = append(buf, lowerhex[s[i+j]&0xF])
  325. }
  326. } else {
  327. if c == '"' || c == '\\' {
  328. buf = append(buf, '\\')
  329. }
  330. buf = append(buf, string(c)...)
  331. }
  332. }
  333. buf = append(buf, '"')
  334. return string(buf)
  335. }
  336. // leadingFraction consumes the leading [0-9]* from s.
  337. // It is used only for fractions, so does not return an error on overflow,
  338. // it just stops accumulating precision.
  339. func leadingFraction(s string) (x int64, scale float64, rem string) {
  340. i := 0
  341. scale = 1
  342. overflow := false
  343. for ; i < len(s); i++ {
  344. c := s[i]
  345. if c < '0' || c > '9' {
  346. break
  347. }
  348. if overflow {
  349. continue
  350. }
  351. if x > (1<<63-1)/10 {
  352. // It's possible for overflow to give a positive number, so take care.
  353. overflow = true
  354. continue
  355. }
  356. y := x*10 + int64(c) - '0'
  357. if y < 0 {
  358. overflow = true
  359. continue
  360. }
  361. x = y
  362. scale *= 10
  363. }
  364. return x, scale, s[i:]
  365. }
  366. var errLeadingInt = errors.New("time: bad [0-9]*") // never printed
  367. // leadingInt consumes the leading [0-9]* from s.
  368. func leadingInt(s string) (x int64, rem string, err error) {
  369. i := 0
  370. for ; i < len(s); i++ {
  371. c := s[i]
  372. if c < '0' || c > '9' {
  373. break
  374. }
  375. if x > (1<<63-1)/10 {
  376. // overflow
  377. return 0, "", errLeadingInt
  378. }
  379. x = x*10 + int64(c) - '0'
  380. if x < 0 {
  381. // overflow
  382. return 0, "", errLeadingInt
  383. }
  384. }
  385. return x, s[i:], nil
  386. }