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: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.