Rcpp Version 0.9.10
Date.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
00002 //
00003 // Date.h: Rcpp R/C++ interface class library -- dates
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__Date_h
00023 #define Rcpp__Date_h
00024 
00025 #include <RcppCommon.h>
00026 
00027 namespace Rcpp {
00028 
00029     class Date {
00030     public:     
00031                 Date();
00032                 Date(SEXP s); 
00033                 Date(const int &dt);    // from integer, just like R (with negative dates before Jan 1, 1970)
00034                 Date(const std::string &s, const std::string &fmt="%Y-%m-%d");
00035                 Date(const unsigned int &m, const unsigned int &d, const unsigned int &y); 
00036                 Date(const Date &copy);
00037                 ~Date() {};
00038                 
00039                 int getDate(void) const { return m_d; } 
00040 
00041                 // intra-day useless for date class
00042                 //int getSeconds() const { return m_tm.tm_sec; }
00043                 //int getMinutes() const { return m_tm.tm_min; }
00044                 //int getHours()   const { return m_tm.tm_hour; }
00045                 int getDay()     const { return m_tm.tm_mday; }
00046                 int getMonth()   const { return m_tm.tm_mon + 1; }              // makes it 1 .. 12
00047                 int getYear()    const { return m_tm.tm_year; }                 // does include 1900 (see Date.cpp)
00048                 int getWeekday() const { return m_tm.tm_wday + 1; }     // makes it 1 .. 7
00049                 int getYearday() const { return m_tm.tm_yday + 1; }     // makes it 1 .. 366
00050 
00051                 static const unsigned int QLtoJan1970Offset;                    // Offset between R / Unix epoch date and the QL base date
00052                 static const unsigned int baseYear;                                             // 1900 as per POSIX mktime() et al
00053 
00054                 Date & operator=(const Date &newdate);                                  // copy assignment operator 
00055 
00056                 // Minimal set of date operations.
00057                 friend Date  operator+(const Date &date, int offset);
00058                 friend int   operator-(const Date& date1, const Date& date2);
00059                 friend bool  operator<(const Date &date1, const Date& date2);
00060                 friend bool  operator>(const Date &date1, const Date& date2);
00061                 friend bool  operator==(const Date &date1, const Date& date2);
00062                 friend bool  operator>=(const Date &date1, const Date& date2);
00063                 friend bool  operator<=(const Date &date1, const Date& date2);
00064                 friend bool  operator!=(const Date &date1, const Date& date2);
00065 
00066     private:
00067         int m_d;                                        // day number, relative to epoch of Jan 1, 1970
00068         struct tm m_tm;                         // standard time representation
00069 
00070                 void update_tm();                       // update m_tm based on m_d
00071 
00072                 double mktime00(struct tm &tm) const; // from R's src/main/datetime.c
00073     };
00074 
00075 
00076     // template specialisation for wrap() on the date 
00077     template <> SEXP wrap<Rcpp::Date>(const Rcpp::Date &date);
00078 
00079     // needed to wrap containers of Date such as vector<Date> or map<string,Date>
00080     namespace internal {
00081                 template<> inline double caster<Rcpp::Date,double>( Rcpp::Date from){
00082                         return static_cast<double>( from.getDate() ) ;
00083                 }
00084                 template<> inline Rcpp::Date caster<double,Rcpp::Date>( double from){
00085                         return Rcpp::Date( static_cast<int>( from ) ) ;
00086                 }
00087     }
00088     
00089     template<> inline SEXP wrap_extra_steps<Rcpp::Date>( SEXP x ){
00090         Rf_setAttrib( x, R_ClassSymbol, Rf_mkString( "Date" ) ) ;
00091         return x ;
00092     }
00093         
00094 }
00095 
00096 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines