Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dealing with microseconds #396

Closed
swt30 opened this issue Oct 9, 2020 · 2 comments · Fixed by #533
Closed

Dealing with microseconds #396

swt30 opened this issue Oct 9, 2020 · 2 comments · Fixed by #533

Comments

@swt30
Copy link

swt30 commented Oct 9, 2020

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
Copy link
Author

swt30 commented Oct 9, 2020

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
Copy link
Author

swt30 commented Jul 18, 2024

Nice one @palday , thank you for making the fix that I never got around to :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant