[Rcpp-commits] r3744 - in pkg/RcppBDT: . R src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Aug 26 01:54:14 CEST 2012


Author: edd
Date: 2012-08-26 01:54:14 +0200 (Sun, 26 Aug 2012)
New Revision: 3744

Added:
   pkg/RcppBDT/src/RcppBDTdu.cpp
Modified:
   pkg/RcppBDT/ChangeLog
   pkg/RcppBDT/DESCRIPTION
   pkg/RcppBDT/R/zzz.R
   pkg/RcppBDT/src/RcppBDTtz.cpp
Log:
added duration objects
added some more functionality to tz objects


Modified: pkg/RcppBDT/ChangeLog
===================================================================
--- pkg/RcppBDT/ChangeLog	2012-08-24 23:14:45 UTC (rev 3743)
+++ pkg/RcppBDT/ChangeLog	2012-08-25 23:54:14 UTC (rev 3744)
@@ -1,8 +1,17 @@
+2012-08-25  Dirk Eddelbuettel  <edd at debian.org>
+
+	* src/RcppBDTdu.cpp: New module 'bdtDu' for duration objects which
+	can represents hours, minutes, seconds, down to milli, micro and
+	nanoseconds (where the last part may be OS-dependent, see Boost docs)
+
 2012-08-24  Dirk Eddelbuettel  <edd at dexter>
 
 	* src/RcppBDTtz.cpp: Use Language evaluation to get location of zone
-	db file, reducing constructor to single argument for region
+	db file, reducing constructor to single argument for region; also
+	store initial region and added DST start/end time accessors
 
+	* R/zzz.R: add default display methods for bdtTz objects
+
 2012-08-22  Dirk Eddelbuettel  <edd at dexter>
 
 	* src/RcppBDTtz.cpp: Rewritten bdtTz to be based on small class
@@ -11,7 +20,7 @@
 
 2012-08-20  Dirk Eddelbuettel  <edd at dexter>
 
-	* src/RcppBDTtz.cpp: New modules 'bdtTz' for time zone functionality
+	* src/RcppBDTtz.cpp: New module 'bdtTz' for time zone functionality
 	* R/zzz.R: Load bdtTzMod module on startup
 	* inst/include/RcppBDT.h: Also include local_time.hpp header
 

Modified: pkg/RcppBDT/DESCRIPTION
===================================================================
--- pkg/RcppBDT/DESCRIPTION	2012-08-24 23:14:45 UTC (rev 3743)
+++ pkg/RcppBDT/DESCRIPTION	2012-08-25 23:54:14 UTC (rev 3744)
@@ -1,7 +1,7 @@
 Package: RcppBDT
 Type: Package
 Title: Rcpp bindings for the Boost Date_Time library 
-Version: 0.2.1.2
+Version: 0.2.1.3
 Date: $Date$
 Author: Dirk Eddelbuettel and Romain Francois
 Maintainer: Dirk Eddelbuettel <edd at debian.org>

Modified: pkg/RcppBDT/R/zzz.R
===================================================================
--- pkg/RcppBDT/R/zzz.R	2012-08-24 23:14:45 UTC (rev 3743)
+++ pkg/RcppBDT/R/zzz.R	2012-08-25 23:54:14 UTC (rev 3744)
@@ -32,6 +32,7 @@
 
 loadModule("bdtMod", TRUE)
 loadModule("bdtTzMod", TRUE)
+loadModule("bdtDuMod", TRUE)
 
 ## create a variable 'bdt' from out bdtMod Module
 ## this variable is used as a package-global instance
@@ -44,9 +45,14 @@
 .format_date <- function(x, ...) format(x$getDate(), ...)
 .show_date <- function(object) print(object$getDate())
 
