检查夏令时 [英] Checking for daylight saving

查看:76
本文介绍了检查夏令时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个功能,以确定在特定日期是否观察到夏令时

(考虑到平台的区域

设置)并且遇到了建议在merlyn.com上测试时间

各个日期的区域偏移,看它是否有变化。基于

,我开发了以下checkDST()函数,据我所知
可以说,应该足够了。


它会检查传递给它的日期或当前日期,其他日期为3

,将来每3个月一次。如果他们的

时区中的任何一个时区与时期不同,那么:


true - 在日期观察到夏令时,或


false - 观察到夏令时但不是日期

返回
。如果没有找到差异,则返回undefined。我将
明确地返回undefined作为样式而不是让

循环切断并默认返回undefined。


任何人都可以建议算法是否合适,如果我的

实现是否正常?如果时区发生变化,则会失败

与夏令时无关的偏移量,如果发生变化,则b / b
的期限少于3个月且在测试的日期。


据我所知,没有任何地方能做到这一点,但他们可能会被可接受的纳入b $ b范围"测试(比如允许更改

到5分钟)并检查对称性 - 如果时区是x / b $ b不同的x个月时间,检查它是否也不同( 12 - x)

个月前,检查是否有相同的金额考虑到

考虑可接受的范围。

function checkDST( d){


d = d || new Date();


var x = new Date(d);

var dt = d.getTimezoneOffset();

var xt;


for(var i = 0; i< 3; i ++){

x.setMonth(x.getMonth()+ 3)

xt = x.getTimezoneOffset();


if(dt!= xt){

return dt< xt;

}

}

返回undefined;

}


function showDST(){


var x = isDST(new Date());

var msg;


if(x === true){

msg =''在这里观察夏令时''+

''并且有效。'';

}否则如果(x === false){

msg =''在这里观察到夏令时'''

''但不是现在。'';

}否则{

msg =''这里根本没有观察到夏令时。''

}

alert(x +'':''+ msg);

}


showDST();


-

Rob

I was investigating a function to determine whether daylight saving
was being observed on a particular date (given the platform''s regional
settings) and came across a suggestion at merlyn.com to test the time
zone offset on a variety of dates to see if it changes. Based on
that, I developed the following checkDST() function which, as far as I
can tell, should be sufficient.

It checks either the date passed to it or the current date with 3
other dates, each 3 months further into the future. If any of their
time zones differs from the epoch then either:

true - daylight saving is being observed on the date, or

false - daylight saving is observed but not on the date

are returned. If no difference is found, undefined is returned. I
explicitly return undefined as a matter of style rather than let the
loop peter out and return undefined by default.

Can anyone suggest whether the algorithm is appropriate and if my
implementation is OK? It will fail if there are changes to time zone
offsets that are not related to daylight saving, and if changes occur
for a period of less than 3 months and fall within the dates tested.

As far as I know, there isn''t anywhere that does that, but they might
be accommodated by an "acceptable range" test (say allow changes of up
to 5 minutes) and checking for symmetry - if the time zone is
different in x months time, check if it was also different (12 - x)
months ago, and check if it differed by the same amount taking into
consideration the acceptable range.
function checkDST(d) {

d = d || new Date();

var x = new Date(d);
var dt = d.getTimezoneOffset();
var xt;

for (var i=0; i<3; i++) {
x.setMonth(x.getMonth() + 3)
xt = x.getTimezoneOffset();

if (dt != xt) {
return dt < xt;
}
}
return undefined;
}

function showDST(){

var x = isDST(new Date());
var msg;

if (x === true) {
msg = ''Daylight saving is observed here '' +
''and is in force.'';
} else if (x === false) {
msg = ''Daylight saving is observed here, '' +
''but not at the moment.'';
} else {
msg = ''Daylight saving is not observed here at all.''
}
alert(x + '': '' + msg);
}

showDST();

--
Rob

推荐答案

RobG在12/20上说了以下内容/ 2007 1:27 AM:


< snip>
RobG said the following on 12/20/2007 1:27 AM:

<snip>

function checkDST(d){
function checkDST(d) {



< snip>

<snip>


funct ion showDST(){


var x = isDST(new Date());
function showDST(){

var x = isDST(new Date());



isDST未定义。 FF2.0

对象预期。 IE7。


今天漫长的一天?


-

兰迪

机会有利于准备好的思想

comp.lang.javascript常见问题 - http ://jibbering.com/faq/index.html

Javascript最佳实践 - http://www.JavascriptToolbox.com/bestpractices/


12月20日下午4:45,Randy Webb< ; HikksNotAtH ... @ aol.comwrote:
On Dec 20, 4:45 pm, Randy Webb <HikksNotAtH...@aol.comwrote:

RobG在2007年12月20日上午1:27发表以下内容:


< snip>
RobG said the following on 12/20/2007 1:27 AM:

<snip>

function checkDST(d){
function checkDST(d) {



< snip>


<snip>


函数showDST(){
function showDST(){


var x = isDST(new Date()) ;
var x = isDST(new Date());



当然应该是:


var x = checkDST(new Date());

That should of course be:

var x = checkDST(new Date());


>

isDST未定义。 FF2.0

对象预期。 IE7。


今天漫长的一天?
>
isDST is not defined. FF2.0
Object Expected. IE7.

Long day today?



:-x bugger ...

我在发布前更改了该功能的名称 - 是否正常工作

给你?我暂时不能搞乱我的区域设置

了,我会尽可能多地测试它。

-

Rob

:-x bugger...
I changed the name of the function just before posting - does it work
for you? I can''t mess with my regional settings for a little while
yet, I''ll test it more when I can.
--
Rob


RobG在2007年12月20日凌晨2:01发表以下内容:
RobG said the following on 12/20/2007 2:01 AM:

12月20日下午4:45,Randy Webb< HikksNotAtH ... @ aol.comwrote:
On Dec 20, 4:45 pm, Randy Webb <HikksNotAtH...@aol.comwrote:

> RobG说以下12 / 20/2007 1:27 AM:

< snip>
>RobG said the following on 12/20/2007 1:27 AM:

<snip>

>> function checkDST(d){
>>function checkDST(d) {


< snip>

<snip>


>> function showDST(){
var x = isDST(new Date( ));
>>function showDST(){
var x = isDST(new Date());



当然应该是:


var x = checkDST(new Date());


That should of course be:

var x = checkDST(new Date());



我更改了该函数的名称。

I changed the name of the function.


> isDST未定义。 FF2.0
对象预期。 IE7。

今天漫长的一天?
>isDST is not defined. FF2.0
Object Expected. IE7.

Long day today?



:-x bugger ...


我在发布前更改了该功能的名称 - 是否有效

给你?我暂时不能搞乱我的区域设置

,我会尽可能地测试它。


:-x bugger...
I changed the name of the function just before posting - does it work
for you? I can''t mess with my regional settings for a little while
yet, I''ll test it more when I can.



我测试了大约5分钟,在我的计算机上愚弄日期。我

从未改变任何时区设置。我只能告诉你,它有适合我的位置的




-

兰迪

机会有利于准备好的心灵

comp.lang.javascript常见问题 - http://jibbering.com/faq/index.html

Javascript最佳实践 - http://www.JavascriptToolbox.com/bestpractices/


这篇关于检查夏令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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