Search code examples
c#datetimessis-2012

C# doing a convert timezone on thousands of rows. Randomly got "The supplied DateTime represents an invalid time."


Just a quick question as I am baffled. I have some code that loops through datarows in a ssis package to convert timezones and it is failing on a row and I have no idea why.

The full error message is:

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.

The code that I run to do the convert time is:

    DateTime easternstandardtime = Row.UniversalTime;

    TimeZoneInfo timeZoneGMT = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
    TimeZoneInfo timeZoneEST = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

    easternstandardtime = TimeZoneInfo.ConvertTime(Row.UniversalTime, timeZoneGMT, timeZoneEST);

    Row.StandardEasternTime = easternstandardtime;

The code fails on the 2nd to last line (The convert time). The value for Row.UniversalTime is 3/27/2016 1:10:03 AM. How is this not ok? The full stats of the variable are below. What's strange to me is that the whole rest of the file processed fine and the datetimes are all similar. Not sure why it is randomly blowing up with a value like this. Any ideas?

Full var stats:

?Row.UniversalTime
{3/27/2016 1:10:03 AM}
Date: {3/27/2016 12:00:00 AM}
Day: 27
DayOfWeek: Sunday
DayOfYear: 87
Hour: 1
Kind: Unspecified
Millisecond: 0
Minute: 10
Month: 3
Second: 3
Ticks: 635946378030000000
TimeOfDay: {01:10:03}
Year: 2016

Solution

  • As the error message somewhat implies, you're attempting to do a conversion on a time that does not actually exist.

    http://www.timeanddate.com/news/time/europe-starts-dst-2016.html

    Most countries in Europe will spring forward 1 hour at 01:00 UTC

    So, if most European countries spring forward at 01:00 (GMT), and you're trying to convert a GMT time that's at 01:10, that time does not actually exist as per the DST adjustment. Seems that's your issue.