• C

Absolute madness with managing time in C

if (row  = mysql_fetch_row(res))
        assert(atoi(row[0]) == contest_id);
        time_t start_time = atoi(row[1]);
        time_t stop_time  = atoi(row[4]);
        int duration = atoi(row[3]);

        struct tm cur_time_tm;
        struct tm start_time_tm;
        struct tm stop_time_tm;

        strncpy(sstart_time, row[1], sizeof(sstart_time));
        strncpy(sstop_time, row[4], sizeof(sstop_time));

        strptime(sstop_time, "%Y-%m-%d %H:%M:%S", &stop_time_tm);
        strptime(sstart_time,  "%Y-%m-%d %H:%M:%S", &start_time_tm);

        printf("ascstarttime=%s;ascstop%s;\n", asctime(&start_time_tm), asctime(&stop_time_tm));
        printf("startstr=%s;stopstr=%s\n", sstart_time, sstop_time);

        start_time = mktime(&start_time_tm);
        stop_time = mktime(&stop_time_tm);

        printf("mkstart=%s;mkstop=%s\n", ctime(&start_time), ctime(&stop_time));

Open in new window


ascstarttime=Sat Jan  1 04:00:01 2000
;ascstopSat Jan  1 04:00:01 2000
startstr=2000-01-01 04:00:01;stopstr=2000-01-01 09:00:01
mkstart=Sat Jan  1 04:00:01 2000
;mkstop=Sat Jan  1 04:00:01 2000

why  asctime(&stop_time_tm)  gives Sat Jan  1 04:00:01 2000
but  sstop_time  outputs                    2000-01-01 09:00:01

why sstart_time, sstop_time are different
but start_time_tm, stop_time_tm are always the same.
Nusrat NuriyevAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi ,

I think the problem is how you use the asctime because this uses an internal buffer which means calling it a second time will seemingly 'change' the result of the first call - see i.e. http://www.cplusplus.com/reference/ctime/asctime/, there you can find as note The returned value points to an internal array whose validity or value may be altered by any subsequent call to asctime or ctime.

Try to replace this line
        printf("ascstarttime=%s;ascstop%s;\n", asctime(&start_time_tm), asctime(&stop_time_tm));

Open in new window

with this code
        printf("ascstarttime=%s;", asctime(&start_time_tm));
        printf("ascstop%s;\n", asctime(&stop_time_tm));

Open in new window

to find out if this is the problem.

Hope that helps,

Nusrat NuriyevAuthor Commented:
Generally, second strptime call returns NULL. So, probably, asctime is not an issue.
Hm - are you sure? Did you test it? Do you know the values that are correct (the two 4:00 or the one 4:00 and the one 9:00)?

At least I'm able to demonstrate the problem I mentioned with asctime with a short sample code:
	time_t now;
	time( &now );

	struct tm tmStart, tmEnd;
	memcpy( &tmStart, localtime( &now ), sizeof( struct tm ) );
	now += 3600; // plus 1 hour
	memcpy( &tmEnd, localtime( &now ), sizeof( struct tm ) );

	printf( "Start: %sEnd: %s\n", asctime( &tmStart ), asctime( &tmEnd ) );
	printf( "Start: %s", asctime( &tmStart ) );
	printf( "End: %s", asctime( &tmEnd ) );

Open in new window

If I call this the output shows:
Start: Tue Mar 03 15:40:15 2015
End: Tue Mar 03 15:40:15 2015

Start: Tue Mar 03 15:40:15 2015
End: Tue Mar 03 16:40:15 2015

Open in new window

This is exactly what I expected: Calling asctime twice as arguments for one printf call leads to equal times while calling them in two different printf calls don't.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nusrat NuriyevAuthor Commented:
However, that was not a solution, this information is useful.
I remembered, the problem was in memory management with standard alloca function, there were absolutely wrong memory allocations.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.