我来自智利,时区当前设置为 GMT -3:00。我的平台配置了这些时区设置:
Oracle database: GMT -3:00
WebLogic server: GMT -3:00
Windows 7 client: GMT -3:00
执行此查询时我得到了奇怪的结果:
1st query: Date = 26/03/08 06:10:00,000000000
2nd query: Date = 24/05/13 18:48:00,000000000
我可以看到(通过 SOAP UI)WebLogic 返回了这个结果:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:fobpgGestacionesPorFichaResponse xmlns:ns2="http://nucleo.alemana.cl/">
<fobpgGestacionesPorFichaResult>
<gestaciones>
<controles>7</controles>
<esGestaTerminada>1</esGestaTerminada>
<fechaPrimerControl>2007-10-05T00:00:00-03:00</fechaPrimerControl>
<fechaTermino>2008-03-26T06:10:00-03:00</fechaTermino>
..............^ date #1 is here
<fur>2007-08-05T00:00:00-03:00</fur>
<furOp>2007-08-05T00:00:00-03:00</furOp>
<gesdaFechaCreacion>2015-08-19T14:58:41.903-03:00</gesdaFechaCreacion>
<gesnuIdGesta>45087</gesnuIdGesta>
<gesnuNroGesta>1</gesnuNroGesta>
<rnMayorPeso>0</rnMayorPeso>
</gestaciones>
<gestaciones>
<controles>12</controles>
<esGestaTerminada>1</esGestaTerminada>
<fechaPrimerControl>2012-10-11T00:00:00-03:00</fechaPrimerControl>
<fechaTermino>2013-05-24T18:48:00-03:00</fechaTermino>
..............^ date #2 is here
<fur>2012-08-27T00:00:00-03:00</fur>
<furOp>2012-08-27T00:00:00-03:00</furOp>
<gesdaFechaCreacion>2015-08-19T14:58:28.071-03:00</gesdaFechaCreacion>
<gesnuIdGesta>45086</gesnuIdGesta>
<gesnuNroGesta>2</gesnuNroGesta>
<rnMayorPeso>1980</rnMayorPeso>
</gestaciones>
</fobpgGestacionesPorFichaResult>
</ns2:fobpgGestacionesPorFichaResponse>
</S:Body>
</S:Envelope>
但是,当我的客户端 WPF 应用程序连接到 WebLogic 服务并检索上述数据时,我得到了这些值(在 Debug模式下):
第一次回复
这比我预期的时间 (6:10) 提前一小时 (7:10)。
第二次回复
这符合预期 (18:48)!
我不确定这里发生了什么。为什么这些结果不一致?
最佳答案
我以前也遇到过这个问题,这让我很困惑。 .NET 在根据机器的时区配置序列化/反序列化 XML 时自动更改 DateTime
值。 .NET 试图提供帮助,但这不是预期的行为,因此往往会造成很多困惑。
在任何应用程序中处理 DateTime
的最佳方法是存储 UTC 日期时间,然后在客户端以本地格式显示它们。
但是,不涉及将所有日期时间转换为 UTC 的更快修复方法是:
尝试更改您的 DateTime
以便未指定 DateTimeKind
,XML 序列化程序不会尝试转换它们。例如,此代码会将日期转换为 DateTimeKind.Unspecified
:
DateTime newDate = DateTime.SpecifyKind(oldDate, DateTimeKind.Unspecified);
您可以在 Coding Best Practices Using DateTime in the .NET Framework 上查看这份冗长的 Microsoft 指南获取更多信息。
关于c# - 日期时间从 WebLogic SOAP 服务更改为 WCF 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32104028/