title | description | ms.date | helpviewer_keywords | ms.assetid | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dateTimeInvalidLocalFormat MDA |
Review the dateTimeInvalidLocalFormat managed debugging assistant (MDA), which is activated when a UTC-stored DateTime value gets a local-only DateTime format. |
03/30/2017 |
|
c4a942bb-2651-4b65-8718-809f892a0659 |
The dateTimeInvalidLocalFormat
MDA is activated when a xref:System.DateTime instance that is stored as a Universal Coordinated Time (UTC) is formatted using a format that is intended to be used only for local xref:System.DateTime instances. This MDA is not activated for unspecified or default xref:System.DateTime instances.
An application is manually serializing a UTC xref:System.DateTime instance using a local format:
DateTime myDateTime = DateTime.UtcNow;
Serialize(myDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffzzz"));
The 'z' format for the xref:System.DateTime.ToString%2A?displayProperty=nameWithType method includes the local time zone offset, for example, "+10:00" for Sydney time. As such, it will only produce a meaningful result if the value of the xref:System.DateTime is local. If the value is UTC time, xref:System.DateTime.ToString%2A?displayProperty=nameWithType includes the local time zone offset, but it does not display or adjust the time zone specifier.
UTC xref:System.DateTime instances should be formatted in a way that indicates that they are UTC. The recommended format for UTC times to use a 'Z' to denote UTC time:
DateTime myDateTime = DateTime.UtcNow;
Serialize(myDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffZ"));
There is also an "o" format that serializes a xref:System.DateTime making use of the xref:System.DateTime.Kind%2A property that serializes correctly regardless of whether the instance is local, UTC, or unspecified:
DateTime myDateTime = DateTime.UtcNow;
Serialize(myDateTime.ToString("o"));
This MDA does not affect the runtime.
There is no special output as a result of this MDA activating., However, the call stack can be used to determine the location of the xref:System.DateTime.ToString%2A call that activated the MDA.
<mdaConfig>
<assistants>
<dateTimeInvalidLocalFormat />
</assistants>
</mdaConfig>
Consider an application that is indirectly serializing a UTC xref:System.DateTime value by using the xref:System.Xml.XmlConvert or xref:System.Data.DataSet class, in the following manner.
DateTime myDateTime = DateTime.UtcNow;
String serialized = XMLConvert.ToString(myDateTime);
The xref:System.Xml.XmlConvert and xref:System.Data.DataSet serializations use local formats for serialization by default. Additional options are required to serialize other kinds of xref:System.DateTime values, such as UTC.
For this specific example, pass in XmlDateTimeSerializationMode.RoundtripKind
to the ToString
call on XmlConvert
. This serializes the data as a UTC time.
If using a xref:System.Data.DataSet, set the xref:System.Data.DataColumn.DateTimeMode%2A property on the xref:System.Data.DataColumn object to xref:System.Data.DataSetDateTime.Utc.
DateTime myDateTime = DateTime.UtcNow;
String serialized = XmlConvert.ToString(myDateTime,
XmlDateTimeSerializationMode.RoundtripKind);
- xref:System.Globalization.DateTimeFormatInfo
- Diagnosing Errors with Managed Debugging Assistants