/*********************************** * Implementation File (Date.cpp) * * Private members of the class const int MAX_APPOINTMENTS = 10; float JDN; int numberAppointments; string appointments [MAX_APPOINTMENTS]; ***********************************/ #include "Date.h" // prototypes for helper functions float today(); float jdn(int , int , int); void mailThis( string subject, string addr, string message); Date::Date() // constructor. JDN is set to today // numberAppointments = 0 { JDN = today(); numberAppointments = 0; } Date::Date(int mm, int dd, int yyyy) // constructor JDN is set to correspond to date specified by // mm dd yyyy, if it is a valid date. Otherwise JDN is set // for today. numberAppointments = 0; { // under construction JDN = 100.5; numberAppointments = 0; } void Date::setDate( int mm, int dd, int yyyy) // set JDN to appropriate value // precondition mm dd yyyy is a valid date // otherwise set to today { //under construction JDN = jdn(mm,dd,yyyy); } void Date::setDate( float jdn) // set JDN of Date object that activeted the method to jdn. { JDN=jdn; } float Date::getJDN() // returns the Julian day number of the date object that activates the // method { return JDN; } int Date::getDayOfWeek() // return int 0..6 representing the day of the week { return int(JDN + 1.5) %7; } void Date::getMonthDayYear(int & m, int &d, int &y) // return ints representing the month, day, and year of a Date { int p, s1, n, s2, i,s3, q,e,s4; //(Eq. 3) p = int(JDN + 0.5) p = int(JDN+0.5); //(Eq. 4) s1 = p + 68569 s1 = p + 68569; //(Eq. 5) n =int(4*s1/146097) n =int(4*s1/146097); //(Eq. 6) s2 = s1 - (146097*n + 3)/4 s2 = s1 - (146097*n + 3)/4; //(Eq. 7) i = 4000*(s2 + 1)/1461001. i = 4000*(s2 + 1)/1461001; //(Eq. 8) s3 = s2 - 1461*i/4 + 31 s3 = s2 - 1461*i/4 + 31; //(Eq. 9) q = 80*s3/2447 q = 80*s3/2447; //(Eq. 10) e = s3 - 2447*q/80 e = s3 - 2447*q/80; //(Eq. 11) s4 = q/11 s4 = q/11; //(Eq. 12) m = q + 2 - 12*s4 m = q + 2 - 12*s4; //(Eq. 13) y = 100*(n - 49) + i + s4 y = 100*(n - 49) + i + s4; //(Eq. 14) d = e + J - p + 0.5 d = e + JDN - p + 0.5; //The date is then day d in month m in year y. // taken from "Astronomy Answers Julian Day Number" // http://www.astro.uu.nl/~strous/AA/en/reken/juliaansedag.html } int Date::numAppointments() // return the number of appointments { return(numberAppointments); } string Date::ShowAppointment( int n) // if 0 <= n < numberAppointments // then return Appointment[n] // Otherwise return "Invalid Appointment Number" + n { // under construction return "Under Construction "+ n; } bool Date::SetAppointment( string appt) // if numberAppointments < MAX_APPOINTMENTS // then add appt as Appointment[numberAppointments++] // return true // Otherwise return false. { if (numberAppointments < MAX_APPOINTMENTS) { appointments[numberAppointments++] = appt; return true; } return false; } bool Date::equals(Date N) // Suppose this is activated by a date object D as // D.equals(N). This returns true if and only if // D.JDN = N.JDN, { // under construction return true; } bool Date::lessThan(Date N) // returns true only if D.JDN < N.JDN { // under construction return true; } bool Date::greaterThan(Date N) // returns true only if D.JDN > N.JDN { // under construction return true; } void Date::mailAppointments( string subject, string sendTo ) // send the appointments for the object that activated this method as email // parameters // subject holds the subject of the email sent // sendTo holds the email address that will receive the appointments // if there are no appointments then no email is sent, otherwise the // appointments are sent as one message { int curAppointment; // points to current appointment string allAppointments; // holds all appointments if (numberAppointments == 0) return; // build the message allAppointments = ""; for (curAppointment =0; curAppointment < numberAppointments; curAppointment ++) { allAppointments = allAppointments + appointments[curAppointment] + "\n"; } //send the email mailThis(subject, sendTo, allAppointments); //that's it! } // Helper functions float today() { #include time_t rawtime; struct tm * timeinfo; int mm, dd, yyyy; float julian; time ( &rawtime ); timeinfo = localtime ( &rawtime ); yyyy = timeinfo->tm_year + 1900; mm = timeinfo->tm_mon+1; dd = timeinfo->tm_mday; julian = jdn(mm,dd,yyyy); return julian; } float jdn( int m, int d, int y) { int A, B, C, E, F; // under construction // got the algorthim at //http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html // assume the date is valid if (m < 3) { m = m + 12; y = y -1; } // assume that we are using this for the modern era A = y/100; B = A/4; C = 2 - A + B; // convert years to days E = 365.25 * y; // do the thing with the month F = 30.6001 * (m + 1); return C+d+E+F+1720994.5; } #include #include void mailThis( string subject, string sendto, string body) { FILE *fin; string complete; char * mailstr; complete = "mailx -s '" + subject + "' " + sendto; mailstr = complete.c_str(); if ((fin = popen(mailstr, "w")) == NULL) { fprintf(stderr, " can't run %s\n", body); exit(1); } fprintf(fin,"%s",body.c_str()); fflush(fin); pclose(fin); }