Server Hour 考虑另一个 TZ 作为参考
Server Hour considering another TZ as reference
我想在特定时间触发工人。问题是我需要知道那个小时服务器的相对时间是多少。客户希望在洛杉矶时间每天晚上 8 点触发工作人员,所以我必须让它足够动态,以占用服务器时间,计算洛杉矶那个小时的等效时间。我也在使用 carbon,但是有什么内置函数吗?或者有什么已知的处理方法吗?
time.LoadLocation
和 time.In
是您需要的两个功能。以下代码是从 time.LoadLocation
示例复制和修改的:
func main() {
location, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
panic(err)
}
timeInUTC := time.Date(2018, 8, 30, 12, 0, 0, 0, time.UTC)
fmt.Println(timeInUTC.In(location))
now := time.Now()
timeThere := time.Date(now.Year(), now.Month(), now.Day(), 8, 0, 0, 0, location)
timeHere := timeThere.In(time.Now().Location())
fmt.Println(timeHere)
}
我想在特定时间触发工人。问题是我需要知道那个小时服务器的相对时间是多少。客户希望在洛杉矶时间每天晚上 8 点触发工作人员,所以我必须让它足够动态,以占用服务器时间,计算洛杉矶那个小时的等效时间。我也在使用 carbon,但是有什么内置函数吗?或者有什么已知的处理方法吗?
time.LoadLocation
和 time.In
是您需要的两个功能。以下代码是从 time.LoadLocation
示例复制和修改的:
func main() {
location, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
panic(err)
}
timeInUTC := time.Date(2018, 8, 30, 12, 0, 0, 0, time.UTC)
fmt.Println(timeInUTC.In(location))
now := time.Now()
timeThere := time.Date(now.Year(), now.Month(), now.Day(), 8, 0, 0, 0, location)
timeHere := timeThere.In(time.Now().Location())
fmt.Println(timeHere)
}