File: Datetime.h

package info (click to toggle)
rcpp 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,480 kB
  • sloc: cpp: 27,436; ansic: 7,778; sh: 53; makefile: 2
file content (173 lines) | stat: -rw-r--r-- 7,021 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// Datetime.h: Rcpp R/C++ interface class library -- Datetime (POSIXct)
//
// Copyright (C) 2010 - 2016  Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
// Rcpp is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Rcpp is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.

#ifndef Rcpp__Datetime_h
#define Rcpp__Datetime_h

#include <RcppCommon.h>

#if defined(WIN32) || defined(__WIN32) || defined(__WIN32__)
#include <time.h>
#endif

namespace Rcpp {

    class Datetime {
    public:
        Datetime() {
            m_dt = 0;
            update_tm();
        }
        Datetime(SEXP s);

        // from double, just like POSIXct
        Datetime(const double &dt) {
            m_dt = dt;
            update_tm();
        }
        Datetime(const std::string &s, const std::string &fmt="%Y-%m-%d %H:%M:%OS");

        double getFractionalTimestamp(void) const { return m_dt; }

        int getMicroSeconds() const { return m_us; }
        int getSeconds()      const { return m_tm.tm_sec; }
        int getMinutes()      const { return m_tm.tm_min; }
        int getHours()        const { return m_tm.tm_hour; }
        int getDay()          const { return m_tm.tm_mday; }
        int getMonth()        const { return m_tm.tm_mon + 1; }      // makes it 1 .. 12
        int getYear()         const { return m_tm.tm_year ; }
        int getWeekday()      const { return m_tm.tm_wday + 1; }     // makes it 1 .. 7
        int getYearday()      const { return m_tm.tm_yday + 1; }     // makes it 1 .. 366

        // Minimal set of date operations.
        friend Datetime  operator+( const Datetime &dt, double offset);
        friend Datetime  operator+( const Datetime &dt, int offset);
        friend double    operator-( const Datetime &dt1, const Datetime& dt2);
        friend bool      operator<( const Datetime &dt1, const Datetime& dt2);
        friend bool      operator>( const Datetime &dt1, const Datetime& dt2);
        friend bool      operator==(const Datetime &dt1, const Datetime& dt2);
        friend bool      operator>=(const Datetime &dt1, const Datetime& dt2);
        friend bool      operator<=(const Datetime &dt1, const Datetime& dt2);
        friend bool      operator!=(const Datetime &dt1, const Datetime& dt2);

        inline int is_na() const { return traits::is_na<REALSXP>(m_dt); }

        operator double()  const { return m_dt; }

        inline std::string format(const char *fmt = "%Y-%m-%d %H:%M:%S") const {
            char txtiso[64], txtsec[64];
            time_t t = static_cast<time_t>(std::floor(m_dt));
            struct tm temp = *localtime(&t); // localtime, not gmtime
            size_t res = ::strftime(txtiso, 63, fmt, &temp);
            if (res == 0) {
                return std::string("");
            } else {
                res = ::snprintf(txtsec, 63, "%s.%06d", txtiso, m_us);
                if (res <= 0) {
                    return std::string("");
                } else {
                    return std::string(txtsec);
                }
            }
        }

        friend inline std::ostream &operator<<(std::ostream & s, const Datetime d);

    private:
        double m_dt;            // fractional seconds since epoch
        struct tm m_tm;         // standard time representation
        unsigned int m_us;      // microsecond (to complement m_tm)

        // update m_tm based on m_dt
        void update_tm() {
            if (R_FINITE(m_dt)) {
                double dt = std::floor(m_dt);
                time_t t = static_cast<time_t>(dt);
                m_tm = *gmtime_(&t);
                // m_us is fractional (micro)secs as diff. between (fractional) m_dt and m_tm
                m_us = static_cast<int>(::Rf_fround( (m_dt - dt) * 1.0e6, 0.0));
            } else {
                m_dt = NA_REAL;         // NaN and Inf need it set
                m_tm.tm_sec = m_tm.tm_min = m_tm.tm_hour = m_tm.tm_isdst = NA_INTEGER;
                m_tm.tm_min = m_tm.tm_hour = m_tm.tm_mday = m_tm.tm_mon  = m_tm.tm_year = NA_INTEGER;
                m_us = NA_INTEGER;
            }
        }

        // 1900 as per POSIX mktime() et al
        static inline unsigned int baseYear() {
            return 1900;
        }

    };


    // template specialisation for wrap() on datetime
    template <> SEXP wrap<Rcpp::Datetime>(const Rcpp::Datetime &dt);

    // needed to wrap containers of Date such as vector<Datetime> or map<string,Datetime>
    namespace internal {
        template<> inline double caster<Rcpp::Datetime,double>(Rcpp::Datetime from) {
            return from.getFractionalTimestamp();
        }
        template<> inline Rcpp::Datetime caster<double,Rcpp::Datetime>(double from) {
            return Rcpp::Datetime( from );
        }
    }

    template<> SEXP wrap_extra_steps<Rcpp::Datetime>(SEXP x);

    inline Datetime operator+(const Datetime &datetime, double offset) {
        Datetime newdt(datetime.m_dt);
        newdt.m_dt += offset;
        double dt = std::floor(newdt.m_dt);
        time_t t = static_cast<time_t>(dt);
        newdt.m_tm = *gmtime_(&t);
        newdt.m_us = static_cast<int>(::Rf_fround( (newdt.m_dt - dt) * 1.0e6, 0.0));
        return newdt;
    }

    inline Datetime operator+(const Datetime &datetime, int offset) {
        Datetime newdt(datetime.m_dt);
        newdt.m_dt += offset;
        double dt = std::floor(newdt.m_dt);
        time_t t = static_cast<time_t>(dt);
        newdt.m_tm = *gmtime_(&t);
        newdt.m_us = static_cast<int>(::Rf_fround( (newdt.m_dt - dt) * 1.0e6, 0.0));
        return newdt;
    }

    inline double  operator-(const Datetime& d1, const Datetime& d2) { return d1.m_dt - d2.m_dt; }
    inline bool    operator<(const Datetime &d1, const Datetime& d2) { return d1.m_dt < d2.m_dt; }
    inline bool    operator>(const Datetime &d1, const Datetime& d2) { return d1.m_dt > d2.m_dt; }
    inline bool    operator==(const Datetime &d1, const Datetime& d2) { return d1.m_dt == d2.m_dt; }
    inline bool    operator>=(const Datetime &d1, const Datetime& d2) { return d1.m_dt >= d2.m_dt; }
    inline bool    operator<=(const Datetime &d1, const Datetime& d2) { return d1.m_dt <= d2.m_dt; }
    inline bool    operator!=(const Datetime &d1, const Datetime& d2) { return d1.m_dt != d2.m_dt; }

    inline std::ostream &operator<<(std::ostream & os, const Datetime d) {
        os << d.format();
        return os;
    }

}

#endif