【问题标题】:UTC time is incorrectly displayed as local timeUTC 时间错误地显示为本地时间
【发布时间】:2022-06-10 20:47:08
【问题描述】:

我发现某些日期(目前我只发现此错误与一些纪元之前的日期有关)与 UTC 相差一小时。

当地时间是 GMT-3

我第一次看到它是用 Javascript 编写的

> new Date("1969-07-26T03:00:00+00:00")
< Fri Jul 25 1969 23:00:00 GMT-0400 (-03) // why is it -0400?

> new Date("1963-07-26T03:00:00+00:00")
< Fri Jul 26 1963 00:00:00 GMT-0300 (-02)

然后我在 Ruby 中尝试了同样的事情

irb(main):288:0> Time.parse("1969-07-26T03:00:00+00:00").localtime
=> 1969-07-25 23:00:00 -0400

但是(也许我做错了)在 Python 中不会发生

In [12]: utc = datetime.fromisoformat("1969-07-26T03:00:00+00:00")

In [13]: utc.replace(tzinfo=tz.tzutc())
Out[13]: datetime.datetime(1969, 7, 26, 3, 0, tzinfo=tzutc())

In [14]: utc.astimezone(tz.tzlocal())
Out[14]: datetime.datetime(1969, 7, 26, 0, 0, tzinfo=tzlocal())

我无法找到有关它的信息。有什么要阅读的内容以及如何处理这些情况?例如 1963-07-26T03:00:00+00:00 按预期工作。

干杯!

【问题讨论】:

  • 差异可能与夏令时法律的变化有关。
  • 我不确定这是否是您的示例的原因,但 1969 年是英国全年保持 DST 的年份 - 参见例如 timeanddate.com/time/change/uk/…
  • 嗨@AHaworth,这是有道理的,但new Date("1969-12-26T03:00:00+00:00") 按预期工作,显示Fri Dec 26 1969 00:00:00 GMT-0300 (-03) ????。

标签: javascript python ruby date unix-timestamp


【解决方案1】:

+00:00 添加到日期字符串的末尾会创建一个UTC 偏移量。由于您指定了零小时、零分钟和零秒,因此它创建了与 GMT 的零偏移量,或者换句话说,将您的时区设置为准确的 GMT。

例如,我在 GMT -5 时区(美国中部)。当我使用今天早上 6 点的日期创建新的 JS 日期时,我得到了 5 小时的减法(因为 GMT -5 是我的时区):

> new Date("2022-06-08T06:00:00+00:00")
// Wed Jun 08 2022 01:00:00 GMT-0500 (Central Daylight Time)

但是,如果我省略了偏移量,那么它会按预期工作,并且我会在我的本地时区中获得日期。

> new Date("2022-06-08T06:00:00")
// Wed Jun 08 2022 06:00:00 GMT-0500 (Central Daylight Time)

我还可以运行您的代码示例并获得预期的结果。

> new Date("1969-07-26T03:00:00")
// Sat Jul 26 1969 03:00:00 GMT-0500 (Central Daylight Time)

有关详细信息,请参阅 ECMAScript 规范中的 Date Time String Format 部分。

【讨论】:

  • 嗨,谢谢,但恕我直言,您没有重现预期的结果,1969-07-26 03:00:00 UTC 应该是 1969-07-26 00:00:00 GMT-3
  • FWIW,JS Date 对象似乎也知道夏令时,这可能是您所看到的来源。例如,new Date("2022-07-26T03:00:00+00:00") 让我得到 GMT -5(中部夏令时间),但 new Date("2022-01-26T03:00:00+00:00") 得到 GMT - 6(中部标准时间)。
【解决方案2】:

我认为原因是从 1969 年 4 月 6 日星期日 00:00 到 1969 年 10 月 5 日星期日 00:00,America/Buenos_Aires 时区观察到 UTC-4h 的 UTC 偏移量。所以我不认为这是一个错误,而是反映了当时的 UTC 偏移量。

在 1963 年 10 月 1 日 00:00(从 ~1946 年开始)之前,UTC 偏移量始终为 3 小时,没有 DST。

因此,该时区的 UTC 偏移量随时间而变化,从 UTC-2 到 UTC-4,具体取决于年份和日期。自 2009 年 3 月 15 日星期日以来,UTC 偏移量一直为 UTC-3。

我将在下面添加更多不同 UTC 偏移量的示例,其中两个来自您的原始示例,还有一些用于说明不同的 UTC 偏移量。

const timeZone = 'America/Buenos_Aires';
const fmt1 = new Intl.DateTimeFormat('en-GB', { timeStyle: "medium",
  dateStyle: "medium", timeZone: 'UTC'});
const fmt2 = new Intl.DateTimeFormat('en-GB', { timeStyle: "long",
  dateStyle: "medium", timeZone});

const examples = [
    new Date("1962-07-26T03:00:00+00:00"),
    new Date("1963-07-26T03:00:00+00:00"),
    new Date("1964-07-26T03:00:00+00:00"),
    new Date("1969-07-26T03:00:00+00:00"),
    new Date("2008-10-19T03:00:00+00:00"),
    new Date("2010-07-26T03:00:00+00:00"),
    new Date("2020-07-26T03:00:00+00:00"), 
];

console.log('UTC'.padEnd(25, ' '), 'Local time');  
for(let dt of examples) { 
    console.log(fmt1.format(dt).padEnd(25, ' '), fmt2.format(dt));
}
 
.as-console-wrapper { max-height: 100% !important; }

这里是从 1950 年开始时区的 UTC 偏移量变化的列表,这说明了这个事实已经改变了很多次。直到 2009 年,它一直保持在 UTC-3。

let { IANAZone, DateTime } = luxon;
const timeZone = new IANAZone('America/Buenos_Aires');

let date = DateTime.fromISO('1950-01-01T03:00:00Z').setZone('UTC');
let endDate = DateTime.now();
let lastOffset = 0;

function formatUTCOffset(offsetMinutes) {
    return (offsetMinutes / 60) + ':' + String(offsetMinutes % 60).padStart(2, '0');
}

console.log('Time'.padEnd(24, ' '), '\t', 'UTC Offset')
while (date < endDate) {
    if (lastOffset !== timeZone.offset(date)) {
        console.log(date.toISO(), '\t', formatUTCOffset(timeZone.offset(date)));
        lastOffset = timeZone.offset(date);
    }
    date = date.plus( { day: 1 } );
}
.as-console-wrapper { max-height: 100% !important; }
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.3.1/luxon.min.js" integrity="sha512-Nw0Abk+Ywwk5FzYTxtB70/xJRiCI0S2ORbXI3VBlFpKJ44LM6cW2WxIIolyKEOxOuMI90GIfXdlZRJepu7cczA==" crossorigin="anonymous" referrerpolicy="no-referrer"&gt;&lt;/script&gt;

【讨论】:

  • 您好,感谢您抽出宝贵时间,但您没有回答我的问题,如果 GMT-3 与 UTC 时间相差 -3 小时,为什么我会得到 -4 小时。
  • 啊,是的,抱歉,我会更新的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-27
  • 2018-02-10
  • 2013-08-26
  • 1970-01-01
  • 2014-07-18
  • 2021-02-04
  • 2010-09-15
相关资源
最近更新 更多