+.format_tz <- function(x, ...) format(x$getRegion(), ...)
+.show_tz <- function(object) print(object$getRegion())
+
 ## define an onLoad expression to set some methods
 evalqOnLoad({
     setMethod("show", "Rcpp_bdtDate", .show_date)
+    setMethod("show", "Rcpp_bdtTz", .show_tz)
     setGeneric("format", function(x,...) standardGeneric("format") )
     setMethod("format", "Rcpp_bdtDate", .format_date )
+    setMethod("format", "Rcpp_bdtTz", .format_tz )
 })

Added: pkg/RcppBDT/src/RcppBDTdu.cpp
===================================================================
--- pkg/RcppBDT/src/RcppBDTdu.cpp	                        (rev 0)
+++ pkg/RcppBDT/src/RcppBDTdu.cpp	2012-08-25 23:54:14 UTC (rev 3744)
@@ -0,0 +1,80 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
+//
+// RcppBDTdu.cpp: Rcpp and Boost Date_Time glue for durations
+//
+// Copyright (C) 2012        Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of RcppBDT.
+//
+// RcppBDT is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//                           
+// RcppBDT is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with RcppBDT.  If not, see <http://www.gnu.org/licenses/>.
+
+// this enables use of nanoseconds 
+#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG 1
+
+#include <RcppBDT.h>
+
+class bdtDu {
+
+public:
+
+    bdtDu(int hours, int minutes, int seconds, int fractionalseconds) : m_td(hours, minutes, seconds, fractionalseconds) { }
+
+    long getHours()             { return(m_td.hours()); }
+    long getMinutes()           { return(m_td.minutes()); }
+    long getSeconds()           { return(m_td.seconds()); }
+    long getTotalSeconds()      { return(m_td.total_seconds()); }
+    long getTotalMilliSeconds() { return(m_td.total_milliseconds()); }
+    long getTotalMicroSeconds() { return(m_td.total_microseconds()); }
+    long getTotalNanoSeconds()  { return(m_td.total_nanoseconds()); }
+    long getFractionalSeconds() { return(m_td.fractional_seconds()); }
+    unsigned short getNumFractionalDigits() { return(m_td.num_fractional_digits()); }
+    long getTicksPerSecond()    { return(m_td.ticks_per_second()); }
+
+    void addHours(int h)        { m_td += boost::posix_time::hours(h); }
+    void addMinutes(int m)      { m_td += boost::posix_time::minutes(m); }
+    void addSeconds(int s)      { m_td += boost::posix_time::seconds(s); }
+    void addMilliSeconds(int s) { m_td += boost::posix_time::milliseconds(s); }
+    void addMicroSeconds(int s) { m_td += boost::posix_time::microseconds(s); }
+    void addNanoSeconds(int s)  { m_td += boost::posix_time::nanoseconds(s); }
+            
+private:
+    boost::posix_time::time_duration m_td;
+            
+};
+
+RCPP_MODULE(bdtDuMod) {
+    Rcpp::class_<bdtDu>("bdtDu")   
+	
+        .constructor<int,int,int,int>("constructor with hours, minutes, seconds and fractional_seconds")  
+
+        .method("getHours",               &bdtDu::getHours,               "get hours of the time duration")
+        .method("getMinutes",             &bdtDu::getMinutes,             "get minutes of the time duration")
+        .method("getSeconds",             &bdtDu::getSeconds,             "get seconds of the time duration")
+        .method("getTotalSeconds",        &bdtDu::getTotalSeconds,        "get total seconds of the time duration")
+        .method("getTotalMilliSeconds",   &bdtDu::getTotalMilliSeconds,   "get total milli seconds of the time duration")
+        .method("getTotalMicroSeconds",   &bdtDu::getTotalMicroSeconds,   "get total micro seconds of the time duration")
+        .method("getTotalNanoSeconds",    &bdtDu::getTotalNanoSeconds,    "get total nano seconds of the time duration")
+        .method("getFractionalSeconds",   &bdtDu::getFractionalSeconds,   "get fractional seconds of time duration")
+
+        .method("getNumFractionalDigits", &bdtDu::getNumFractionalDigits, "get number of fractional seconds (6 or 9)")
+        .method("getTicksPerSecond",      &bdtDu::getTicksPerSecond,      "get number of fractional seconds (10^6 or 10^9)")
+
+        .method("addHours",               &bdtDu::addHours,               "add given hours to duration object")
+        .method("addMinutes",             &bdtDu::addMinutes,             "add given minutes to duration object")
+        .method("addSeconds",             &bdtDu::addSeconds,             "add given seconds to duration object")
+        .method("addMilliSeconds",        &bdtDu::addMilliSeconds,        "add given milliseconds to duration object")
+        .method("addMicroSeconds",        &bdtDu::addMicroSeconds,        "add given microseconds to duration object")
+        .method("addNanoSeconds",         &bdtDu::addNanoSeconds,         "add given nanoseconds to duration object")
+    ;
+}

