123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- package now
- import (
- "errors"
- "regexp"
- "time"
- )
- // BeginningOfMinute beginning of minute
- func (now *Now) BeginningOfMinute() time.Time {
- return now.Truncate(time.Minute)
- }
- // BeginningOfHour beginning of hour
- func (now *Now) BeginningOfHour() time.Time {
- y, m, d := now.Date()
- return time.Date(y, m, d, now.Time.Hour(), 0, 0, 0, now.Time.Location())
- }
- // BeginningOfDay beginning of day
- func (now *Now) BeginningOfDay() time.Time {
- y, m, d := now.Date()
- return time.Date(y, m, d, 0, 0, 0, 0, now.Time.Location())
- }
- // BeginningOfWeek beginning of week
- func (now *Now) BeginningOfWeek() time.Time {
- t := now.BeginningOfDay()
- weekday := int(t.Weekday())
- if WeekStartDay != time.Sunday {
- weekStartDayInt := int(WeekStartDay)
- if weekday < weekStartDayInt {
- weekday = weekday + 7 - weekStartDayInt
- } else {
- weekday = weekday - weekStartDayInt
- }
- }
- return t.AddDate(0, 0, -weekday)
- }
- // BeginningOfMonth beginning of month
- func (now *Now) BeginningOfMonth() time.Time {
- y, m, _ := now.Date()
- return time.Date(y, m, 1, 0, 0, 0, 0, now.Location())
- }
- // BeginningOfQuarter beginning of quarter
- func (now *Now) BeginningOfQuarter() time.Time {
- month := now.BeginningOfMonth()
- offset := (int(month.Month()) - 1) % 3
- return month.AddDate(0, -offset, 0)
- }
- // BeginningOfYear BeginningOfYear beginning of year
- func (now *Now) BeginningOfYear() time.Time {
- y, _, _ := now.Date()
- return time.Date(y, time.January, 1, 0, 0, 0, 0, now.Location())
- }
- // EndOfMinute end of minute
- func (now *Now) EndOfMinute() time.Time {
- return now.BeginningOfMinute().Add(time.Minute - time.Nanosecond)
- }
- // EndOfHour end of hour
- func (now *Now) EndOfHour() time.Time {
- return now.BeginningOfHour().Add(time.Hour - time.Nanosecond)
- }
- // EndOfDay end of day
- func (now *Now) EndOfDay() time.Time {
- y, m, d := now.Date()
- return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), now.Location())
- }
- // EndOfWeek end of week
- func (now *Now) EndOfWeek() time.Time {
- return now.BeginningOfWeek().AddDate(0, 0, 7).Add(-time.Nanosecond)
- }
- // EndOfMonth end of month
- func (now *Now) EndOfMonth() time.Time {
- return now.BeginningOfMonth().AddDate(0, 1, 0).Add(-time.Nanosecond)
- }
- // EndOfQuarter end of quarter
- func (now *Now) EndOfQuarter() time.Time {
- return now.BeginningOfQuarter().AddDate(0, 3, 0).Add(-time.Nanosecond)
- }
- // EndOfYear end of year
- func (now *Now) EndOfYear() time.Time {
- return now.BeginningOfYear().AddDate(1, 0, 0).Add(-time.Nanosecond)
- }
- // Monday monday
- func (now *Now) Monday() time.Time {
- t := now.BeginningOfDay()
- weekday := int(t.Weekday())
- if weekday == 0 {
- weekday = 7
- }
- return t.AddDate(0, 0, -weekday+1)
- }
- // Sunday sunday
- func (now *Now) Sunday() time.Time {
- t := now.BeginningOfDay()
- weekday := int(t.Weekday())
- if weekday == 0 {
- return t
- }
- return t.AddDate(0, 0, (7 - weekday))
- }
- // EndOfSunday end of sunday
- func (now *Now) EndOfSunday() time.Time {
- return New(now.Sunday()).EndOfDay()
- }
- func parseWithFormat(str string) (t time.Time, err error) {
- for _, format := range TimeFormats {
- t, err = time.Parse(format, str)
- if err == nil {
- return
- }
- }
- err = errors.New("Can't parse string as time: " + str)
- return
- }
- 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
- 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
- // Parse parse string to time
- func (now *Now) Parse(strs ...string) (t time.Time, err error) {
- var (
- setCurrentTime bool
- parseTime []int
- currentTime = []int{now.Nanosecond(), now.Second(), now.Minute(), now.Hour(), now.Day(), int(now.Month()), now.Year()}
- currentLocation = now.Location()
- onlyTimeInStr = true
- )
- for _, str := range strs {
- hasTimeInStr := hasTimeRegexp.MatchString(str) // match 15:04:05, 15
- onlyTimeInStr = hasTimeInStr && onlyTimeInStr && onlyTimeRegexp.MatchString(str)
- if t, err = parseWithFormat(str); err == nil {
- location := t.Location()
- if location.String() == "UTC" {
- location = currentLocation
- }
- parseTime = []int{t.Nanosecond(), t.Second(), t.Minute(), t.Hour(), t.Day(), int(t.Month()), t.Year()}
- for i, v := range parseTime {
- // Don't reset hour, minute, second if current time str including time
- if hasTimeInStr && i <= 3 {
- continue
- }
- // If value is zero, replace it with current time
- if v == 0 {
- if setCurrentTime {
- parseTime[i] = currentTime[i]
- }
- } else {
- setCurrentTime = true
- }
- // if current time only includes time, should change day, month to current time
- if onlyTimeInStr {
- if i == 4 || i == 5 {
- parseTime[i] = currentTime[i]
- continue
- }
- }
- }
- t = time.Date(parseTime[6], time.Month(parseTime[5]), parseTime[4], parseTime[3], parseTime[2], parseTime[1], parseTime[0], location)
- currentTime = []int{t.Nanosecond(), t.Second(), t.Minute(), t.Hour(), t.Day(), int(t.Month()), t.Year()}
- }
- }
- return
- }
- // MustParse must parse string to time or it will panic
- func (now *Now) MustParse(strs ...string) (t time.Time) {
- t, err := now.Parse(strs...)
- if err != nil {
- panic(err)
- }
- return t
- }
- // Between check time between the begin, end time or not
- func (now *Now) Between(begin, end string) bool {
- beginTime := now.MustParse(begin)
- endTime := now.MustParse(end)
- return now.After(beginTime) && now.Before(endTime)
- }
|