time.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package util
  2. import "fmt"
  3. /*
  4. 时间常量
  5. */
  6. const (
  7. //定义每分钟的秒数
  8. SecondsPerMinute int64 = 60
  9. //定义每小时的秒数
  10. SecondsPerHour int64 = SecondsPerMinute * 60
  11. //定义每天的秒数
  12. SecondsPerDay int64 = SecondsPerHour * 24
  13. )
  14. /*
  15. 时间转换函数
  16. */
  17. func resolveTime(seconds int64) (day int64, hour int64, minute int64) {
  18. //每分钟秒数
  19. minute = seconds / SecondsPerMinute
  20. //每小时秒数
  21. hour = seconds / SecondsPerHour
  22. //每天秒数
  23. day = seconds / SecondsPerDay
  24. return
  25. }
  26. func TimeTransformation(unix int64) string {
  27. _, hour, _ := resolveTime(unix)
  28. now := ""
  29. if hour < 10 {
  30. now = fmt.Sprintf("0%d", hour)
  31. } else {
  32. now = fmt.Sprintf("%d", hour)
  33. }
  34. minute := (unix - (hour * SecondsPerHour)) / 60
  35. fmt.Println(minute, (hour * SecondsPerHour))
  36. if minute < 10 {
  37. now = fmt.Sprintf("%s:0%d", now, minute)
  38. } else {
  39. now = fmt.Sprintf("%s:%d", now, minute)
  40. }
  41. seconds := unix - (hour*SecondsPerHour + SecondsPerMinute*minute)
  42. if seconds < 10 {
  43. now = fmt.Sprintf("%s:0%d", now, seconds)
  44. } else {
  45. now = fmt.Sprintf("%s:%d", now, seconds)
  46. }
  47. return now
  48. }