Modified: pkg/RcppBDT/src/RcppBDTtz.cpp
===================================================================
--- pkg/RcppBDT/src/RcppBDTtz.cpp	2012-08-24 23:14:45 UTC (rev 3743)
+++ pkg/RcppBDT/src/RcppBDTtz.cpp	2012-08-25 23:54:14 UTC (rev 3744)
@@ -25,20 +25,20 @@
 
 public:
 
-    bdtTz(std::string region) {
+    bdtTz(std::string region) : m_tzone(region) {
 	// nice little trick: evaluate a Language() object corresponding to the R call
 	//    system.file("data", "date_time_zonespec.csv", package="RcppBDT")
 	// but from C++
-	Rcpp::Language ll = Rcpp::Language("system.file", "data", 
-					   "date_time_zonespec.csv", 
+	Rcpp::Language ll = Rcpp::Language("system.file", "data", "date_time_zonespec.csv", 
 					   Rcpp::Named("package","RcppBDT"));
 	std::string zonefile = Rcpp::as<std::string>(ll.eval(R_GlobalEnv));
-
     	m_tz.load_from_file(zonefile); 			// load db from csv zonefile
     	m_tzp = m_tz.time_zone_from_region(region);	// init with given region
+        if (m_tzp == NULL) 
+            ::Rf_error("Unknown region supplied, no tz object created");
     }
 
-    std::vector<std::string> getRegions() { return( m_tz.region_list() ); }
+    std::vector<std::string> getAllRegions() { return( m_tz.region_list() ); }
 
     long getUtcTotalSec() {
 	boost::posix_time::time_duration tdt = m_tzp->base_utc_offset();
@@ -54,12 +54,48 @@
     std::string getStdZoneAbbrev() { return(m_tzp->std_zone_abbrev()); }
     std::string getDstZoneName()   { return(m_tzp->dst_zone_name());   }
     std::string getStdZoneName()   { return(m_tzp->std_zone_name());   }
+    std::string getRegion()        { return(m_tzone);                  }
     bool        hasDst()           { return(m_tzp->has_dst());         }
     std::string getPosixString()   { return(m_tzp->to_posix_string()); }
 
+    // we (currently ?) cannot return Rcpp::Datetime with a tzone attribute, though returning a SEXP works
+    // SEXP getDstStart(const int year) {
+    //     boost::posix_time::ptime t = m_tzp->dst_local_start_time(year);
+    //     boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
+    //     boost::posix_time::time_duration::sec_type x = // need to correct difference to epoch by UTC offset
+    //         (t - epoch - m_tzp->base_utc_offset()).total_seconds();
+    //     Rcpp::RObject dt = Rcpp::wrap(Rcpp::Datetime(x));
+    //     dt.attr("tzone") = m_tzone;
+    //     return dt;
+    // }
+    Rcpp::Datetime getDstLocalStart(const int year) {
+        boost::posix_time::ptime t = m_tzp->dst_local_start_time(year);
+        boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
+        boost::posix_time::time_duration::sec_type x = // need to correct difference to epoch by UTC offset
+            (t - epoch - m_tzp->base_utc_offset()).total_seconds();
+        return Rcpp::Datetime(x);
+    }
+    // SEXP getDstEnd(const int year) {
+    //     boost::posix_time::ptime t = m_tzp->dst_local_end_time(year);
+    //     boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
+    //     boost::posix_time::time_duration::sec_type x = // need to correct difference to epoch by UTC offset
+    //         (t - epoch - m_tzp->base_utc_offset()).total_seconds();
+    //     Rcpp::RObject dt = Rcpp::wrap(Rcpp::Datetime(x));
+    //     dt.attr("tzone") = m_tzone;
+    //     return dt;
+    // }
+    Rcpp::Datetime getDstLocalEnd(const int year) {
+        boost::posix_time::ptime t = m_tzp->dst_local_end_time(year);
+        boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
+        boost::posix_time::time_duration::sec_type x = // need to correct difference to epoch by UTC offset
+            (t - epoch - m_tzp->base_utc_offset()).total_seconds();
+        return Rcpp::Datetime(x);
+    }
+
 private:
 
     bdtTz() {};			// hide default constructor
+    std::string m_tzone;
 
     boost::local_time::tz_database m_tz;
     boost::local_time::time_zone_ptr m_tzp;
@@ -70,28 +106,25 @@
 
     Rcpp::class_<bdtTz>("bdtTz")   
 	
-    .constructor<std::string>("constructor with region string (eg \"Europe/London\")")  
+        .constructor<std::string>("constructor with region string (eg \"Europe/London\")")  
 
-    .method("getRegions",       &bdtTz::getRegions,       "get vector of TZ region names")
+        .method("getRegion",        &bdtTz::getRegion,        "get region string of this tz object")
+        .method("getAllRegions",    &bdtTz::getAllRegions,    "get vector of TZ region names")
+        
+        .method("getUtcOffset",     &bdtTz::getUtcTotalSec,   "get UTC offset in seconds")
+        .method("getDstOffset",     &bdtTz::getDstTotalSec  , "get DST offset in seconds")
 
-    .method("getUtcOffset",     &bdtTz::getUtcTotalSec,   "get UTC offset in seconds")
-    .method("getDstOffset",     &bdtTz::getDstTotalSec  , "get DST offset in seconds")
+        .method("getDstZoneAbbrev", &bdtTz::getDstZoneAbbrev, "get DST zone abbreviation")
+        .method("getStdZoneAbbrev", &bdtTz::getStdZoneAbbrev, "get standard zone abbreviation")
+        .method("getDstZoneName",   &bdtTz::getDstZoneName,   "get DST zone name")
+        .method("getStdZoneName",   &bdtTz::getStdZoneName,   "get standard zone name")
+        .method("hasDst",           &bdtTz::hasDst,           "true if timezone has daylight savings")
+        .method("getPosixString",   &bdtTz::getPosixString,   "get posix time zone representation")
+        
+        .method("getDstLocalStart", &bdtTz::getDstLocalStart, "get start of DST in given year (in local tzone)")
+        .method("getDstLocalEnd",   &bdtTz::getDstLocalEnd,   "get end of DST in given year (in local tzone)")
+        //.method("getDstStart",      &bdtTz::getDstStart,      "get start of DST in given year (as Datetime with tzone)")
+        //.method("getDstEnd",        &bdtTz::getDstEnd,        "get end of DST in given year (as Datetime with tzone)")
 
-    .method("getDstZoneAbbrev", &bdtTz::getDstZoneAbbrev, "get DST zone abbreviation")
-    .method("getStdZoneAbbrev", &bdtTz::getStdZoneAbbrev, "get standard zone abbreviation")
-    .method("getDstZoneName",   &bdtTz::getDstZoneName,   "get DST zone name")
-    .method("getStdZoneName",   &bdtTz::getStdZoneName,   "get standard zone name")
-    .method("hasDst",           &bdtTz::hasDst,           "true if timezone has daylight savings")
-    .method("getPosixString",   &bdtTz::getPosixString,   "get posix time zone representation")
-
     ;
-
 }
-
-
-
-
-
-
-
-



More information about the Rcpp-commits mailing list