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

fix posixct conv back and forth #589

Merged
merged 5 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/rust/src/conversion_r_to_s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ fn recursive_robjname2series_tree(x: &Robj, name: &str) -> pl::PolarsResult<Seri
//todo this could probably in fewer allocations
let dt = pl::DataType::Datetime(pl::TimeUnit::Milliseconds, tz);
Ok(SeriesTree::Series(
(s.cast(&pl::DataType::Int64)? * 1_000i64).cast(&dt)?,
((s * 1000f64).cast(&pl::DataType::Int64)?).cast(&dt)?,
))
}
Ok(SeriesTree::Series(s)) if x.inherits("Date") => {
Expand Down
14 changes: 7 additions & 7 deletions src/rust/src/conversion_s_to_r.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,19 @@ pub fn pl_series_to_list(
}),

Datetime(tu, opt_tz) => {
let tu_i64: i64 = match tu {
pl::TimeUnit::Nanoseconds => 1_000_000_000,
pl::TimeUnit::Microseconds => 1_000_000,
pl::TimeUnit::Milliseconds => 1_000,
let tu_f64: f64 = match tu {
pl::TimeUnit::Nanoseconds => 1_000_000_000.0,
pl::TimeUnit::Microseconds => 1_000_000.0,
pl::TimeUnit::Milliseconds => 1_000.0,
};

//resolve timezone
let tz = opt_tz.as_ref().map(|s| s.as_str()).unwrap_or("");
s.cast(&Int64)?
.i64()
s.cast(&Float64)?
.f64()
.map(|ca| {
ca.into_iter()
.map(|opt| opt.map(|val| (val / tu_i64) as f64))
.map(|opt| opt.map(|val| val / tu_f64))
.collect_robj()
})
// TODO set_class and set_attrib reallocates the vector, find some way to modify without.
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-expr_datetime.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ test_that("pl$lit posix", {
pl$lit(as.POSIXct("2022-01-01", tz = "GMT"))$to_r(),
as.POSIXct("2022-01-01", tz = "GMT")
)


x = as.POSIXct(
c(
"2020-01-01 13:45:48.343", "2020-01-01 13:45:48.815"
, "2020-01-01 13:45:49.289", "2020-01-01 13:45:49.974"
, "2020-01-01 13:45:51.190", "2020-01-01 13:45:51.631"
), tz = "UTC"
)
expect_identical(pl$lit(x)$to_r(), x) #preserve millisecond precision
expect_failure(expect_identical(pl$lit(x)$to_r(), x+0.001)) #control, detect 1ms offset
eitsupi marked this conversation as resolved.
Show resolved Hide resolved

})


Expand Down Expand Up @@ -846,3 +858,7 @@ test_that("$dt$time()", {
c(0.00e+00, 2.16e+13, 4.32e+13, 6.48e+13, 0.00e+00)
)
})




Loading