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

Improve detectDates. closes #288 #291

Merged
merged 4 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: openxlsx
Title: Read, Write and Edit xlsx Files
Version: 4.2.5
Version: 4.2.5.1
Date: 2021-12-13
Authors@R:
c(person(given = "Philipp",
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# openxlsx (development version)

## Improvements

* Improve detectDates ([#288](https://github.com/ycphs/openxlsx/issues/288))

# openxlsx 4.2.5

## Fixes
Expand Down
Binary file added inst/extdata/gh_issue_288.xlsx
Binary file not shown.
18 changes: 12 additions & 6 deletions src/write_file.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "openxlsx.h"
#include <regex>


//' @import Rcpp
Expand Down Expand Up @@ -197,7 +198,8 @@ SEXP buildMatrixMixed(CharacterVector v,
for(int i = 0;i < k; i++)
m(rowInd[i], colInd[i]) = v[i];


// convert numerics to dates
Rcpp::Function cTD("convertToDate");

// this will be the return data.frame
List dfList(nCols);
Expand Down Expand Up @@ -225,16 +227,20 @@ SEXP buildMatrixMixed(CharacterVector v,
if(!notNAElements[ri]){
datetmp[ri] = NA_REAL; //IF TRUE, TRUE else FALSE
}else{
// dt_str = as<std::string>(m(ri,i));
dt_str = m(ri,i);

try{
datetmp[ri] = Rcpp::Date(atoi(dt_str.substr(5,2).c_str()), atoi(dt_str.substr(8,2).c_str()), atoi(dt_str.substr(0,4).c_str()) );
}catch(...) {
// Some values are incorrectly detected as dates. This regex determines if they are numerics.
// If so, they are converted to Dates.
if (std::regex_match( dt_str, std::regex( ( "((\\+|-)?[[:digit:]]+)(\\.(([[:digit:]]+)?))?" ) ) )) {
datetmp[ri] = as<Rcpp::Date>(cTD(dt_str));
} else {
datetmp[ri] = Rcpp::Date(atoi(dt_str.substr(5,2).c_str()), atoi(dt_str.substr(8,2).c_str()), atoi(dt_str.substr(0,4).c_str()) );
}
} catch(...) {
Rcpp::Rcerr << "Error reading date:\n" << dt_str << "\nrow: " << ri+1 << "\ncol: " << i+1 << "\n";
throw;
}
//datetmp[ri] = Date(atoi(m(ri,i)) - originAdj);
//datetmp[ri] = Date(as<std::string>(m(ri,i)));
}
}

Expand Down
19 changes: 16 additions & 3 deletions tests/testthat/test-date_time_conversion.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@


context("Date/Time Conversions")



test_that("convert to date", {
dates <- as.Date("2015-02-07") + -10:10
origin <- 25569L
Expand All @@ -18,7 +16,6 @@ test_that("convert to date", {
})



test_that("convert to datetime", {
x <- 43037 + 2 / 1440
expect_equal(object = convertToDateTime(x, tx = Sys.timezone()), expected = as.POSIXct("2017-10-29 00:02:00", tz = Sys.timezone()))
Expand All @@ -33,3 +30,19 @@ test_that("convert to datetime", {
x_datetime <- convertToDateTime(x, tx = "UTC")
attr(x_datetime, "tzone") <- "UTC"
})


test_that("read.xlsx detectDates", {

xlsxFile <- system.file("extdata", "gh_issue_288.xlsx", package = "openxlsx")

ref_dat <- data.frame(Date = c(as.Date(c("2021-10-20", "2021-11-03"))))
ref_num <- data.frame(Date = c(44489.4, 44503.0))

tst_dat <- read.xlsx(xlsxFile, detectDates = TRUE)
tst_num <- read.xlsx(xlsxFile, detectDates = FALSE)

expect_equal(ref_dat, tst_dat)
expect_equal(ref_num, tst_num)

})