struct_tm variables seem to be interfering

The program below is loosely based on some of the examples in the reference section of this site. It should take in two dates (currently they are defined directly in the program for easier debugging) and output the day of the week of the first one. Instead, it outputs the day of the week of the second one. Any idea why?

Thanks!

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
#include <time.h> // library for storing and performing simple functions on dates using time_t and struct tm
#include <iostream>

using namespace std;

int main()
{
   // vars
   time_t currt1, currt2; // store current time
   struct tm * date1, * date2; // store the two dates
   int y1, y2, m1, m2, d1, d2; // store user input

   // get user input
   cout << "Please enter the first date (m d yyyy):" << endl;
   //cin >> m1 >> d1 >> y1;
   cout << "Please enter the second date (m d yyyy):" << endl;
   //cin >> m2 >> d2 >> y2;
   
   m1 = 5;
   d1 = 20;
   y1 = 2000;
   m2 = 6;
   d2 = 21;
   y2 = 2001;

   // put user input into date1, date2
   time(&currt1);
   date1 = localtime(&currt1);
   date1->tm_year = y1 - 1900;
   date1->tm_mon = m1 - 1;
   date1->tm_mday = d1;
   
   time(&currt1);
   date2 = localtime(&currt1);
   date2->tm_year = y2 - 1900;
   date2->tm_mon = m2 - 1;
   date2->tm_mday = d2;

   // fill in the blanks in date1, date2
   mktime(date1);
   mktime(date2);

   // output
   cout << date1->tm_wday << endl;

   return 0;
}
Last edited on
tm doesn't automatically update its other members when you change something.
helios - I know, but as I understand it mktime will do that that, at least for tm_wday. This program works just fine when only one date is involved, but the second one seems to be interfering with the first.
Sorry, I missed the call.

IIRC, localtime() returns a pointer to a shared structure, so date1 and date2 point to the same thing.
Wht you could do is make them non-pointers and then date=*localtime();
helios - Thanks! That worked. Here is the new code for anyone interested:

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
#include <time.h> // library for storing and performing simple functions on dates using time_t and struct tm
#include <iostream>

using namespace std;

int main()
{
   // vars
   time_t currt1, currt2; // store current time
   struct tm date1, date2; // store the two dates
   int y1, y2, m1, m2, d1, d2; // store user input

   // get user input
   cout << "Please enter the first date (m d yyyy):" << endl;
   //cin >> m1 >> d1 >> y1;
   cout << "Please enter the second date (m d yyyy):" << endl;
   //cin >> m2 >> d2 >> y2;
   
   m1 = 5;
   d1 = 20;
   y1 = 2000;
   m2 = 6;
   d2 = 21;
   y2 = 2001;

   // put user input into date1, date2
   time(&currt1);
   date1 = *localtime(&currt1);
   date1.tm_year = y1 - 1900;
   date1.tm_mon = m1 - 1;
   date1.tm_mday = d1;
   
   time(&currt1);
   date2 = *localtime(&currt1);
   date2.tm_year = y2 - 1900;
   date2.tm_mon = m2 - 1;
   date2.tm_mday = d2;

   // fill in the blanks in date1, date2
   mktime(&date1);
   mktime(&date2);

   // output
   cout << date1.tm_wday << endl;

   return 0;
}
Topic archived. No new replies allowed.