@@ -9,11 +9,27 @@ import (
"time"
)
func legalCheck ( limitCode string , baseDate time . Time , opts ... startimer . TimerOptions ) ( startimer . StarTimer , error ) {
if limitCode == "" {
return startimer . NewTimer ( baseDate , opts ... )
}
timer , err := startimer . NewTimer ( baseDate , opts ... )
if err != nil {
return timer , err
}
if strings . Contains ( limitCode , "今" ) {
if timer . NextTimer ( ) . Day ( ) != time . Now ( ) . Day ( ) {
return timer , errors . New ( "无法添加,指定的提醒时刻不在未来。" )
}
}
return timer , err
}
func matchPeriodPattern01 ( base time . Time , str string ) ( startimer . StarTimer , error ) {
str = transChn ( str )
var rpt startimer . Repeats
var duration time . Duration
reg := regexp . MustCompile ( ` (每隔|每)?(\d { 0,4}年)?(\d { 0,5}个?月)?(\d { 0,4}[明后大] { 0,4}[日号天])?([上中下午夜早凌清晨傍晚里]+)?(\d { 1,4}个?[点小时钟:]+)?(\d { 0,4}半?[分:秒]?钟?)?(\d { 0,10}半?秒?钟?)?(后)? ` )
reg := regexp . MustCompile ( ` (每隔|每)?(\d { 0,4}年)?(\d { 0,5}个?月)?(\d { 0,4}[明后大] { 0,4}[日号天])?([今天日 上中下午夜早凌清晨傍晚里]+)?(\d { 1,4}个?[点小时钟:: ]+)?(\d { 0,4}半?[分:: 秒]?钟?)?(\d { 0,10}半?秒?钟?)?(后)? ` )
if reg . MatchString ( str ) {
pts := reg . FindStringSubmatch ( str )
setAsDate := false
@@ -52,11 +68,11 @@ func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, erro
if rpt . Every {
setAsDate = true
}
case "明天" :
case "明天" , "明日" :
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_DAY , Value : uint32 ( now . Add ( time . Hour * 24 ) . Day ( ) ) } )
case "后天" :
case "后天" , "后日" :
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_DAY , Value : uint32 ( now . Add ( time . Hour * 48 ) . Day ( ) ) } )
case "大后天" :
case "大后天" , "大后日" :
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_DAY , Value : uint32 ( now . Add ( time . Hour * 72 ) . Day ( ) ) } )
case "大大后天" :
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_DAY , Value : uint32 ( now . Add ( time . Hour * 96 ) . Day ( ) ) } )
@@ -79,7 +95,7 @@ func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, erro
var hour uint32
if pts [ 6 ] != "" {
hour = uint32 ( getNumbers ( pts [ 6 ] ) )
if timeParse == "下午" || strings . Contains ( timeParse , "晚" ) || strings . Contains ( timeParse , "夜" ) {
if hour < 12 && ( timeParse == "下午" || strings . Contains ( timeParse , "晚" ) || strings . Contains ( timeParse , "夜" ) ) {
hour += 12
}
if rpt . Every == setAsDate {
@@ -129,7 +145,7 @@ func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, erro
} else {
base . Add ( tmodel * time . Duration ( uint32 ( getNumbers ( pts [ 7 ] ) ) ) )
}
} else if hour != 0 {
} else if hour != 0 || pts [ 5 ] != "" {
if rpt . Every == setAsDate {
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_MINUTE , Value : 0 } )
}
@@ -146,7 +162,7 @@ func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, erro
} else {
base . Add ( time . Second * time . Duration ( uint32 ( getNumbers ( pts [ 8 ] ) ) ) )
}
} else if hour != 0 || pts [ 7 ] != "" {
} else if hour != 0 || pts [ 5 ] != "" || pts [ 7 ] != "" {
if rpt . Every == setAsDate {
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_SECOND , Value : 0 } )
}
@@ -172,7 +188,7 @@ func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, erro
now = now . Add ( time . Duration ( sr . Value ) * time . Second )
}
}
return startimer . NewTimer ( time . Now ( ) , startimer . WithStaticDate ( now ) , startimer . WithRunCountLimit ( count ) ) , nil
return legalCheck ( timeParse , time . Now ( ) , startimer . WithStaticDate ( now ) , startimer . WithRunCountLimit ( count ) )
}
if duration . Seconds ( ) > 0 {
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_SECOND , Value : uint32 ( duration . Seconds ( ) ) } )
@@ -180,15 +196,25 @@ func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, erro
if rpt . Every == setAsDate {
rpt . Every = false
}
return startimer . NewTimer ( base , startimer . WithRepeats ( & rpt ) , startimer . WithRunCountLimit ( count ) ) , nil
return legalCheck ( timeParse , base , startimer . WithRepeats ( & rpt ) , startimer . WithRunCountLimit ( count ) )
}
return startimer . StarTimer { } , errors . New ( "no Match" )
}
func matchPeroidPattern02 ( base time . Time , str string ) ( startimer . StarTimer , error ) {
preReg := "(周|礼拜|星期)([1-7一二三四五六七日天])([0-9一二三四五六七八九零])"
preExp := regexp . MustCompile ( preReg )
if preExp . MatchString ( str ) {
str = preExp . ReplaceAllString ( str , "${1}${2}的${3}" )
}
str = transChn ( str )
str = strings . ReplaceAll ( str , "周日" , "周0" )
str = strings . ReplaceAll ( str , "礼拜天" , "周0" )
str = strings . ReplaceAll ( str , "星期日" , "周0" )
str = strings . ReplaceAll ( str , "星期天" , "周0" )
str = strings . ReplaceAll ( str , "周天" , "周0" )
var rpt startimer . Repeats
reg := regexp . MustCompile ( ` (每)?([周星期礼拜][周礼拜星期到1-6日天]+)+ 的?([上中下午夜早凌清晨傍晚里]+)?(\d { 1,4}[个点时:]+)?(\d { 0,4}[分半:]?钟?)?(\d { 1,10}秒?钟?)? ` )
reg := regexp . MustCompile ( ` (每)?([周星期礼拜日天]\d { 0,1})到?([周礼拜星期日天]?\d { 0,1})? 的?([上中下午夜早凌清晨傍晚里]+)?(\d{ 1,4}[个点时: :]+)?(\d { 0,4}[分半:: ]?钟?)?(\d { 1,10}秒?钟?)? ` )
if reg . MatchString ( str ) {
pts := reg . FindStringSubmatch ( str )
scs := len ( strings . Split ( pts [ 0 ] , "周" ) )
@@ -199,15 +225,9 @@ func matchPeroidPattern02(base time.Time, str string) (startimer.StarTimer, erro
return startimer . StarTimer { } , errors . New ( "Invalid stings" )
}
if pts [ 2 ] != "" {
wk := strings . ReplaceAll ( pts [ 2 ] , "周日" , "周0" )
wk = strings . ReplaceAll ( wk , "礼拜天" , "周0" )
wk = strings . ReplaceAll ( wk , "星期日" , "周0" )
wk = strings . ReplaceAll ( wk , "星期天" , "周0" )
wk = strings . ReplaceAll ( wk , "周天" , "周0" )
if strings . Contains ( wk , "到" ) {
pk := strings . Split ( wk , "到" )
startWk := getNumbers ( pk [ 0 ] )
endWk := getNumbers ( pk [ 1 ] )
if len ( pts [ 3 ] ) != 0 {
startWk := getNumbers ( pts [ 2 ] )
endWk := getNumbers ( pts [ 3 ] )
if endWk < startWk {
endWk += 7
}
@@ -219,15 +239,15 @@ func matchPeroidPattern02(base time.Time, str string) (startimer.StarTimer, erro
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_WEEK , Value : uint32 ( num ) } )
}
} else {
wkNum := getNumbers ( wk )
wkNum := getNumbers ( pts [ 2 ] )
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_WEEK , Value : uint32 ( wkNum ) } )
}
}
timeParse := pts [ 3 ]
timeParse := pts [ 4 ]
var hour uint32
if pts [ 4 ] != "" {
hour = uint32 ( getNumbers ( pts [ 4 ] ) )
if pts [ 5 ] != "" {
hour = uint32 ( getNumbers ( pts [ 5 ] ) )
if timeParse == "下午" || strings . Contains ( timeParse , "晚" ) || strings . Contains ( timeParse , "夜" ) {
hour += 12
}
@@ -251,16 +271,16 @@ func matchPeroidPattern02(base time.Time, str string) (startimer.StarTimer, erro
}
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_HOUR , Value : hour } )
}
if pts [ 5 ] != "" {
if pts [ 5 ] == "半" {
pts [ 5 ] = "30"
if pts [ 6 ] != "" {
if pts [ 6 ] == "半" {
pts [ 6 ] = "30"
}
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_MINUTE , Value : uint32 ( getNumbers ( pts [ 5 ] ) ) } )
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_MINUTE , Value : uint32 ( getNumbers ( pts [ 6 ] ) ) } )
} else if hour != 0 {
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_MINUTE , Value : 0 } )
}
if pts [ 6 ] != "" {
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_SECOND , Value : uint32 ( getNumbers ( pts [ 6 ] ) ) } )
if pts [ 7 ] != "" {
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_SECOND , Value : uint32 ( getNumbers ( pts [ 7 ] ) ) } )
} else if hour != 0 {
rpt . Repeat = append ( rpt . Repeat , startimer . Repeat { Unit : startimer . STAR_SECOND , Value : 0 } )
}
@@ -268,7 +288,7 @@ func matchPeroidPattern02(base time.Time, str string) (startimer.StarTimer, erro
if pts [ 1 ] != "" {
count = 0
}
return startimer . NewTimer ( base , startimer . WithRepeats ( & rpt ) , startimer . WithRunCountLimit ( count ) ) , nil
return startimer . NewTimer ( base , startimer . WithRepeats ( & rpt ) , startimer . WithRunCountLimit ( count ) )
}
return startimer . StarTimer { } , errors . New ( "no Match" )
}