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.4k Golang : How to check if a website is served via HTTPS
+6.7k Golang : When to use make or new?
+17k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+4.7k Mac OSX : Get disk partitions' size, type and name
+9.5k Golang : Apply Histogram Equalization to color images
+19k Golang : Padding data for encryption and un-padding data for decryption
+14.5k Golang : On enumeration
+10.7k Golang : Bubble sort example
+7k Golang : How to setup a disk space used monitoring service with Telegram bot
+8.9k Golang : Find network service name from given port and protocol
+22.5k Golang : Convert Unix timestamp to UTC timestamp
+24k Golang : Fix type interface{} has no field or no methods and type assertions example