00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- 00002 // 00003 // RcppDatetime.h: Rcpp R/C++ interface class library -- Datetime type support 00004 // 00005 // Copyright (C) 2008 - 2009 Dirk Eddelbuettel 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 RcppDatetime_h 00023 #define RcppDatetime_h 00024 00025 #include <RcppCommon.h> 00026 00027 class RcppDatetime { 00028 private: 00029 double m_d; 00030 bool m_parsed; 00031 int m_us; // microseconds giving us fractional seconds 00032 struct tm m_tm; 00033 void parseTime(); 00034 00035 protected: 00036 friend class ColDatum; 00037 00038 public: 00039 RcppDatetime(void); 00040 RcppDatetime(const double d); 00041 00042 double getFractionalTimestamp(void) const { return m_d; } 00043 int getYear(void) { if (!m_parsed) parseTime(); return m_tm.tm_year + 1900; } 00044 int getMonth(void) { if (!m_parsed) parseTime(); return m_tm.tm_mon + 1; } 00045 int getDay(void) { if (!m_parsed) parseTime(); return m_tm.tm_mday; } 00046 int getWeekday(void) { if (!m_parsed) parseTime(); return m_tm.tm_wday; } 00047 int getHour(void) { if (!m_parsed) parseTime(); return m_tm.tm_hour; } 00048 int getMinute(void) { if (!m_parsed) parseTime(); return m_tm.tm_min; } 00049 int getSecond(void) { if (!m_parsed) parseTime(); return m_tm.tm_sec; } 00050 int getMicroSec(void) { if (!m_parsed) parseTime(); return m_us; } 00051 00052 friend double operator-(const RcppDatetime& dt1, const RcppDatetime& dt2) { return dt2.m_d - dt1.m_d; } 00053 friend bool operator<(const RcppDatetime &dt1, const RcppDatetime& dt2) { return dt1.m_d < dt2.m_d; } 00054 friend bool operator<=(const RcppDatetime &dt1, const RcppDatetime& dt2) { return dt1.m_d <= dt2.m_d; } 00055 friend bool operator>(const RcppDatetime &dt1, const RcppDatetime& dt2) { return dt1.m_d > dt2.m_d; } 00056 friend bool operator>=(const RcppDatetime &dt1, const RcppDatetime& dt2) { return dt1.m_d >= dt2.m_d; } 00057 friend bool operator==(const RcppDatetime &dt1, const RcppDatetime& dt2) { return dt1.m_d == dt2.m_d; } // remember float math... 00058 friend std::ostream& operator<<(std::ostream& os, const RcppDatetime &datetime); 00059 friend RcppDatetime operator+(const RcppDatetime &date, double offset); 00060 }; 00061 00062 #endif