We help IT Professionals succeed at work.
Get Started

Absolute madness with managing time in C

Nusrat Nuriyev
Last Modified: 2015-03-06
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
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE