The UTC time represented when the offset is applied must be between year 0 and 10,000. Parameter name: offset

我只是一个虾纸丫 提交于 2019-11-28 02:32:06

问题


I have the following code in an ASP.NET MVC3 Controller:

public PartialViewResult GetCalendar(int? month, int? year)
    {
        var test = new DateTime((year.HasValue ? year.Value : 1), (month.HasValue ? month.Value : 1), 1);
        return PartialView("Calendar", new DateTimeOffset(test));
    }

My view model is DateTimeOffset?

What is the reason for the exception thrown?


回答1:


The DateTimeOffset constructor first converts any DateTime that is not of Kind 'UTC' to the equivalent UTC time. It will then check whether the UTC-equivalent DateTime falls outside of the bounds of DateTimeOffset.MinValue and DateTimeOffset.MaxValue, and if it does, will throw an ArgumentOutOfRangeException similar to the one you are experiencing.

Check the DateTime.Kind of the variable test that you are using, and if it is not 'UTC', work out if a conversion to UTC will make the DateTime specified by test fall outside of those bounds - according to the MSDN documentation, the MinValue and MaxValue (in UTC) are '1/1/0001 12:00:00 AM +00:00' and '12/31/9999 11:59:59 PM +00:00' respectively.

The docs (DateTimeOffset.MinValue) note that:

"Any DateTimeOffset value is converted to Coordinated Universal Time (UTC) before the method performs the comparison with MinValue. This means that a DateTimeOffset value whose date and time are close to the minimum range, but whose offset is positive, may throw an exception. For example, the value 1/1/0001 1:00:00 AM +02:00 is out of range because it is one hour earlier than MinValue when it is converted to UTC."

And also (DateTimeOffset.MaxValue):

"Any DateTimeOffset value is converted to Coordinated Universal Time (UTC) before the method compares it with MaxValue. This means that a DateTimeOffset value whose date and time are close to the maximum range, but whose offset is negative, may throw an exception. For example, the value 12/31/9999 11:00 PM -02:00 is out of range because it is one hour later than MaxValue when it is converted to UTC."

And as per the docs (DateTimeOffset Constructor), the offset that is applied to a non-UTC Kind is the "offset of the local system's current time zone".




回答2:


I just had this issue, introduced by the part of my team that's on a negative UTC zone...

What chamila_c posted is the real reason why this happens, but I needed a quick fix.

To "solve it" I basically created this extension:

public static class DateTimeExtensions
{
    public static DateTimeOffset ToDateTimeOffset(this DateTime dateTime)
    {
        return dateTime.ToUniversalTime() <= DateTimeOffset.MinValue.UtcDateTime
                   ? DateTimeOffset.MinValue 
                   : new DateTimeOffset(dateTime);
    }
}

you might also want to check against the MaxValue.



来源:https://stackoverflow.com/questions/13799214/the-utc-time-represented-when-the-offset-is-applied-must-be-between-year-0-and-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!