Date class with problems
Jan 20, 2011 at 5:11am UTC
hello, i have create a date class to compare dates, but there is a problem in my code, when i create a new date object it changes the values of others date objects!!
here is the code:
Date.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#ifndef DATE_H
#define DATE_H
#include <string>
#include <sstream>
#include <utility>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Date{
private :
time_t rawtime;//segundos desde 1/1/1970 a las 0:0:0
tm *tm_info;
public :
// Constructores:
Date():rawtime(0),tm_info(NULL){
rawtime = time(NULL);
tm_info = localtime(&rawtime);
};
Date(const int & y, const int & m, const int & d):rawtime(0),tm_info(NULL){
rawtime = time(NULL);
tm_info = localtime(&rawtime);
tm_info->tm_year = y-1900;
tm_info->tm_mon = m-1;
tm_info->tm_mday = d;
tm_info->tm_hour = 0;
tm_info->tm_min = 0;
tm_info->tm_sec = 0;
rawtime = mktime(tm_info);
};
Date(const int & y, const int & m, const int & d, const int & hh, const int & mm, const int & ss):rawtime(0),tm_info(NULL){
rawtime = time(NULL);
tm_info = localtime(&rawtime);
tm_info->tm_year = y-1900;
tm_info->tm_mon = m-1;
tm_info->tm_mday = d;
tm_info->tm_hour = hh;
tm_info->tm_min = mm;
tm_info->tm_sec = ss;
rawtime = mktime(tm_info);
};
bool esBisiesto();
bool operator ==(const Date& b) const ;
bool operator <(const Date& b) const ;
string toString();
};
using namespace rel_ops;
#endif
Date.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include "Date.h"
bool Date::operator ==(const Date& b) const {
return (difftime(rawtime,b.rawtime)==0);
}
bool Date::operator <(const Date& b) const {
return (difftime(rawtime,b.rawtime)<0);
}
bool Date::esBisiesto(){
return ((tm_info->tm_year % 4 == 0 && tm_info->tm_year % 100 != 0) || tm_info->tm_year % 400 == 0);
}
string Date::toString(){
stringstream ss;
ss << tm_info->tm_mday << "/" << tm_info->tm_mon << "/" << (tm_info->tm_year+1900) << " at " << tm_info->tm_hour << ":" << tm_info->tm_min << ":" << tm_info->tm_sec;
return ss.str();
}
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include "Date.h"
using namespace std;
int main(){
Date one, two(2010,2,2), three, four(1999,9,9);
cout <<one.toString()<<endl;
cout <<two.toString()<<endl;
cout <<three.toString()<<endl;
cout <<four.toString()<<endl;
return 0;
}
and the output is
8/8/1999 at 23:0:0
8/8/1999 at 23:0:0
8/8/1999 at 23:0:0
8/8/1999 at 23:0:0
can anyone give me a little help on this??
thanks
Jan 20, 2011 at 6:33am UTC
http://www.cplusplus.com/reference/clibrary/ctime/localtime/
Return Value
A pointer to a tm structure with the time information filled in.
This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the content of this structure is overwritten.
Jan 20, 2011 at 2:26pm UTC
ok... xD
thanks, i have actually read that but i didnt realize what it means till now...
bye
Topic archived. No new replies allowed.