1ºDay夏令时Java和JS表现出不同的行为 [英] 1º Day of Daylight Saving Time Java and JS showing a different behavior

查看:68
本文介绍了1ºDay夏令时Java和JS表现出不同的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设巴西利亚GMT -0300:DST于2012年10月21日00:00:00,应将时钟提前一小时

Assuming Brasilia GMT -0300: DST on 21/10/2012 at 00:00:00, when the clock should be advanced by one hour

Java

new Date(2012 - 1900, 9, 21, 0, 0, 0)
Sun Oct 21 01:00:00 BRST 2012

Chrome / FireFox(控制台)

Chrome/FireFox (console)

new Date(2012, 9, 21, 0, 0 ,0)
Sat Oct 20 2012 23:00:00 GMT-0300 (Hora oficial do Brasil)

Java中的结果是我所期望的,但是JS中的结果我无法理解。我发现了这个发布其中bjornd说

The result in Java is what I was expecting, but the result in JS I can not understand. I found this post where bjornd says


这是绝对正确的行为

This is an absolutely correct behavior

,但没有解释为什么这种行为可以。

but didn't explain why this behavior is OK.

我的问题是:

为什么JS过去一小时返回日期?

PS我知道日期已标记为弃用,但我使用的是GWT;日期是我唯一的选择。

P.S. I know Date is marked for "deprecation", but I'm using GWT; Date is my only option.

推荐答案

基本上,就我所知,答案是错误的。甚至我也不完全满意Java版本。

Basically, that answer was incorrect as far as I can see. I'm not entirely happy with the Java version, even.

从根本上说,您正在尝试构造一个从未发生过的本地日期/时间。从本地时间转换为UTC总是很棘手,因为存在三种可能性:

Fundamentally, you're trying to construct a local date/time which never happened. Translating from local time to UTC is always tricky, as there are three possibilities:


  • 明确的映射,在大多数时区都是这样每年除两个小时外的所有时间

  • 在向后过渡期间,在同一时间段两次出现相同的映射(例如,当地时间上午12:59,凌晨1点) ,... 1:59 am、1am、1:59am、2am)

  • 空白映射,其中根本不存在本地时间段(例如,本地时间为12:59 am ,凌晨2点,凌晨2:01)

巴西在午夜向前移动时钟,因此当地时间实际上是:

Brazil moves its clocks forward at midnight, so local time actually goes:

October 20th 11:58pm
October 20th 11:59pm
October 21st 01:00am
October 21st 01:01am

您要求的当地时间从未发生过。看起来Java只是假设您想将其向前滚动...而JavaScript却变得混乱:(如果您在2013年2月16日开始时要求午夜,则JavaScript结果更容易理解(但仍然不正确),例如-时钟将在15日回到晚上11点,而16日在凌晨12点是明确的,因为它只能在15日的第二 11 pm-11:59pm之后发生。

The local time you've asked for simply never happened. It looks like Java is just assuming you want to roll it forward... whereas JavaScript is getting confused :( The JavaScript result would be more understandable (but still incorrect) if you were asking for midnight at the start of February 16th 2013, for example - where the clocks would have gone back to 11pm on the 15th. 12am on the 16th is unambiguous, as it can only happen after the "second" 11pm-11:59pm on the 15th.

好的日期/时间API(在我看来非常偏颇的观点)会迫使您说出在进行转换时想要如何产生歧义和空白。

A good date/time API (in my very biased view) would force you to say how you want to happen ambiguity and gaps when you do the conversion.

这篇关于1ºDay夏令时Java和JS表现出不同的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