如何照顾DST,同时比较C#中的时间值 [英] How to take care of DST while comparing time values in C#

查看:130
本文介绍了如何照顾DST,同时比较C#中的时间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个时间( String )进行比较。这些时间值是格式,因为可以从下面的代码中理解:



第一个时间值: / p>

  String fileTime = new FileInfo(fileName).LastWriteTime.ToUniversalTime()。ToString(MM / dd / yyyy hh:mm tt ); 

第二个时间值: 从数据库中可用的印度计时列表)

  DateTime date = DateTime.ParseExact(eachBinary.Date,MM / dd / yyyy hh:mm tt,CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None); 
date = TimeZoneInfo.ConvertTime(date,TimeZoneInfo.FindSystemTimeZoneById(India Standard Time),TimeZoneInfo.Utc);
eachBinary.Date = date.ToString(MM / dd / yyyy hh:mm tt);

如上所示,我想比较系统文件计时与DB记录中的计时。
这里,我看到一个潜在的问题,不等的时间,即使在转换为UTC,然后比较字符串之后,不等时间。



我知道这里比较的时间一个文件(例如 file1 )相等。但是程序(或)应用程序返回的是不相等的。



在我的代码中,DST在这里有问题吗?



第一个时间值 = 02/23/2012 09:08 AM(从太平洋地区转换,转换前是02/23 / 2012 12:08 AM)



第二时间值 = 02/23/2012 08:08 AM(转换前为02/23/2012 01:38 PM)

解决方案

我认为第一次转换( fileTime 是返回错误值的那个。我不是在PST,我的文化使用24小时钟,所以我不得不模拟你的第一个代码示例与以下代码:

  // 02/23/2012 12:08 AM(而不是读取LastWriteTime)
DateTime original = new DateTime(2012,02,23,00,08,00);

//而不是ToUniversalTime(),它将使用我的本地时区
TimeZoneInfo pstzone = TimeZoneInfo.FindSystemTimeZoneById(Pacific Standard Time);
DateTime utc = TimeZoneInfo.ConvertTimeToUtc(original,pstzone);

//而不是一个纯ToString(),它将使用我的本地文化
CultureInfo us = CultureInfo.CreateSpecificCulture(en-US);
String display = utc.ToString(MM / dd / yyyy hh:mm tt,us);

// output:display = 02/23/2012 08:08 AM

所以,它在我的机器上工作。我建议你试试这个在你的机器上。如果它工作,逐渐更换部分(新的DateTime - > LastWriteTime; ConvertTimeToUtc - > ToUniversalTime;等),直到你到达原来的单行例子。请注意哪个步骤导致转换失败 - 现在您知道哪个部分负责返回错误的结果。


I've two timings (as String) to be compared. These timing values are of Format as it could be understood from the code below:

1st Timing value:

String fileTime = new FileInfo(fileName).LastWriteTime.ToUniversalTime().ToString("MM/dd/yyyy hh:mm tt");

2nd Timing value: (I take from list of Indian timings available in Database)

DateTime date = DateTime.ParseExact(eachBinary.Date, "MM/dd/yyyy hh:mm tt", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
date = TimeZoneInfo.ConvertTime(date, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"), TimeZoneInfo.Utc); 
eachBinary.Date = date.ToString("MM/dd/yyyy hh:mm tt");

As you can see above, I want to compare the system file timings with those in DB records. Here, I see a potential problem of unequal timings even after conversion to UTC and then comparison of the Strings.

I know that the timings getting compared here for a file (say file1) are equal. But the program (or) application returns as they're unequal.

Is DST a problem here in my code? If so, Can you pl help in taking care of DST in comparisons.

EDIT1:

1st Timing value = 02/23/2012 09:08AM (it got converted from Pacific Zone, before conversion it was 02/23/2012 12:08 AM)

2nd Timing value = 02/23/2012 08:08AM (before conversion it was 02/23/2012 01:38PM)

解决方案

I think the first conversion (fileTime) is the one returning the wrong value. I'm not in PST and my culture uses 24 hour clocks, so I had to simulate your first code example with the following code:

// 02/23/2012 12:08 AM (instead of reading LastWriteTime)
DateTime original = new DateTime(2012, 02, 23, 00, 08, 00);

// instead of ToUniversalTime(), which would use my local time zone
TimeZoneInfo pstzone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
DateTime utc = TimeZoneInfo.ConvertTimeToUtc(original, pstzone); 

// instead of a plain ToString(), which would use my local culture
CultureInfo us = CultureInfo.CreateSpecificCulture("en-US");
String display = utc.ToString("MM/dd/yyyy hh:mm tt", us);

// output: display = 02/23/2012 08:08 AM

So, it "works on my machine". I suggest that you try this on your machine. If it works as well, gradually replace parts (new DateTime -> LastWriteTime; ConvertTimeToUtc -> ToUniversalTime; etc.), until you arrive at your original one-line example. Note which step caused the conversion to fail -- now you know which part is responsible for returning the wrong result.

这篇关于如何照顾DST,同时比较C#中的时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