Skip to content

Commit

Permalink
dt should be a POSIXct object only
Browse files Browse the repository at this point in the history
  • Loading branch information
anderic1 committed Nov 27, 2017
1 parent ef3ff6f commit 46409f0
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 20 deletions.
2 changes: 1 addition & 1 deletion R/RcppExports.R
Expand Up @@ -37,7 +37,7 @@ exampleFormat <- function() {
#' @title Return difference between two time zones at a given date.
#' @param tzfrom The first time zone as a character vector.
#' @param tzto The second time zone as a character vector.
#' @param dt A Date or Datetime object specifying when the difference is to be computed.
#' @param dt A Datetime object specifying when the difference is to be computed.
#' @param verbose A boolean toggle indicating whether more verbose operations
#' are desired, default is \code{FALSE}.
#' @return A numeric value with the difference (in hours) between the first and
Expand Down
2 changes: 1 addition & 1 deletion man/tzDiff.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/RcppExports.cpp
Expand Up @@ -72,14 +72,14 @@ BEGIN_RCPP
END_RCPP
}
// tzDiff
Rcpp::NumericVector tzDiff(const std::string tzfrom, const std::string tzto, Rcpp::RObject dt, bool verbose);
Rcpp::NumericVector tzDiff(const std::string tzfrom, const std::string tzto, const Rcpp::NumericVector& dt, bool verbose);
RcppExport SEXP _RcppCCTZ_tzDiff(SEXP tzfromSEXP, SEXP tztoSEXP, SEXP dtSEXP, SEXP verboseSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const std::string >::type tzfrom(tzfromSEXP);
Rcpp::traits::input_parameter< const std::string >::type tzto(tztoSEXP);
Rcpp::traits::input_parameter< Rcpp::RObject >::type dt(dtSEXP);
Rcpp::traits::input_parameter< const Rcpp::NumericVector& >::type dt(dtSEXP);
Rcpp::traits::input_parameter< bool >::type verbose(verboseSEXP);
rcpp_result_gen = Rcpp::wrap(tzDiff(tzfrom, tzto, dt, verbose));
return rcpp_result_gen;
Expand Down
21 changes: 5 additions & 16 deletions src/utilities.cpp
Expand Up @@ -11,7 +11,6 @@ namespace sc = std::chrono; // shorthand

double tzDiffAtomic(const cctz::time_zone& tz1, const cctz::time_zone& tz2, const Rcpp::Datetime& dt, bool verbose);

#define SECS_PER_DAY 86400.0

//' Difference between two given timezones at a specified date.
//'
Expand All @@ -21,7 +20,7 @@ double tzDiffAtomic(const cctz::time_zone& tz1, const cctz::time_zone& tz2, cons
//' @title Return difference between two time zones at a given date.
//' @param tzfrom The first time zone as a character vector.
//' @param tzto The second time zone as a character vector.
//' @param dt A Date or Datetime object specifying when the difference is to be computed.
//' @param dt A Datetime object specifying when the difference is to be computed.
//' @param verbose A boolean toggle indicating whether more verbose operations
//' are desired, default is \code{FALSE}.
//' @return A numeric value with the difference (in hours) between the first and
Expand All @@ -36,7 +35,7 @@ double tzDiffAtomic(const cctz::time_zone& tz1, const cctz::time_zone& tz2, cons
// [[Rcpp::export]]
Rcpp::NumericVector tzDiff(const std::string tzfrom,
const std::string tzto,
Rcpp::RObject dt,
const Rcpp::NumericVector& dt,
bool verbose=false) {

cctz::time_zone tz1, tz2;
Expand All @@ -46,19 +45,9 @@ Rcpp::NumericVector tzDiff(const std::string tzfrom,

Rcpp::NumericVector res;

if (dt.inherits("Date")) {
auto dtv = Rcpp::as<Rcpp::DateVector>(dt);
res = Rcpp::NumericVector(dtv.size());
std::transform(dtv.begin(), dtv.end(), res.begin(),
[&tz1, &tz2, verbose](double dtval){
Rcpp::Datetime dtt(dtval * SECS_PER_DAY);
return tzDiffAtomic(tz1, tz2, dtt, verbose);
});

} else if (dt.inherits("POSIXct")) {
auto dtv = Rcpp::as<Rcpp::DatetimeVector>(dt);
res = Rcpp::NumericVector(dtv.size());
std::transform(dtv.begin(), dtv.end(), res.begin(),
if (dt.inherits("POSIXct")) {
res = Rcpp::NumericVector(dt.size());
std::transform(dt.begin(), dt.end(), res.begin(),
[&tz1, &tz2, verbose](double dtval){
Rcpp::Datetime dtt(dtval);
return tzDiffAtomic(tz1, tz2, dtt, verbose);
Expand Down

0 comments on commit 46409f0

Please sign in to comment.