Rcpp Version 0.9.10
Datetime.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
00002 //
00003 // Datetime.h: Rcpp R/C++ interface class library -- Datetime (POSIXct)
00004 //
00005 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
00006 //
00007 // This file is part of Rcpp.
00008 //
00009 // Rcpp is free software: you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 2 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // Rcpp is distributed in the hope that it will be useful, but
00015 // WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
00021 
00022 #ifndef Rcpp__Datetime_h
00023 #define Rcpp__Datetime_h
00024 
00025 #include <RcppCommon.h>
00026 
00027 namespace Rcpp {
00028 
00029     class Datetime {
00030     public:     
00031                 Datetime();
00032                 Datetime(SEXP s); 
00033                 Datetime(const double &dt);     // from double, just like POSIXct
00034                 Datetime(const std::string &s, const std::string &fmt="%Y-%m-%d %H:%M:%OS");
00035                 Datetime(const Datetime &copy);
00036                 ~Datetime() {};
00037                 
00038                 double getFractionalTimestamp(void) const { return m_dt; } 
00039 
00040                 int getMicroSeconds() const { return m_us; }
00041                 int getSeconds()      const { return m_tm.tm_sec; }
00042                 int getMinutes()      const { return m_tm.tm_min; }
00043                 int getHours()        const { return m_tm.tm_hour; }
00044                 int getDay()          const { return m_tm.tm_mday; }
00045                 int getMonth()        const { return m_tm.tm_mon + 1; }          // makes it 1 .. 12
00046                 int getYear()         const { return m_tm.tm_year + 1900; }
00047                 int getWeekday()      const { return m_tm.tm_wday + 1; }         // makes it 1 .. 7
00048                 int getYearday()      const { return m_tm.tm_yday + 1; }     // makes it 1 .. 366
00049 
00050                 Datetime & operator=(const Datetime &newdt);            // copy assignment operator 
00051 
00052                 // Minimal set of date operations.
00053                 friend Datetime  operator+(const Datetime &dt, double offset);
00054                 friend double    operator-(const Datetime& dt1, const Datetime& dt2);
00055                 friend bool      operator<(const Datetime &dt1, const Datetime& dt2);
00056                 friend bool      operator>(const Datetime &dt1, const Datetime& dt2);
00057                 friend bool      operator==(const Datetime &dt1, const Datetime& dt2);
00058                 friend bool      operator>=(const Datetime &dt1, const Datetime& dt2);
00059                 friend bool      operator<=(const Datetime &dt1, const Datetime& dt2);
00060                 friend bool      operator!=(const Datetime &dt1, const Datetime& dt2);
00061 
00062     private:
00063         double m_dt;                            // fractional seconds since epoch
00064         struct tm m_tm;                         // standard time representation
00065                 unsigned int m_us;                      // microsecond (to complement m_tm)
00066 
00067                 void update_tm();                       // update m_tm based on m_dt
00068 
00069     };
00070 
00071 
00072     // template specialisation for wrap() on datetime
00073     template <> SEXP wrap<Rcpp::Datetime>(const Rcpp::Datetime &dt);
00074 
00075     // needed to wrap containers of Date such as vector<Datetime> or map<string,Datetime>
00076     namespace internal {
00077                 template<> inline double caster<Rcpp::Datetime,double>( Rcpp::Datetime from){
00078                         return from.getFractionalTimestamp() ;
00079                 }
00080                 template<> inline Rcpp::Datetime caster<double,Rcpp::Datetime>( double from){
00081                         return Rcpp::Datetime( from ) ;
00082                 }
00083     }
00084     
00085     template<> inline SEXP wrap_extra_steps<Rcpp::Datetime>( SEXP x ){
00086                 Rf_setAttrib(x, R_ClassSymbol, internal::getPosixClasses() ); 
00087                 return x ;
00088     }
00089         
00090 }
00091 
00092 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines