'How to pass timezone to cron expression using gocraft?

I am using github.com/gocraft/work for creating cron jobs. My server time is in UTC and I want cron job to be executed based on specific timezone, is there any way to specify cron expression with timezone in gocraft worker.

    // Code here
    pool := work.NewWorkerPool(Context{}, 10, "test_namespace", redisPool)
    pool.PeriodicallyEnqueue("0 30 10 * * *", "export") // Runs at 10:30am everyday
    pool.Job("export", jobsHandler.SendExportReport)
   
    // Start processing jobs
    pool.Start()

    // Wait for a signal to quit:
    signalChan := make(chan os.Signal, 1)
    signal.Notify(signalChan, os.Interrupt, os.Kill)
    <-signalChan


Solution 1:[1]

Since the package you are using is expecting the cron time in the local time of the server (UTC) and you have a specific timezone input - simple convert your local (TZ) time to UTC:

nyc, err := time.LoadLocation("America/New_York")
if err != nil {
    log.Fatal(err)
}

// 12:15:23 -0400 EDT
localTime := time.Date(
    2022, 5, 13, // date (ignored below)
    12,  // hour
    15,  // minute
    23,  // seconds,
    0,   // ns
    nyc, // tz
)

// tz Time in UTC
h, m, s := localTime.UTC().Clock()  // 16 15 23

cronTime := fmt.Sprintf("%d %d %d * * *", s, m, h) // 23 15 16 * * *

https://go.dev/play/p/QdpoLsDiPwR

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 colm.anseo