I'm trying to construct a Clock class based on the functions in ctime.
Though it's not going very well :thumbdown: I get some odd behavior with it adding/removing a hour when it shouldn't. So thought I should just pick one from the internet, look at it and see what I did wrong and probably use that one too. But googling got me nowhere.
So I turned here ^^
Well what I need is a clock. Not a Timer, I want it to be able to use/view/read time in this format: YY-MM-DD hh:mm:ss
Thx!
**EDIT** The code if you might spot what I did wrong. I've marked the areas where it goes wrong. So long I have narrowed it down to operator+=
//Header file
class Clock {
public:
// Functions removed to make more room!
private:
// The time data
tm * currentTime;
};
//Source file
Clock::Clock() {
this->currentTime = Clock::nowTM();
}
Clock::Clock(tm * time) {
this->currentTime = (tm *) malloc(sizeof(tm));
memcpy(this->currentTime, time, sizeof(tm));
}
Clock::Clock(const std::string& time) {
this->currentTime = (tm *) malloc(sizeof(tm));
strptime(time.c_str(), "%Y-%m-%d %H:%M:%S", this->currentTime);
}
Clock::Clock(const Clock& clock) {
this->currentTime = (tm *) malloc(sizeof(tm));
memcpy(this->currentTime, clock.currentTime, sizeof(tm));
}
Clock::~Clock() {
free(this->currentTime);
}
Clock Clock::operator+(time_t i) const {
time_t t = mktime(this->currentTime);
t += i;
return Clock(localtime(&t));
}
Clock Clock::operator-(time_t i) const {
time_t t = mktime(this->currentTime);
t -= i;
return Clock(localtime(&t));
}
Clock& Clock::operator=(time_t i) {
memcpy(this->currentTime, localtime(&i), sizeof(tm));
return *this;
}
Clock& Clock::operator=(const string& time) {
tm date = *this->currentTime;
strptime(time.c_str(), "%Y-%m-%d %H:%M:%S", &date);
memcpy(this->currentTime, &date, sizeof(tm));
return *this;
}
Clock& Clock::operator=(const Clock& time) {
this->currentTime = (tm *) malloc(sizeof(tm));
memcpy(this->currentTime, time.currentTime, sizeof(tm));
return *this;
}
Clock& Clock::operator+=(time_t i) {
// Output here will show correct time
time_t t = mktime(this->currentTime);
t += i;
memcpy(this->currentTime, localtime(&t), sizeof(tm));
// Output here will have decreased time with 1h. If I add +3600 it works
// the first time but the next time it's 1h in the future
return *this;
}
Clock& Clock::operator-=(time_t i) {
Clock c = this->operator-(i);
memcpy(this->currentTime, c.currentTime, sizeof(tm));
return *this;
}
bool Clock::operator>(const Clock& clock) const {
time_t sec1 = this->toSeconds();
time_t sec2 = clock.toSeconds();
return sec1 > sec2;
}
bool Clock::operator<(const Clock& clock) const {
time_t sec1 = this->toSeconds();
time_t sec2 = clock.toSeconds();
return sec1 < sec2;
}
bool Clock::operator>=(const Clock& clock) const {
time_t sec1 = this->toSeconds();
time_t sec2 = clock.toSeconds();
return sec1 >= sec2;
}
bool Clock::operator<=(const Clock& clock) const {
time_t sec1 = this->toSeconds();
time_t sec2 = clock.toSeconds();
return sec1 <= sec2;
}
string Clock::toDefaultString() const {
char buffer[512];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", this->currentTime);
return string(buffer);
}
string Clock::toHourTime() const {
char buffer[4];
strftime(buffer, sizeof(buffer), "%H", this->currentTime);
return string(buffer);
}
string Clock::toMinuteTime() const {
char buffer[4];
strftime(buffer, sizeof(buffer), "%M", this->currentTime);
return string(buffer);
}
string Clock::toSecondTime() const {
char buffer[4];
strftime(buffer, sizeof(buffer), "%S", this->currentTime);
return string(buffer);
}
time_t Clock::toSeconds() const {
return mktime(this->currentTime);
}
tm * Clock::nowTM() {
time_t secs; time(&secs);
return Clock::getTM(secs);
}
tm * Clock::getTM(time_t t) {
tm * timeraw = localtime(&t);
tm * timecpy = (tm *) malloc(sizeof(tm));
memcpy(timecpy, timeraw, sizeof(tm));
return timecpy;
}


Sign In
Create Account


Back to top











