MinuteToDataTime.go 957 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package lib
  2. import (
  3. "strconv"
  4. )
  5. /*
  6. 时间常量
  7. */
  8. const (
  9. //定义每分钟的秒数
  10. SecondsPerMinute = 60
  11. //定义每小时的秒数
  12. SecondsPerHour = SecondsPerMinute * 60
  13. //定义每天的秒数
  14. SecondsPerDay = SecondsPerHour * 24
  15. )
  16. /*
  17. 时间转换函数
  18. */
  19. func resolveTime(seconds int) (day int, hour int, minute int, second int) {
  20. //秒
  21. second = seconds % 60
  22. //天
  23. day = seconds / SecondsPerDay
  24. seconds -= day * SecondsPerDay
  25. //时
  26. hour = seconds / SecondsPerHour
  27. seconds -= hour * SecondsPerHour
  28. //分
  29. minute = seconds / SecondsPerMinute
  30. seconds -= minute * SecondsPerHour
  31. return
  32. }
  33. func MinuteToDataTime(t int) string {
  34. str := ""
  35. day, hour, minute, second := resolveTime(t)
  36. if day > 0 {
  37. str += strconv.Itoa(day) + "天 "
  38. }
  39. if hour > 0 {
  40. str += strconv.Itoa(hour) + "小时 "
  41. }
  42. if minute > 0 {
  43. str += strconv.Itoa(minute) + "分钟 "
  44. }
  45. if second > 0 {
  46. str += strconv.Itoa(second) + "秒"
  47. }
  48. return str
  49. }