//Class describing a watch #ifndef DMCGRATH_WATCH_H #define DMCGRATH_WATCH_H #include #include #include #include using namespace std; static const string days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; static const string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "December"}; class Watch{ private: //keep track of all parts separately int hours; int seconds; int minutes; int year; int day; bool hours24; //store as an int, index into array int month; int day_of_week; //time in seconds since 00:00:00 1 January 1970 time_t epoch; int day_of_year; float timezone; //offset from UTC //any helper functions should go here as well void populate_values(tm* gmt){ //see man 3 gmtime for a description of the tm struct this->hours = gmt->tm_hour; this->minutes = gmt->tm_min; this->seconds = gmt->tm_sec; this->day = gmt->tm_mday; this->month = gmt->tm_mon; //tm_year is stored as year - 1900, so adjust back up this->year = gmt->tm_year + 1900; this->day_of_week = gmt->tm_wday; this->day_of_year = gmt->tm_yday; this->hours24 = true; } public: Watch(){ //see man 3 gmtime tm* gmt; this->epoch = time(NULL); gmt = gmtime(&epoch); //call helper function to set all the data members populate_values(gmt); //assume they are in EST as the default timezone = -5; } Watch(int hours, int minutes, int seconds, int day, int month, int year){ this->hours = hours; this->minutes = minutes; this->seconds = seconds; this->timezone = -5; //assume they are in EST as default this->day = day; this->month = month; this->year = year; } Watch(int hours, int minutes, int seconds, int day, int month, int year, int offset){ this->hours = hours; this->minutes = minutes; this->seconds = seconds; this->timezone = offset; this->day = day; this->month = month; this->year = year; } ~Watch(){ //do nothing for now } //accessors string get_time(){ string time; stringstream ss; string m = ""; this->hours += this->timezone; if (!this->hours24){ if (this->hours > 12){ hours -= 12; m = " pm"; } else m = " am"; } if (this->hours < 10) ss << "0"; ss << this->hours << ":"; if (this->minutes < 10) ss << "0"; ss << this->minutes << ":"; if (this->seconds < 10) ss << "0"; ss << this->seconds << m; ss >> time; return time; } float get_timezone(){ return this->timezone; } int get_hours(){ return this->hours; } int get_minutes(){ return this->minutes; } int get_seconds(){ return this->seconds; } string get_month(){ return months[this->month]; } string get_day_of_week(){ return days[this->day]; } int get_day_of_year(){ return this->day_of_year; } int get_day_of_month(){ return this->day; } string get_date(){ stringstream ss; ss << days[day_of_week] << ", " << day << " " << months[month] << " " << year + 1900; string date; ss >> date; return date; } //mutators bool set_time(int hours, int minutes, int seconds){ this->hours = hours; this->minutes = minutes; this->seconds = seconds; } bool set_time(int hours, int minutes){ this->hours = hours; this->minutes = minutes; } bool set_timezone(float offset){ this->timezone = offset; } }; #endif