time_diff.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package mhayaTime
  2. // DiffInYears 相差多少年
  3. func (c MhayaTime) DiffInYears(end MhayaTime) int64 {
  4. return c.DiffInMonths(end) / 12
  5. }
  6. // DiffInYearsWithAbs 相差多少年(绝对值)
  7. func (c MhayaTime) DiffInYearsWithAbs(end MhayaTime) int64 {
  8. return GetAbsValue(c.DiffInYears(end))
  9. }
  10. // DiffInMonths 相差多少月
  11. func (c MhayaTime) DiffInMonths(end MhayaTime) int64 {
  12. dy, dm, dd := end.Year()-c.Year(), end.Month()-c.Month(), end.Day()-c.Day()
  13. if dd < 0 {
  14. dm = dm - 1
  15. }
  16. if dy == 0 && dm == 0 {
  17. return 0
  18. }
  19. if dy == 0 && dm != 0 && dd != 0 {
  20. if int(end.DiffInHoursWithAbs(c)) < c.DaysInMonth()*HoursPerDay {
  21. return 0
  22. }
  23. return int64(dm)
  24. }
  25. return int64(dy*MonthsPerYear + dm)
  26. }
  27. // DiffInMonthsWithAbs 相差多少月(绝对值)
  28. func (c MhayaTime) DiffInMonthsWithAbs(end MhayaTime) int64 {
  29. return GetAbsValue(c.DiffInMonths(end))
  30. }
  31. // DiffInWeeks 相差多少周
  32. func (c MhayaTime) DiffInWeeks(end MhayaTime) int64 {
  33. return c.DiffInDays(end) / DaysPerWeek
  34. }
  35. // DiffInWeeksWithAbs 相差多少周(绝对值)
  36. func (c MhayaTime) DiffInWeeksWithAbs(end MhayaTime) int64 {
  37. return GetAbsValue(c.DiffInWeeks(end))
  38. }
  39. // DiffInDays 相差多少天
  40. func (c MhayaTime) DiffInDays(end MhayaTime) int64 {
  41. return c.DiffInSeconds(end) / SecondsPerDay
  42. }
  43. // DiffInDaysWithAbs 相差多少天(绝对值)
  44. func (c MhayaTime) DiffInDaysWithAbs(end MhayaTime) int64 {
  45. return GetAbsValue(c.DiffInDays(end))
  46. }
  47. // DiffInHours 相差多少小时
  48. func (c MhayaTime) DiffInHours(end MhayaTime) int64 {
  49. return c.DiffInSeconds(end) / SecondsPerHour
  50. }
  51. // DiffInHoursWithAbs 相差多少小时(绝对值)
  52. func (c MhayaTime) DiffInHoursWithAbs(end MhayaTime) int64 {
  53. return GetAbsValue(c.DiffInHours(end))
  54. }
  55. // DiffInMinutes 相差多少分钟
  56. func (c MhayaTime) DiffInMinutes(end MhayaTime) int64 {
  57. return c.DiffInSeconds(end) / SecondsPerMinute
  58. }
  59. // DiffInMinutesWithAbs 相差多少分钟(绝对值)
  60. func (c MhayaTime) DiffInMinutesWithAbs(end MhayaTime) int64 {
  61. return GetAbsValue(c.DiffInMinutes(end))
  62. }
  63. // DiffInSeconds 相差多少秒
  64. func (c MhayaTime) DiffInSeconds(end MhayaTime) int64 {
  65. return end.ToSecond() - c.ToSecond()
  66. }
  67. // DiffInSecondsWithAbs 相差多少秒(绝对值)
  68. func (c MhayaTime) DiffInSecondsWithAbs(end MhayaTime) int64 {
  69. return GetAbsValue(c.DiffInSeconds(end))
  70. }
  71. // DiffInMillisecond 相差多少毫秒
  72. func (c MhayaTime) DiffInMillisecond(end MhayaTime) int64 {
  73. return end.ToMillisecond() - c.ToMillisecond()
  74. }
  75. // DiffInMicrosecond 相关多少微秒
  76. func (c MhayaTime) DiffInMicrosecond(end MhayaTime) int64 {
  77. return end.ToMicrosecond() - c.ToMicrosecond()
  78. }
  79. // DiffINanosecond 相关多少纳秒
  80. func (c MhayaTime) DiffINanosecond(end MhayaTime) int64 {
  81. return end.ToNanosecond() - c.ToNanosecond()
  82. }