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

small refactoring to allow formats to be added #4

Merged
merged 1 commit into from Sep 14, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
@@ -1,5 +1,10 @@
2016-09-13 Dirk Eddelbuettel <edd@debian.org>

* 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 <edd@debian.org>
Expand Down
8 changes: 8 additions & 0 deletions R/RcppExports.R
Expand Up @@ -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))
}

20 changes: 20 additions & 0 deletions src/RcppExports.cpp
Expand Up @@ -17,3 +17,23 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// getFormats
std::vector<std::string> 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
}
125 changes: 81 additions & 44 deletions src/anytime.cpp
Expand Up @@ -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<std::string> formats;
std::vector<std::locale> locales;
public:
TimeFormats() {
for (size_t i=0; i<nsformats; i++) {
//Rcpp::Rcout << i << std::endl;
formats.push_back(sformats[i]);
locales.push_back(std::locale(std::locale::classic(),
new bt::time_input_facet(sformats[i])));
}
}
void addFormat(const std::string txt) {
formats.insert(formats.begin(), txt);
locales.insert(locales.begin(), std::locale(std::locale::classic(),
new bt::time_input_facet(txt)));
};
std::locale getLocale(int i) { return locales[i]; }
std::string getFormat(int i) { return formats[i]; }
size_t getN() { return formats.size(); }
std::vector<std::string> getFormats() { return formats; }
};
const size_t nformats = sizeof(formats)/sizeof(formats[0]);

static TimeFormats timeformats;

double stringToTime(const std::string s) {

Expand All @@ -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;
}

Expand Down Expand Up @@ -164,3 +192,12 @@ Rcpp::NumericVector anytime_cpp(SEXP x, std::string tz = "UTC") {
}
}

// [[Rcpp::export]]
std::vector<std::string> getFormats() {
return timeformats.getFormats();
}

// [[Rcpp::export]]
void addFormat(std::string fmt) {
timeformats.addFormat(fmt);
}