Help us understand the problem. What is going on with this article?

Safari には、1970年以前のDateが西半球で1ミリ秒ずれるバグがある

More than 1 year has passed since last update.

Safari の JavaScript エンジンには、バグがあるようだ。

Safari では、時間帯が負の数のときに、
1970年以前の ISO 8601 (W3C DateTime) 表記の日時文字列を
Date に突っ込むと、時刻が1ミリ秒ズレてしまう。

new Date("1969-12-31T23:59:59.999+01:00").toJSON();
// OK! => "1969-12-31T22:59:59.999Z"

new Date("1969-12-31T23:59:59.999-01:00").toJSON();
// NG! => "1970-01-01T00:59:59.998Z"

本来なら 59.999 秒のところ、59.998 秒となる。
きっと、計算途中で小数点以下の丸め誤差が発生しているんだろう。

タイムゾーン 1970年以降 1969年以前
+00:00 以上(東半球) ✅  OK ✅  OK
-00:00 未満(西半球) ✅  OK ❌  NG

以下のコードでバグ有無を判別できる。→ gist

function checkDateBug() {
  var date = +new Date("1969-12-31T23:59:59.999-01:00");
  return date % 1000 !== 999;
}

JavaScript で 64bit の time_t とナノ秒を扱うクラスを書いています。
https://www.npmjs.com/package/timestamp-nano

kawanet
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした