以编程方式确定在一天/时间的偏移量处使用 UTC 时间

Posted

技术标签:

【中文标题】以编程方式确定在一天/时间的偏移量处使用 UTC 时间【英文标题】:Programmatically determine using the UTC time at what offset a day/time is 【发布时间】:2015-06-20 18:39:12 【问题描述】:

我有一个每小时触发一次的 cron 作业。在我的数据库中,我有包含 UTC 偏移量的行。我正在尝试以编程方式执行以下操作。

function returnThisHourOffset($timeofday)
    //using the current UTC hour gmdate('G'), determine at what offset from the UTC the $timeofday currently exists (**and which day**).

我已经写出了每个 UTC 在午夜和凌晨 5 点发生的事情。

    UTC 23:00:00, At offset +1 is 12AM the next UTC day. At offset +6 is 5AM, the next UTC day 
    UTC 22:00:00, At offset +2 is 12AM the next UTC day. At offset +7 is 5AM, the next UTC day
    UTC 21:00:00, At offset +3 is 12AM the next UTC day. At offset +8 is 5AM, the next UTC day
    UTC 20:00:00, At offset +4 is 12AM the next UTC day. At offset +9 is 5AM, the next UTC day
    UTC 19:00:00, At offset +5 is 12AM the next UTC day. At offset +10 is 5AM, the next UTC day
    UTC 18:00:00, At offset +6 is 12AM the next UTC day. At offset +11 is 5AM, the next UTC day
    UTC 17:00:00, At offset +7 is 12AM the next UTC day. At offset +12 is 5AM, the next UTC day
    UTC 16:00:00, At offset +8 is 12AM the next UTC day. At offset -11 is 5AM, this UTC day 
    UTC 15:00:00, At offset +9 is 12AM the next UTC day. At offset -10 is 5AM, this UTC day 
    UTC 14:00:00, At offset +10 is 12AM the next UTC day. At offset -9 is 5AM, this UTC day 
    UTC 13:00:00, At offset +11 is 12AM the next UTC day. At offset -8 is 5AM, this UTC day 
    UTC 12:00:00, At offset +12 is 12AM the next UTC day. At offset -7 is 5AM, this UTC day 
    UTC 11:00:00, At offset -11 is 12AM on the UTC day. At offset -6 is 5AM, this UTC day 
    UTC 10:00:00, At offset -10 is 12AM on the UTC day. At offset -5 is 5AM, this UTC day 
    UTC 09:00:00, At offset -9 is 12AM on the UTC day. At offset -4 is 5AM, this UTC day 
    UTC 08:00:00, At offset -8 is 12AM on the UTC day. At offset -3 is 5AM, this UTC day 
    UTC 07:00:00, At offset -7 is 12AM on the UTC day. At offset -2 is 5AM, this UTC day 
    UTC 06:00:00, At offset -6 is 12AM on the UTC day. At offset -1 is 5AM, this UTC day  
    UTC 05:00:00, At offset -5 is 12AM on the UTC day. At offset 0 is 5AM, this UTC day
    UTC 04:00:00, At offset -4 is 12AM on the UTC day. At offset +1 is 5AM, this UTC day
    UTC 03:00:00, At offset -3 is 12AM on the UTC day. At offset +2 is 5AM, this UTC day
    UTC 02:00:00, At offset -2 is 12AM on the UTC day. At offset +3 is 5AM, this UTC day
    UTC 01:00:00, At offset -1 is 12AM on the UTC day. At offset +4 is 5AM, this UTC day
    UTC 00:00:00, At offset 0 is 12AM on the UTC day. At offset +5 is 5AM, this UTC day

因此,例如returnThisHourOffset(5) 在 UTC 上午 8 点应该返回 -3 和今天的 UTC 日期。而returnThisHourOffset(5) 在 UTC 晚上 9 点(21:00:00)应该返回 8 和明天的 UTC 日期(因为今天是上午 9 点,8 小时后是明天早上 5 点)。

显然,我对夏令时或偏移量 > 12 的兴趣不大。只是想保持简单,但它看起来仍然非常复杂。

这是我的代码,它严格用于获取午夜发生的偏移量或可能是 returnThisHourOffset(0) 我正在寻找一天中的任何时间 (0-23)。

if(gmdate('A') == 'PM')
        $offset = (24 - gmdate('G'));
        $today = gmdate("Y-m-d\T00\:00\:00", strtotime('tomorrow UTC'));
     else 
        $offset = -1*(gmdate('G'));
        $today = gmdate("Y-m-d\T00\:00\:00", strtotime('today UTC'));
    

我相信我的代码不能解决这个问题,所以我希望其他人以前曾尝试过以某种方式解决这个问题。任何见解都值得赞赏。

【问题讨论】:

可能是这样的,***.com/questions/7276304/…? 我认为我不需要使用时区。 【参考方案1】:

这个看起来可以,但我愿意接受更好的建议(因为它显然很混乱)。

$utcHour = gmdate('G');
$timeofday = 5; // (0-23)

if($utcHour <= $timeofday)
    $offset = $timeofday - $utcHour;
    $today = gmdate("Y-m-d\T00\:00\:00", strtotime('today UTC'));
 elseif ($utcHour > $timeofday && $utcHour - $timeofday < 12)
    $offset = -1*($utcHour - $timeofday);
    $today = gmdate("Y-m-d\T00\:00\:00", strtotime('today UTC'));
 else 
    $offset = (24 - $utcHour) + $timeofday;
    $today = gmdate("Y-m-d\T00\:00\:00", strtotime('tomorrow UTC'));    

【讨论】:

以上是关于以编程方式确定在一天/时间的偏移量处使用 UTC 时间的主要内容,如果未能解决你的问题,请参考以下文章

Unix系统编程()open,read,write和lseek的综合练习

在给定偏移量处将音频剪辑插入原始音频文件

C++ 宏在自定义偏移量处设置类字段

如何确定服务器时区的 UTC 偏移量?

给定 UTC 时间戳和 UTC 偏移量,是不是可以在 Python 中获取时区?

使用 pytz 从 UTC 偏移“规范”?