Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1045683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:01:25+00:00 2026-05-16T16:01:25+00:00

I am a newbie. Using class clsMRDateTime and am creating 2 instances of objects

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:01:26+00:00Added an answer on May 16, 2026 at 4:01 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java newbie. I am trying to run a java main class from cmd line
Newbie question... I am using silverlight to POST data to my GAE application class
Possible Duplicate: Class names that start with C. I am a newbie using c++
I am a newbie to using jackson library. I am trying to do this
1st i'm newbie I import object/Class using lazy() now my questions are 1) what
I am (newbie) using Kohana V 3.0.3 and my directory structure is: pojectsys (kohana's
I am newbie at using DataSet. I am writing a program with VB.NET, there
Newbie question since I'm not up to speed using maven at all. I'm trying
I'm a newbie to SCons and also using pydev. Can someone help me with
I'm definitely a newbie to ruby (and using 1.9.1), so any help is appreciated.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.