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
+15.7k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+14k Golang : concatenate(combine) strings
+6.8k Golang : Get expvar(export variables) to work with multiplexer
+3.6k Java : Get FX sentiment from website example
+7.9k Javascript : Put image into Chrome browser's console
+8.9k Golang : Gaussian blur on image and camera video feed examples
+13.6k Golang : Set image canvas or background to transparent
+11.3k Golang : Post data with url.Values{}
+4.7k JavaScript: Add marker function on Google Map
+13.9k Golang : Human readable time elapsed format such as 5 days ago
+9.9k Golang : Check if user agent is a robot or crawler example
+4.3k Javascript : How to show different content with noscript?