timeutils.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package timeutils
  2. import (
  3. "time"
  4. )
  5. var (
  6. monthMap = map[string]string{
  7. "01": "一月",
  8. "02": "二月",
  9. "03": "三月",
  10. "04": "四月",
  11. "05": "五月",
  12. "06": "六月",
  13. "07": "七月",
  14. "08": "八月",
  15. "09": "九月",
  16. "10": "十月",
  17. "11": "十一月",
  18. "12": "十二月",
  19. }
  20. weekMap = map[string]string{
  21. "Monday": "星期一",
  22. "Tuesday": "星期二",
  23. "Wednesday": "星期三",
  24. "Thursday": "星期四",
  25. "Friday": "星期五",
  26. "Saturday": "星期六",
  27. "Sunday": "星期日",
  28. }
  29. )
  30. func ParseLocalDateTime(dateTime string) *time.Time {
  31. t, err := time.ParseInLocation(time.DateTime, dateTime, time.Local)
  32. if err != nil {
  33. panic(err)
  34. }
  35. return &t
  36. }
  37. func ParseLocalDateOnly(date string) *time.Time {
  38. t, err := time.ParseInLocation(time.DateOnly, date, time.Local)
  39. if err != nil {
  40. panic(err)
  41. }
  42. return &t
  43. }
  44. func ParseNowDateOnly() *time.Time {
  45. return ParseLocalDateOnly(FormatNowDateOnly())
  46. }
  47. func FormatNowDateTime() string {
  48. return time.Now().Format(time.DateTime)
  49. }
  50. func FormatLocalDateTime(t *time.Time) string {
  51. if t == nil {
  52. return ""
  53. }
  54. return t.Format(time.DateTime)
  55. }
  56. func FormatNowDateOnly() string {
  57. return time.Now().Format(time.DateOnly)
  58. }
  59. func FormatLocalDateOnly(t *time.Time) string {
  60. if t == nil {
  61. return ""
  62. }
  63. return t.Format(time.DateOnly)
  64. }
  65. func GetYear(t *time.Time) int {
  66. if t == nil {
  67. return 0
  68. }
  69. year, _ := t.ISOWeek()
  70. return year
  71. }
  72. func GetWeek(t *time.Time) int {
  73. if t == nil {
  74. return 0
  75. }
  76. _, week := t.ISOWeek()
  77. return week
  78. }
  79. func DiffNatureDays(t1str string, t2str string) int {
  80. t1 := ParseLocalDateTime(t1str).Unix()
  81. t2 := ParseLocalDateTime(t2str).Unix()
  82. var secondsOfDay int64 = 86400
  83. if t1 == t2 {
  84. return -1
  85. }
  86. if t1 > t2 {
  87. t1, t2 = t2, t1
  88. }
  89. diffDays := 0
  90. secDiff := t2 - t1
  91. if secDiff > secondsOfDay {
  92. tmpDays := int(secDiff / secondsOfDay)
  93. t1 += int64(tmpDays) * secondsOfDay
  94. diffDays += tmpDays
  95. }
  96. st := time.Unix(t1, 0)
  97. et := time.Unix(t2, 0)
  98. dateFormatTpl := "20060102"
  99. if st.Format(dateFormatTpl) != et.Format(dateFormatTpl) {
  100. diffDays += 1
  101. }
  102. return diffDays
  103. }
  104. func GetChinaMonth(t *time.Time) string {
  105. if t == nil {
  106. return ""
  107. }
  108. month := t.Format("01")
  109. return monthMap[month]
  110. }
  111. func GetChinaWeek(t *time.Time) string {
  112. if t == nil {
  113. return ""
  114. }
  115. week := t.Weekday()
  116. return weekMap[week.String()]
  117. }