Author Topic: DataNode DateTime  (Read 2497 times)

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
DataNode DateTime
« 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.  

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: DataNode DateTime
« Reply #1 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.

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: DataNode DateTime
« Reply #2 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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: DataNode DateTime
« Reply #3 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.
« Last Edit: July 20, 2016, 04:06:45 AM by ArenMook »

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: DataNode DateTime
« Reply #4 on: July 20, 2016, 06:25:26 AM »
Felt awkward having to convert it back and forth and not work like this natively...  thanks!