夏令时神秘 [英] daylight savings time mystery

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

问题描述

你好,


我正在编写一个应用程序来执行一些简单的天文数字计算。我需要的变量之一是今年过去的小时数
。我写了以下函数


public static double GetHoursofYear(DateTime aTime)

{

DateTime StartYear = new DateTime (aTime.Year,1,1);

return(aTime.ToOADate() - StartYear.ToOADate())* 24;

}


我希望这个功能能够适应夏令时,但它不会是b $ b。我在萨斯喀彻温省,我们不会改变,但如果我将我的

系统时区更改为东方,他们确实改变了我得到相同的价值

对于给定的日期和时间。


实际上我不希望我的应用程序自动进行调整,但我

不要明白为什么它不是!


感谢您的任何见解。


Marc Pelletier

解决方案

DateTime对时区一无所知。它只是假设你所做算术的日期都在同一时区。请参阅:

http://msdn.microsoft.com/library/en...classtopic.asp

" Marc Pelletier" <无****** @ please.com>在消息中写道

news:Xn ********************* @ 207.46.248.16 ...

你好,

我正在写一个做一些简单的天文计算的应用程序。我需要的变量之一是今年过去的小时数。我写了以下函数

public static double GetHoursofYear(DateTime aTime)
{DateTime StartYear = new DateTime(aTime.Year,1,1);
return(aTime.ToOADate() - StartYear.ToOADate())* 24;
}
我希望这个功能能够适应夏令时但是它没有'' T。我在萨斯喀彻温省,我们不会改变,但如果我将我的
系统时区改为东部,他们确实改变了,我会在给定的日期和时间内获得相同的价值


实际上我不希望我的应用程序自动进行调整,但我不明白为什么它不会!

感谢您的任何见解。

Marc Pelletier



" Michael A. Covington" < lo ** @ www.covingtoninnovations.com.for.address>

在新闻中写道:ua ************** @ TK2MSFTNGP09.phx.gbl:

DateTime对时区一无所知。它只是假设你所做的算术的所有日期都在同一时间
区域。请参阅:

http: //msdn.microsoft.com/library/en...ystemdatetimec
lasstopic.asp


错了。 DateTime是时区可识别的,因为它在该文档中说明了

此结构中的方法和属性始终

在计算时使用本地时区

或比较。





时间值以100纳秒为单位测量

叫做刻度线,特定日期是自公元1月1日午夜12点起的刻度数

格里高利历日历中的
(CE)

因此,如果当地时区使用DST,那么5月1日中午是1小时

之前(即自零时间以来经过的时间减少)比当地时间

区域不使用夏令时。由于我的计算将1月1日的OATime与

进行比较,因此当我将系统更改为dst

时区时,我预计会减少一小时。


文件是错误的,或者我是密集的,因为你显然是正确的。
。该函数返回正确的小时数

自年初以来只有在传递的时间没有为bst调整为



感谢您的帮助,但我仍然感到困惑。


Marc

" Marc Pelletier" <无****** @ please.com>在消息中写道
新闻:Xn ********************* @ 207.46.248.16 ...

你好,

我正在编写一个执行一些简单的天文计算的应用程序。我需要的变量之一是今年通过的小时数。我写了以下函数

public static double GetHoursofYear(DateTime aTime)
{DateTime StartYear = new DateTime(aTime.Year,1,1);
return(aTime.ToOADate() - StartYear.ToOADate())* 24;
}
我希望这个功能能够适应夏令时但是它没有'' T。我在萨斯喀彻温省,我们不会改变,但是如果我将我的系统时区改为东方,那么他们会改变我得到相同的值

返回

给定的日期和时间。

实际上我不希望我的应用程序自动进行调整,但
我不是明白为什么它不是!

感谢您的任何见解。

Marc Pelletier





嗨Marc,


你可以使用


public static double GetHoursofYear (DateTime aTime)

{

DateTime StartYear = new DateTime(aTime.Year,1,1);

double total =(aTime - StartYear).TotalHours;

if(TimeZone.CurrentTimeZone.IsDaylightSavingTime(a time))

{

DaylightTime dt = TimeZone.CurrentTimeZone.GetDaylightChanges (aTime。年);

总计 - = dt.Delta.TotalHours;

}

返回总额;

}


快乐编码!

Morten Wennevik [C#MVP]

Hello,

I am writing an application that does some simple astronomical
calculations. One of the variables I need is the number of hours passed in
this year. I''ve written the following function

public static double GetHoursofYear( DateTime aTime )
{
DateTime StartYear = new DateTime( aTime.Year, 1, 1 );
return ( aTime.ToOADate() - StartYear.ToOADate() ) * 24;
}

I would expect this function to adapt to daylight savings time but it
doesn''t. I''m in Saskatchewan where we don''t change, but if I change my
system time zone to Eastern, where they do change I get the same value back
for a given date and time.

In fact I don''t want my app to make the adjustment automatically, but I
don''t understand why it doesn''t!

Thanks for any insight.

Marc Pelletier

解决方案

DateTime doesn''t know anything about time zones. It just assumes that all
the dates on which you are doing arithmetic are in the same time zone. See:

http://msdn.microsoft.com/library/en...classtopic.asp
"Marc Pelletier" <no******@please.com> wrote in message
news:Xn*********************@207.46.248.16...

Hello,

I am writing an application that does some simple astronomical
calculations. One of the variables I need is the number of hours passed in
this year. I''ve written the following function

public static double GetHoursofYear( DateTime aTime )
{
DateTime StartYear = new DateTime( aTime.Year, 1, 1 );
return ( aTime.ToOADate() - StartYear.ToOADate() ) * 24;
}

I would expect this function to adapt to daylight savings time but it
doesn''t. I''m in Saskatchewan where we don''t change, but if I change my
system time zone to Eastern, where they do change I get the same value back for a given date and time.

In fact I don''t want my app to make the adjustment automatically, but I
don''t understand why it doesn''t!

Thanks for any insight.

Marc Pelletier



"Michael A. Covington" <lo**@www.covingtoninnovations.com.for.address>
wrote in news:ua**************@TK2MSFTNGP09.phx.gbl:

DateTime doesn''t know anything about time zones. It just assumes that
all the dates on which you are doing arithmetic are in the same time
zone. See:

http://msdn.microsoft.com/library/en...ystemdatetimec
lasstopic.asp


Wrong. DateTime is time zone aware because it says in that document that

Methods and properties in this structure always
use the local time zone when making calculations
or comparisons.

and

Time values are measured in 100-nanosecond units
called ticks, and a particular date is the number
of ticks since 12:00 midnight, January 1, 1 A.D.
(C.E.) in the GregorianCalendar calendar
So if the local time zone uses DST then noon on May 1 is one hour
earlier (ie less elapsed time since time zero ) than if the local time
zone doesn''t use DST. Since my calculation compares the OATime against
January 1, I expected one hour less when I changed my system to a dst
time zone.

Either the documentation is wrong, or I am dense, because you are
obviously right. The function returns the correct number of hours passed
since the start of the year only if the time passed in hasn''t been
adjusted for dst.

Thanks for the help, but I''m still confused.

Marc
"Marc Pelletier" <no******@please.com> wrote in message
news:Xn*********************@207.46.248.16...

Hello,

I am writing an application that does some simple astronomical
calculations. One of the variables I need is the number of hours
passed in this year. I''ve written the following function

public static double GetHoursofYear( DateTime aTime )
{
DateTime StartYear = new DateTime( aTime.Year, 1, 1 );
return ( aTime.ToOADate() - StartYear.ToOADate() ) * 24;
}

I would expect this function to adapt to daylight savings time but it
doesn''t. I''m in Saskatchewan where we don''t change, but if I change
my system time zone to Eastern, where they do change I get the same
value


back

for a given date and time.

In fact I don''t want my app to make the adjustment automatically, but
I don''t understand why it doesn''t!

Thanks for any insight.

Marc Pelletier






Hi Marc,

You can use

public static double GetHoursofYear( DateTime aTime )
{
DateTime StartYear = new DateTime( aTime.Year, 1, 1 );
double total = (aTime - StartYear).TotalHours;
if(TimeZone.CurrentTimeZone.IsDaylightSavingTime(a Time))
{
DaylightTime dt = TimeZone.CurrentTimeZone.GetDaylightChanges(aTime. Year);
total -= dt.Delta.TotalHours;
}
return total;
}

Happy coding!
Morten Wennevik [C# MVP]


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

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