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 10 5231
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]
Marc Pelletier wrote: 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
IMHO the following method is not right because it return the same result
whatever hour in a day you call it:
public static double GetHoursofYear( DateTime aTime ) { DateTime StartYear = new DateTime( aTime.Year, 1, 1 ); return ( aTime.ToOADate() - StartYear.ToOADate() ) * 24; }
IMHO, this is the right method:
public static intGetHoursofYear(DateTime aTime)
{
DateTime StartYear = new DateTime(aTime.Year, 1, 1);
TimeSpan t = aTime-StartYear;
return t.Hours;
}
I would expect this function to adapt to daylight savings time but it doesn't.
Obviously the number of hours passed in this year
does _not_ depend on daylight savings time.
Daylight savings time regards only
the representation of a specific time instance.
- Dario
=?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRoZSBv76 yDY2XigKYpIg==?=
<da***@despammed.com> wrote in news:c97a6f$3l3$1
@grillo.cs.interbusiness.it: Obviously the number of hours passed in this year does _not_ depend on daylight savings time. Daylight savings time regards only the representation of a specific time instance.
Well, the number of hours I've lived this year doesn't depend on dst, but
the number of hours between may 1 at noon, and the new year does. In my
case I have a date and time, and if it is during the dst period obviously 1
hour less has passed.
Marc
"Morten Wennevik" <Mo************@hotmail.com> wrote in
news:opr8o73ea5klbvpo@morten_x.edunord: if(TimeZone.CurrentTimeZone.IsDaylightSavingTime(a Time)) { DaylightTime dt = TimeZone.CurrentTimeZone.GetDaylightChanges(aTime. Year); total -= dt.Delta.TotalHours; }
Right, I'm doing something like this, but this is dependent on the time
zone the computer is set for. I'm in Saskatchewan but calculating the
tides in Louisiana ( and elsewhere ), so I determine whether dst applies
the long way.
As I said before, I just don't understand why I have to correct. From the
documentation it seems that the current time zone would affect it
automatically.
cheers
Marc
"Marc Pelletier" <no******@please.com> wrote in message
news:Xn********************@207.46.248.16...
quoting the documentation: Methods and properties in this structure always use the local time zone when making calculations or comparisons.
I think they meant to say that it's not aware of time zones at all, i.e.,
what you give it is what you get.
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
Note that no time zone is specified here.
On Fri, 28 May 2004 08:47:50 -0700, Marc Pelletier
<ma**@goldak.stopspam.ca> wrote: Right, I'm doing something like this, but this is dependent on the time zone the computer is set for. I'm in Saskatchewan but calculating the tides in Louisiana ( and elsewhere ), so I determine whether dst applies the long way.
I can't offer any advice for the specific problem you're
having. But for all astronomical computations I recommend using GMT,
then convert to the local time zone when you want to display times to
the user.
Just for fun, a list of all different types of time for
astronomical use: http://www.ucolick.org/~sla/leapsecs/timescales.html
Greg:
Good advice; do you use an NTP time source to get the GMT time prior to
adjusting to the local computer?
"Greg Miller" <gm*****@gregmiller.net> wrote in message
news:5h********************************@4ax.com... On Fri, 28 May 2004 08:47:50 -0700, Marc Pelletier <ma**@goldak.stopspam.ca> wrote:
I can't offer any advice for the specific problem you're having. But for all astronomical computations I recommend using GMT, then convert to the local time zone when you want to display times to the user.
=?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRoZSBv76 yDY2XigKYpIg==?=
<da***@despammed.com> wrote in news:c97a6f$3l3$1
@grillo.cs.interbusiness.it: IMHO the following method is not right because it return the same result whatever hour in a day you call it:
public static double GetHoursofYear( DateTime aTime ) { DateTime StartYear = new DateTime( aTime.Year, 1, 1 ); return ( aTime.ToOADate() - StartYear.ToOADate() ) * 24; }
IMHO, this is the right method:
public static intGetHoursofYear(DateTime aTime) { DateTime StartYear = new DateTime(aTime.Year, 1, 1); TimeSpan t = aTime-StartYear; return t.Hours; }
Sorry, I should have responded earlier to this. You are right, yours is
better. Mine was ok because I always start at midnight, but yours is more
elegant and will still work when I don't start at midnight.
cheers
Marc This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bathroom_Monkey |
last post by:
For posterity's sake, here is an algorithm I created to take a GMT
time and convert it to U.S. central time, accounting for daylight
saving time. Note: this algorithm can be modified to work for...
|
by: hourang |
last post by:
ok im getting tired of looking for an answer and coming up short with
scripts that dont work. i have a application that uses GMT for all its
times and needs the clients timeoffset for showing the...
|
by: Ernie |
last post by:
I am creating an .ics file that when opened (via Outlook) inserts a
meeting into the calendar.
All works fine unless the date and time I'm inserting happens to occur
on the hour the time changes...
|
by: chrisdevey |
last post by:
Is there any way to make a System.Timers.Timer adjust for daylight
savings time change? In a long running process I set a timer as follows
for a daily expiration:
_myTimer = new...
|
by: Anna |
last post by:
Hi,
I've already found out that .Net Framework 1.1 does not properly handle
anything that is related to a non-current timezone. Since my project should
be released before the release of...
|
by: mmuras |
last post by:
I did not see any discussions / threads for this but if there is one please let me know:
-First, I am one of only two individuals within my company's IT Dept.
-We have a Windows Server 2003 R2...
|
by: Polaris431 |
last post by:
I have a web application in ASP.NET that will be used globally. Data
is collected on mobile devices running Windows Mobile and sent to the
web server where it is stored and can be viewed. Data is...
|
by: Generic Usenet Account |
last post by:
Hi,
Is there any way to make time-of-day adjustments for daylight savings
using only standard time functions? We have a program that executes
daily at a fixed time of day. After daylight...
|
by: RobG |
last post by:
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |