/* name: timestamp.cxx author: the C++ course @X purpose: example for INF585 - time stamp method implementation source: GNU C++ history: 100126 work started */ #include #include #include #include "timestamp.h" TimeStamp::TimeStamp() : timestamp(0) { using namespace std; cout << "TimeStamp constructor: called by " << this << endl; } TimeStamp::~TimeStamp() { using namespace std; cout << "TimeStamp destructor: called by " << this << endl; } void TimeStamp::update(void) throw (TimeStampException) { using namespace std; struct timeval tv; struct timezone tz; try { int ret = gettimeofday(&tv, &tz); } catch (...) { std::cout << "catching all exceptions" << std::endl; throw TimeStampException(); } timestamp = tv.tv_sec; } long TimeStamp::get(void) const { return timestamp; } TimeStampException::TimeStampException() {} TimeStampException::~TimeStampException() {} std::ostream& operator<< (std::ostream& s, TimeStamp& t) throw (TimeStampException) { using namespace std; time_t theTime = (time_t) t.get(); char* buffer; try { buffer = ctime(&theTime); } catch (...) { cerr << "TimeStamp::operator<<(): error: couldn't call ctime()\n"; throw TimeStampException(); } buffer[strlen(buffer) - 1] = '\0'; s << buffer; return s; }