Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm updating an application that reads from an XML file using System.Xml.XmlReader and needs to read a value that's an Xml date-time and generate a string representation of that as local time, without time zone info.
The previous code just pulled the attribute value string apart at fixed positions and then re-constructed a string with string.Format(). I thought this was a bit fragile (and very ugly).
I changed it to:
C#
DateTime runDate = reader.ReadElementContentAsDateTime();
string output = runDate.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

On my system (US PDT), this seemed to work perfectly:
The xml date time string: 2015-02-24T10:21:42.3414491-08:00
Gave back: 02/24/2015 10:21:42
However: This fails the unit tests when they are run on the integration build server, because that server is in a different time zone.

So, is there a way to parse a string into a local DateTime ignoring the time zone, preferably without string manipulation.
I know that I could use a Regex but it just seems wrong not to use DateTime as the intermediate form for data which is a date time.
---
This is quite frustrating, because the ReadElementContentAsDateTime solution does work in the normal case of converting the xml immediately after it is generated, on the same system. It's just the build server unit tests that fail.

What I have tried:

I tried ReadElementContentAsString and then using XmlConvert.ToDateTime with the various XmlDateTimeSerializationMode values.
No luck.
There just doesn't seem to be a way to parse ignoring the timezone info.
Posted
Updated 29-Jun-16 12:20pm

1 solution

Not the most beautiful code, but try changing the thread current culture temporarily and restoring afterwards, something like this (Check the timezone by the way, the code uses US-us as an example)

C#
CultureInfo currentCultureInfo = Thread.CurrentThread.CurrentCulture;
try 
{
   Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
   DateTime runDate = reader.ReadElementContentAsDateTime();
   string output = runDate.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
} finally 
{
   Thread.CurrentThread.CurrentCulture = currentCultureInfo;
}


Good luck!

ps. the use of CultureInfo.InvariantCulture in the ToString method is only used for formatting, not for converting or anything.
 
Share this answer
 
v2
Comments
Matt T Heffron 29-Jun-16 18:28pm    
I don't think that does what you think it does.
Both of the systems I'm dealing with already have the "en-US" CultureInfo.
The time zone would be a property of the system clock.
I cannot change the system clock on a per-thread basis!
E.F. Nijboer 1-Jul-16 13:08pm    
Best solution for controlling the date/clock is to use a DateTimeProvider instead of DateTime.Now directly. That way you would normally have it return the current date and time but for tests you have complete control over it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900