From 5d700c1842f9c062ec2ef221ec4aaf362b2faf57 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Sun, 19 Jul 2020 10:14:09 +0530 Subject: [PATCH] Parse the At() time with time.Parse --- gocron.go | 56 +++++++++++++++++++++++++------------------------- gocron_test.go | 4 ++-- scheduler.go | 2 +- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/gocron.go b/gocron.go index d7c0acbe..38ad9552 100644 --- a/gocron.go +++ b/gocron.go @@ -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 @@ -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 } diff --git a/gocron_test.go b/gocron_test.go index 72fc29b4..c349d232 100644 --- a/gocron_test.go +++ b/gocron_test.go @@ -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, @@ -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 diff --git a/scheduler.go b/scheduler.go index 605eb7e7..1f90d1c6 100644 --- a/scheduler.go +++ b/scheduler.go @@ -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