10

I am getting an exception thrown on DateTime.Now on our server running a few websites. This has now happened twice to me in the past 3 days. Really strange. I am wondering whether this has started to happen with the latest Windows Update and if any of you have seen similar behaviour coming in.

The exception thrown is:

BASE EXCEPTION:
  TYPE: System.ArgumentOutOfRangeException
  MESSAGE: Value to add was out of range.
Parameter name: value
  STACK TRACE:
   at System.DateTime.Add(Double value, Int32 scale)
   at System.TimeZoneInfo.TransitionTimeToDateTime(Int32 year, TransitionTime transitionTime)
   at System.TimeZoneInfo.GetDaylightTime(Int32 year, AdjustmentRule rule)
   at System.TimeZoneInfo.GetIsDaylightSavingsFromUtc(DateTime time, Int32 Year, TimeSpan utc, AdjustmentRule rule, Boolean& isAmbiguousLocalDst)
   at System.TimeZoneInfo.GetDateTimeNowUtcOffsetFromUtc(DateTime time, Boolean& isAmbiguousLocalDst)
   at System.DateTime.get_Now()
   at (my code).FrontEnd.FrontEndPage.Page_Load(Object sender, EventArgs e) in (my code file)\code\presentation\FrontEndPage.cs:line 118
   at (my code).purchase.Page_Load(Object sender, EventArgs e) in (my code file)\purchase.aspx.cs:line 94
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
   at System.Web.UI.Control.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

The code where this happens is the first line in the if-statement:

HttpCookie loggedIn = Request.Cookies[Config.Instance.LoggedInCookieName];
if (loggedIn != null)
{
    loggedIn.Expires = DateTime.Now.AddHours(4);
    Response.Cookies.Add(loggedIn);
}

Although there is an AddHours in there and the exception is talking about DateTime.Add, I don't believe it has anything to do with the AddHours, but is caused by the call to Now as you can see in the stack trace.

The server I am on is running Windows Server 2003, and is running the English (United Kingdom) locale.

Thanks for any help.

  • 1
    Sounds like either a dodgy Windows Update that caused it or a corrupted installation of .NET really... – Noldorin Apr 25 '12 at 21:15
  • Can you provide the full stack trace? Maybe this has something to do with it? http://blog.brianhartsock.com/2009/02/21/systemargumentoutofrangeexception-at-systemwebhttpcachepolicyutcsetlastmodifieddatetime-utcdate/ – mellamokb Apr 25 '12 at 21:17
  • I know this may seem unlikely, but is the system time set correctly? – hatchet - done with SOverflow Apr 25 '12 at 21:38
  • 2
    same problem in this question: http://stackoverflow.com/questions/8678681/argumentoutofrangeexception-on-cookiecontainer-getcookiesuri-uri, can you add the full stacktrace? – Peter Apr 25 '12 at 21:42
  • 1
    That exception would get thrown if the conversion happening in TransitionTimeToDateTime tries to add or subtract more than 10,000 years to the DateTime. Odd. – hatchet - done with SOverflow Apr 25 '12 at 21:50
  • I have included the full stack trace. It might help. Thanks for the suggestions. As an additional piece of info: DateTime.Now happens a lot in this application with storing data in the underlying database. With having only 2 errors in 3 days that means this is happening very rarely. – Gerard van de Ven Apr 25 '12 at 23:06
  • 2
    It happens in code that adjusts the local date/time for daylight savings transitions from the UTC time. The transition rules are read from the registry, weirdo problems like this are often corrupt registry problems. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones registry key. – Hans Passant Apr 25 '12 at 23:13
  • I have looked at the registry and there are a whole lot of entries in there. How would I find out whether anything is corrupted there? – Gerard van de Ven Apr 25 '12 at 23:25

1 Answers1

1

Reviewing the code in Reflector, your exception is occurring when an in-lined AddDays is given bad data in TransitionTimeToDateTime.

Both occurrences of AddDays process transitionTime.DayOfWeek where transitionTime is rule.DaylightTransitionStart or rule.DaylightTransitionEnd from GetDaylightTime (as well as time.DayOfWeek, but this is always Mod 7).

This seems to mean GetTimeZoneInformation is occasionally returning bad data where the AdjustmentRule is generated in GetOneYearLocalFromUtc which calls GetCurrentOneYearLocal.

Because it is happening rarely, I don't think it will be due to a corrupt registry (I'd expect it to happen every time.), but to help further checking the MSDN documentation for TIME_ZONE_INFORMATION describes the registry entries to check.

Note this information is not cached and is retrieved every time you call DateTime.Now, (which is of course the correct thing to do as DST could have just changed, or the user could have changed the current time zone) a good excuse for some premature optimisation by using DateTime.UtcNow as much as possible and applying .ToLocalTime only when you need to display the time to the user.

Mark Hurd
  • 10,665
  • 10
  • 68
  • 101