简体   繁体   English

在DataSet.GetXml()之后从UTC进行不正确的日期时间转换

[英]Incorrect datetime conversion from UTC after DataSet.GetXml()

My app pulls an object via a web service call, puts it in a typed dataset and sends the DataSet.GetXml() to a stored procedure for insert/update on the database. 我的应用程序通过Web服务调用拉出对象,将其放入类型化的数据集中,然后将DataSet.GetXml()发送到存储过程以在数据库上进行插入/更新。

The problem I'm facing is with two properties of the object : StartTime / EndTime . 我面临的问题是对象的两个属性: StartTime / EndTime The web service sends these in UTC format. Web服务将它们以UTC格式发送。 Eg. 例如。 sample StartTime -> "2012-11-06T05:00:00Z" The DataSet.GetXml() attempts to convert this UTC value into local time, and my app server is in EST. 示例StartTime >“ 2012-11-06T05:00:00Z” DataSet.GetXml()尝试将此UTC值转换为本地时间,并且我的应用服务器处于EST中。 The resultant value should be reported as "2012-11-06T00:00:00-05:00" but instead it is "2012-11-06T05:00:00-05:00". 结果值应报告为“ 2012-11-06T00:00:00-05:00”,但应为“ 2012-11-06T05:00:00-05:00”。 The offset value is being added but the time component is not changed. 正在添加偏移值,但时间分量未更改。

Is there something incorrect with my understanding here? 根据我的理解,这里有不正确的地方吗? I'm finding it hard to digest that the GetXml() method could have such a bug, and I haven't found anyone else here complain of a similar problem yet. 我发现很难理解GetXml()方法可能有这样的错误,而且我还没有发现这里的其他人抱怨过类似的问题。

Here's a stripped down version of the code: 这是简化的代码版本:

public void SaveOrder(int intOrderID)
{
        OrderDataSet objOrderDS = null;
        OrderDataSet.OrdersRow objOrderRow = null;
        ExternHandler handler = null;
        Order objOrder;

        Order objOrder = handler.GetOrder(intOrderID);

        objOrderRow = objOrderDS.Orders.NewOrdersRow();
        objOrderRow.OrderID = objOrder.OrderID;
        objOrderRow.StartTime = objOrder.StartTime;
        objOrderRow.EndTime = objOrder.EndTime;
        objOrderDS.Orders.AddOrdersRow(objOrderRow);

        if (objOrderDS.Orders.Rows.Count > 0)
        {
            objOrderDS.Namespace = string.Empty;
            objMappingObjects.Add(new MappingObject("Table", "Orders"));
            objSqlParams.Add(new SqlParameter("@pOrdersXml", objOrderDS.GetXml()));
            objOrderDS.Clear();
            objOrderDS.Merge(SqlHelper.ExecuteDataset(ConfigConnectionDB.Trim(), CommandType.StoredProcedure, "usp_InsertOrderMetaData", objMappingObjects.ToArray(), objSqlParams.ToArray()));
        }
}

OK, I found why the UTC-time was being converted to "incorrect local time". 好的,我发现了为什么将UTC时间转换为“不正确的本地时间”。 The fields 'Startime'/'EndTime' in the typed dataset are of type 'DateTime'. 类型化数据集中的“ Startime” /“ EndTime”字段的类型为“ DateTime”。 There is a property associated with DataColumn of DateTime type called 'DateTimeMode' which is set to 'UnspecifiedLocal' by default. 有一个与DateTime类型的DataColumn相关联的属性,称为“ DateTimeMode”,默认情况下设置为“ UnspecifiedLocal”。 http://msdn.microsoft.com/en-us/library/system.data.datasetdatetime.aspx http://msdn.microsoft.com/en-us/library/system.data.datasetdatetime.aspx

Basically, this option adds the offset to the datetime value without converting it. 基本上,此选项将偏移量添加到datetime值,而不进行转换。 As a test, I changed the 'DateTimeMode' to 'Utc' and retried. 作为测试,我将“ DateTimeMode”更改为“ Utc”并重试。 The XML received from DataSet.GetXml() was able to convert the value correctly: "2012-11-06T05:00:00Z" 从DataSet.GetXml()接收到的XML能够正确转换值:“ 2012-11-06T05:00:00Z”

As a solution to my problem, I'm keeping the property to 'UnspecifiedLocal' as it is. 为了解决我的问题,我将属性保留为“ UnspecifiedLocal”。 Instead, while adding the value to the dataset I convert it to local time. 相反,在将值添加到数据集时,我将其转换为本地时间。

    objOrderRow.EndTime = objOrder.EndTime.ToLocalTime();

(Note that this works for me because my app server and database server are in the same timezone and conversion to UTC is uncomplicated.) (请注意,这对我有用,因为我的应用程序服务器和数据库服务器位于同一时区,并且转换为UTC并不复杂。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM