go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/cron/on_the_hour_at.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package cron 9 10 import ( 11 "fmt" 12 "time" 13 ) 14 15 var ( 16 _ Schedule = (*OnTheHourAtSchedule)(nil) 17 _ fmt.Stringer = (*OnTheHourAtSchedule)(nil) 18 ) 19 20 // EveryHourOnTheHour returns a schedule that fires every 60 minutes on the 00th minute. 21 func EveryHourOnTheHour() Schedule { 22 return OnTheHourAtSchedule{} 23 } 24 25 // EveryHourAt returns a schedule that fires every hour at a given minute. 26 func EveryHourAt(minute, second int) Schedule { 27 return OnTheHourAtSchedule{Minute: minute, Second: second} 28 } 29 30 // OnTheHourAtSchedule is a schedule that fires every hour on the given minute. 31 type OnTheHourAtSchedule struct { 32 Minute int 33 Second int 34 } 35 36 // String returns a string representation of the schedule. 37 func (o OnTheHourAtSchedule) String() string { 38 return fmt.Sprintf("on the hour at %v:%v", o.Minute, o.Second) 39 } 40 41 // Next implements the chronometer Schedule api. 42 func (o OnTheHourAtSchedule) Next(after time.Time) time.Time { 43 var returnValue time.Time 44 now := Now() 45 if after.IsZero() { 46 returnValue = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), o.Minute, o.Second, 0, time.UTC) 47 if returnValue.Before(now) { 48 returnValue = returnValue.Add(time.Hour) 49 } 50 } else { 51 returnValue = time.Date(after.Year(), after.Month(), after.Day(), after.Hour(), o.Minute, o.Second, 0, time.UTC) 52 if returnValue.Before(after) { 53 returnValue = returnValue.Add(time.Hour) 54 } 55 } 56 return returnValue 57 }