We help IT Professionals succeed at work.

Absolute madness with managing time in C

Nusrat Nuriyev
Nusrat Nuriyev asked
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.
Watch Question

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,



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.



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.