Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

javascript - 如何处理节省夏令时的浏览器兼容性问题(How to deal with double daylight savings time browser compatibility issues)

The issue I'm having is that Chrome and IE/Edge give different results serializing certain JavaScript datetimes to JSON - for example summer of 1945 in the UK.

(我遇到的问题是Chrome和IE / Edge在将某些JavaScript日期时间序列化为JSON时给出不同的结果-例如1945年夏天在英国。)

I have a JavaScript object containing a Date type set via a calendar component which sets time to midnight local time.

(我有一个JavaScript对象,其中包含一个通过日历组件设置的Date类型,该组件将时间设置为当地时间午夜。)

This object is serialized via JSON.stringify which in turn uses Date.toJSON which in turn uses Date.toISOString.

(该对象通过JSON.stringify序列化,而JSON.stringify使用Date.toJSON,而Date.toJSON使用Date.toISOString。)

If I have a local datetime of say 1st May 1977 in my UK locale, this serializes to "1977-04-30T23:00:00.000Z" , ie UTC 11pm 30th April 1977. This is fine, however if I start with local 1st May 1945, I end up with different results using Chrome vs IE/Edge.

(如果我在英国的语言环境中有一个本地日期时间1977年5月1日,则该日期序列化为“ 1977-04-30T23:00:00.000Z” ,即1977年4月30日晚上11点。这很好,但是如果我从本地1st开始1945年5月,我最终使用Chrome vs IE / Edge获得了不同的结果。)

Chrome correctly gives me "1945-04-30T22:00:00.000Z" .

(Chrome正确地给我“ 1945-04-30T22:00:00.000Z” 。)

The reason this is 10pm rather than 11pm is because the UK operated double daylight savings time during WW2.

(之所以选择晚上10点而不是晚上11点,是因为第二次世界大战期间英国实行了双夏令时。)

IE/Edge however subtracts one hour meaning the JSON UTC datetime string is out by an hour.

(IE / Edge却减去一小时,这意味着JSON UTC日期时间字符串超出了一个小时。)

What this means for me is that I cannot trust the JSON serialized data passed back to the server unless I know which browser it came from.

(这对我来说意味着,除非我知道它来自哪个浏览器,否则我不能相信传递回服务器的JSON序列化数据 。)

I'm contemplating overriding Data.prototype.toJSON to change the serialization to something based on local time, but including the timezone offset, such as "1945-05-01T00:00:00.000+0200".

(我正在考虑重写Data.prototype.toJSON以将序列化更改为基于本地时间的内容,但包括时区偏移量,例如“ 1945-05-01T00:00:00.000 + 0200”。)

At least then I know what the user actually intended.

(至少那时我知道用户的实际意图。)

However this does seem a bit extreme and wonder if I'm missing something.

(但是,这似乎有些极端,想知道我是否缺少任何东西。)

JSON.stringify is the usual method for serializing data to be shipped to a server, but this whole mechanism seems flawed.

(JSON.stringify是序列化要传送到服务器的数据的常用方法,但是整个机制似乎有缺陷。)

  ask by Spiny Norman translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...