Dgraph4j datetime problem

In case anyone is looking at this issue, and still would like to use the NQuad message classes in Java, I have ported the golang time.Time.MarshalBinary() method into Java. Sharing the code below:

public class DGraphDateTimeConverter {
    // https://golang.org/src/time/time.go search for unixToInternal
    private static final long UNIX_TO_INTERNAL = 62135638488L;

    public static ByteString convertTimeStampBinary(Long epochMillis) {
        // based on golang's time.Time.MarshalBinary()
        // https://golang.org/src/time/time.go?s=35686:35731#L1158

        int offsetMin = -1; // UTC
        long sec = UNIX_TO_INTERNAL + epochMillis / 1000;
        long nsec = epochMillis % 1000 * 1000000;

        byte[] byteArray = new byte[]{
                (byte) 1, // byte 0 : version
                (byte)(sec >> 56),   // bytes 1-8: seconds
                (byte)(sec >> 48),
                (byte)(sec >> 40),
                (byte)(sec >> 32),
                (byte)(sec >> 24),
                (byte)(sec >> 16),
                (byte)(sec >> 8),
                (byte)(sec),
                (byte)(nsec >> 24), // bytes 9-12: nanoseconds
                (byte)(nsec >> 16),
                (byte)(nsec >> 8),
                (byte)(nsec),
                (byte)(offsetMin >> 8), // bytes 13-14: zone offset in minutes
                (byte)(offsetMin),
        };

        return ByteString.copyFrom(byteArray);
    }
}

Hope this helps. I would like to put this method into dgraph4j as utlity method.

1 Like