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
+16.7k Golang : Set up source IP address before making HTTP request
+4.3k Golang : Valued expressions and functions example
+7.6k Golang : Generate human readable password
+9.1k Golang : Apply Histogram Equalization to color images
+14.2k Golang : Parsing or breaking down URL
+3.2k Golang : Fix go-cron set time not working issue
+7.4k Android Studio : AlertDialog to get user attention example
+17.5k How to enable MariaDB/MySQL logs ?
+6.2k PHP : Proper way to get UTF-8 character or string length
+5.2k Golang : Return multiple values from function
+6k Golang : Get missing location after unmarshal binary and gob decode time.
+7.2k Golang : Accessing dataframe-go element by row, column and name example