diff --git a/ChangeLog b/ChangeLog index 44c4b2e..0c6bc28 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2023-09-13 Dirk Eddelbuettel + + * 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 * tests/tinytest.R: Simplified diff --git a/src/anytime.cpp b/src/anytime.cpp index 2fc756d..52f0c16 100644 --- a/src/anytime.cpp +++ b/src/anytime.cpp @@ -362,15 +362,10 @@ Rcpp::NumericVector convertToTime(const Rcpp::Vector& 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& 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 @@ -435,6 +431,11 @@ Rcpp::NumericVector convertToTime(const Rcpp::Vector& 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