var Int53 = require('int53')
var julian = require('julian')
console.log(Int53)
// implements parquet timestamp, as per
// which 8 bytes of little endian nano seconds since start of julian day
// then 4 bytes of little endian julian day. https://en.wikipedia.org/wiki/Julian_day
// https://github.com/prestodb/presto/blob/c73359fe2173e01140b7d5f102b286e81c1ae4a8/presto-hive/src/main/java/com/facebook/presto/hive/parquet/ParquetTimestampUtils.java#L52-L54

exports.encode = function (timestamp) {
  if(isNaN(+timestamp)) throw new Error('expected timestamp, got NaN')
  var buffer = new Buffer(12)
  var ms_day = timestamp % 86400000
  var ns_day = ms_day * 1000000
  console.log('nano', ns_day)
  Int53.writeUInt64LE(ns_day, buffer, 0)

  buffer.writeInt32LE(+julian(timestamp - ms_day), 8)
  return buffer
}
exports.decode = function (buffer) {
  var julian_day = buffer.readInt32LE(8) //or should it be signed?
  var nano = Int53.readUInt64LE(buffer, 0)
  //convert to ms since midnight
    console.log('j d ', julian_day)
  console.log('juli', +julian.toDate(julian_day))
  console.log('nano     ', nano/1000000)
  var ms = nano / 1000000
  return ms + (+julian.toDate(julian_day))
}




@@ -57,26 +57,8 @@
 n')%0A
-console.log(Int53)
 %0A// 
@@ -436,738 +436,802 @@
 54%0A%0A
-exports.encode = function (timestamp) %7B%0A  if(isNaN(+timestamp)) throw new Error('expected timestamp, got NaN')%0A  var buffer = new Buffer(12)%0A  var ms_day = timestamp %25 86400000%0A  var ns_day = ms_day * 1000000%0A  console.log('nano', ns_day)%0A  Int53.writeUInt64LE(ns_day, buffer, 0)%0A%0A  buffer.writeInt32LE(+julian(timestamp - ms_day), 8)%0A  return buffer%0A%7D%0Aexports.decode = function (buffer) %7B%0A  var julian_day = buffer.readInt32LE(8) //or should it be signed?%0A  var nano = Int53.readUInt64LE(buffer, 0)%0A  //convert to ms since midnight%0A    console.log('j d ', julian_day)%0A  console.log('juli', +julian.toDate(julian_day))%0A  console.log('nano     ', nano/1000000)%0A  var ms = nano / 1000000%0A  return ms + (+julian.toDate(julian_day
+var M = 1000000 //multiply milli by 1 million to get nano%0A%0Aexports.encode = function (timestamp, buffer, offset) %7B%0A  if(!buffer) %7B%0A    buffer = new Buffer(12)%0A    offset = 0%0A  %7D%0A  if(isNaN(+timestamp)) throw new Error('expected timestamp, got NaN')%0A  var buffer = new Buffer(12)%0A  Int53.writeUInt64LE(%0A    julian.toMillisecondsInJulianDay(timestamp)*M,%0A    buffer, offset%0A  )%0A  buffer.writeInt32LE(+julian.toJulianDay(timestamp), offset+8)%0A  return buffer%0A%7D%0Aexports.decode = function (buffer, offset) %7B%0A  offset = offset %7C%7C 0%0A  return julian.fromJulianDayAndMilliseconds(%0A    buffer.readInt32LE(offset+8),%0A    Int53.readUInt64LE(buffer, offset)/M%0A  )%0A%7D%0A%0Aif(!module.parent) %7B%0A  var n = Date.now()%0A  console.log(n)%0A  console.log(exports.encode(n))%0A  console.log(exports.decode(exports.encode(n)
 ))%0A%7D%0A%0A
-%0A%0A

convert timestamps