Best practice for storing DateTime values in Firebase with Unity3d

Viewed 3984

I'm building a Unity3d 2D game where I'm using Firebase as the database. I need to store quite a lot of DateTime values (as UTC) in the database and I'm wondering what the best practice is for doing this.

The DateTime fields will mainly be used for sorting/filtering.

According to the Firebase documentation, I should make use of the Firebase.Database.ServerValues.TIMESTAMP which is just an object

Thing is, I'm using strongly typed models for my Unity game, like so:

[Serializable]
public class Profile {

    [NonSerialized]
    public string ProfileKey;

    // Serializable attributes
    public string userId;
    public string name;
    public int age;
    public string gender; // "M" or "F"
    public DateTime utcCreated; // UTC   
    public DateTime utcUpdated; // UTC
}   

and when I save/update a Profile I'm using Unitys JsonUtility.ToJson() method, like this:

var profiles = db.Child ("profiles");

Profile profile = new Profile {
    userId = GameManager.Instance.User.UserId,
    name = "My new profile",
    age = 23,
    gender = "M",
    utcCreated = ?? // Should somehow use Firebase.Database.ServerValues.TIMESTAMP here, I guess?
};              

string json = JsonUtility.ToJson (profile);     

var profileKey = profiles.Push ().Key;

profiles.Child (profileKey).SetRawJsonValueAsync (json, 1).ContinueWith (t => {
    // .. handle response here
});

I'm not sure what exactly I should set the utcCreated field to on my model. Tried to simply make the field an object, but no value was inserted into Firebase. Also tried to make it a string with a .ToString() to the Firebase.Database.ServerValues.TIMESTAMPobject, but no value was inserted in Firebase.

Does anyone have an idea how to do this? ;-) Or just any help/hint on best practices for storing dates and timestamps in Firebase?

Thanks in advance.

1 Answers
Related