|
Rcpp Version 0.9.10
|
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 // The mktime00() function is 00008 // Copyright (C) 2000 - 2010 The R Development Core Team. 00009 // 00010 // This file is part of Rcpp. 00011 // 00012 // Rcpp is free software: you can redistribute it and/or modify it 00013 // under the terms of the GNU General Public License as published by 00014 // the Free Software Foundation, either version 2 of the License, or 00015 // (at your option) any later version. 00016 // 00017 // Rcpp is distributed in the hope that it will be useful, but 00018 // WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of the GNU General Public License 00023 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00024 00025 #include <Rcpp/Datetime.h> 00026 #include <Rcpp/Function.h> 00027 #include <Rmath.h> // for Rf_fround 00028 #include <time.h> // for gmtime 00029 00030 namespace Rcpp { 00031 00032 Datetime::Datetime() { 00033 m_dt = 0; 00034 update_tm(); 00035 } 00036 00037 Datetime::Datetime(SEXP d) { 00038 m_dt = Rcpp::as<double>(d); 00039 update_tm(); 00040 } 00041 00042 Datetime::Datetime(const double &dt) { 00043 m_dt = dt; 00044 update_tm(); 00045 } 00046 00047 Datetime::Datetime(const std::string &s, const std::string &fmt) { 00048 Rcpp::Function strptime("strptime"); // we cheat and call strptime() from R 00049 Rcpp::Function asPOSIXct("as.POSIXct"); // and we need to convert to POSIXct 00050 m_dt = Rcpp::as<double>(asPOSIXct(strptime(s, fmt))); 00051 update_tm(); 00052 } 00053 00054 Datetime::Datetime(const Datetime ©) { 00055 m_dt = copy.m_dt; 00056 m_us = copy.m_us; 00057 m_tm = copy.m_tm; 00058 } 00059 00060 Datetime & Datetime::operator=(const Datetime & newdt) { 00061 if (this != &newdt) { 00062 m_dt = newdt.m_dt; 00063 m_us = newdt.m_us; 00064 m_tm = newdt.m_tm; 00065 } 00066 return *this; 00067 } 00068 00069 void Datetime::update_tm() { 00070 time_t t = static_cast<time_t>(floor(m_dt)); 00071 m_tm = *gmtime(&t); // this may need a Windows fix, re-check R's datetime.c 00072 // m_us is fractional (micro)secs as diff. between (fractional) m_dt and m_tm 00073 m_us = static_cast<int>(::Rf_fround( (m_dt - t) * 1.0e6, 0.0)); 00074 } 00075 00076 Datetime operator+(const Datetime &datetime, double offset) { 00077 Datetime newdt(datetime.m_dt); 00078 newdt.m_dt += offset; 00079 time_t t = static_cast<time_t>(floor(newdt.m_dt)); 00080 newdt.m_tm = *gmtime(&t); // this may need a Windows fix, re-check R's dat 00081 newdt.m_us = static_cast<int>(::Rf_fround( (newdt.m_dt - t) * 1.0e6, 0.0)); 00082 return newdt; 00083 } 00084 00085 double operator-(const Datetime& d1, const Datetime& d2) { return d2.m_dt - d1.m_dt; } 00086 bool operator<(const Datetime &d1, const Datetime& d2) { return d1.m_dt < d2.m_dt; } 00087 bool operator>(const Datetime &d1, const Datetime& d2) { return d1.m_dt > d2.m_dt; } 00088 bool operator==(const Datetime &d1, const Datetime& d2) { return d1.m_dt == d2.m_dt; } 00089 bool operator>=(const Datetime &d1, const Datetime& d2) { return d1.m_dt >= d2.m_dt; } 00090 bool operator<=(const Datetime &d1, const Datetime& d2) { return d1.m_dt <= d2.m_dt; } 00091 bool operator!=(const Datetime &d1, const Datetime& d2) { return d1.m_dt != d2.m_dt; } 00092 00093 template <> SEXP wrap(const Datetime &date) { 00094 return internal::new_posixt_object( date.getFractionalTimestamp() ) ; 00095 } 00096 00097 }