Skip to content

Commit

Permalink
Merge pull request #130 from eddelbuettel/feature/na_warning
Browse files Browse the repository at this point in the history
Warn if NA value seen (closes #129)
  • Loading branch information
eddelbuettel authored Sep 13, 2023
2 parents e8a1b3c + 3a677de commit fe4f60b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
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

0 comments on commit fe4f60b

Please sign in to comment.