dongque8332 2019-05-29 16:12
浏览 78
已采纳

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

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..

  • 写回答

1条回答 默认 最新

  • dpca31461 2019-05-29 19:37
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog