Serialize/Deserializar Datetime

Asked

Viewed 531 times

1

I am saving in text file a serialized object that contains only fields of type Datetime, however, the date is saving in the wrong way.

I will demonstrate with an example what I am doing.

My Databaseviewmodel object has two Datetime-like fields

[Serializable]
public class DataBaseViewModel
{
    public DateTime ultimaAtualizacaoTfs { get; set; }
    public DateTime ultimaAtualizacaoSis { get; set; }
}

I add values to my object

var datasBase = new DatasBaseViewModel();
datasBase.ultimaAtualizacaoSis = new DateTime(2015, 11, 9, 15, 10, 10);
datasBase.ultimaAtualizacaoTfs = new DateTime(2015, 11, 9, 15, 11, 11);

So save the object serialized in text

using (Stream stream = File.Open(_pathToDatasBase, FileMode.Create))
using (StreamWriter streamWriter = new StreamWriter(stream))
{
    streamWriter.WriteLine(new JavaScriptSerializer().Serialize(dataBase));
}

Another time, I will read the contents of my text file.

var datasBase = new DataBaseViewModel();
using (Stream stream = File.Open(_pathToDatasBase, FileMode.Open))
using (StreamReader streamReader = new StreamReader(stream))
{
    datasBase = new JavaScriptSerializer().Deserialize<DataBaseViewModel>(streamReader.ReadToEnd());
}

After reading and deserializar, my object is with the incorrect date, different from what I saved.

In that case, I saved the dates 09/11/2015 15:10:10 and 09/11/2015 15:11:11 and in the deserialized object I have the dates 09/11/2015 17:10:10 and 09/11/2015 17:11:11.

The text file with the serialized object is saved this way:

{"ultimaAtualizacaoTfs":"\/Date(1447089071000)\/","ultimaAtualizacaoSis":"\/Date(1447089010000)\/"}
  • It is certainly a time zone problem. To know why you would need to see more context. I don’t know if I’m missing something, but if you save it one way and read it the same, it shouldn’t be a problem.

  • Must be using utc date... try to change new StreamReader(stream,Encoding.UTF8))

  • You have this answer here that I may be able to help you http://answall.com/questions/931/data-incorrect

  • Exactly @bigown. I’m saving UTC-2 (local) and deserializing UTC. I converted the date the moment I read it: datesBase.ultimaryAtualizacaoTfs.Tolocaltime()

  • There’s no conversion in your code. I think the problem is somewhere else and without posting something that we can see and test, it is difficult to help.

  • The code is just that, I think at the time of deserializar should assume that it is UTC, because at no time before saving I change the time zone.

  • Could be, why I think every date should be UTC.

Show 2 more comments

1 answer

3


If you change the type of the two properties of your class to DateTimeOffset you will be in debug that at the end of the value it will be with -2, when decenrialize the object and look at the same value in the new class the value will be 0, are exactly the two hours that are increasing.

Just change the way you create the date it will work normal.

            datasBase.ultimaAtualizacaoTfs = new DateTime(2015, 11, 9, 15, 10, 10,  DateTimeKind.Utc);
            datasBase.ultimaAtualizacaoSis = new DateTime(2015, 11, 9, 15, 11, 11, DateTimeKind.Utc

You will also notice that the serialized value is different now because it is setting the date to the correct value.

No need to change the value of properties to DateTimeOffset, only if you are curious to see the difference I spoke of. It works normally with DateTime

            var dataCerta = new DateTime();
            var dataErrada = new DateTime();

            dataCerta = new DateTime(2015, 11, 9, 15, 10, 10,  DateTimeKind.Utc);
            dataErrada = new DateTime(2015, 11, 9, 15, 10, 10);


            var dataCertaSerializada = new JavaScriptSerializer().Serialize(dataCerta);
            var dataErradaSerializada = new JavaScriptSerializer().Serialize(dataErrada);

            Console.Write(String.Format("Valor serializado da data certa:  {0} \n", dataCertaSerializada.ToString()));
            Console.Write(String.Format("Valor serializado da data errada: {0} \n", dataErradaSerializada.ToString()));

            var dataCertaDecerializada = new JavaScriptSerializer().Deserialize<DateTime>(dataCertaSerializada);
            var dataErradaDecerializada = new JavaScriptSerializer().Deserialize<DateTime>(dataErradaSerializada);

            Console.Write(String.Format("Valor Deserializado da data certa:  {0} \n", dataCertaDecerializada.ToString()));
            Console.Write(String.Format("Valor Deserializado da data errada: {0} \n", dataErradaDecerializada.ToString()));
  • Thanks @Ricardo. I did the conversion at the time of reading too, if you want to complement your answer datasBase.ultimaAtualizacaoTfs = datasBase.ultimaAtualizacaoTfs.ToLocalTime(); Both work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.