LoginSignup
10
2

More than 5 years have passed since last update.

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

Posted at

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

10
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
2