[英]Deserialized DateTime value changes across different versions of the .NET framework
When I compile and run the following program targeting .NET Framework 3.5, the DateTime
that's printed to the screen is 11/1/2006 7:05:00 PM
. 当我编译并运行以下针对.NET Framework 3.5的程序时,打印到屏幕上的DateTime
是11/1/2006 7:05:00 PM
。 (I am in the Central time zone.) If I change my project to target .NET framework 4.0 or higher and run the program I get an output of 11/1/2006 6:05:00 PM
, 1 hour earlier. (我在中央时区。)如果我将项目更改为目标.NET Framework 4.0或更高版本并运行该程序, 11/1/2006 6:05:00 PM
1小时前获得11/1/2006 6:05:00 PM
的输出。
I've noticed that when using Framework 3.5 if I change my computer's checkbox for Daylight Saving Time the output changes to 6:05 PM, but when using Framework 4.x making changes to the Daylight Saving Time checkbox doesn't affect the output of the program. 我注意到,使用Framework 3.5时,如果我将计算机的夏令时复选框更改为输出,则输出更改为6:05 PM,但使用Framework 4.x时,如果更改夏令时复选框,则不会影响输出。该程序。
What is going on here and which time is the "correct" time? 这是怎么回事,什么时候是“正确的”时间? Why would changing the targeted framework affect that? 为什么更改目标框架会对此产生影响?
using Newtonsoft.Json;
using System;
namespace Test
{
public class MyData
{
public DateTime? ActivationDate { get; set; }
}
public class Program
{
public static void Main()
{
string json = "{ \"ActivationDate\":\"\\/Date(1162425900000-0400)\\/\"}";
Console.WriteLine(JsonConvert.DeserializeObject<MyData>(json).ActivationDate);
}
}
}
I found this similar question ( DateTime value is different across different versions of .NET framework ) but the answer says that it is the locale settings and not the framework that are causing the issue. 我发现了类似的问题( 不同版本的.NET Framework的DateTime值不同 ),但答案表明,导致此问题的是语言环境设置而不是框架。 However, this doesn't seem to be in accordance with what I'm witnessing with my program wherein changing nothing but the framework (and reinstalling the Nuget package for Newtonsoft JSON) seems to be affecting the output. 但是,这似乎与我在程序中看到的不一致,其中仅更改框架(并重新安装Newtonsoft JSON的Nuget包)似乎会影响输出。
After digging through numerous websites looking for an answer I stumbled across https://blog.appliedis.com/2013/03/06/beware-daylight-saving-time-transitions-in-dot-net/ which pointed me in the right direction. 在浏览众多网站以寻找答案后,我偶然发现https://blog.appliedis.com/2013/03/06/beware-daylight-saving-time-transitions-in-dot-net/ ,这使我找到了正确的答案方向。 As it turns out, .NET Framework 3.5 apparently didn't have a clear picture on when Daylight Saving Time ended in 2006 as evidenced by this test program: 事实证明,如以下测试程序所示,.NET Framework 3.5显然尚无清晰的夏时制何时于2006年结束的图片:
using System;
namespace Test
{
public class Program
{
public static void Main()
{
DateTime begin = new DateTime(2006, 10, 24);
while (begin < new DateTime(2006, 12, 25))
{
Console.WriteLine(begin + " - " + begin.IsDaylightSavingTime());
begin = begin.AddDays(1);
}
}
}
}
Running this program compiled against Framework 3.5 gives the following results: 运行针对Framework 3.5编译的程序可获得以下结果:
10/24/2006 12:00:00 AM - True
10/25/2006 12:00:00 AM - True
10/26/2006 12:00:00 AM - True
10/27/2006 12:00:00 AM - True
10/28/2006 12:00:00 AM - True
10/29/2006 12:00:00 AM - True
10/30/2006 12:00:00 AM - True
10/31/2006 12:00:00 AM - True
11/1/2006 12:00:00 AM - True
11/2/2006 12:00:00 AM - True
11/3/2006 12:00:00 AM - True
11/4/2006 12:00:00 AM - True
11/5/2006 12:00:00 AM - True
11/6/2006 12:00:00 AM - False
11/7/2006 12:00:00 AM - False
11/8/2006 12:00:00 AM - False
11/9/2006 12:00:00 AM - False
11/10/2006 12:00:00 AM - False
11/11/2006 12:00:00 AM - False
while running it against Framework 4.0 shows this: 在Framework 4.0上运行时显示如下:
10/24/2006 12:00:00 AM - True
10/25/2006 12:00:00 AM - True
10/26/2006 12:00:00 AM - True
10/27/2006 12:00:00 AM - True
10/28/2006 12:00:00 AM - True
10/29/2006 12:00:00 AM - True
10/30/2006 12:00:00 AM - False
10/31/2006 12:00:00 AM - False
11/1/2006 12:00:00 AM - False
11/2/2006 12:00:00 AM - False
11/3/2006 12:00:00 AM - False
11/4/2006 12:00:00 AM - False
11/5/2006 12:00:00 AM - False
11/6/2006 12:00:00 AM - False
11/7/2006 12:00:00 AM - False
11/8/2006 12:00:00 AM - False
11/9/2006 12:00:00 AM - False
11/10/2006 12:00:00 AM - False
11/11/2006 12:00:00 AM - False
At least it looks like Microsoft is aware of the issue. 至少看起来Microsoft知道了此问题。 https://support.microsoft.com/en-us/kb/933509 https://support.microsoft.com/zh-CN/kb/933509
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.