| 29 | namespace Rcpp { |
| 30 | |
| 31 | class Date { |
| 32 | public: |
| 33 | Date() { |
| 34 | m_d = 0; |
| 35 | update_tm(); |
| 36 | } |
| 37 | Date(SEXP s); |
| 38 | |
| 39 | // from integer (with negative dates before Jan 1, 1970) |
| 40 | Date(const int &dt) { |
| 41 | m_d = dt; |
| 42 | update_tm(); |
| 43 | } |
| 44 | |
| 45 | // from fractional integer since epoch, just like R |
| 46 | Date(const double &dt) { |
| 47 | m_d = dt; |
| 48 | update_tm(); |
| 49 | } |
| 50 | Date(const std::string &s, const std::string &fmt="%Y-%m-%d"); |
| 51 | |
| 52 | Date(const unsigned int &mon, const unsigned int &day, const unsigned int &year) { |
| 53 | m_tm.tm_sec = m_tm.tm_min = m_tm.tm_hour = m_tm.tm_isdst = 0; |
| 54 | |
| 55 | // allow for ISO-notation case (yyyy, mm, dd) which we prefer over (mm, dd, year) |
| 56 | if (mon >= baseYear() && day <= 12 && year <= 31) { |
| 57 | m_tm.tm_year = mon - baseYear(); |
| 58 | m_tm.tm_mon = day - 1; // range 0 to 11 |
| 59 | m_tm.tm_mday = year; |
| 60 | } else { |
| 61 | m_tm.tm_mday = day; |
| 62 | m_tm.tm_mon = mon - 1; // range 0 to 11 |
| 63 | m_tm.tm_year = year - baseYear(); |
| 64 | } |
| 65 | double tmp = mktime00(m_tm); // use mktime() replacement borrowed from R |
| 66 | m_tm.tm_year += baseYear(); // we'd rather keep it as a normal year |
| 67 | m_d = tmp/(24*60*60); |
| 68 | } |
| 69 | |
| 70 | double getDate(void) const { |
| 71 | return m_d; |
| 72 | } |
| 73 | |
| 74 | // intra-day useless for date class |
| 75 | //int getSeconds() const { return m_tm.tm_sec; } |
| 76 | //int getMinutes() const { return m_tm.tm_min; } |
| 77 | //int getHours() const { return m_tm.tm_hour; } |
| 78 | int getDay() const { return m_tm.tm_mday; } |
| 79 | int getMonth() const { return m_tm.tm_mon + 1; } // makes it 1 .. 12 |
| 80 | int getYear() const { return m_tm.tm_year; } // does include 1900 (see Date.cpp) |
| 81 | int getWeekday() const { return m_tm.tm_wday + 1; } // makes it 1 .. 7 |
| 82 | int getYearday() const { return m_tm.tm_yday + 1; } // makes it 1 .. 366 |
| 83 | |
| 84 | // 1900 as per POSIX mktime() et al |
| 85 | static inline unsigned int baseYear() { |
| 86 | return 1900; |
| 87 | } |
| 88 |
no outgoing calls
no test coverage detected