timeutil.go 13 KB

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