time() functionality and tm structs w/ pointers

I am writing a Rolodex-type program to store information like birthday, phone number, etc. I have defined a Rolodex structure, and then declared an array of Rolodex structures, with one element in the array storing information for one person:

1
2
3
4
5
6
7
struct Rolodex
{
  struct tm Birthday;
  double AgeCalc;
};

Rolodex Entry[N_Records];


The data for the Birthday members are read in from a file; the AgeCalc value must be calculated. I then want to utilize the time() functionality to calculate a person's age based on current time. I have the code for getting current time,

1
2
time_t now;
now = time(NULL);


but I need to create a time_t object from each Birthday member of the Rolodex array, which would allow me to use the diff() function - which wants 2 time_t data types. I *think* I need to create a pointer to Entry (or Entry[i].Birthday - I'm not certain which I need):

1
2
Rolodex *ptrEntry;
ptrEntry = Entry;


and then use mktime() to return a time_t data type from the pointer (shown below just referencing a single element of the Rolodex array)

1
2
3
4
time_t ref;
struct tm *temp2;
temp2 = &(ptrEntry[1].Birthday);
ref = mktime(temp2);


The print statement

 
cout << now << "\t" << ref << "\t" <<difftime(now,mktime(temp2));


returns:
1307078828 -1 1.30708e+09

which means the mktime() statement used to define ref doesn't like how I am pointing to my Birthday tm struct in assigning a value to temp2. I have been working on this for a while, but haven't made much progress. I'd like to define a good problem statement, get some feedback from you nice folks, and then work on it some more myself. I think I am close but my pointer/array syntax clumsiness is getting the best of me. Please let me know if you need any additional information. Thanks.



kbw, thanks for the links, but i do not think they address my specific question. i want to use diff() to calculate the difference in time between 2 time_t objects. i think i am generating the first time_t object (i.e., the current time) correctly, however the second needs to be generated from a struct tm ('Birthday' in my case). i do intend to use mktime() to convert the struct tm to a time_t type, but there are 2 issues.

first, mktime() does not receive a struct tm, it receives a pointer to a struct tm. if i just plug in the struct tm directly i get "error: cannot convert `tm' to `tm*' in assignment".

second, the struct tm is itself a member of the Rolodex structure i have defined. note i am dealing with an array of Rolodex structures, so i need to take this into account as well.

so my question is, how (if it is possible) do i point to a struct tm object that is a member of a defined
structure, which is, in turn, an element of an array? my best guess was

1
2
3
4
time_t ref;
struct tm *temp2;
temp2 = &(ptrEntry[1].Birthday);
ref = mktime(temp2);


but when i plug 'ref' into mktime(), mktime() returns -1. suggestions on the pointer syntax?
Hey I have written one function which takes a string(since I am suing juce so i have used juce::String) which in format like "2011-06-02 14:06:07" this function returns a string(giving the sec/min/hr/day/monts/yr ago) like

May be you find it useful. But I say this is not that much accurate since(I have assumed that year has 365 days and months have 30 days)

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
juce::String SPEditDictDialog::fun(juce::String &inTimeStr)
{
	/* Creates a tm struct for the two times to be compared. */
	struct tm *dicModTM = new tm;
	std::string inTimeStamp1 = inTimeStr.toCString();

	/* Parse the string to extract the indiviual time infromation from string and fill it into the tm struct. */
	dicModTM->tm_year	= atoi(inTimeStamp1.substr(0, 4).c_str());
	dicModTM->tm_mon 	= atoi(inTimeStamp1.substr(5, 2).c_str());
	dicModTM->tm_mday	= atoi(inTimeStamp1.substr(8, 2).c_str());
	dicModTM->tm_hour	= atoi(inTimeStamp1.substr(11, 2).c_str());
	dicModTM->tm_min	= atoi(inTimeStamp1.substr(14, 2).c_str());
	dicModTM->tm_sec	= atoi(inTimeStamp1.substr(17, 2).c_str());
	dicModTM->tm_year	= dicModTM->tm_year - 1900;
	dicModTM->tm_mon	= dicModTM->tm_mon  - 1;
	dicModTM->tm_mday	= dicModTM->tm_mday;

	/* Creates time_t variable from the tm struct. */
	time_t dictModTime = mktime(dicModTM);
	
	time_t presentTime;
	time(&presentTime);

	time_t timeDiff = presentTime - dictModTime;

	juce::String modTimeInfoStr;
	if(timeDiff >= 0 && timeDiff < 60)
	{
		modTimeInfoStr << (int)timeDiff;
		modTimeInfoStr +=	" secs ago";
	}
	else if(timeDiff >= 60 && timeDiff < 3600)
	{
		int min = (timeDiff / 60);
		int sec = (timeDiff % 60);
		
		modTimeInfoStr << min;
		if(min == 1)
			modTimeInfoStr +=	" min ";
		else
			modTimeInfoStr +=	" mins ";
		if(sec)
		{
			modTimeInfoStr << sec;
			if(sec == 1)
				modTimeInfoStr +=	" sec";
			else
				modTimeInfoStr +=	" secs";
		}
		modTimeInfoStr +=	" ago";
	}
	else if(timeDiff >= 3600 && timeDiff < 86400)
	{
		int hr = (timeDiff / 3600);
		int min = (timeDiff % 3600) / 60;
		
		modTimeInfoStr << hr;
		if(hr == 1)
			modTimeInfoStr +=	" hr ";
		else
			modTimeInfoStr +=	" hrs ";
		if(min)
		{ 
			modTimeInfoStr << min;
			if(min == 1)
				modTimeInfoStr +=	" min";
			else
				modTimeInfoStr +=	" mins";
		}
		modTimeInfoStr +=	" ago";
	}
	else if(timeDiff >= 86400 && timeDiff < 2592000)
	{
		int day = (timeDiff / 86400);
		int hr = (timeDiff % 86400) / 3600;
		
		modTimeInfoStr  << day;
		if(day ==1)
			modTimeInfoStr +=	" day ";
		else
			modTimeInfoStr +=	" days ";
		if(hr)
		{
			modTimeInfoStr << hr;
			if(hr == 1)
				modTimeInfoStr +=	" hr";
			else
				modTimeInfoStr +=	" hrs";
		}
		modTimeInfoStr +=	" ago";
	}
	else if(timeDiff >= 2592000 && timeDiff < 946080000)
	{
		int mon = (timeDiff / 2592000);
		int day = (timeDiff % 2592000) / 86400;
		
		modTimeInfoStr << mon;
		if(mon == 1)
			modTimeInfoStr +=	" month ";
		else
			modTimeInfoStr +=	" months ";
		if(day)
		{
			modTimeInfoStr  << day;
			if(day == 1)
				modTimeInfoStr +=	" day";
			else
				modTimeInfoStr +=	" days";
		}
		modTimeInfoStr +=	"ago";
	}
	else if(timeDiff >= 946080000)
	{
		int yr = (timeDiff / 31104000);
		int mon = (timeDiff % 31104000) / 2592000;
		
		modTimeInfoStr << yr;
		if(yr == 1)
			modTimeInfoStr +=	" yr ";
		else
			modTimeInfoStr +=	" yrs ";
		if(mon)
		{
			modTimeInfoStr << mon;
			if(mon == 1)
				modTimeInfoStr +=	" month";
			else
				modTimeInfoStr +=	" months";
		}
		modTimeInfoStr +=	" ago";
	}

	if(dicModTM)
	{
		delete dicModTM;
		dicModTM = NULL;
	}

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