now.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 now.WeekStartDay != time.Sunday {
  26. weekStartDayInt := int(now.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. // BeginningOfHalf beginning of half year
  47. func (now *Now) BeginningOfHalf() time.Time {
  48. month := now.BeginningOfMonth()
  49. offset := (int(month.Month()) - 1) % 6
  50. return month.AddDate(0, -offset, 0)
  51. }
  52. // BeginningOfYear BeginningOfYear beginning of year
  53. func (now *Now) BeginningOfYear() time.Time {
  54. y, _, _ := now.Date()
  55. return time.Date(y, time.January, 1, 0, 0, 0, 0, now.Location())
  56. }
  57. // EndOfMinute end of minute
  58. func (now *Now) EndOfMinute() time.Time {
  59. return now.BeginningOfMinute().Add(time.Minute - time.Nanosecond)
  60. }
  61. // EndOfHour end of hour
  62. func (now *Now) EndOfHour() time.Time {
  63. return now.BeginningOfHour().Add(time.Hour - time.Nanosecond)
  64. }
  65. // EndOfDay end of day
  66. func (now *Now) EndOfDay() time.Time {
  67. y, m, d := now.Date()
  68. return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), now.Location())
  69. }
  70. // EndOfWeek end of week
  71. func (now *Now) EndOfWeek() time.Time {
  72. return now.BeginningOfWeek().AddDate(0, 0, 7).Add(-time.Nanosecond)
  73. }
  74. // EndOfMonth end of month
  75. func (now *Now) EndOfMonth() time.Time {
  76. return now.BeginningOfMonth().AddDate(0, 1, 0).Add(-time.Nanosecond)
  77. }
  78. // EndOfQuarter end of quarter
  79. func (now *Now) EndOfQuarter() time.Time {
  80. return now.BeginningOfQuarter().AddDate(0, 3, 0).Add(-time.Nanosecond)
  81. }
  82. // EndOfHalf end of half year
  83. func (now *Now) EndOfHalf() time.Time {
  84. return now.BeginningOfHalf().AddDate(0, 6, 0).Add(-time.Nanosecond)
  85. }
  86. // EndOfYear end of year
  87. func (now *Now) EndOfYear() time.Time {
  88. return now.BeginningOfYear().AddDate(1, 0, 0).Add(-time.Nanosecond)
  89. }
  90. // Monday monday
  91. func (now *Now) Monday() time.Time {
  92. t := now.BeginningOfDay()
  93. weekday := int(t.Weekday())
  94. if weekday == 0 {
  95. weekday = 7
  96. }
  97. return t.AddDate(0, 0, -weekday+1)
  98. }
  99. // Sunday sunday
  100. func (now *Now) Sunday() time.Time {
  101. t := now.BeginningOfDay()
  102. weekday := int(t.Weekday())
  103. if weekday == 0 {
  104. return t
  105. }
  106. return t.AddDate(0, 0, (7 - weekday))
  107. }
  108. // EndOfSunday end of sunday
  109. func (now *Now) EndOfSunday() time.Time {
  110. return New(now.Sunday()).EndOfDay()
  111. }
  112. func (now *Now) parseWithFormat(str string, location *time.Location) (t time.Time, err error) {
  113. for _, format := range now.TimeFormats {
  114. t, err = time.ParseInLocation(format, str, location)
  115. if err == nil {
  116. return
  117. }
  118. }
  119. err = errors.New("Can't parse string as time: " + str)
  120. return
  121. }
  122. 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
  123. 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
  124. // Parse parse string to time
  125. func (now *Now) Parse(strs ...string) (t time.Time, err error) {
  126. var (
  127. setCurrentTime bool
  128. parseTime []int
  129. currentLocation = now.Location()
  130. onlyTimeInStr = true
  131. currentTime = formatTimeToList(now.Time)
  132. )
  133. for _, str := range strs {
  134. hasTimeInStr := hasTimeRegexp.MatchString(str) // match 15:04:05, 15
  135. onlyTimeInStr = hasTimeInStr && onlyTimeInStr && onlyTimeRegexp.MatchString(str)
  136. if t, err = now.parseWithFormat(str, currentLocation); err == nil {
  137. location := t.Location()
  138. parseTime = formatTimeToList(t)
  139. for i, v := range parseTime {
  140. // Don't reset hour, minute, second if current time str including time
  141. if hasTimeInStr && i <= 3 {
  142. continue
  143. }
  144. // If value is zero, replace it with current time
  145. if v == 0 {
  146. if setCurrentTime {
  147. parseTime[i] = currentTime[i]
  148. }
  149. } else {
  150. setCurrentTime = true
  151. }
  152. // if current time only includes time, should change day, month to current time
  153. if onlyTimeInStr {
  154. if i == 4 || i == 5 {
  155. parseTime[i] = currentTime[i]
  156. continue
  157. }
  158. }
  159. }
  160. t = time.Date(parseTime[6], time.Month(parseTime[5]), parseTime[4], parseTime[3], parseTime[2], parseTime[1], parseTime[0], location)
  161. currentTime = formatTimeToList(t)
  162. }
  163. }
  164. return
  165. }
  166. // MustParse must parse string to time or it will panic
  167. func (now *Now) MustParse(strs ...string) (t time.Time) {
  168. t, err := now.Parse(strs...)
  169. if err != nil {
  170. panic(err)
  171. }
  172. return t
  173. }
  174. // Between check time between the begin, end time or not
  175. func (now *Now) Between(begin, end string) bool {
  176. beginTime := now.MustParse(begin)
  177. endTime := now.MustParse(end)
  178. return now.After(beginTime) && now.Before(endTime)
  179. }