Tasharen Entertainment Forum

Support => TNet 3 Support => Topic started by: devomage on July 19, 2016, 12:49:49 AM

Title: DataNode DateTime
Post by: devomage on July 19, 2016, 12:49:49 AM
I have a typical character being saved as datanode.  How to store DateTime correctly?

  1. player.SetChild("created", character.created);//created is DateTime
  2. player.SetChild("deletedate", character.deletedate);//deletedate is DateTime
  3.  


  1. characters
  2.         Focus
  3.                 active = true
  4.                 avatar = "Ethan"
  5.                 charid = 70
  6.                 created = System.DateTime
  7.                 deletedate = System.DateTime
  8.                 experience = 0
  9.                 lastactive = System.DateTime
  10.                 level = 1
  11.  
  12.  
Title: Re: DataNode DateTime
Post by: cmifwdll on July 19, 2016, 06:59:02 AM
Are you sure it's not being parsed properly? Strange that you'd see System.DateTime if it weren't being parsed. Maybe it's sending the typename as a string.

Anyway, you could store / send it as a long instead: DateTime.UtcNow.Ticks. To reconstruct, use DateTime dt = new DateTime(value, DateTimeKind.Utc).
You could add this to TNSerializer.cs to automate the process. Might also need to add it to the DataNode class, not sure.
Title: Re: DataNode DateTime
Post by: devomage on July 19, 2016, 06:08:05 PM
In the past I forced my enum's to int and datetime's to long's...   this time around I'd really like to avoid this.

In my example shown - "Ethan" is an enum (avatar)...  so, the DateTime seemed odd.
Title: Re: DataNode DateTime
Post by: ArenMook on July 20, 2016, 03:49:23 AM
DateTime class is not serializable via reflection as there are no public fields, and the properties are not serialized as the class isn't marked with [SerializeProperties]. You can add support for it yourself in TNet.Serializer if you like, but the easiest thing to do is what cmifwdll suggested.

Edit: I've gone ahead and added support for it in TNSerializer. It will automatically get converted to a long and saved as such.
Title: Re: DataNode DateTime
Post by: devomage on July 20, 2016, 06:25:26 AM
Felt awkward having to convert it back and forth and not work like this natively...  thanks!