Skip to content

Commit

Permalink
Merge pull request #48 from arjunmahishi/cleanup/parse-time-with-layout
Browse files Browse the repository at this point in the history
Parse the At() time with time.Parse()
  • Loading branch information
Streppel authored Jul 20, 2020
2 parents 5832008 + 5d700c1 commit 3f4608d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
56 changes: 28 additions & 28 deletions gocron.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,26 @@ import (
"errors"
"fmt"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)

// Error declarations for gocron related errors
var (
ErrTimeFormat = errors.New("time format error")
ErrParamsNotAdapted = errors.New("the number of params is not adapted")
ErrNotAFunction = errors.New("only functions can be schedule into the job queue")
ErrPeriodNotSpecified = errors.New("unspecified job period")
ErrNotScheduledWeekday = errors.New("job not scheduled weekly on a weekday")
ErrJobNotFoundWithTag = errors.New("no jobs found with given tag")
ErrTimeFormat = errors.New("time format error")
ErrParamsNotAdapted = errors.New("the number of params is not adapted")
ErrNotAFunction = errors.New("only functions can be schedule into the job queue")
ErrPeriodNotSpecified = errors.New("unspecified job period")
ErrNotScheduledWeekday = errors.New("job not scheduled weekly on a weekday")
ErrJobNotFoundWithTag = errors.New("no jobs found with given tag")
ErrUnsupportedTimeFormat = errors.New("the given time format is not supported")
)

// regex patterns for supported time formats
var (
timeWithSeconds = regexp.MustCompile(`(?m)^\d\d:\d\d:\d\d$`)
timeWithoutSeconds = regexp.MustCompile(`(?m)^\d\d:\d\d$`)
)

type timeUnit int
Expand Down Expand Up @@ -63,27 +70,20 @@ func getFunctionKey(funcName string) string {
return fmt.Sprintf("%x", h.Sum(nil))
}

func formatTime(t string) (hour, min, sec int, err error) {
ts := strings.Split(t, ":")
if len(ts) < 2 || len(ts) > 3 {
return 0, 0, 0, ErrTimeFormat
}

if hour, err = strconv.Atoi(ts[0]); err != nil {
return 0, 0, 0, err
}
if min, err = strconv.Atoi(ts[1]); err != nil {
return 0, 0, 0, err
}
if len(ts) == 3 {
if sec, err = strconv.Atoi(ts[2]); err != nil {
return 0, 0, 0, err
}
func parseTime(t string) (hour, min, sec int, err error) {
var timeLayout string
switch {
case timeWithSeconds.Match([]byte(t)):
timeLayout = "15:04:05"
case timeWithoutSeconds.Match([]byte(t)):
timeLayout = "15:04"
default:
return 0, 0, 0, ErrUnsupportedTimeFormat
}

if hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 || sec > 59 {
return 0, 0, 0, ErrTimeFormat
parsedTime, err := time.Parse(timeLayout, t)
if err != nil {
return 0, 0, 0, ErrUnsupportedTimeFormat
}

return hour, min, sec, nil
return parsedTime.Hour(), parsedTime.Minute(), parsedTime.Second(), nil
}
4 changes: 2 additions & 2 deletions gocron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestFormatTime(t *testing.T) {
},
{
name: "normal_with_second",
args: "6:18:01",
args: "06:18:01",
wantHour: 6,
wantMin: 18,
wantSec: 1,
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestFormatTime(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotHour, gotMin, gotSec, err := formatTime(tt.args)
gotHour, gotMin, gotSec, err := parseTime(tt.args)
if tt.wantErr {
assert.NotEqual(t, nil, err, tt.args)
return
Expand Down
2 changes: 1 addition & 1 deletion scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (s *Scheduler) Do(jobFun interface{}, params ...interface{}) (*Job, error)
// At schedules the Job at a specific time of day in the form "HH:MM:SS" or "HH:MM"
func (s *Scheduler) At(t string) *Scheduler {
j := s.getCurrentJob()
hour, min, sec, err := formatTime(t)
hour, min, sec, err := parseTime(t)
if err != nil {
j.err = ErrTimeFormat
return s
Expand Down

0 comments on commit 3f4608d

Please sign in to comment.