Search code examples
javadatetimeneo4jcypherneo4j-apoc

Neo4j getting the system's datetime containing micro or nano seconds with cypher


In neo4j I know how to get the current datetime as in now() with milliseconds precision, however I would like to be able to store microseconds or nanoseconds. Is there a way to achieve this via apoc, or adjusting my system time. I was reading about the 'limitations' of java Date and Calendar only storing .SSS ?

CREATE (l:LogEvent{
        eventID : 'I01'
        , dt : System.nanoTime({timezone:'UTC'})
        });

Solution

  • The temporal instants DateTime, LocalDateTime, Time, and LocalTime support the microsecond and nanosecond fields. Those fields can be set when using the appropriate temporal function to create an instant.

    For example:

    RETURN datetime({year:1984, month:11, day:11, hour:12, minute:31, second:14, nanosecond: 645876123, timezone:'Europe/Stockholm'})
    

    In addition, if you have an epoch time with microsecond (or nanosecond) precision that you want to store, just break it down into 2 parts: its epoch seconds, and the remaining microseconds (or nanoseconds). With those 2 parts, you can create a DateTime as illustrated here (assuming secs, micros, and nanos are passed as parameters):

    RETURN datetime({epochSeconds: $secs, microsecond: $micros})
    

    or

    RETURN datetime({epochSeconds: $secs, nanosecond: $nanos})