You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

time_str.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2022 Gitea. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package util
  4. import (
  5. "fmt"
  6. "math"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. )
  11. var (
  12. // Time estimate match regex
  13. rTimeEstimateOnlyHours = regexp.MustCompile(`^([\d]+)$`)
  14. rTimeEstimateWeeks = regexp.MustCompile(`([\d]+)w`)
  15. rTimeEstimateDays = regexp.MustCompile(`([\d]+)d`)
  16. rTimeEstimateHours = regexp.MustCompile(`([\d]+)h`)
  17. rTimeEstimateMinutes = regexp.MustCompile(`([\d]+)m`)
  18. )
  19. // TimeEstimateFromStr returns time estimate in seconds from formatted string
  20. func TimeEstimateFromStr(timeStr string) int64 {
  21. timeTotal := 0
  22. // If single number entered, assume hours
  23. timeStrMatches := rTimeEstimateOnlyHours.FindStringSubmatch(timeStr)
  24. if len(timeStrMatches) > 0 {
  25. raw, _ := strconv.Atoi(timeStrMatches[1])
  26. timeTotal += raw * (60 * 60)
  27. } else {
  28. // Find time weeks
  29. timeStrMatches = rTimeEstimateWeeks.FindStringSubmatch(timeStr)
  30. if len(timeStrMatches) > 0 {
  31. raw, _ := strconv.Atoi(timeStrMatches[1])
  32. timeTotal += raw * (60 * 60 * 24 * 7)
  33. }
  34. // Find time days
  35. timeStrMatches = rTimeEstimateDays.FindStringSubmatch(timeStr)
  36. if len(timeStrMatches) > 0 {
  37. raw, _ := strconv.Atoi(timeStrMatches[1])
  38. timeTotal += raw * (60 * 60 * 24)
  39. }
  40. // Find time hours
  41. timeStrMatches = rTimeEstimateHours.FindStringSubmatch(timeStr)
  42. if len(timeStrMatches) > 0 {
  43. raw, _ := strconv.Atoi(timeStrMatches[1])
  44. timeTotal += raw * (60 * 60)
  45. }
  46. // Find time minutes
  47. timeStrMatches = rTimeEstimateMinutes.FindStringSubmatch(timeStr)
  48. if len(timeStrMatches) > 0 {
  49. raw, _ := strconv.Atoi(timeStrMatches[1])
  50. timeTotal += raw * (60)
  51. }
  52. }
  53. return int64(timeTotal)
  54. }
  55. // TimeEstimateStr returns formatted time estimate string from seconds (e.g. "2w 4d 12h 5m")
  56. func TimeEstimateToStr(amount int64) string {
  57. var timeParts []string
  58. timeSeconds := float64(amount)
  59. // Format weeks
  60. weeks := math.Floor(timeSeconds / (60 * 60 * 24 * 7))
  61. if weeks > 0 {
  62. timeParts = append(timeParts, fmt.Sprintf("%dw", int64(weeks)))
  63. }
  64. timeSeconds -= weeks * (60 * 60 * 24 * 7)
  65. // Format days
  66. days := math.Floor(timeSeconds / (60 * 60 * 24))
  67. if days > 0 {
  68. timeParts = append(timeParts, fmt.Sprintf("%dd", int64(days)))
  69. }
  70. timeSeconds -= days * (60 * 60 * 24)
  71. // Format hours
  72. hours := math.Floor(timeSeconds / (60 * 60))
  73. if hours > 0 {
  74. timeParts = append(timeParts, fmt.Sprintf("%dh", int64(hours)))
  75. }
  76. timeSeconds -= hours * (60 * 60)
  77. // Format minutes
  78. minutes := math.Floor(timeSeconds / (60))
  79. if minutes > 0 {
  80. timeParts = append(timeParts, fmt.Sprintf("%dm", int64(minutes)))
  81. }
  82. return strings.Join(timeParts, " ")
  83. }