RCall.jl icon indicating copy to clipboard operation
RCall.jl copied to clipboard

Dealing with microseconds

Open swt30 opened this issue 4 years ago • 1 comments

R permits fractional seconds in POSIXct objects, seemingly to arbitrary accuracy. But DateTime only goes to millisecond accuracy.

julia> using RCall

julia> rcopy(R"as.POSIXct('2020-10-09 12:09:46.123')")
2020-10-09T11:09:46.123

julia> rcopy(R"as.POSIXct('2020-10-09 12:09:46.123456789')")
ERROR: InexactError: Int64(6.373792498612346e13)
Stacktrace:
 [1] Int64 at ./float.jl:710 [inlined]
 [2] convert at ./number.jl:7 [inlined]
 [3] Millisecond at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Dates/src/types.jl:33 [inlined]
 [4] rcopy at /Users/sthomas/.julia/packages/RCall/AEOQ7/src/convert/datetime.jl:9 [inlined]
 [5] rcopy(::Type{Dates.DateTime}, ::Ptr{RealSxp}) at /Users/sthomas/.julia/packages/RCall/AEOQ7/src/convert/datetime.jl:6
 [6] rcopy(::Ptr{RealSxp}; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{,Tuple{}}}) at /Users/sthomas/.julia/packages/RCall/AEOQ7/src/convert/default.jl:14
 [7] rcopy at /Users/sthomas/.julia/packages/RCall/AEOQ7/src/convert/default.jl:9 [inlined]
 [8] #rcopy#24 at /Users/sthomas/.julia/packages/RCall/AEOQ7/src/convert/default.jl:6 [inlined]
 [9] rcopy(::RObject{RealSxp}) at /Users/sthomas/.julia/packages/RCall/AEOQ7/src/convert/default.jl:6
 [10] top-level scope at /Users/sthomas/.julia/packages/RCall/AEOQ7/src/macros.jl:71

I work with timestamps from BigQuery that have microsecond accuracy, so I can't get the results from a query without hitting that error.

Is there a way to tell RCall to truncate to the accuracy of the DateTime type when converting? I don't actually need the microseconds, I just want the data to fit into a DateTime.

swt30 avatar Oct 09 '20 11:10 swt30

In the meantime, I can get this behaviour by pirating:

function RCall.rcopy(::Type{DateTime}, x::Float64)
    truncated_ms = Dates.Millisecond(floor(((isnan(x) ? 0 : x) + 62135683200) * 1000))
    DateTime(Dates.UTInstant(truncated_ms))
end

swt30 avatar Oct 09 '20 11:10 swt30