package now
import (
"errors"
"regexp"
"time"
)
func (now *Now ) BeginningOfMinute () time .Time {
return now .Truncate (time .Minute )
}
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 ())
}
func (now *Now ) BeginningOfDay () time .Time {
y , m , d := now .Date ()
return time .Date (y , m , d , 0 , 0 , 0 , 0 , now .Time .Location ())
}
func (now *Now ) BeginningOfWeek () time .Time {
t := now .BeginningOfDay ()
weekday := int (t .Weekday ())
if now .WeekStartDay != time .Sunday {
weekStartDayInt := int (now .WeekStartDay )
if weekday < weekStartDayInt {
weekday = weekday + 7 - weekStartDayInt
} else {
weekday = weekday - weekStartDayInt
}
}
return t .AddDate (0 , 0 , -weekday )
}
func (now *Now ) BeginningOfMonth () time .Time {
y , m , _ := now .Date ()
return time .Date (y , m , 1 , 0 , 0 , 0 , 0 , now .Location ())
}
func (now *Now ) BeginningOfQuarter () time .Time {
month := now .BeginningOfMonth ()
offset := (int (month .Month ()) - 1 ) % 3
return month .AddDate (0 , -offset , 0 )
}
func (now *Now ) BeginningOfHalf () time .Time {
month := now .BeginningOfMonth ()
offset := (int (month .Month ()) - 1 ) % 6
return month .AddDate (0 , -offset , 0 )
}
func (now *Now ) BeginningOfYear () time .Time {
y , _ , _ := now .Date ()
return time .Date (y , time .January , 1 , 0 , 0 , 0 , 0 , now .Location ())
}
func (now *Now ) EndOfMinute () time .Time {
return now .BeginningOfMinute ().Add (time .Minute - time .Nanosecond )
}
func (now *Now ) EndOfHour () time .Time {
return now .BeginningOfHour ().Add (time .Hour - time .Nanosecond )
}
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 ())
}
func (now *Now ) EndOfWeek () time .Time {
return now .BeginningOfWeek ().AddDate (0 , 0 , 7 ).Add (-time .Nanosecond )
}
func (now *Now ) EndOfMonth () time .Time {
return now .BeginningOfMonth ().AddDate (0 , 1 , 0 ).Add (-time .Nanosecond )
}
func (now *Now ) EndOfQuarter () time .Time {
return now .BeginningOfQuarter ().AddDate (0 , 3 , 0 ).Add (-time .Nanosecond )
}
func (now *Now ) EndOfHalf () time .Time {
return now .BeginningOfHalf ().AddDate (0 , 6 , 0 ).Add (-time .Nanosecond )
}
func (now *Now ) EndOfYear () time .Time {
return now .BeginningOfYear ().AddDate (1 , 0 , 0 ).Add (-time .Nanosecond )
}
func (now *Now ) Monday (strs ...string ) time .Time {
var parseTime time .Time
var err error
if len (strs ) > 0 {
parseTime , err = now .Parse (strs ...)
if err != nil {
panic (err )
}
} else {
parseTime = now .BeginningOfDay ()
}
weekday := int (parseTime .Weekday ())
if weekday == 0 {
weekday = 7
}
return parseTime .AddDate (0 , 0 , -weekday +1 )
}
func (now *Now ) Sunday (strs ...string ) time .Time {
var parseTime time .Time
var err error
if len (strs ) > 0 {
parseTime , err = now .Parse (strs ...)
if err != nil {
panic (err )
}
} else {
parseTime = now .BeginningOfDay ()
}
weekday := int (parseTime .Weekday ())
if weekday == 0 {
weekday = 7
}
return parseTime .AddDate (0 , 0 , (7 - weekday ))
}
func (now *Now ) EndOfSunday () time .Time {
return New (now .Sunday ()).EndOfDay ()
}
func (now *Now ) Quarter () uint {
return (uint (now .Month ())-1 )/3 + 1
}
func (now *Now ) parseWithFormat (str string , location *time .Location ) (t time .Time , err error ) {
for _ , format := range now .TimeFormats {
t , err = time .ParseInLocation (format , str , location )
if err == nil {
return
}
}
err = errors .New ("Can't parse string as time: " + str )
return
}
var hasTimeRegexp = regexp .MustCompile (`(\s+|^\s*|T)\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))(\s*$|[Z+-])` )
var onlyTimeRegexp = regexp .MustCompile (`^\s*\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))\s*$` )
func (now *Now ) Parse (strs ...string ) (t time .Time , err error ) {
var (
setCurrentTime bool
parseTime []int
currentLocation = now .Location ()
onlyTimeInStr = true
currentTime = formatTimeToList (now .Time )
)
for _ , str := range strs {
hasTimeInStr := hasTimeRegexp .MatchString (str )
onlyTimeInStr = hasTimeInStr && onlyTimeInStr && onlyTimeRegexp .MatchString (str )
if t , err = now .parseWithFormat (str , currentLocation ); err == nil {
location := t .Location ()
parseTime = formatTimeToList (t )
for i , v := range parseTime {
if hasTimeInStr && i <= 3 {
continue
}
if v == 0 {
if setCurrentTime {
parseTime [i ] = currentTime [i ]
}
} else {
setCurrentTime = true
}
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 = formatTimeToList (t )
}
}
return
}
func (now *Now ) MustParse (strs ...string ) (t time .Time ) {
t , err := now .Parse (strs ...)
if err != nil {
panic (err )
}
return t
}
func (now *Now ) Between (begin , end string ) bool {
beginTime := now .MustParse (begin )
endTime := now .MustParse (end )
return now .After (beginTime ) && now .Before (endTime )
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .