Comparing timeofday in C

This is where general Agony discussion takes place.

Moderator: Staff

User avatar
End Game
Posts: 582
Joined: Mon May 25, 2009 6:08 pm
Location: Austin, TX

Comparing timeofday in C

Postby Xephos » Thu Sep 16, 2010 11:10 am

I haven't written in C in god knows how long and I have to use "timestamps" to find the travel time of a message from when I send it until I receive it again. For the life of me I cannot figure out how to get and compare the time of day in C so I can do this.

I also realize that a time of day function my not be the best solution for this, it was just a suggestion from the professor.

Any help would be sweet.

Permanent Agony FNG
"I only wanna do it once, and I want to be strapped into a dude"

End Game
Posts: 1472
Joined: Fri Nov 04, 2005 1:28 pm
Location: San Diego, CA

Postby remagi » Thu Sep 16, 2010 4:09 pm

Code: Select all

#include <time.h>

int main(void)
    time_t     now;
    time_t     then;
    double     diff_in_seconds;

    then = time(NULL);

    /* do stuff here, event comes back */

    now = time(NULL);
    diff_in_seconds = difftime( now, then );

There are often substantially more efficient ways to access timers on a given platform, but if you want straight C, time_t and time() are the complete standard way.

On most platforms you can just subtract a time_t value from another, as they most often doubles, but difftime is the portable way.


"Please don't lock up these words." - Lenny Bruce

Return to “Agony General”

Who is online

Users browsing this forum: No registered users and 3 guests