【问题标题】:The UTC time represented when the offset is applied must be between year 0 and 10,000. Parameter name: offset应用偏移量时表示的 UTC 时间必须介于 0 年和 10,000 年之间。参数名称:偏移量
【发布时间】:2012-11-27 18:19:59
【问题描述】:

我在 ASP.NET MVC3 控制器中有以下代码:

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));
    }

我的视图模型是DateTimeOffset?

抛出异常的原因是什么?

【问题讨论】:

  • 那么在失败的情况下monthyear 的值是多少? (注意,null-coalescing 运算符会使这更简单,顺便说一句。)
  • 1 和 1,所以它就像 DateTime.MinValue
  • @JonSkeet 感谢您提醒有关空合并运算符

标签: asp.net-mvc exception datetimeoffset


【解决方案1】:

DateTimeOffset 构造函数首先将任何不属于Kind 'UTC' 的DateTime 转换为等效的UTC 时间。然后,它会检查与 UTC 等效的 DateTime 是否超出了 DateTimeOffset.MinValueDateTimeOffset.MaxValue 的范围,如果超出,将抛出与您遇到的类似的 ArgumentOutOfRangeException

检查您正在使用的变量testDateTime.Kind,如果它不是“UTC”,则计算转换为UTC 是否会使test 指定的DateTime 不在这些范围内界限 - 根据 MSDN 文档,MinValueMaxValue(UTC 时间)是 '1/1/0001 12:00:00 AM +00:00' 和 '12/31/9999 11:59:59分别是下午 +00:00'。

文档 (DateTimeOffset.MinValue) 请注意:

"在方法执行与 MinValue 比较之前,任何 DateTimeOffset 值都会转换为协调世界时 (UTC)。这意味着日期和时间接近最小范围但偏移量为正的 DateTimeOffset 值可能会抛出例外。例如,值 1/1/0001 1:00:00 AM +02:00 超出范围,因为它在转换为 UTC 时比 MinValue 早一小时。"

还有(DateTimeOffset.MaxValue):

“任何 DateTimeOffset 值在该方法与 MaxValue 比较之前都会转换为协调世界时 (UTC)。这意味着日期和时间接近最大范围但偏移量为负的 DateTimeOffset 值可能会抛出例外。例如,值 12/31/9999 11:00 PM -02:00 超出范围,因为在转换为 UTC 时它比 MaxValue 晚一小时。"

根据文档 (DateTimeOffset Constructor),应用于非 UTC Kind 的偏移量是“本地系统当前时区的偏移量”。

【讨论】:

  • 不知道为什么不行,最后我把模型改成DateTime解决了?
  • 就像 chamila 说的那样,它失败了,因为日期超出了允许的范围。 new DateTimeOffset(DateTime.MinValue) 将在时区早于 UTC(正偏移)的任何计算机上失败,new DateTimeOffset(DateTime.MaxValue) 将在时区晚于 UTC(负偏移)的任何计算机上失败。
  • 部分问题是 MinValue/MaxValue 有 DateTimeKind = unspecified。如果将它们转换为 UTC,它就可以工作。 var x1 = new DateTimeOffset(DateTime.MaxValue.ToUniversalTime()); var x2 = new DateTimeOffset(DateTime.MinValue.ToUniversalTime());
【解决方案2】:

我刚刚遇到了这个问题,由我的团队中位于负 UTC 区域的那部分人介绍...

chamila_c 发布的内容是发生这种情况的真正原因,但我需要快速修复。

为了“解决它”,我基本上创建了这个扩展:

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

您可能还想检查 MaxValue。

【讨论】:

    【解决方案3】:

    如果您处理的数据类型是 DateTime,您应该创建一个 DateTime 对象来指定 Kind。

    DateTime maxDate = DateTime.SpecifyKind(DateTime.MaxValue, DateTimeKind.UTC);
    

    当它转换为 DateTimeOffset 数据类型时,您不会收到该错误。

    【讨论】:

      【解决方案4】:

      当您在 function.json 中设置计时器触发计划使其永远不会运行时,可能会在 Azure Function 开发期间发生这种情况,如下所示:

      {
        "scriptFile": "__init__.py",
        "bindings": [
          {
              "name": "mytimer",
              "type": "timerTrigger",
              "direction": "in",
              "schedule": "0 0 0 31 2 *"
          }
        ]
      }
      

      ... 我已将其设置为在 2 月 31 日运行(即从不)。显然“从不”超出了允许的日期范围。解决方案是不那么棘手,只需在遥远的未来设定一个真实的日期:

      {
        "scriptFile": "__init__.py",
        "bindings": [
          {
              "name": "mytimer",
              "type": "timerTrigger",
              "direction": "in",
              "schedule": "0 0 0 1 1 * "
          }
        ]
      }
      

      (1月1日运行一次,明年才运行。)

      再次感谢 Chamila Chulatunga 对问题根源的描述。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-05
        • 2016-05-07
        • 1970-01-01
        • 1970-01-01
        • 2011-03-22
        相关资源
        最近更新 更多