.NET Framework - when the clock is adjusted forward, any time in the period that isskipped is invalid

Asked By pamela fluente on 19-Mar-11 09:17 AM
Hi friend, take the following simple code (vb or c#)

It raises the exception:

The supplied DateTime represents an invalid time.  For example,
when the clock is adjusted forward, any time in the period that is
skipped is invalid.
Parameter name: dateTime

Could i have some enlightening on this?  (should be related with
daylight)
And how should i modify my loop to proceed normally in the generation
of new dates avoiding the error ?


[BTW i am generating time series of random prices, this is time, the


-Pam



-----------------------------------------------
Public TimeZoneInfo_LOCAL As TimeZoneInfo = TimeZoneInfo.Local
Public TimeZoneInfo_EDT As TimeZoneInfo =
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Dim d As New Date(Now.Year, Now.Month, Now.Day)
Do
Dim NewDate = d.AddMinutes(-1)
Dim NewDate_EDT As Date =
TimeZoneInfo.ConvertTime(NewDate, TimeZoneInfo_EDT,
TimeZoneInfo_LOCAL)

d = NewDate
Loop

End Sub
-----------------------------------------------


-----------------------------------------------
public TimeZoneInfo TimeZoneInfo_LOCAL = TimeZoneInfo.Local;
public TimeZoneInfo TimeZoneInfo_EDT =
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

private void Button1_Click(System.Object sender, System.EventArgs e)
{
System.DateTime d = new System.DateTime(DateAndTime.Now.Year,
DateAndTime.Now.Month, DateAndTime.Now.Day);
do {
dynamic NewDate = d.AddMinutes(-1);
System.DateTime NewDate_EDT = TimeZoneInfo.ConvertTime(NewDate,
TimeZoneInfo_EDT, TimeZoneInfo_LOCAL);

d = NewDate;
} while (true);

}

-----------------------------------------------




pamela fluente replied to pamela fluente on 19-Mar-11 09:24 AM
Or maybe it is clearer if i pose the question in a time "forward" mode:

This also gives the same error:

--------------------------

Public TimeZoneInfo_LOCAL As TimeZoneInfo = TimeZoneInfo.Local
Public TimeZoneInfo_EDT As TimeZoneInfo =
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click


Dim d As New Date(2011, 3, 1)
Do
Dim NewDate = d.AddMinutes(1)
Dim NewDate_EDT As Date =
TimeZoneInfo.ConvertTime(NewDate, TimeZoneInfo_EDT,
TimeZoneInfo_LOCAL)

d = NewDate
Loop

End Sub

--------------------------

I am looking for a way to have the loop proceed normally generating
edt dates (a gap of 1 hour is fine, no problem).

-Pam
Arne_Vajhøj replied to pamela fluente on 19-Mar-11 09:39 AM
If your app is to be able to handle DST changes, then you should
use UTC time internally and only convert to local time in display.

Arne
pamela fluente replied to Arne_Vajhøj on 19-Mar-11 09:43 AM
mple,

Yes. It' seemed just more convenient for me to deal with real dates
because these are real prices.
After all i need only 2 version of dates: LOCAL and EDT when i display
it.
If possible i'd like just to find an easy way to skip the hour. it is
right after all: these are prices randomly generated.

-Pam
Peter Duniho replied to pamela fluente on 19-Mar-11 12:03 PM
There is an easy way: use UTC time internally and only convert to local
time in display.

Hopefully, that advice sounds familiar to you.  :)

BTW, if you are going to post code in the C# newsgroup, it would be best
if you would make sure that code is C# code.  If you want help with
VB.NET code, there is a different forum for that.

Pete
pamela fluente replied to Peter Duniho on 19-Mar-11 03:32 PM
thanks.

(c# and vb.net are the same to me.)

-Pam