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
+8.6k Golang : Apply Histogram Equalization to color images
+14.9k Golang : invalid character ',' looking for beginning of value
+12k Golang : HTTP response JSON encoded data
+8.9k Golang : Create unique title slugs example
+10.6k Golang : Create S3 bucket with official aws-sdk-go package
+10k Android Studio : Checkbox for user to select options example
+8.2k Golang : Set or add headers for many or different handlers
+14k Golang : Overwrite previous output with count down timer
+12.8k Golang : Count number of runes in string
+9.7k Golang : Get login name from environment and prompt for password
+15.9k CodeIgniter/PHP : Create directory if does not exist example
+18.7k Golang : Fix cannot download, $GOPATH not set error