Skip to content

Commit 136168e

Browse files
authored
apacheGH-44502: [R] Negative fractional dates must be converted to integers by floor, not trunc (apache#46873)
### Rationale for this change Sub-day precision Dates specified as negative fractional values converted to wrong value if not floored first ### What changes are included in this PR? Ensure they are converted with `floor()` ### Are these changes tested? Yes ### Are there any user-facing changes? Yes * GitHub Issue: apache#44502 Authored-by: Nic Crane <[email protected]> Signed-off-by: Nic Crane <[email protected]>
1 parent 4663460 commit 136168e

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

r/src/r_to_arrow.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,13 @@ class RPrimitiveConverter<T, enable_if_t<is_date_type<T>::value>>
595595
return VisitVector(it, size, append_null, append_value);
596596
}
597597

598-
static int FromRDate(const Date32Type*, double from) { return static_cast<int>(from); }
598+
static int FromRDate(const Date32Type*, double from) {
599+
return static_cast<int>(std::floor(from));
600+
}
599601

600602
static int64_t FromRDate(const Date64Type*, double from) {
601603
constexpr int64_t kMilliSecondsPerDay = 86400000;
602-
return static_cast<int64_t>(from * kMilliSecondsPerDay);
604+
return static_cast<int64_t>(std::floor(from * kMilliSecondsPerDay));
603605
}
604606

605607
static int FromPosixct(const Date32Type*, double from) {

r/tests/testthat/test-Array.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,3 +1397,9 @@ test_that("Can convert R integer/double to decimal (ARROW-11631)", {
13971397
"Conversion to decimal from non-integer/double"
13981398
)
13991399
})
1400+
1401+
test_that("Array handles negative fractional dates correctly (GH-46873)", {
1402+
d <- as.Date(-0.1)
1403+
arr <- arrow_array(d)
1404+
expect_equal(as.vector(arr), as.Date("1969-12-31"))
1405+
})

0 commit comments

Comments
 (0)