获取给定持续时间内两个时区之间的时区偏移
我想知道是否有一种方法可以获取给定持续时间内两个时区之间给定日期范围的时区偏移量。
getTimezoneOffset(startDate,endDate,timezone1,timezone2){
...missing magic to go here...
}
应返回在给定持续时间内有效的时区偏移量。但是,如果偏移量发生变化,它应该返回其有效的日期范围。
所以我正在寻找这样的东西:
getTimezoneOFfset("march 9 12 am", "march 15 12 am", "UTC", "US/NEW_YORK")
返回值这样的东西
timezoneoffset[0]["range"]=[march 9 12am to march 11 2 am]
timezoneoffset[0]["offset"]=5
timezoneoffset[1]["range"]=[march 9 2 am to march 15 12 am]
timezoneoffset[1]["offset"]=4
我只是不想计算给定范围内每个计划项目的时区偏移量。正在寻找是否有某种方法可以直接查找偏移量(如果要更改)。
我正在使用 PHP,但任何语言的解决方案都将受到赞赏。
MySQL 解决方案也将起作用,因为它将在 MySQL 中进行更优化。
I wanted to know if there is a way to get the time zone offset for a given date range between two timezones for a given duration.
getTimezoneOffset(startDate,endDate,timezone1,timezone2){
...missing magic to go here...
}
should return the time zone offset which is valid for the a given duration. However if the offset changes, it should return the date range for which it's valid.
So I am looking at something like this:
getTimezoneOFfset("march 9 12 am", "march 15 12 am", "UTC", "US/NEW_YORK")
return value something like this
timezoneoffset[0]["range"]=[march 9 12am to march 11 2 am]
timezoneoffset[0]["offset"]=5
timezoneoffset[1]["range"]=[march 9 2 am to march 15 12 am]
timezoneoffset[1]["offset"]=4
I just don't want to calculate timezone offsets for every scheduled item for the given range. Was looking if there is some way to get a direct lookup for offsets if it's going to change.
I am working with PHP, but a solution in any language will be appreciated.
MySQL solution will also work as it will be more optimized to do this in MySQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您有较新版本的 php (5.2.0+) DateTimeZone 类是 PHP 核心的一部分,可让您使用 getOffset() 函数进行这些计算。它会让您传入 dateTime 对象 并指定时区。如果您对两个日期都执行此操作,您应该能够计算出您想要抓住的所有物品。使用 php.net 中的示例:
Assuming that you have a newer version of php (5.2.0+) the DateTimeZone class is part of the PHP core and will let you use the
getOffset()
function to make these calculations. It will let you pass in a dateTime object and specify a timezone. If you do this for both your dates you should be able to calculate all of the pieces you're looking to grab. To use an example from php.net:这对我有用:
使用它
来源:
http://php.net/manual/en/function.timezone-偏移-get.php
This working for me:
To use it
Source:
http://php.net/manual/en/function.timezone-offset-get.php
我认为 PHP 的
DateTimeZone::getTransitions
方法可以带你去那里。它有一个过程别名,timezone_transitions_get()
。此方法返回给定时间范围内该时区从一个时区偏移值到另一个时区偏移值的所有“转换”。
出于您的目的,您需要为每个时区创建
DateTimeZone
对象,针对您的日期范围调用DateTimeZone::getTransitions
以获取该时间的转换数组区,然后对两个转换数组进行合并和排序。这将为您提供与您寻找的timezoneoffset[]
数组等效的值。类似于:
转换数组的格式没有很好的记录。该方法文档显示了一些示例条目:
我推测:
ts
指的是以纪元秒为单位的 PHP 时间戳,由time()
返回,给出了时间的瞬间偏移量更改为该记录中的值。time
指同一时刻,作为格式化字符串日期时间。offset
是时区与 UTC 的偏移量(以秒为单位),从即时time
/ts
开始,向前到下一个转换。如果偏移量是指夏令时偏移量,则 isdst 为 1,否则为 0。abbr
是时区的字符串缩写。如果有人有关于此数据结构的可靠信息,请将其添加到 文档。I think PHP's
DateTimeZone::getTransitions
method can get you there. It has a procedural alias,timezone_transitions_get()
.This method returns all "transitions" from one time zone offset value to another, for that timezone, in a given time range.
For your purposes, you will want to create
DateTimeZone
objects for each of your time zones, callDateTimeZone::getTransitions
for your date range to get an array of transitions for that time zone, then merge and sort the two arrays of transitions. This will give you the equivalent of thetimezoneoffset[]
array you seek.Something like:
The format of the transitions array isn't well documented. The method documentation shows some example entries:
I speculate that:
ts
refers to a PHP timestamp in epoch seconds, as returned bytime()
, giving the instant in time at which the offset changes to the value in this record.time
refers to the same instant, as a formatted string date-time.offset
is the timezone's offset in seconds from UTC, as of the instanttime
/ts
, forward to the next transition.isdst
is 1 if the offset refers to a daylight savings time offset, 0 otherwise.abbr
is a string abbreviation for the time zone. If anyone has solid information about this data structure, it would be a kindness to add it to the documentation.