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

allow conversion (with loss of precision) for arbitrary precision POSIXct objects #533

Merged
merged 7 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "RCall"
uuid = "6f49c342-dc21-5d91-9882-a32aef131414"
authors = ["Douglas Bates <dmbates@gmail.com>", "Randy Lai <randy.cs.lai@gmail.com>", "Simon Byrne <simonbyrne@gmail.com>"]
version = "0.14.2"
version = "0.14.3"

[deps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Expand All @@ -23,6 +23,7 @@ CategoricalArrays = "0.8, 0.9, 0.10"
Conda = "1.4"
DataFrames = "0.21, 0.22, 1.0"
DataStructures = "0.5, 0.6, 0.7, 0.8, 0.9, 0.10, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18"
Logging = "0, 1"
Preferences = "1"
Requires = "0.5.2, 1"
StatsModels = "0.6, 0.7"
Expand All @@ -33,10 +34,11 @@ julia = "1.6"
AxisArrays = "39de3d68-74b9-583c-8d2d-e117c070f3a9"
CondaPkg = "992eb4ea-22a4-4c89-a5bb-47a3300528ab"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Dates", "AxisArrays", "REPL", "Test", "Random", "CondaPkg", "Pkg"]
test = ["Dates", "AxisArrays", "REPL", "Test", "Random", "CondaPkg", "Pkg", "Logging"]
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
AxisArrays = "39de3d68-74b9-583c-8d2d-e117c070f3a9"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
RCall = "6f49c342-dc21-5d91-9882-a32aef131414"

[compat]
Documenter = "0.27"
10 changes: 10 additions & 0 deletions docs/src/conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ r = robject(d)
rcopy(r)
```

Note that R's `POSIXct` supports higher precision than DateTime:

```@example 1
r = reval("as.POSIXct('2020-10-09 12:09:46.1234')")
```

```@example 1
d = rcopy(r)
```

## DataFrames

```@example 1
Expand Down
10 changes: 8 additions & 2 deletions src/convert/datetime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ rcopy(::Type{Date}, s::Ptr{RealSxp}) = rcopy(Date, s[1])
rcopy(::Type{DateTime}, s::Ptr{RealSxp}) = rcopy(DateTime, s[1])

rcopy(::Type{Date}, x::Float64) = Date(Dates.UTInstant(Dates.Day((isnan(x) ? 0 : x) + 719163)))
rcopy(::Type{DateTime}, x::Float64) =
DateTime(Dates.UTInstant(Dates.Millisecond(((isnan(x) ? 0 : x) + 62135683200) * 1000)))
function rcopy(::Type{DateTime}, x::Float64)
ms = ((isnan(x) ? 0 : x) + 62135683200) * 1_000
if !isinteger(ms)
@debug "Precision lost in conversion to DateTime"
end
ms = round(Int, ms)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're rounding anyway, you could skip the trunc that isinteger does and just check for equality of values pre- and post-round. Doesn't really matter though.

return DateTime(Dates.UTInstant(Millisecond(ms)))
end

# implicit conversion `rcopy(d)`.
function rcopytype(::Type{RClass{:Date}}, s::Ptr{RealSxp})
Expand Down
5 changes: 5 additions & 0 deletions test/convert/datetime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,8 @@ r = RObject(d)
@test ismissing(rcopy(r)[2])
@test rcopy(Array, r)[[1,3]] == d[[1,3]]
@test ismissing(rcopy(Array, r)[2])

# microseconds on R side #396
@test_logs((:debug, "Precision lost in conversion to DateTime"),
min_level=Logging.Debug,
rcopy(R"as.POSIXct('2020-10-09 12:09:46.1234')"))
palday marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RCall
using Logging
using Test

using DataStructures: OrderedDict
Expand Down
Loading