夏令时神秘

编程入门 行业动态 更新时间:2024-10-26 16:29:33
本文介绍了夏令时神秘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好, 我正在编写一个应用程序来执行一些简单的天文数字计算。我需要的变量之一是今年过去的小时数。我写了以下函数 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对时区一无所知。它只是假设你所做算术的日期都在同一时区。请参阅: msdn.microsoft/library/en...classtopic.asp " Marc Pelletier" <无****** @ please>在消息中写道 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.for.address> 在新闻中写道:ua ************** @ TK2MSFTNGP09.phx.gbl:

DateTime对时区一无所知。它只是假设你所做的算术的所有日期都在同一时间区域。请参阅: http: //msdn.microsoft/library/en...ystemdatetimec lasstopic.asp 错了。 DateTime是时区可识别的,因为它在该文档中说明了 此结构中的方法和属性始终 在计算时使用本地时区 或比较。 和 时间值以100纳秒为单位测量 叫做刻度线,特定日期是自公元1月1日午夜12点起的刻度数 格里高利历日历中的(CE) 因此,如果当地时区使用DST,那么5月1日中午是1小时 之前(即自零时间以来经过的时间减少)比当地时间 区域不使用夏令时。由于我的计算将1月1日的OATime与 进行比较,因此当我将系统更改为dst 时区时,我预计会减少一小时。 文件是错误的,或者我是密集的,因为你显然是正确的。。该函数返回正确的小时数 自年初以来只有在传递的时间没有为bst调整为。 感谢您的帮助,但我仍然感到困惑。 Marc " Marc Pelletier" <无****** @ please>在消息中写道新闻: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: msdn.microsoft/library/en...classtopic.asp "Marc Pelletier" <no******@please> 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.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: msdn.microsoft/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> 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]

更多推荐

夏令时神秘

本文发布于:2023-10-28 08:19:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536071.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:夏令时   神秘

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!