tags:

views:

57

answers:

3

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);

    }
+1  A: 

You don't show clsMRDateTime.h, but I'm betting in that file you have your member variables declared as static. They shouldn't be.

Fred Larson
+1 for the bet.
ereOn
indeed a bet as the OP already said he checked for class static member variables.
celavek
@Marius: I missed that part. It'd still be good to see `clsMRDateTime.h`, though.
Fred Larson
@Fred Larson: indeed it will be good to see it as I'm clueless and I might subscribe to your bet eventually :)
celavek
A: 

I would scrutinize any calls to gmtime() or localtime() since they share and reuse a statically allocated structure.

The only other candidate that sticks out is the empty else statement in the first constructor.

Arnold Spence
Why would that empty else make the first instantiated object take the string date value of the second one?
celavek
I don't know. Without the header file I can only venture guesses.
Arnold Spence
+1  A: 

clsMRDateTime::tmClassMainTimeTM is a pointer to a struct tm, so when you do this->tmClassMainTimeTM = localtime(&this->timetClassMainTime); you are in fact saving off a pointer to the return value from localtime.

The localtime function reuses a static tm structure, so the return value is the same every time. What you've done is set it up so each instance of clsMRDateTime winds up storing a pointer to the same struct tm so they all end up representing the most recently set date/time.

What you need to do is make tmClassMainTimeTM NOT a pointer (make it just a struct tm) and then do something like this->tmClassMainTimeTM = *localtime(&this->timetClassMainTime);.

Better is to review the man page for localtime_r which is thread safe and allows you to pass in your own struct and use that instead.

Mark B
Thank you Mark B!I originally had done what you have here, but the .exe would terminate.I changed it back after your answer and cout debugged it to this problem: string strURLDate = objMRDateTimeURL.strGetFormatedTime("%m/%d/%Y");Should be:string strURLDate;strURLDate = objMRDateTimeURL.strGetFormatedTime("%m/%d/%Y");Thanks again for everyone's help!
Mike R