Skip to content

Commit

Permalink
allow conversion (with loss of precision) for arbitrary precision POS…
Browse files Browse the repository at this point in the history
…IXct objects (#533)

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

* version bump

* use debug instead of warn

* docs note

* fix test, add note
  • Loading branch information
palday authored Jul 18, 2024
1 parent 1ce81bc commit caa906f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
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"]
14 changes: 14 additions & 0 deletions docs/src/conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ 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)
```

!!! note "Conversions to `DateTime` are given in UTC!"
`POSIXct` stores times internally as UTC with a timezone attribute.
The conversion to `DateTime` necessarily strips away timezone information, resulting in UTC values.

## DataFrames

```@example 1
Expand Down
11 changes: 9 additions & 2 deletions src/convert/datetime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ 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
ms_int = round(Int, ms)
if ms != ms_int
@debug "Precision lost in conversion to DateTime"
end

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

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

# note that POSIXct stores times internaly as UTC, but assumes local
# timezone information
@test rcopy(R"as.POSIXct('2020-10-09 12:09:46', tz='UTC')") == DateTime("2020-10-09T12:09:46")

# 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')"))
@test rcopy(R"as.POSIXct('2020-10-09 12:09:46.1234', tz='UTC')") == DateTime("2020-10-09T12:09:46.123")
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

2 comments on commit caa906f

@palday
Copy link
Collaborator Author

@palday palday commented on caa906f Jul 18, 2024

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/111271

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.14.3 -m "<description of version>" caa906f5986f17c6ac881016a979ea37aa916453
git push origin v0.14.3

Please sign in to comment.