Skip to content

Commit 08a2cff

Browse files
committed
added format() methods for Date and Datetime
rolled dev version in config.h
1 parent 3bdd1f2 commit 08a2cff

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

ChangeLog

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
2016-11-27 Dirk Eddelbuettel <edd@debian.org>
2+
3+
* inst/include/Rcpp/date_datetime/Date.h (format, operator<<): Added
4+
* inst/include/Rcpp/date_datetime/Datetime.h (format, operator<<): Ditto
5+
6+
2016-11-23 Dirk Eddelbuettel <edd@debian.org>
7+
8+
* inst/include/Rcpp/config.h (RCPP_DEV_VERSION): Set minor version
9+
110
2016-11-22 Dirk Eddelbuettel <edd@debian.org>
211

312
* DESCRIPTION (Date, Version): roll minor version
413

514
* inst/include/Rcpp/date_datetime/newDatetimeVector.h (Rcpp): Small
615
correction concerning timezone attribute to ctor from RTYPE
16+
717
2016-11-22 Jim Hester <james.f.hester@gmail.com>
818

919
* inst/src/api.cpp: Cleanup to stack message parseing

inst/NEWS.Rd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
\itemize{
99
\item The exception stack message is now correctly demangled on all
1010
compiler versions (Jim Hester in \ghpr{598})
11+
\item Date and Datetime object now have format methods
1112
}
1213
\item Changes in Rcpp unit tests
1314
\itemize{

inst/include/Rcpp/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#define RCPP_VERSION Rcpp_Version(0,12,8)
3131

3232
// the current source snapshot
33-
#define RCPP_DEV_VERSION RcppDevVersion(0,12,8,0)
33+
#define RCPP_DEV_VERSION RcppDevVersion(0,12,8,1)
3434

3535
#endif
3636

inst/include/Rcpp/date_datetime/Date.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,20 @@ namespace Rcpp {
102102
return m_d;
103103
}
104104

105+
inline std::string format(const char *fmt = "%Y-%m-%d") const {
106+
char txt[32];
107+
struct tm temp = m_tm;
108+
temp.tm_year -= baseYear(); // adjust for fact that system has year rel. to 1900
109+
::strftime(txt, 31, fmt, &temp);
110+
return std::string(txt);
111+
}
112+
113+
inline std::ostream &operator<<(std::ostream & s) const {
114+
s << this->format() << std::endl;
115+
return s;
116+
}
117+
118+
105119
private:
106120
double m_d; // (fractional) day number, relative to epoch of Jan 1, 1970
107121
struct tm m_tm; // standard time representation

inst/include/Rcpp/date_datetime/Datetime.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ namespace Rcpp {
6868

6969
operator double() const { return m_dt; }
7070

71+
inline std::string format(const char *fmt = "%Y-%m-%d %H:%M:%S") const {
72+
char txtiso[64], txtsec[64];
73+
time_t t = static_cast<time_t>(std::floor(m_dt));
74+
struct tm temp = *localtime(&t); // localtime, not gmtime
75+
::strftime(txtiso, 63, fmt, &temp);
76+
::snprintf(txtsec, 63, "%s.%06d", txtiso, m_us);
77+
return std::string(txtsec);
78+
}
79+
80+
inline std::ostream &operator<<(std::ostream & s) const {
81+
s << this->format() << std::endl;
82+
return s;
83+
}
84+
7185
private:
7286
double m_dt; // fractional seconds since epoch
7387
struct tm m_tm; // standard time representation
@@ -88,6 +102,11 @@ namespace Rcpp {
88102
}
89103
}
90104

105+
// 1900 as per POSIX mktime() et al
106+
static inline unsigned int baseYear() {
107+
return 1900;
108+
}
109+
91110
};
92111

93112

@@ -132,6 +151,7 @@ namespace Rcpp {
132151
inline bool operator<=(const Datetime &d1, const Datetime& d2) { return d1.m_dt <= d2.m_dt; }
133152
inline bool operator!=(const Datetime &d1, const Datetime& d2) { return d1.m_dt != d2.m_dt; }
134153

154+
135155
}
136156

137157
#endif

0 commit comments

Comments
 (0)