2
votes

I'm serializing a DateTime value, and it works OK, creates an ISODateTime value. But it is always serializing using UTC.

YYY-MM-DDThh:mm:ss+hh:mm:ss

When I create the DateTime instance, I force LocalTime:

 DateTime localDateTime = new DateTime(DateTime.Now.Ticks,DateTimeKind.Local);

But, for some reason it always serializes to UTC (I always get '+01:00' at the end). It is supposed to automatically read get DataTimeKind and serialize properly, but no... :(

How do I configure the serializer or the attribute to force the DateTime to be serialized in Local Time?

EDIT: This is a test class

[Serializable]
public class DateTimeTest
{
    [System.Xml.Serialization.XmlElement("DateTime")]
    public DateTime dateTime;
}

This is serialization code

    public static string XMLSerializeToString<ObjectType>(ObjectType objetToSerialize, string defaultNamespace)
    {
        TextWriter stringWriter = new StringWriter();
        XmlSerializer serializer =
            defaultNamespace==null ? new XmlSerializer(typeof(ObjectType)) : new XmlSerializer(typeof(ObjectType),defaultNamespace);
        serializer.Serialize(xmlWriter, objetToSerialize);
        return stringWriter.ToString();
    }

This is callin' the serializer

    DateTime localDateTime = new DateTime(DateTime.Now.Ticks, DateTimeKind.Local);
    DateTimeTest dateTimeTest = new DateTimeTest();
    dateTimeTest.dateTime = localDateTime;
    string ser = XMLProcessor.XMLSerializeToString<DateTimeTest>(dateTimeTest, null);

This is the resulting string

<?xml version="1.0" encoding="utf-16"?>
<DateTimeTest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <DateTime>2013-10-25T17:01:35.7228+01:00</DateTime>
</DateTimeTest>

I dont want to get 17:01:35+01:00. I want to get 17:01:35. How can I get it?

Thanks :)

1
You haven't said what serializer you're using, but you might try setting its Kind to DateTimeKind.Unspecified if you don't want to see the timezone offset. - Joe
what do you mean by serialization? call ToString method? - Grundy
+01:00 means the value is not UTC, else it'd be Z. It's the offset of your local time relative to UTC. So I don't get your problem. You also don't describe how you serialize it. - CodesInChaos
btw. DateTime.Now is already returning a local DateTime value, so your weird way of constructing the date doesn't have any effect for most dates, but causes bugs when the local time is ambiguous due to DST switching. - CodesInChaos
For the record, you can change the DateTime kind without resorting to using Ticks. You definitely shouldn't need to change the kind to local when using DateTime.Now (it's already local, as CodesInChaos mentioned). To change what kind of date a DateTime is use the DateTime.SpecifyKind method. - Shaamaan

1 Answers

2
votes

As I said in a comment:

You haven't said what serializer you're using, but you might try setting its Kind to DateTimeKind.Unspecified if you don't want to see the timezone offset.

Your code to set the DateTime:

DateTime localDateTime = new DateTime(DateTime.Now.Ticks, DateTimeKind.Local);

is unnecessarily unwieldly, being exactly equivalent to:

DateTime localDateTime = DateTime.Now;

If you want a date time that will serialize without an offset, it needs to have DateTimeKind.Unspecified:

DateTime localDateTime = DateTime.Now.SpecifyKind(DateTimeKind.Unspecified);