Rcpp Version 0.10.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
Datetime.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
2 //
3 // Datetime.h: Rcpp R/C++ interface class library -- Datetime (POSIXct)
4 //
5 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp__Datetime_h
23 #define Rcpp__Datetime_h
24 
25 #include <RcppCommon.h>
26 
27 namespace Rcpp {
28 
29  class Datetime {
30  public:
31  Datetime();
32  Datetime(SEXP s);
33  Datetime(const double &dt); // from double, just like POSIXct
34  Datetime(const std::string &s, const std::string &fmt="%Y-%m-%d %H:%M:%OS");
35  Datetime(const Datetime &copy);
36  ~Datetime() {};
37 
38  double getFractionalTimestamp(void) const { return m_dt; }
39 
40  int getMicroSeconds() const { return m_us; }
41  int getSeconds() const { return m_tm.tm_sec; }
42  int getMinutes() const { return m_tm.tm_min; }
43  int getHours() const { return m_tm.tm_hour; }
44  int getDay() const { return m_tm.tm_mday; }
45  int getMonth() const { return m_tm.tm_mon + 1; } // makes it 1 .. 12
46  int getYear() const { return m_tm.tm_year + 1900; }
47  int getWeekday() const { return m_tm.tm_wday + 1; } // makes it 1 .. 7
48  int getYearday() const { return m_tm.tm_yday + 1; } // makes it 1 .. 366
49 
50  Datetime & operator=(const Datetime &newdt); // copy assignment operator
51 
52  // Minimal set of date operations.
53  friend Datetime operator+(const Datetime &dt, double offset);
54  friend double operator-(const Datetime& dt1, const Datetime& dt2);
55  friend bool operator<(const Datetime &dt1, const Datetime& dt2);
56  friend bool operator>(const Datetime &dt1, const Datetime& dt2);
57  friend bool operator==(const Datetime &dt1, const Datetime& dt2);
58  friend bool operator>=(const Datetime &dt1, const Datetime& dt2);
59  friend bool operator<=(const Datetime &dt1, const Datetime& dt2);
60  friend bool operator!=(const Datetime &dt1, const Datetime& dt2);
61 
62  private:
63  double m_dt; // fractional seconds since epoch
64  struct tm m_tm; // standard time representation
65  unsigned int m_us; // microsecond (to complement m_tm)
66 
67  void update_tm(); // update m_tm based on m_dt
68 
69  };
70 
71 
72  // template specialisation for wrap() on datetime
73  template <> SEXP wrap<Rcpp::Datetime>(const Rcpp::Datetime &dt);
74 
75  // needed to wrap containers of Date such as vector<Datetime> or map<string,Datetime>
76  namespace internal {
77  template<> inline double caster<Rcpp::Datetime,double>( Rcpp::Datetime from){
78  return from.getFractionalTimestamp() ;
79  }
80  template<> inline Rcpp::Datetime caster<double,Rcpp::Datetime>( double from){
81  return Rcpp::Datetime( from ) ;
82  }
83  }
84 
85  template<> SEXP wrap_extra_steps<Rcpp::Datetime>( SEXP x ) ;
86 
87 }
88 
89 #endif