Am 19.03.2011 20:31, schrieb pamela fluente:
> Hi friend, take the following simple code
> 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
> "x" axis]
>
> --------------------------
> 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_loc 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
> the dates
> NewDate_loc dates (a gap of 1 hour is fine, no problem).
As you already wrote, due to daylight saving time, not every point in time
is a valid local time. With EST, daylight saving time seems to start at
3/13/2011 2:00:00 AM. If the clock is set 1 hour ahead, the sequence is:
3/13/2011, 01:58 AM
3/13/2011, 01:59 AM
3/13/2011, 03:00 AM
3/13/2011, 03:01 AM
So, if you're trying to convert any time between
3/13/2011, 02:00 AM (inclusive) and 3/13/2011, 03:00 AM (exclusive),
the error occurs. Any point in time in this timespan simply does
not exist in that time zone.
On the other hand, whenever DST ends, there's an ambiguous timespan.
The clock is set back one hour on 11/6/2011, 02:00AM. Consequently the
sequence is
11/6/2011, 01:00 AM
11/6/2011, 01:01 AM
....
11/6/2011, 01:58 AM
11/6/2011, 01:59 AM
11/6/2011, 01:00 AM
11/6/2011, 01:01 AM
11/6/2011, 01:02 AM
You have ambiguous local times within 1 AM and 2 AM. Have a look at
TimeZoneInfo.GetAmbiguousTimeOffsets and the documentation of
TimeZoneInfo.ConvertTime how this is handled. IIRC I read something
about it there.
The resolution is to only work with UTC internally. You can
still display the local time. Or, use DateTimeOffset instead
of DateTime; then it's also always convertible.
--
Armin
|