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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助