Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

sec_to_time.go 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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(duration int64) string {
  16. formattedTime := ""
  17. // The following four variables are calculated by taking
  18. // into account the previously calculated variables, this avoids
  19. // pitfalls when using remainders. As that could lead to incorrect
  20. // results when the calculated number equals the quotient number.
  21. remainingDays := duration / (60 * 60 * 24)
  22. years := remainingDays / 365
  23. remainingDays -= years * 365
  24. months := remainingDays * 12 / 365
  25. remainingDays -= months * 365 / 12
  26. weeks := remainingDays / 7
  27. remainingDays -= weeks * 7
  28. days := remainingDays
  29. // The following three variables are calculated without depending
  30. // on the previous calculated variables.
  31. hours := (duration / 3600) % 24
  32. minutes := (duration / 60) % 60
  33. seconds := duration % 60
  34. // Extract only the relevant information of the time
  35. // If the time is greater than a year, it makes no sense to display seconds.
  36. switch {
  37. case years > 0:
  38. formattedTime = formatTime(years, "year", formattedTime)
  39. formattedTime = formatTime(months, "month", formattedTime)
  40. case months > 0:
  41. formattedTime = formatTime(months, "month", formattedTime)
  42. formattedTime = formatTime(weeks, "week", formattedTime)
  43. case weeks > 0:
  44. formattedTime = formatTime(weeks, "week", formattedTime)
  45. formattedTime = formatTime(days, "day", formattedTime)
  46. case days > 0:
  47. formattedTime = formatTime(days, "day", formattedTime)
  48. formattedTime = formatTime(hours, "hour", formattedTime)
  49. case hours > 0:
  50. formattedTime = formatTime(hours, "hour", formattedTime)
  51. formattedTime = formatTime(minutes, "minute", formattedTime)
  52. default:
  53. formattedTime = formatTime(minutes, "minute", formattedTime)
  54. formattedTime = formatTime(seconds, "second", formattedTime)
  55. }
  56. // The formatTime() function always appends a space at the end. This will be trimmed
  57. return strings.TrimRight(formattedTime, " ")
  58. }
  59. // formatTime appends the given value to the existing forammattedTime. E.g:
  60. // formattedTime = "1 year"
  61. // input: value = 3, name = "month"
  62. // output will be "1 year 3 months "
  63. func formatTime(value int64, name, formattedTime string) string {
  64. if value == 1 {
  65. formattedTime = fmt.Sprintf("%s1 %s ", formattedTime, name)
  66. } else if value > 1 {
  67. formattedTime = fmt.Sprintf("%s%d %ss ", formattedTime, value, name)
  68. }
  69. return formattedTime
  70. }