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

Warn if NA value seen (closes #129) #130

Merged
merged 2 commits into from
Sep 13, 2023
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
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2023-09-13 Dirk Eddelbuettel <edd@debian.org>

* src/anytime.cpp (convertToTime): Warn if NA values resulting from
conversion to character (and only one warning will be give)

2023-09-09 Dirk Eddelbuettel <edd@debian.org>

* tests/tinytest.R: Simplified
Expand Down
5 changes: 5 additions & 0 deletions inst/tinytest/test_gh_issue_129.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

library(anytime)

dtchar <- "13/13/2023" # ambivalent input, should result in warning
expect_warning(anytime(dtchar))
13 changes: 7 additions & 6 deletions src/anytime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,10 @@ Rcpp::NumericVector convertToTime(const Rcpp::Vector<RTYPE>& sxpvec,
const bool asDate = false,
const bool useR = false) {

bool have_warned = false;
// step one: create a results vector, and class it as POSIXct
int n = sxpvec.size();
Rcpp::DatetimeVector pv(n, tz.c_str());
// if (asDate) {
// pv.attr("class") = Rcpp::CharacterVector::create("Date");
// } else {
// pv.attr("class") = Rcpp::CharacterVector::create("POSIXct", "POSIXt");
// }
// pv.attr("tzone") = tz;

// step two: loop over input, cast each element to string and then convert
for (int i=0; i<n; i++) {
Expand All @@ -383,6 +378,7 @@ Rcpp::NumericVector convertToTime(const Rcpp::Vector<RTYPE>& sxpvec,

if (s == "NA") {
pv[i] = NA_REAL; // #nocov
Rcpp::warning("Input conversion resulted in 'NA' results.");
} else {
if (debug) Rcpp::Rcout << "before tests: " << s << std::endl;
// Boost Date_Time gets the 'YYYYMMDD' format wrong, even
Expand Down Expand Up @@ -435,6 +431,11 @@ Rcpp::NumericVector convertToTime(const Rcpp::Vector<RTYPE>& sxpvec,
} else {
pv[i] = stringToTime(s, asUTC, asDate);
}
if (R_IsNA(pv[i]) && !have_warned) {
Rcpp::warning(tfm::format("Input conversion of '%s' resulted in 'NA' results.", s));
have_warned = true;
}

}
}
// There is an issue with datetime parsing under TZ=Europe/London, see eg #36 and #51
Expand Down