1- // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
1+ // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
22//
33// Datetime.h: Rcpp R/C++ interface class library -- Datetime (POSIXct)
44//
5- // Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
5+ // Copyright (C) 2010 - 2015 Dirk Eddelbuettel and Romain Francois
66//
77// This file is part of Rcpp.
88//
@@ -28,47 +28,47 @@ namespace Rcpp {
2828
2929 class Datetime {
3030 public:
31- Datetime () {
32- m_dt = 0 ;
33- update_tm ();
34- }
35- Datetime (SEXP s);
36-
37- // from double, just like POSIXct
38- Datetime (const double &dt){
39- m_dt = dt;
40- update_tm ();
41- }
42- Datetime (const std::string &s, const std::string &fmt=" %Y-%m-%d %H:%M:%OS" );
43-
44- double getFractionalTimestamp (void ) const { return m_dt; }
45-
46- int getMicroSeconds () const { return m_us; }
47- int getSeconds () const { return m_tm.tm_sec ; }
48- int getMinutes () const { return m_tm.tm_min ; }
49- int getHours () const { return m_tm.tm_hour ; }
50- int getDay () const { return m_tm.tm_mday ; }
51- int getMonth () const { return m_tm.tm_mon + 1 ; } // makes it 1 .. 12
52- int getYear () const { return m_tm.tm_year ; }
53- int getWeekday () const { return m_tm.tm_wday + 1 ; } // makes it 1 .. 7
54- int getYearday () const { return m_tm.tm_yday + 1 ; } // makes it 1 .. 366
55-
56- // Minimal set of date operations.
57- friend Datetime operator +(const Datetime &dt, double offset);
58- friend double 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- friend bool operator ==(const Datetime &dt1, const Datetime& dt2);
62- friend bool operator >=(const Datetime &dt1, const Datetime& dt2);
63- friend bool operator <=(const Datetime &dt1, const Datetime& dt2);
64- friend bool operator !=(const Datetime &dt1, const Datetime& dt2);
65-
66- inline int is_na () const { return traits::is_na<REALSXP>( m_dt ) ; }
31+ Datetime () {
32+ m_dt = 0 ;
33+ update_tm ();
34+ }
35+ Datetime (SEXP s);
36+
37+ // from double, just like POSIXct
38+ Datetime (const double &dt) {
39+ m_dt = dt;
40+ update_tm ();
41+ }
42+ Datetime (const std::string &s, const std::string &fmt=" %Y-%m-%d %H:%M:%OS" );
43+
44+ double getFractionalTimestamp (void ) const { return m_dt; }
45+
46+ int getMicroSeconds () const { return m_us; }
47+ int getSeconds () const { return m_tm.tm_sec ; }
48+ int getMinutes () const { return m_tm.tm_min ; }
49+ int getHours () const { return m_tm.tm_hour ; }
50+ int getDay () const { return m_tm.tm_mday ; }
51+ int getMonth () const { return m_tm.tm_mon + 1 ; } // makes it 1 .. 12
52+ int getYear () const { return m_tm.tm_year ; }
53+ int getWeekday () const { return m_tm.tm_wday + 1 ; } // makes it 1 .. 7
54+ int getYearday () const { return m_tm.tm_yday + 1 ; } // makes it 1 .. 366
55+
56+ // Minimal set of date operations.
57+ friend Datetime operator +( const Datetime &dt, double offset);
58+ friend double 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+ friend bool operator ==(const Datetime &dt1, const Datetime& dt2);
62+ friend bool operator >=(const Datetime &dt1, const Datetime& dt2);
63+ friend bool operator <=(const Datetime &dt1, const Datetime& dt2);
64+ friend bool operator !=(const Datetime &dt1, const Datetime& dt2);
65+
66+ inline int is_na () const { return traits::is_na<REALSXP>(m_dt) ; }
6767
6868 private:
69- double m_dt; // fractional seconds since epoch
70- struct tm m_tm; // standard time representation
71- unsigned int m_us; // microsecond (to complement m_tm)
69+ double m_dt; // fractional seconds since epoch
70+ struct tm m_tm; // standard time representation
71+ unsigned int m_us; // microsecond (to complement m_tm)
7272
7373 // update m_tm based on m_dt
7474 void update_tm () {
@@ -78,10 +78,10 @@ namespace Rcpp {
7878 // m_us is fractional (micro)secs as diff. between (fractional) m_dt and m_tm
7979 m_us = static_cast <int >(::Rf_fround ( (m_dt - t) * 1.0e6 , 0.0 ));
8080 } else {
81- m_dt = NA_REAL; // NaN and Inf need it set
81+ m_dt = NA_REAL; // NaN and Inf need it set
8282 m_tm.tm_sec = m_tm.tm_min = m_tm.tm_hour = m_tm.tm_isdst = NA_INTEGER;
8383 m_tm.tm_min = m_tm.tm_hour = m_tm.tm_mday = m_tm.tm_mon = m_tm.tm_year = NA_INTEGER;
84- m_us = NA_INTEGER;
84+ m_us = NA_INTEGER;
8585 }
8686 }
8787
@@ -93,23 +93,23 @@ namespace Rcpp {
9393
9494 // needed to wrap containers of Date such as vector<Datetime> or map<string,Datetime>
9595 namespace internal {
96- template <> inline double caster<Rcpp::Datetime,double >( Rcpp::Datetime from){
97- return from.getFractionalTimestamp () ;
98- }
99- template <> inline Rcpp::Datetime caster<double ,Rcpp::Datetime>( double from){
100- return Rcpp::Datetime ( from ) ;
101- }
96+ template <> inline double caster<Rcpp::Datetime,double >(Rcpp::Datetime from) {
97+ return from.getFractionalTimestamp ();
98+ }
99+ template <> inline Rcpp::Datetime caster<double ,Rcpp::Datetime>(double from) {
100+ return Rcpp::Datetime ( from );
101+ }
102102 }
103103
104- template <> SEXP wrap_extra_steps<Rcpp::Datetime>( SEXP x ) ;
104+ template <> SEXP wrap_extra_steps<Rcpp::Datetime>(SEXP x) ;
105105
106106 inline Datetime operator +(const Datetime &datetime, double offset) {
107- Datetime newdt (datetime.m_dt );
108- newdt.m_dt += offset;
109- time_t t = static_cast <time_t >(std::floor (newdt.m_dt ));
110- newdt.m_tm = *gmtime_ (&t);
111- newdt.m_us = static_cast <int >(::Rf_fround ( (newdt.m_dt - t) * 1.0e6 , 0.0 ));
112- return newdt;
107+ Datetime newdt (datetime.m_dt );
108+ newdt.m_dt += offset;
109+ time_t t = static_cast <time_t >(std::floor (newdt.m_dt ));
110+ newdt.m_tm = *gmtime_ (&t);
111+ newdt.m_us = static_cast <int >(::Rf_fround ( (newdt.m_dt - t) * 1.0e6 , 0.0 ));
112+ return newdt;
113113 }
114114
115115 inline double operator -(const Datetime& d1, const Datetime& d2) { return d1.m_dt - d2.m_dt ; }
0 commit comments