zoukankan      html  css  js  c++  java
  • DateTimeOffset的构造函数抛出异常:UTC offset of local dateTime does not match the offset argument(转载)

    DateTimeOffset Error: UTC offset of local dateTime does not match the offset argument

    问:


    I'm trying to create a small method that converts the time from one timezone to another. I thought it would be simple enough, but when I deploy it I get this error The UTC Offset of the local dateTime parameter does not match the offset argument. My guess is that it's because the server is not in the same timezone as the user which is not helpful since this would be used from around the world.

    public object ConvertDate(DateTime inputTime, string fromOffset, string toZone)
    {
        var fromTimeOffset = new TimeSpan(0, - int.Parse(fromOffset), 0);
        var to = TimeZoneInfo.FindSystemTimeZoneById(toZone);
        var offset = new DateTimeOffset(inputTime, fromTimeOffset);
        var destination = TimeZoneInfo.ConvertTime(offset, to); 
        return destination.DateTime;
    }

    Where fromOffset is a number, converted to timespan from the users timezone and toZone is the name of the zone we're converting to. The error occurs on this line var offset = new DateTimeOffset(inputTime, fromTimeOffset);

    Any ideas on how to get this working?

    答:


    See the documentation for why the exception is being thrown:

    ArgumentException: dateTime.Kind equals Local and offset does not equal the offset of the system's local time zone.

    The DateTime argument that you receive has its Kind property set to Local. The simplest way around this problem is to set the Kind to Unspecified.

    public object ConvertDate(DateTime inputTime, string fromOffset, string toZone)
    {
        // Ensure that the given date and time is not a specific kind.
        inputTime = DateTime.SpecifyKind(inputTime, DateTimeKind.Unspecified);
    
        var fromTimeOffset = new TimeSpan(0, - int.Parse(fromOffset), 0);
        var to = TimeZoneInfo.FindSystemTimeZoneById(toZone);
        var offset = new DateTimeOffset(inputTime, fromTimeOffset);
        var destination = TimeZoneInfo.ConvertTime(offset, to); 
        return destination.DateTime;
    }

    所以我们在使用DateTimeOffset的构造函数将DateTime类型转换为DateTimeOffset类型时要小心,特别是将DateTime.Now返回的DateTime对象转换为DateTimeOffset类型时,要使用DateTime.SpecifyKind方法,来将DateTime类型的Kind属性设置为DateTimeKind.Unspecified,这样才能将DateTime对象转换为非操作系统时区的DateTimeOffset对象,如下代码所示:

    DateTime now = DateTime.Now;//DateTime.Now返回的DateTime类型,默认情况下其Kind属性为DateTimeKind.Local
    DateTimeKind dateTimeKind = now.Kind;//DateTimeKind.Local
    
    //DateTimeOffset nowOffset = new DateTimeOffset(now, TimeSpan.FromHours(10));//因为当前操作系统设置的时区是UTC+8,所以如果这里DateTimeOffset构造函数的第二个参数不是8小时的TimeSpan(这里我们设置为了UTC+10),就会抛出异常:System.ArgumentException: 'The UTC Offset of the local dateTime parameter does not match the offset argument. (Parameter 'offset')'
    now = DateTime.SpecifyKind(now, DateTimeKind.Unspecified);//所以我们要将DateTime类型的Kind属性,通过方法DateTime.SpecifyKind转换为DateTimeKind.Unspecified
    DateTimeOffset nowOffset = new DateTimeOffset(now, TimeSpan.FromHours(10));//这样DateTimeOffset构造函数的第二个参数就可以是表示任何时区的TimeSpan了,不会抛出异常,这里我们设置为了UTC+10

    原文链接

  • 相关阅读:
    详解JVM中的内存模型是什么?
    【亲测】手把手教你如何破解pycharm(附安装包和破解文件)
    Spring实战第4版PDF下载含源码
    Tomcat的基本使用及相关知识的概述(超详细版)
    JVM中垃圾回收机制如何判断是否死亡?详解引用计数法和可达性分析 !
    详解JVM中的五大内存区域
    [Django][python][工具]阿里云平台短信验证功能
    [数据分析][评价方法]打分式评价-信息熵理论与熵权法
    [工具]用OmniGraffle 五步绘制流程图
    [操作系统]设备分配中的数据结构:设备控制表(DCT)、控制器控制表(COCT)、通道控制表(CHCT)和系统设备表(SDT)
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/12372079.html
Copyright © 2011-2022 走看看