Here follows the header file.
/*******************************************************
** Name: timestamp.h
** Author: Leo Liberti
** Source: GNU C++
** Purpose: www exploring topologizer: TimeStamp class header
** History: 060820 work started
*******************************************************/
#ifndef _WETTIMESTAMPH
#define _WETTIMESTAMPH
#include<iostream>
#include<sys/time.h>
class TimeStampException {
public:
TimeStampException();
~TimeStampException();
};
class TimeStamp {
public:
TimeStamp() : timestamp(0) { } ;
~TimeStamp() { }
long get(void) const;
void set(long theTimeStamp);
// read the system clock and update the timestamp
void update(void) throw (TimeStampException);
private:
long timestamp;
};
// print the timestamp in humanly readable form
std::ostream& operator<<(std::ostream& s, TimeStamp& t)
throw (TimeStampException);
#endif
The following points are worth emphasizing.
where
Here follows the implementation file.
/*******************************************************
** Name: timestamp.cxx
** Author: Leo Liberti
** Source: GNU C++
** Purpose: www exploring topologizer: TimeStamp class
** History: 060820 work started
*******************************************************/
#include <iostream>
#include <ctime>
#include "timestamp.h"
TimeStampException::TimeStampException() { }
TimeStampException::~TimeStampException() { }
long TimeStamp::get(void) const {
return timestamp;
}
void TimeStamp::set(long theTimeStamp) {
timestamp = theTimeStamp;
}
void TimeStamp::update(void) throw (TimeStampException) {
struct timeval tv;
struct timezone tz;
using namespace std;
try {
int retVal = gettimeofday(&tv, &tz);
if (retVal == -1) {
cerr << "TimeStamp::updateTimeStamp(): couldn't get system time" << endl;
throw TimeStampException();
}
} catch (...) {
cerr << "TimeStamp::updateTimeStamp(): couldn't get system time" << endl;
throw TimeStampException();
}
timestamp = tv.tv_sec;
}
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::updateTimeStamp(): couldn't print system time" << endl;
throw TimeStampException();
}
buffer[strlen(buffer) - 1] = '\0';
s << buffer;
return s;
}
Leo Liberti 2008-01-12