如何为不同时区设置多个克朗

Following my question about cron job and timezone

I would like to know what would be the best way to schedule things at different time zones:

  • Something to run at : 16h30 Tokyo time
  • Then another to run at : 10AM London time
  • Another at 3PM NYC Time , etc etc.

I have multiple cron jobs like those that i need to run and my code from previous post doesn't seem to cut it.

I don't know what is the best way to proceed as it should be independent of the server time, so working with UTC time doesn't really cut it.

package main

import (
    "fmt"
    "github.com/robfig/cron"
    "os"
    "os/signal"
    "time"
)

func helloWorld() {
  fmt.Println("hello world")
}

func main() {
    s, err1 := cron.Parse("30 10 * * *")
    fmt.Println(err1)
    l, err := time.LoadLocation("Asia/Tokyo")
    fmt.Println(err)
    c := cron.NewWithLocation(l)
    c.Schedule(s, cron.FuncJob(helloWorld))
    c.Start()

    sig := make(chan os.Signal)
    signal.Notify(sig, os.Interrupt, os.Kill)
    <-sig
}

After my previous post i've found that you could check the scheduler this way :

    test := c.Entries()
    log.Println(test[0].Schedule)
    log.Println(test[0].Next)
    log.Println(test[0].Prev)

and got as result (the first 2 nils being the err "handling"):

    <nil>
    <nil>
    2019/05/30 00:59:21 &{1073741824 1024 9223372036871553023 9223372041149743102 9223372036854783998 9223372036854775935}
    2019/05/30 00:59:21 2019-05-30 01:10:30 +0900 JST
    2019/05/30 00:59:21 0001-01-01 00:00:00 +0000 UTC

You can see that it's scheduled to run at 01:10:30 Japan time ( JST ) instead of 10:30 AM JST that i expected.

Anybody know what's happening. I'm gonna need to do this for upward of 70 timezones..

As stated in the docs s, err1 := cron.Parse("30 10 * * *") creates a new cron job executed hourly at exactly 10mins and 30sec.

Because your time was 2019/05/30 00:59:21 the next avaiable slot would be exactly like stated 01:10:30 +0900 JST.

To create a new job executed daily at 10:30 local time use s, err1 := cron.Parse("0 30 10 * *") instead. For a quick overview use Predefined schedules in the docs, it displays it in a nice table.