00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <RcppDate.h>
00024
00025 const int RcppDate::Jan1970Offset = 2440588;
00026 const int RcppDate::QLtoJan1970Offset = 25569;
00027
00028 RcppDate::RcppDate() : month(1),
00029 day(1),
00030 year(1970) {
00031 mdy2jdn();
00032 }
00033
00034 RcppDate::RcppDate(int Rjdn) {
00035 jdn = Rjdn+Jan1970Offset;
00036 jdn2mdy();
00037 }
00038
00039 RcppDate::RcppDate(int month_, int day_, int year_) : month(month_),
00040 day(day_),
00041 year(year_) {
00042 if (month < 1 || month > 12 || day < 1 || day > 31)
00043 throw std::range_error("RcppDate: invalid date");
00044 mdy2jdn();
00045 }
00046
00047
00048 std::ostream& operator<<(std::ostream& os, const RcppDate& date) {
00049 os << date.getYear() << "-" << date.getMonth() << "-" << date.getDay();
00050 return os;
00051 }
00052
00053
00054 RcppDate operator+(const RcppDate& date, int offset) {
00055 RcppDate temp(date.month, date.day, date.year);
00056 temp.jdn += offset;
00057 temp.jdn2mdy();
00058 return temp;
00059 }
00060
00061 int operator-(const RcppDate& date2, const RcppDate& date1) {
00062 return date2.jdn - date1.jdn;
00063 }
00064
00065 bool operator<(const RcppDate &date1, const RcppDate& date2) {
00066 return date1.jdn < date2.jdn;
00067 }
00068
00069 bool operator>(const RcppDate &date1, const RcppDate& date2) {
00070 return date1.jdn > date2.jdn;
00071 }
00072
00073 bool operator>=(const RcppDate &date1, const RcppDate& date2) {
00074 return date1.jdn >= date2.jdn;
00075 }
00076
00077 bool operator<=(const RcppDate &date1, const RcppDate& date2) {
00078 return date1.jdn <= date2.jdn;
00079 }
00080
00081 bool operator==(const RcppDate &date1, const RcppDate& date2) {
00082 return date1.jdn == date2.jdn;
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 void RcppDate::mdy2jdn() {
00099 int m = month, d = day, y = year;
00100 int a = (14 - m)/12;
00101 y += 4800 - a;
00102 m += 12*a - 3;
00103 jdn = (d + (153*m + 2)/5 + 365*y
00104 + y/4 - y/100 + y/400 - 32045);
00105 }
00106
00107
00108 void RcppDate::jdn2mdy() {
00109 int jul = jdn + 32044;
00110 int g = jul/146097;
00111 int dg = jul % 146097;
00112 int c = (dg/36524 + 1)*3/4;
00113 int dc = dg - c*36524;
00114 int b = dc/1461;
00115 int db = dc % 1461;
00116 int a = (db/365 + 1)*3/4;
00117 int da = db - a*365;
00118 int y = g*400 + c*100 + b*4 + a;
00119 int m = (da*5 + 308)/153 - 2;
00120 int d = da - (m + 4)*153 /5 + 122;
00121 y = y - 4800 + (m + 2)/12;
00122 m = (m + 2) % 12 + 1;
00123 d = d + 1;
00124 month = m;
00125 day = d;
00126 year = y;
00127 }