now.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package now
  2. import (
  3. "errors"
  4. "regexp"
  5. "time"
  6. )
  7. // BeginningOfMinute beginning of minute
  8. func (now *Now) BeginningOfMinute() time.Time {
  9. return now.Truncate(time.Minute)
  10. }
  11. // BeginningOfHour beginning of hour
  12. func (now *Now) BeginningOfHour() time.Time {
  13. y, m, d := now.Date()
  14. return time.Date(y, m, d, now.Time.Hour(), 0, 0, 0, now.Time.Location())
  15. }
  16. // BeginningOfDay beginning of day
  17. func (now *Now) BeginningOfDay() time.Time {
  18. y, m, d := now.Date()
  19. return time.Date(y, m, d, 0, 0, 0, 0, now.Time.Location())
  20. }
  21. // BeginningOfWeek beginning of week
  22. func (now *Now) BeginningOfWeek() time.Time {
  23. t := now.BeginningOfDay()
  24. weekday := int(t.Weekday())
  25. if WeekStartDay != time.Sunday {
  26. weekStartDayInt := int(WeekStartDay)
  27. if weekday < weekStartDayInt {
  28. weekday = weekday + 7 - weekStartDayInt
  29. } else {
  30. weekday = weekday - weekStartDayInt
  31. }
  32. }
  33. return t.AddDate(0, 0, -weekday)
  34. }
  35. // BeginningOfMonth beginning of month
  36. func (now *Now) BeginningOfMonth() time.Time {
  37. y, m, _ := now.Date()
  38. return time.Date(y, m, 1, 0, 0, 0, 0, now.Location())
  39. }
  40. // BeginningOfQuarter beginning of quarter
  41. func (now *Now) BeginningOfQuarter() time.Time {
  42. month := now.BeginningOfMonth()
  43. offset := (int(month.Month()) - 1) % 3
  44. return month.AddDate(0, -offset, 0)
  45. }
  46. // BeginningOfYear BeginningOfYear beginning of year
  47. func (now *Now) BeginningOfYear() time.Time {
  48. y, _, _ := now.Date()
  49. return time.Date(y, time.January, 1, 0, 0, 0, 0, now.Location())
  50. }
  51. // EndOfMinute end of minute
  52. func (now *Now) EndOfMinute() time.Time {
  53. return now.BeginningOfMinute().Add(time.Minute - time.Nanosecond)
  54. }
  55. // EndOfHour end of hour
  56. func (now *Now) EndOfHour() time.Time {
  57. return now.BeginningOfHour().Add(time.Hour - time.Nanosecond)
  58. }
  59. // EndOfDay end of day
  60. func (now *Now) EndOfDay() time.Time {
  61. y, m, d := now.Date()
  62. return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), now.Location())
  63. }
  64. // EndOfWeek end of week
  65. func (now *Now) EndOfWeek() time.Time {
  66. return now.BeginningOfWeek().AddDate(0, 0, 7).Add(-time.Nanosecond)
  67. }
  68. // EndOfMonth end of month
  69. func (now *Now) EndOfMonth() time.Time {
  70. return now.BeginningOfMonth().AddDate(0, 1, 0).Add(-time.Nanosecond)
  71. }
  72. // EndOfQuarter end of quarter
  73. func (now *Now) EndOfQuarter() time.Time {
  74. return now.BeginningOfQuarter().AddDate(0, 3, 0).Add(-time.Nanosecond)
  75. }
  76. // EndOfYear end of year
  77. func (now *Now) EndOfYear() time.Time {
  78. return now.BeginningOfYear().AddDate(1, 0, 0).Add(-time.Nanosecond)
  79. }
  80. // Monday monday
  81. func (now *Now) Monday() time.Time {
  82. t := now.BeginningOfDay()
  83. weekday := int(t.Weekday())
  84. if weekday == 0 {
  85. weekday = 7
  86. }
  87. return t.AddDate(0, 0, -weekday+1)
  88. }
  89. // Sunday sunday
  90. func (now *Now) Sunday() time.Time {
  91. t := now.BeginningOfDay()
  92. weekday := int(t.Weekday())
  93. if weekday == 0 {
  94. return t
  95. }
  96. return t.AddDate(0, 0, (7 - weekday))
  97. }
  98. // EndOfSunday end of sunday
  99. func (now *Now) EndOfSunday() time.Time {
  100. return New(now.Sunday()).EndOfDay()
  101. }
  102. func parseWithFormat(str string) (t time.Time, err error) {
  103. for _, format := range TimeFormats {
  104. t, err = time.Parse(format, str)
  105. if err == nil {
  106. return
  107. }
  108. }
  109. err = errors.New("Can't parse string as time: " + str)
  110. return
  111. }
  112. var hasTimeRegexp = regexp.MustCompile(`(\s+|^\s*)\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))\s*$`) // match 15:04:05, 15:04:05.000, 15:04:05.000000 15, 2017-01-01 15:04, etc
  113. var onlyTimeRegexp = regexp.MustCompile(`^\s*\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))\s*$`) // match 15:04:05, 15, 15:04:05.000, 15:04:05.000000, etc
  114. // Parse parse string to time
  115. func (now *Now) Parse(strs ...string) (t time.Time, err error) {
  116. var (
  117. setCurrentTime bool
  118. parseTime []int
  119. currentTime = []int{now.Nanosecond(), now.Second(), now.Minute(), now.Hour(), now.Day(), int(now.Month()), now.Year()}
  120. currentLocation = now.Location()
  121. onlyTimeInStr = true
  122. )
  123. for _, str := range strs {
  124. hasTimeInStr := hasTimeRegexp.MatchString(str) // match 15:04:05, 15
  125. onlyTimeInStr = hasTimeInStr && onlyTimeInStr && onlyTimeRegexp.MatchString(str)
  126. if t, err = parseWithFormat(str); err == nil {
  127. location := t.Location()
  128. if location.String() == "UTC" {
  129. location = currentLocation
  130. }
  131. parseTime = []int{t.Nanosecond(), t.Second(), t.Minute(), t.Hour(), t.Day(), int(t.Month()), t.Year()}
  132. for i, v := range parseTime {
  133. // Don't reset hour, minute, second if current time str including time
  134. if hasTimeInStr && i <= 3 {
  135. continue
  136. }
  137. // If value is zero, replace it with current time
  138. if v == 0 {
  139. if setCurrentTime {
  140. parseTime[i] = currentTime[i]
  141. }
  142. } else {
  143. setCurrentTime = true
  144. }
  145. // if current time only includes time, should change day, month to current time
  146. if onlyTimeInStr {
  147. if i == 4 || i == 5 {
  148. parseTime[i] = currentTime[i]
  149. continue
  150. }
  151. }
  152. }
  153. t = time.Date(parseTime[6], time.Month(parseTime[5]), parseTime[4], parseTime[3], parseTime[2], parseTime[1], parseTime[0], location)
  154. currentTime = []int{t.Nanosecond(), t.Second(), t.Minute(), t.Hour(), t.Day(), int(t.Month()), t.Year()}
  155. }
  156. }
  157. return
  158. }
  159. // MustParse must parse string to time or it will panic
  160. func (now *Now) MustParse(strs ...string) (t time.Time) {
  161. t, err := now.Parse(strs...)
  162. if err != nil {
  163. panic(err)
  164. }
  165. return t
  166. }
  167. // Between check time between the begin, end time or not
  168. func (now *Now) Between(begin, end string) bool {
  169. beginTime := now.MustParse(begin)
  170. endTime := now.MustParse(end)
  171. return now.After(beginTime) && now.Before(endTime)
  172. }