Rcpp Version 1.1.2
Loading...
Searching...
No Matches
Datetime.h
Go to the documentation of this file.
1
2// Datetime.h: Rcpp R/C++ interface class library -- Datetime (POSIXct)
3//
4// Copyright (C) 2010 - 2026 Dirk Eddelbuettel and Romain Francois
5//
6// This file is part of Rcpp.
7//
8// Rcpp is free software: you can redistribute it and/or modify it
9// under the terms of the GNU General Public License as published by
10// the Free Software Foundation, either version 2 of the License, or
11// (at your option) any later version.
12//
13// Rcpp is distributed in the hope that it will be useful, but
14// WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20
21#ifndef Rcpp__Datetime_h
22#define Rcpp__Datetime_h
23
24#include <RcppCommon.h>
25
26#if defined(WIN32) || defined(__WIN32) || defined(__WIN32__)
27#include <time.h>
28#endif
29
30namespace Rcpp {
31
32 class Datetime {
33 public:
35 m_dt = 0;
36 update_tm();
37 }
38 Datetime(SEXP s);
39
40 // from double, just like POSIXct
41 Datetime(const double &dt) {
42 m_dt = dt;
43 update_tm();
44 }
45 Datetime(const std::string &s, const std::string &fmt="%Y-%m-%d %H:%M:%OS");
46
47 double getFractionalTimestamp(void) const { return m_dt; }
48
49 int getMicroSeconds() const { return m_us; }
50 int getSeconds() const { return m_tm.tm_sec; }
51 int getMinutes() const { return m_tm.tm_min; }
52 int getHours() const { return m_tm.tm_hour; }
53 int getDay() const { return m_tm.tm_mday; }
54 int getMonth() const { return m_tm.tm_mon + 1; } // makes it 1 .. 12
55 int getYear() const { return m_tm.tm_year ; }
56 int getWeekday() const { return m_tm.tm_wday + 1; } // makes it 1 .. 7
57 int getYearday() const { return m_tm.tm_yday + 1; } // makes it 1 .. 366
58
59 // Minimal set of date operations.
60 friend Datetime operator+( const Datetime &dt, double offset);
61 friend Datetime operator+( const Datetime &dt, int offset);
62 friend double 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 friend bool operator==(const Datetime &dt1, const Datetime& dt2);
66 friend bool operator>=(const Datetime &dt1, const Datetime& dt2);
67 friend bool operator<=(const Datetime &dt1, const Datetime& dt2);
68 friend bool operator!=(const Datetime &dt1, const Datetime& dt2);
69
70 inline int is_na() const { return traits::is_na<REALSXP>(m_dt); }
71
72 operator double() const { return m_dt; }
73
74 inline std::string format(const char *fmt = "%Y-%m-%d %H:%M:%S") const {
75 char txtiso[64], txtsec[64];
76 time_t t = static_cast<time_t>(std::floor(m_dt));
77 struct tm temp = *localtime(&t); // localtime, not gmtime
78 size_t res = ::strftime(txtiso, 63, fmt, &temp);
79 if (res == 0) {
80 return std::string("");
81 } else {
82 res = ::snprintf(txtsec, 63, "%s.%06d", txtiso, m_us);
83 if (res <= 0) {
84 return std::string("");
85 } else {
86 return std::string(txtsec);
87 }
88 }
89 }
90
91 friend inline std::ostream &operator<<(std::ostream & s, const Datetime d);
92
93 private:
94 double m_dt; // fractional seconds since epoch
95 struct tm m_tm; // standard time representation
96 int m_us; // microsecond (to complement m_tm)
97
98 // update m_tm based on m_dt
99 void update_tm() {
100 if (R_FINITE(m_dt)) {
101 double dt = std::floor(m_dt);
102 time_t t = static_cast<time_t>(dt);
103 m_tm = *gmtime_(&t);
104 // m_us is fractional (micro)secs as diff. between (fractional) m_dt and m_tm
105 m_us = static_cast<int>(::Rf_fround( (m_dt - dt) * 1.0e6, 0.0));
106 } else {
107 m_dt = NA_REAL; // NaN and Inf need it set
108 m_tm.tm_sec = m_tm.tm_min = m_tm.tm_hour = m_tm.tm_isdst = NA_INTEGER;
109 m_tm.tm_min = m_tm.tm_hour = m_tm.tm_mday = m_tm.tm_mon = m_tm.tm_year = NA_INTEGER;
110 m_us = NA_INTEGER;
111 }
112 }
113
114 // 1900 as per POSIX mktime() et al
115 static inline unsigned int baseYear() {
116 return 1900;
117 }
118
119 };
120
121
122 // template specialisation for wrap() on datetime
123 template <> SEXP wrap<Rcpp::Datetime>(const Rcpp::Datetime &dt);
124
125 // needed to wrap containers of Date such as vector<Datetime> or map<string,Datetime>
126 namespace internal {
127 template<> inline double caster<Rcpp::Datetime,double>(Rcpp::Datetime from) {
128 return from.getFractionalTimestamp();
129 }
130 template<> inline Rcpp::Datetime caster<double,Rcpp::Datetime>(double from) {
131 return Rcpp::Datetime( from );
132 }
133 }
134
135 template<> SEXP wrap_extra_steps<Rcpp::Datetime>(SEXP x);
136
137 inline Datetime operator+(const Datetime &datetime, double offset) {
138 Datetime newdt(datetime.m_dt);
139 newdt.m_dt += offset;
140 double dt = std::floor(newdt.m_dt);
141 time_t t = static_cast<time_t>(dt);
142 newdt.m_tm = *gmtime_(&t);
143 newdt.m_us = static_cast<int>(::Rf_fround( (newdt.m_dt - dt) * 1.0e6, 0.0));
144 return newdt;
145 }
146
147 inline Datetime operator+(const Datetime &datetime, int offset) {
148 Datetime newdt(datetime.m_dt);
149 newdt.m_dt += offset;
150 double dt = std::floor(newdt.m_dt);
151 time_t t = static_cast<time_t>(dt);
152 newdt.m_tm = *gmtime_(&t);
153 newdt.m_us = static_cast<int>(::Rf_fround( (newdt.m_dt - dt) * 1.0e6, 0.0));
154 return newdt;
155 }
156
157 inline double operator-(const Datetime& d1, const Datetime& d2) { return d1.m_dt - d2.m_dt; }
158 inline bool operator<(const Datetime &d1, const Datetime& d2) { return d1.m_dt < d2.m_dt; }
159 inline bool operator>(const Datetime &d1, const Datetime& d2) { return d1.m_dt > d2.m_dt; }
160 inline bool operator==(const Datetime &d1, const Datetime& d2) { return d1.m_dt == d2.m_dt; }
161 inline bool operator>=(const Datetime &d1, const Datetime& d2) { return d1.m_dt >= d2.m_dt; }
162 inline bool operator<=(const Datetime &d1, const Datetime& d2) { return d1.m_dt <= d2.m_dt; }
163 inline bool operator!=(const Datetime &d1, const Datetime& d2) { return d1.m_dt != d2.m_dt; }
164
165 inline std::ostream &operator<<(std::ostream & os, const Datetime d) {
166 os << d.format();
167 return os;
168 }
169
170}
171
172#endif
double m_dt
Definition Datetime.h:94
int getMicroSeconds() const
Definition Datetime.h:49
friend bool operator>(const Datetime &dt1, const Datetime &dt2)
Definition Datetime.h:159
friend bool operator==(const Datetime &dt1, const Datetime &dt2)
Definition Datetime.h:160
friend bool operator<=(const Datetime &dt1, const Datetime &dt2)
Definition Datetime.h:162
double getFractionalTimestamp(void) const
Definition Datetime.h:47
int getMonth() const
Definition Datetime.h:54
int getYear() const
Definition Datetime.h:55
friend std::ostream & operator<<(std::ostream &s, const Datetime d)
Definition Datetime.h:165
void update_tm()
Definition Datetime.h:99
friend double operator-(const Datetime &dt1, const Datetime &dt2)
Definition Datetime.h:157
static unsigned int baseYear()
Definition Datetime.h:115
std::string format(const char *fmt="%Y-%m-%d %H:%M:%S") const
Definition Datetime.h:74
int getSeconds() const
Definition Datetime.h:50
struct tm m_tm
Definition Datetime.h:95
int is_na() const
Definition Datetime.h:70
int getMinutes() const
Definition Datetime.h:51
friend bool operator>=(const Datetime &dt1, const Datetime &dt2)
Definition Datetime.h:161
friend Datetime operator+(const Datetime &dt, double offset)
Definition Datetime.h:137
Datetime(const double &dt)
Definition Datetime.h:41
int getDay() const
Definition Datetime.h:53
int getYearday() const
Definition Datetime.h:57
int getWeekday() const
Definition Datetime.h:56
int getHours() const
Definition Datetime.h:52
friend bool operator<(const Datetime &dt1, const Datetime &dt2)
Definition Datetime.h:158
friend bool operator!=(const Datetime &dt1, const Datetime &dt2)
Definition Datetime.h:163
internal implementation details
Definition Date.h:43
double caster< Rcpp::Datetime, double >(Rcpp::Datetime from)
Definition Datetime.h:127
Rcpp::Datetime caster< double, Rcpp::Datetime >(double from)
Definition Datetime.h:130
bool is_na< REALSXP >(double x)
Definition is_na.h:42
Rcpp API.
Definition algo.h:28
std::ostream & operator<<(std::ostream &os, const Date d)
Definition Date.h:172
bool operator>=(const Date &d1, const Date &d2)
Definition Date.h:168
bool operator!=(const Date &d1, const Date &d2)
Definition Date.h:170
double operator-(const Date &d1, const Date &d2)
Definition Date.h:164
static struct tm tm
Definition date.cpp:435
bool operator>(const Date &d1, const Date &d2)
Definition Date.h:166
attribute_hidden struct tm * gmtime_(const time_t *const x)
Definition routines.h:136
SEXP wrap< Rcpp::Datetime >(const Rcpp::Datetime &dt)
SEXP wrap_extra_steps< Rcpp::Datetime >(SEXP)
Definition Datetime.h:38
bool operator<(const Date &d1, const Date &d2)
Definition Date.h:165
bool operator==(const Date &d1, const Date &d2)
Definition Date.h:167
bool operator<=(const Date &d1, const Date &d2)
Definition Date.h:169
Date operator+(const Date &date, int offset)
Definition Date.h:156