diff --git a/ChangeLog b/ChangeLog index 600e1d8..6a64994 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2016-09-13 Dirk Eddelbuettel + * src/anytime.cpp: Refactored so that formats are now simple + string kept in vector (from which locales are built in the ctor of + a simple container class); now new formats can be added (at the + front); formats get be retrieved as well + * README.md: Add badges and CRAN installation note 2016-09-12 Dirk Eddelbuettel diff --git a/R/RcppExports.R b/R/RcppExports.R index 7ed4c34..69a2551 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -5,3 +5,11 @@ anytime_cpp <- function(x, tz = "UTC") { .Call('anytime_anytime_cpp', PACKAGE = 'anytime', x, tz) } +getFormats <- function() { + .Call('anytime_getFormats', PACKAGE = 'anytime') +} + +addFormat <- function(fmt) { + invisible(.Call('anytime_addFormat', PACKAGE = 'anytime', fmt)) +} + diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 10e096e..95b9855 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -17,3 +17,23 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } +// getFormats +std::vector getFormats(); +RcppExport SEXP anytime_getFormats() { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + rcpp_result_gen = Rcpp::wrap(getFormats()); + return rcpp_result_gen; +END_RCPP +} +// addFormat +void addFormat(std::string fmt); +RcppExport SEXP anytime_addFormat(SEXP fmtSEXP) { +BEGIN_RCPP + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< std::string >::type fmt(fmtSEXP); + addFormat(fmt); + return R_NilValue; +END_RCPP +} diff --git a/src/anytime.cpp b/src/anytime.cpp index 05601c1..a152ed8 100644 --- a/src/anytime.cpp +++ b/src/anytime.cpp @@ -27,51 +27,78 @@ namespace bt = boost::posix_time; -const std::locale formats[] = { - std::locale(std::locale::classic(), new bt::time_input_facet("%Y-%m-%d %H:%M:%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%Y/%m/%d %H:%M:%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%Y%m%d %H%M%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%Y%m%d %H:%M:%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%m/%d/%Y %H:%M:%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%m-%d-%Y %H:%M:%S%f")), - // std::locale(std::locale::classic(), new bt::time_input_facet("%d.%m.%Y %H:%M:%S%f")), - - std::locale(std::locale::classic(), new bt::time_input_facet("%Y-%b-%d %H:%M:%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%Y/%b/%d %H:%M:%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%Y%b%d %H%M%S%F")), - std::locale(std::locale::classic(), new bt::time_input_facet("%Y%b%d %H:%M:%S%F")), - std::locale(std::locale::classic(), new bt::time_input_facet("%b/%d/%Y %H:%M:%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%b-%d-%Y %H:%M:%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%d.%b.%Y %H:%M:%S%f")), - - std::locale(std::locale::classic(), new bt::time_input_facet("%Y-%B-%d %H:%M:%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%Y/%B/%d %H:%M:%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%Y%B%d %H%M%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%Y%B%d %H:%M:%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%B/%d/%Y %H:%M:%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%B-%d-%Y %H:%M:%S%f")), - std::locale(std::locale::classic(), new bt::time_input_facet("%d.%B.%Y %H:%M:%S%f")), +const std::string sformats[] = { + "%Y-%m-%d %H:%M:%S%f", + "%Y/%m/%d %H:%M:%S%f", + "%Y%m%d %H%M%S%f", + "%Y%m%d %H:%M:%S%f", + "%m/%d/%Y %H:%M:%S%f", + "%m-%d-%Y %H:%M:%S%f", + // "%d.%m.%Y %H:%M:%S%f", + + "%Y-%b-%d %H:%M:%S%f", + "%Y/%b/%d %H:%M:%S%f", + "%Y%b%d %H%M%S%F", + "%Y%b%d %H:%M:%S%F", + "%b/%d/%Y %H:%M:%S%f", + "%b-%d-%Y %H:%M:%S%f", + "%d.%b.%Y %H:%M:%S%f", + + "%Y-%B-%d %H:%M:%S%f", + "%Y/%B/%d %H:%M:%S%f", + "%Y%B%d %H%M%S%f", + "%Y%B%d %H:%M:%S%f", + "%B/%d/%Y %H:%M:%S%f", + "%B-%d-%Y %H:%M:%S%f", + "%d.%B.%Y %H:%M:%S%f", // see http://stackoverflow.com/questions/39259184/formatting-dates-with-r for next one - std::locale(std::locale::classic(), new bt::time_input_facet("%a %b %d %H:%M:%S%F %Y")), - - std::locale(std::locale::classic(), new bt::time_input_facet("%Y-%m-%d")), - std::locale(std::locale::classic(), new bt::time_input_facet("%Y%m%d")), - std::locale(std::locale::classic(), new bt::time_input_facet("%m/%d/%Y")), - std::locale(std::locale::classic(), new bt::time_input_facet("%m-%d-%Y")), - - std::locale(std::locale::classic(), new bt::time_input_facet("%Y-%b-%d")), - std::locale(std::locale::classic(), new bt::time_input_facet("%Y%b%d")), - std::locale(std::locale::classic(), new bt::time_input_facet("%b/%d/%Y")), - std::locale(std::locale::classic(), new bt::time_input_facet("%b-%d-%Y")), - - std::locale(std::locale::classic(), new bt::time_input_facet("%Y-%B-%d")), - std::locale(std::locale::classic(), new bt::time_input_facet("%Y%B%d")), - std::locale(std::locale::classic(), new bt::time_input_facet("%B/%d/%Y")), - std::locale(std::locale::classic(), new bt::time_input_facet("%B-%d-%Y")) - + "%a %b %d %H:%M:%S%F %Y", + + "%Y-%m-%d", + "%Y%m%d", + "%m/%d/%Y", + "%m-%d-%Y", + + "%Y-%b-%d", + "%Y%b%d", + "%b/%d/%Y", + "%b-%d-%Y", + + "%Y-%B-%d", + "%Y%B%d", + "%B/%d/%Y", + "%B-%d-%Y" +}; +const size_t nsformats = sizeof(sformats)/sizeof(sformats[0]); + + +// this was lines 30 to 73 +class TimeFormats { +private: + std::vector formats; + std::vector locales; +public: + TimeFormats() { + for (size_t i=0; i getFormats() { return formats; } }; -const size_t nformats = sizeof(formats)/sizeof(formats[0]); + +static TimeFormats timeformats; double stringToTime(const std::string s) { @@ -81,9 +108,10 @@ double stringToTime(const std::string s) { bt::ptime pt, ptbase; // loop over formats and try them til one fits - for (size_t i=0; pt == ptbase && i < nformats; ++i) { + for (size_t i=0; pt == ptbase && i < timeformats.getN(); ++i) { std::istringstream is(s); - is.imbue(formats[i]); + //bRcpp::Rcout << timeformats.getFormat(i) << std::endl; + is.imbue(timeformats.getLocale(i)); is >> pt; } @@ -164,3 +192,12 @@ Rcpp::NumericVector anytime_cpp(SEXP x, std::string tz = "UTC") { } } +// [[Rcpp::export]] +std::vector getFormats() { + return timeformats.getFormats(); +} + +// [[Rcpp::export]] +void addFormat(std::string fmt) { + timeformats.addFormat(fmt); +}