ScottPJones / LibBSON.jl

libbson bindings for the Julia programming language
Other
9 stars 16 forks source link

MongoDB uses milliseconds for DateTime while Julia uses seconds #16

Closed turtleslow closed 8 years ago

turtleslow commented 8 years ago

Peter, great work on the LIbBSON / Mongo drivers! DateTimes didn't seem to make it into the last release, so I looked at adding the functionality and noticed that you basically have it implemented in your master branch. However, there seems to be a small inconsistency at the moment since MongoDB uses milliseconds and Julia uses seconds.

For reference see: https://docs.mongodb.org/manual/reference/bson-types/#date http://docs.julialang.org/en/release-0.4/stdlib/dates/#Dates.unix2datetime

I believe the simple fixes below should do the trick:

In LibBSON.jl/src/BSONIter.jl:

        elseif ty == BSON_TYPE_DATE_TIME
        ts = ccall(
            (:bson_iter_date_time, libbson),
            Int64, (Ptr{UInt8}, ),
            bsonIter._wrap_
            ) / 1000  # millisecond to second conversion
        return Dates.unix2datetime(ts)

In LibBSON.jl/src/BSONObject.jl:

function append(bsonObject::BSONObject, key::AbstractString, val::Union{Date,DateTime})
    keyCStr = bytestring(key)
    ts = typeof(val) == Date ? datetime2unix(DateTime(val)) : datetime2unix(val)
    ccall(
        (:bson_append_date_time, libbson),
        Bool, (Ptr{Void}, Ptr{UInt8}, Cint, Clong),
        bsonObject._wrap_,
        keyCStr,
        length(keyCStr),
        ts * 1000   # second to millisecond conversion
        ) || error("libBSON: overflow")

In LibBSON.jl/src/BSONArray.jl:

function append(bsonArray::BSONArray, val::Union{Date,DateTime})
    keyCStr = bytestring(string(length(bsonArray)))
    ts = typeof(val) == Date ? datetime2unix(DateTime(val)) : datetime2unix(val)
    ccall(
        (:bson_append_date_time, libbson),
        Bool, (Ptr{Void}, Ptr{UInt8}, Cint, Clong),
        bsonArray._wrap_,
        keyCStr,
        length(keyCStr),
        ts * 1000   # second to millisecond conversion
        ) || error("libBSON: overflow")
end

Turtle

ghost commented 8 years ago

Hi there, sorry for the delay on this. I just merged a pull request that should take care of this problem. Happy holidays!