date.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package dara
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. )
  7. type Date struct {
  8. date time.Time
  9. }
  10. func NewDate(dateInput string) (*Date, error) {
  11. var t time.Time
  12. var err error
  13. // 解析输入时间,如果输入格式不对,返回错误
  14. formats := []string{
  15. "2006-01-02 15:04:05",
  16. "2006-01-02 15:04:05.999999999 -0700 MST",
  17. "2006-01-02T15:04:05-07:00",
  18. "2006-01-02T15:04:05Z",
  19. }
  20. for _, format := range formats {
  21. t, err = time.Parse(format, dateInput)
  22. if err == nil {
  23. return &Date{date: t}, nil
  24. }
  25. }
  26. return nil, fmt.Errorf("unable to parse date: %v", dateInput)
  27. }
  28. func (t *Date) Format(layout string) string {
  29. layout = strings.Replace(layout, "yyyy", "2006", 1)
  30. layout = strings.Replace(layout, "MM", "01", 1)
  31. layout = strings.Replace(layout, "dd", "02", 1)
  32. // layout = strings.Replace(layout, "HH", "15", 1)
  33. layout = strings.Replace(layout, "hh", "15", 1)
  34. layout = strings.Replace(layout, "mm", "04", 1)
  35. layout = strings.Replace(layout, "ss", "05", 1)
  36. layout = strings.Replace(layout, "a", "PM", 1)
  37. layout = strings.Replace(layout, "EEEE", "Monday", 1)
  38. layout = strings.Replace(layout, "E", "Mon", 1)
  39. return t.date.Format(layout)
  40. }
  41. func (t *Date) Unix() int64 {
  42. return t.date.Unix()
  43. }
  44. func (t *Date) UTC() string {
  45. return t.date.UTC().Format("2006-01-02 15:04:05.000000000 -0700 MST")
  46. }
  47. func (t *Date) Sub(amount int, unit string) *Date {
  48. var duration time.Duration
  49. switch unit {
  50. case "second", "seconds":
  51. duration = time.Duration(-amount) * time.Second
  52. case "minute", "minutes":
  53. duration = time.Duration(-amount) * time.Minute
  54. case "hour", "hours":
  55. duration = time.Duration(-amount) * time.Hour
  56. case "day", "days":
  57. duration = time.Duration(-amount) * 24 * time.Hour
  58. case "week", "weeks":
  59. duration = time.Duration(-amount) * 7 * 24 * time.Hour
  60. case "month", "months":
  61. return &Date{date: t.date.AddDate(0, -amount, 0)}
  62. case "year", "years":
  63. return &Date{date: t.date.AddDate(-amount, 0, 0)}
  64. default:
  65. return nil
  66. }
  67. newDate := t.date.Add(duration)
  68. return &Date{date: newDate}
  69. }
  70. func (t *Date) Add(amount int, unit string) *Date {
  71. var duration time.Duration
  72. switch unit {
  73. case "second", "seconds":
  74. duration = time.Duration(amount) * time.Second
  75. case "minute", "minutes":
  76. duration = time.Duration(amount) * time.Minute
  77. case "hour", "hours":
  78. duration = time.Duration(amount) * time.Hour
  79. case "day", "days":
  80. duration = time.Duration(amount) * 24 * time.Hour
  81. case "week", "weeks":
  82. duration = time.Duration(amount) * 7 * 24 * time.Hour
  83. case "month", "months":
  84. return &Date{date: t.date.AddDate(0, amount, 0)}
  85. case "year", "years":
  86. return &Date{date: t.date.AddDate(amount, 0, 0)}
  87. default:
  88. return nil
  89. }
  90. newDate := t.date.Add(duration)
  91. return &Date{date: newDate}
  92. }
  93. func (t *Date) Diff(amount string, diffDate *Date) int64 {
  94. switch amount {
  95. case "second", "seconds":
  96. return int64(t.date.Sub(diffDate.date).Seconds())
  97. case "minute", "minutes":
  98. return int64(t.date.Sub(diffDate.date).Minutes())
  99. case "hour", "hours":
  100. return int64(t.date.Sub(diffDate.date).Hours())
  101. case "day", "days":
  102. return int64(t.date.Sub(diffDate.date).Hours() / 24)
  103. case "week", "weeks":
  104. return int64(t.date.Sub(diffDate.date).Hours() / (24 * 7))
  105. case "month", "months":
  106. return int64(diffDate.date.Year()*12 + int(diffDate.date.Month()) - (t.date.Year()*12 + int(t.date.Month())))
  107. case "year", "years":
  108. return int64(t.date.Year() - diffDate.date.Year())
  109. default:
  110. return 0
  111. }
  112. }
  113. func (t *Date) Hour() int {
  114. return t.date.Hour()
  115. }
  116. func (t *Date) Minute() int {
  117. return t.date.Minute()
  118. }
  119. func (t *Date) Second() int {
  120. return t.date.Second()
  121. }
  122. func (t *Date) Month() int {
  123. return int(t.date.Month())
  124. }
  125. func (t *Date) Year() int {
  126. return t.date.Year()
  127. }
  128. func (t *Date) DayOfMonth() int {
  129. return t.date.Day()
  130. }
  131. func (t *Date) DayOfWeek() int {
  132. weekday := int(t.date.Weekday())
  133. if weekday == 0 {
  134. return 7 // Sunday
  135. }
  136. return weekday
  137. }
  138. func (t *Date) WeekOfYear() int {
  139. _, week := t.date.ISOWeek()
  140. return week
  141. }