Golang : Fix go-cron set time not working issue
Problem :
You followed the go-cron example at https://pkg.go.dev/github.com/go-co-op/gocron#section-readme but later on found out that the scheduled task did not execute at the specified time.
s := gocron.NewScheduler(time.UTC)
// set time
s.Every(1).Day().At("10:30").Do(func(){ ... })
// set multiple times
s.Every(1).Day().At("10:30;08:00").Do(func(){ ... })
Solution :
Instead of using time.UTC
, set the time location first to whatever timezone your server/machine is running at and then use the time for the go-cron scheduler.
For example :
localTime, err := time.LoadLocation("Asia/Singapore")
if err != nil {
logs.Fatal().Err(err).Msg("Error in time load location")
}
cronScheduler := gocron.NewScheduler(localTime)
Hope this helps and happy coding!
See also : Golang : How to get time zone and load different time zone?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.7k Golang : Gorilla web tool kit secure cookie example
+21.3k Golang : Set and Get HTTP request headers example
+31.3k Golang : Regular Expression for alphanumeric and underscore
+4.9k Golang : Launching your executable inside a console under Linux
+16.6k Golang : Simple client server example
+7.6k Golang : Another camera capture GUI application with GTK and OpenCV
+8k Golang : How to get username from email address
+35.1k Golang : Convert date or time stamp from string to time.Time type
+7.9k Golang : Capture text return from exec function example
+12.8k Golang : convert rune to unicode hexadecimal value and back to rune character
+5.9k Golang : How to fix html/template : "somefile" is undefined error?
+7.8k Golang : Gorilla web tool kit schema example