DataView返回带有UTC日期的不正确结果. [英] DataView returning incorrect results with UTC dates.

查看:69
本文介绍了DataView返回带有UTC日期的不正确结果.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用DataView基于DateTime列中的值来过滤DataTable中的数据,并且DataView返回的结果不正确.我怀疑问题在于所有日期和时间都已表达 在UTC.下面的代码说明了该问题:

I am attempting to use a DataView to filter the data in a DataTable based on the values in a DateTime column and the DataView is returning incorrect results.  I suspect that the issue is down to the fact that all of the dates and times are expressed in UTC.  The code below illustrates the problem:

DateTime utcNow =新的DateTime(2014、7、8、11、0、0,DateTimeKind.Utc);
字符串rowFilter = string.Format("TestTime<#{0}#",utcNow.ToString("o")));

DateTime utcNow = new DateTime(2014, 7, 8, 11, 0, 0, DateTimeKind.Utc);
string rowFilter = string.Format("TestTime < #{0}#", utcNow.ToString("o"));

DataTable dt =新的DataTable();
dt.Columns.Add("TestTime",typeof(DateTime));
dt.Columns [0] .DateTimeMode = DataSetDateTime.Utc;
dt.Rows.Add(utcNow);

DataTable dt = new DataTable();
dt.Columns.Add("TestTime", typeof(DateTime));
dt.Columns[0].DateTimeMode = DataSetDateTime.Utc;
dt.Rows.Add(utcNow);

DataView dv =新的DataView(dt,rowFilter,null,DataViewRowState.CurrentRows);
Debug.WriteLine("{0}行,TestTime< {1}",dv.Count,utcNow);
如果(dv.Count> 0)
    Debug.WriteLine("TestTime = {0}",dv [0] ["TestTime"]);

DataView dv = new DataView(dt, rowFilter, null, DataViewRowState.CurrentRows);
Debug.WriteLine("{0} rows with TestTime < {1}", dv.Count, utcNow);
if (dv.Count > 0)
    Debug.WriteLine("TestTime = {0}", dv[0]["TestTime"]);

如您所见,该代码向DataTable添加了一个时间为11AM的单个记录,然后DataView过滤掉时间小于11AM的记录,但是它返回了记录!我怀疑代码中的某个地方 实际上将其中一种时间视为本地时间,这就是导致此问题的原因.

As you can see, the code adds a single record with a time of 11AM to the DataTable and the DataView then filters for records with times that are less than 11AM and yet it returns the record!  I suspect that somewhere along the line the code is actually treating one of the times as local time and this is what is causing the issue.

顺便说一句,我的本地计算机设置为英国夏令时(比UTC提前1小时).

By the way, my local machine is set to British Summer Time (1 hour ahead of UTC).

最诚挚的问候,

Tony

推荐答案

托尼,

我完全没有看到这种行为.当我尝试您的代码时,DataView应该有0条记录.我在VS 2010和2013中都尝试过.我的时间是太平洋夏令时间,比世界协调时间晚7小时(不知道这是否重要).

I'm not seeing that behavior at all. When I try your code, the DataView has 0 records, as it should. I tried it in both VS 2010 and 2013. I'm in Pacific Daylight Time, 7 hours behind UTC (don't know if this matters or not).

我想这个答复根本对您没有帮助,因为我真的不知道我们得到不同结果的原因,而且我什至对为什么会这样都没有很好的猜测.

I suppose this reply doesn't help you at all, because I don't really know the reason why we get different results and I don't even have any good guesses as to why that may be.


这篇关于DataView返回带有UTC日期的不正确结果.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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