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; -*-
2
2
//
3
3
// Datetime.h: Rcpp R/C++ interface class library -- Datetime (POSIXct)
4
4
//
5
- // Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
5
+ // Copyright (C) 2010 - 2015 Dirk Eddelbuettel and Romain Francois
6
6
//
7
7
// This file is part of Rcpp.
8
8
//
@@ -28,47 +28,47 @@ namespace Rcpp {
28
28
29
29
class Datetime {
30
30
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) ; }
67
67
68
68
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)
72
72
73
73
// update m_tm based on m_dt
74
74
void update_tm () {
@@ -78,10 +78,10 @@ namespace Rcpp {
78
78
// m_us is fractional (micro)secs as diff. between (fractional) m_dt and m_tm
79
79
m_us = static_cast <int >(::Rf_fround ( (m_dt - t) * 1.0e6 , 0.0 ));
80
80
} else {
81
- m_dt = NA_REAL; // NaN and Inf need it set
81
+ m_dt = NA_REAL; // NaN and Inf need it set
82
82
m_tm.tm_sec = m_tm.tm_min = m_tm.tm_hour = m_tm.tm_isdst = NA_INTEGER;
83
83
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;
85
85
}
86
86
}
87
87
@@ -93,23 +93,23 @@ namespace Rcpp {
93
93
94
94
// needed to wrap containers of Date such as vector<Datetime> or map<string,Datetime>
95
95
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
+ }
102
102
}
103
103
104
- template <> SEXP wrap_extra_steps<Rcpp::Datetime>( SEXP x ) ;
104
+ template <> SEXP wrap_extra_steps<Rcpp::Datetime>(SEXP x) ;
105
105
106
106
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;
113
113
}
114
114
115
115
inline double operator -(const Datetime& d1, const Datetime& d2) { return d1.m_dt - d2.m_dt ; }
0 commit comments