Search code examples
neo4jclient

Getting timestamp() value using C# client


Just a quick one, I have a node that gets a value set using the value of timestamp(), when I query for the node using

public List<Thing> ListThings()
    {
        return client.Cypher
                     .Match("(thing:Thing)")
                     .Return<Thing>("thing").Results.ToList();
    }

I have a class called Thing that looks like this:

public class Thing{
    public string name {get; set;}
    public int id {get;set;}
    public DateTimeOffset timestamp{get;set}
}

We create the 'thing' ahead of time by using:

Create(thing:Thing{id:6,name:'thing1', timestamp:timestamp()}) return thing

I get every value back apart from timestamp when calling from my script, which is a little annoying, any ideas? I get all the values back using the query in Neo4j browser, so I was wondering if I was actually doing something wrong?

Thanks


Solution

  • Tatham is right, the timestamp() returns a long that's not convertible into DateTimeOffset or DateTime. One of the problems is that the returned value is Milliseconds since 1/1/1970, so you need to calculate that, unfortunately, JSON.Net can't do that automagically.

    If you can change your Thing class you could have a property that isn't serialized but is calculated by the Timestamp property, something like this:

    public class Thing
    {
        private static DateTime _epoch = new DateTime(1970, 1, 1);
        public string id { get; set; }
        public string name { get; set; }
    
        private long _ts;
    
        public long timestamp
        {
            get { return _ts; }
            set
            {
                _ts = value;
                TimestampAsDateTime = _epoch.AddMilliseconds(_ts);
            }
        }
    
        [JsonIgnore]
        public DateTime TimestampAsDateTime { get; set; }
    }
    

    The TimestampAsDateTime property would only ever be available in your code.