struct tm and strftime issues?

Hi, I'm trying to do an e-mail-like program. But I'm having an issue with the date of the "e-mail's". Say, I created an e-mail, if, after that, I create a new one, the date of the oldest one changes.

This is my Message class constructor and private fields:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Message(string m_subject, string m_content, string m_sender, string m_recipient)
{
	subject=m_subject;
	content=m_content;
	time_t d_raw;
	time(&d_raw);
	date=localtime(&d_raw);
	sender=m_sender;
	recipient=m_recipient;
	read=false;
}
////
private:
	string subject;
	string content;
	struct tm * date;
	string sender;
	string recipient;
	bool read;


And this is the tests I've been running:
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
char strng[18];
Message msg1("test1","body1","sndr","rcpnt");
Sleep(60000);
Message msg2("test2","body2","sndr2","rcpnt2");
cout<<"001 | ";
strftime (strng,18,"%c",msg1.getDate());
cout<<strng<<" | ";
for(unsigned int n=0;n<54;n++)
{
	if(n>msg1.getSubject().size())
	{
		cout<<"\n";
		break;
	}
	else cout<<msg1.getSubject()[n];
}
cout<<"002 | ";
strftime (strng,18,"%c",msg2.getDate());
cout<<strng<<" | ";
for(unsigned int n=0;n<54;n++)
{
	if(n>msg2.getSubject().size())
	{
		cout<<"\n";
		break;
	}
	else cout<<msg2.getSubject()[n];
}

With this, the output would be something like:
001 | 07/12/13 | 14:05:54 | test1
002 | 07/12/13 | 14:05:54 | test2

Instead of:
001 | 07/12/13 | 14:04:54 | test1
002 | 07/12/13 | 14:05:54 | test2

Does anyone know how to "fix" this problem?
Usually the m_ prefix is used for member variables but you use it for function parameters instead. Oh well, I don't care, I just felt like pointing it out in case made it by mistake.

I think the problem is that localtime returns a pointer to a static internal tm object. When localtime is called a second time it will reuse the same object it used the first time, change it and return the same pointer to it. All Message::date pointers will point to the same object so that is why you get the same output.
Thanks, i didn't know that about the m_prefix. Then, is it 'common practice' to use the this pointer? Like:
1
2
3
4
5
6
7
8
9
10
11
Message(string subject, string content, string sender, string recipient)
{
	this->subject=m_subject;
	this->content=m_content;
	time_t d_raw;
	time(&d_raw);
	this->date=localtime(&d_raw);
	this->sender=m_sender;
	this->recipient=m_recipient;
	this->read=false;
}

And thanks on the actual problem, that makes sense. I'll try changing it in a second.

[EDIT]:
Problem Solved! Here is the changes I did to the code:
-to the private fields:
struct tm date;
-to the constructor:
date=*localtime(&d_raw);
-to the tests:
strftime (strng,18,"%c",&(msg2.getDate()));
Once again, thanks for the help!
Last edited on
Good that you solved it.

About the prefixes, I don't use them myself but the assignment would look like this: m_subject = subject; and I don't think there is any point in using this-> here.

If you initialize the variables in the constructor initialization list (which I recommend you do if possible) it doesn't matter so much if parameters and member variables have the same names.
1
2
3
4
5
6
7
8
9
10
11
Message(string subject, string content, string sender, string recipient)
:	subject(subject),
	content(content),
	sender(sender),
	recipient(recipient),
	read(false)
{
	time_t d_raw;
	time(&d_raw);
	date=*localtime(&d_raw);
}
Last edited on
Topic archived. No new replies allowed.