Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@kawanet

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

More than 3 years have 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

2
Help us understand the problem. What is going on with this article?
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
kawanet

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?