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.

sec_to_time.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2022 Gitea. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package util
  4. import (
  5. "fmt"
  6. "strings"
  7. )
  8. // SecToTime converts an amount of seconds to a human-readable string. E.g.
  9. // 66s -> 1 minute 6 seconds
  10. // 52410s -> 14 hours 33 minutes
  11. // 563418 -> 6 days 12 hours
  12. // 1563418 -> 2 weeks 4 days
  13. // 3937125s -> 1 month 2 weeks
  14. // 45677465s -> 1 year 6 months
  15. func SecToTime(durationVal any) string {
  16. duration, _ := ToInt64(durationVal)
  17. formattedTime := ""
  18. // The following four variables are calculated by taking
  19. // into account the previously calculated variables, this avoids
  20. // pitfalls when using remainders. As that could lead to incorrect
  21. // results when the calculated number equals the quotient number.
  22. remainingDays := duration / (60 * 60 * 24)
  23. years := remainingDays / 365
  24. remainingDays -= years * 365
  25. months := remainingDays * 12 / 365
  26. remainingDays -= months * 365 / 12
  27. weeks := remainingDays / 7
  28. remainingDays -= weeks * 7
  29. days := remainingDays
  30. // The following three variables are calculated without depending
  31. // on the previous calculated variables.
  32. hours := (duration / 3600) % 24
  33. minutes := (duration / 60) % 60
  34. seconds := duration % 60
  35. // Extract only the relevant information of the time
  36. // If the time is greater than a year, it makes no sense to display seconds.
  37. switch {
  38. case years > 0:
  39. formattedTime = formatTime(years, "year", formattedTime)
  40. formattedTime = formatTime(months, "month", formattedTime)
  41. case months > 0:
  42. formattedTime = formatTime(months, "month", formattedTime)
  43. formattedTime = formatTime(weeks, "week", formattedTime)
  44. case weeks > 0:
  45. formattedTime = formatTime(weeks, "week", formattedTime)
  46. formattedTime = formatTime(days, "day", formattedTime)
  47. case days > 0:
  48. formattedTime = formatTime(days, "day", formattedTime)
  49. formattedTime = formatTime(hours, "hour", formattedTime)
  50. case hours > 0:
  51. formattedTime = formatTime(hours, "hour", formattedTime)
  52. formattedTime = formatTime(minutes, "minute", formattedTime)
  53. default:
  54. formattedTime = formatTime(minutes, "minute", formattedTime)
  55. formattedTime = formatTime(seconds, "second", formattedTime)
  56. }
  57. // The formatTime() function always appends a space at the end. This will be trimmed
  58. return strings.TrimRight(formattedTime, " ")
  59. }
  60. // formatTime appends the given value to the existing forammattedTime. E.g:
  61. // formattedTime = "1 year"
  62. // input: value = 3, name = "month"
  63. // output will be "1 year 3 months "
  64. func formatTime(value int64, name, formattedTime string) string {
  65. if value == 1 {
  66. formattedTime = fmt.Sprintf("%s1 %s ", formattedTime, name)
  67. } else if value > 1 {
  68. formattedTime = fmt.Sprintf("%s%d %ss ", formattedTime, value, name)
  69. }
  70. return formattedTime
  71. }