I am a newbie. Using class clsMRDateTime and am creating 2 instances of objects in the main code. When I activate the line:
clsMRDateTime objMRDateTimeURL("10_05_2011");//THIS CAUSES THE PROBLEM!!!!!
It causes the date in the first instance of the class to match the second instance of the class. I checked for static class variables, and cannot figure this out.
I removed all of the inactive methods.
Thank you for any help you can provide.
Michael
Sample Test Main():
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "clsMRConvert.h"
#include "clsMRDebug.h"
#include "clsMRDateTime.h"
int main(int argc, char *argv[])
{
printf("Content-type: text/html\n\n");
cout << "";
cout << "Hello Test";
clsMRDateTime objMRDateTimeToday("08_25_2010");
cout << "<BR>";
string strTodayDate = objMRDateTimeToday.strGetFormatedTime("%m/%d/%Y");
cout << "objMRDateTimeToday: " << strTodayDate;
cout << "<BR>";
cout << "<BR>";
clsMRDateTime objMRDateTimeURL("10_05_2011");//THIS CAUSES THE PROBLEM!!!!!
cout << "<BR>";
//string strURLDate = objMRDateTimeURL.strGetFormatedTime("%m/%d/%Y");
//cout << "objMRDateTimeURL: " << strURLDate;
cout << "<BR>";
cout << "<BR>";
strTodayDate = objMRDateTimeToday.strGetFormatedTime("%m/%d/%Y");
cout << "objMRDateTimeToday: " << strTodayDate << " [SHOULD BE SAME AS ABOVE!!!]";
}
Class clsMRDateTime:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "clsMRDateTime.h"
using namespace std;
clsMRDateTime::clsMRDateTime(string strDateAsString)
{
size_t found = strDateAsString.find('_');
if (found!=string::npos)
{
this->intSetDateTimeURL(strDateAsString);
}
else
{
}
}
clsMRDateTime::clsMRDateTime( time_t timetDateAsTimeT)
{
this->intSetDateTime(timetDateAsTimeT);
}
int clsMRDateTime::intSetDateTime(string strDateAsString)
{
int intDay, intMonth, intYear;
sscanf((char *)(strDateAsString.c_str()), "%d/%d/%d", &intMonth, &intDay, &intYear);
this->timetClassMainTime = this->timetMakeTime(12, 0, 0,
intMonth, intDay, intYear);
this->tmClassMainTimeTM = this->tmGetTimeInTMFormat();
}
int clsMRDateTime::intSetDateTimeURL(string strDateAsString)
{
int intDay, intMonth, intYear;
sscanf((char *)(strDateAsString.c_str()), "%d_%d_%d", &intMonth, &intDay, &intYear);
this->timetClassMainTime = this->timetMakeTime(12, 0, 0,
intMonth, intDay, intYear);
this->tmClassMainTimeTM = this->tmGetTimeInTMFormat();
}
int clsMRDateTime::intSetDateTime(time_t timetDateAsTimeT)
{
this->timetClassMainTime = timetDateAsTimeT;
this->tmClassMainTimeTM = this->tmGetTimeInTMFormat();
}
int clsMRDateTime::intSetDateTime(struct tm * tmDateAsStructTM)
{
this->timetClassMainTime = mktime(tmDateAsStructTM);
this->tmClassMainTimeTM = this->tmGetTimeInTMFormat();
}
time_t clsMRDateTime::timetMakeTime(int intHour, int intMin, int intSec,
int intMonth, int intDay, int intYear)
{
struct tm * timeinfo;
time_t time_tSeconds;
time_t rawtime;
try{
//time ( &rawtime );
timeinfo = localtime ( &this->timetClassMainTime );
timeinfo->tm_year = intYear-1900;
timeinfo->tm_mon = intMonth -1;
timeinfo->tm_mday = intDay;
timeinfo->tm_hour = intHour;
timeinfo->tm_min = intMin;
timeinfo->tm_sec = intSec;
timeinfo->tm_isdst = 0;
time_tSeconds = mktime(timeinfo);
}
catch (char * str )
{
cout << "Exception raised: " << str << '\n';
return(1);
}
return(time_tSeconds);
}
string clsMRDateTime::strGetFormatedTime(string strFormat)
{
string strFormattedTime = "";
strFormattedTime = this->strGetFormatedTimeForNewDates(strFormat, this->tmClassMainTimeTM);
return(strFormattedTime);
}
string clsMRDateTime::strGetFormatedTimeForNewDates(string strFormat, struct tm *tmNewDate)
{
string strFormattedTime;
char s[80];
size_t i;
strftime(s,80,strFormat.c_str(),tmNewDate);
strFormattedTime = s;
return(strFormattedTime);
}
time_t clsMRDateTime::timetGetGregoreanTimeStamp()
{
return(this->timetClassMainTime);
}
struct tm *clsMRDateTime::tmGetTimeInTMFormat()
{
this->tmClassMainTimeTM = localtime(&this->timetClassMainTime);
}
clsMRDateTime::tmClassMainTimeTMis a pointer to astruct tm, so when you dothis->tmClassMainTimeTM = localtime(&this->timetClassMainTime);you are in fact saving off a pointer to the return value fromlocaltime.The
localtimefunction reuses a statictmstructure, so the return value is the same every time. What you’ve done is set it up so each instance ofclsMRDateTimewinds up storing a pointer to the samestruct tmso they all end up representing the most recently set date/time.What you need to do is make
tmClassMainTimeTMNOT a pointer (make it just astruct tm) and then do something likethis->tmClassMainTimeTM = *localtime(&this->timetClassMainTime);.Better is to review the man page for
localtime_rwhich is thread safe and allows you to pass in your own struct and use that instead.