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
+18.6k Golang : Reset or rewind io.Reader or io.Writer
+20.1k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+4.4k Python : Delay with time.sleep() function example
+13.6k Golang : Missing Bazaar command
+19.7k Golang : How to read float value from standard input ?
+12.1k Golang : Read from buffered reader until specific number of bytes
+5.7k Golang : Reverse by word
+4.1k Golang : Convert lines of string into list for delete and insert operation
+14k Golang : rune literal not terminated error
+4.3k Responsive Google Adsense
+61.4k Golang : Convert HTTP Response body to string
+18.2k Golang : Convert seconds to human readable time format example