Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trouble with mktime

Status
Not open for further replies.

rtnMichael

Programmer
Aug 5, 2002
152
US
can someone help me out here, I am having trouble seeing what's wrong with this code. the first portion works, but the second doesn't like how I did the tm structure fill (I assume).

*************************first portion********************
struct tm file_tm = {30,40,3,15,2,73};
struct tm *file_time;
time_t TheTime;

if( (TheTime = mktime(&file_tm) ) != -1 )
printf(&quot;<%s>\n&quot; , asctime(&file_tm) );

file_time = localtime(&TheTime);

printf(&quot;%s\n&quot;, asctime(file_time));


************************second portion*******************
struct tm *file_time;
struct tm file_tm;
time_t TheTime;

if((sscanf(target->hdr.time,&quot;%2d%2d%1d&quot;, &file_tm.tm_hour, &file_tm.tm_min, &tenth) != 3) ||
(sscanf(target->hdr.date,&quot;%2d%2d%2d&quot;, &file_tm.tm_mon, &file_tm.tm_mday, &file_tm.tm_year) != 3))
return(&quot;Unknown&quot;);

file_tm.tm_sec = tenth * 6;
file_tm.tm_year += (file_tm.tm_year < 70 ) ? 2000 : 1900;

///////Here's where it gives me the problem, it returns -1
if((TheTime = mktime(&file_tm)) != -1 )
printf(&quot;<%s>\n&quot; , asctime(&file_tm) );

file_time = localtime(&TheTime);

printf(&quot;%s\n&quot;, asctime(file_time));


Any help would be greatly appreciated.

Thanks
Michael
 
Hi:

The solution to your problem is to change the line which reads

file_tm.tm_year += (file_tm.tm_year < 70 ) ? 2000 : 1900;

to the following:

file_tm.tm_year += (file_tm.tm_year < 70 ) ? 100 : 0;


Remember that time_t structure needs the year as: (current year minus 1900)

Hope this helps.

-- Rahul
 
yup, it did the trick...stupid me forgot to convert.

Thanks
Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top