Skip to content

Commit

Permalink
fix test, add note
Browse files Browse the repository at this point in the history
  • Loading branch information
palday committed Jul 18, 2024
1 parent ca5bf98 commit ad6314f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/src/conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ r = reval("as.POSIXct('2020-10-09 12:09:46.1234')")
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
7 changes: 4 additions & 3 deletions src/convert/datetime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ 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)))
function rcopy(::Type{DateTime}, x::Float64)
ms = ((isnan(x) ? 0 : x) + 62135683200) * 1_000
if !isinteger(ms)
ms_int = round(Int, ms)
if ms != ms_int
@debug "Precision lost in conversion to DateTime"
end
ms = round(Int, ms)
return DateTime(Dates.UTInstant(Millisecond(ms)))

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

# implicit conversion `rcopy(d)`.
Expand Down
6 changes: 5 additions & 1 deletion test/convert/datetime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,12 @@ r = RObject(d)
@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')") == DateTime("2020-10-09T12:09:46.123")
@test rcopy(R"as.POSIXct('2020-10-09 12:09:46.1234', tz='UTC')") == DateTime("2020-10-09T12:09:46.123")

0 comments on commit ad6314f

Please sign in to comment.