Nusrat Nuriyev
asked on
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));
}
Output:
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.
How?
ASKER
No.
Generally, second strptime call returns NULL. So, probably, asctime is not an issue.
Generally, second strptime call returns NULL. So, probably, asctime is not an issue.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
I remembered, the problem was in memory management with standard alloca function, there were absolutely wrong memory allocations.
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
Open in new window
with this codeOpen in new window
to find out if this is the problem.Hope that helps,
ZOPPO