Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8299137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:02:29+00:00 2026-06-08T16:02:29+00:00

I was wondering whether there is a function in C that takes time in

  • 0

I was wondering whether there is a function in C that takes time in the following format (current date and time in seconds are in Epoch format)

a.1343323725
b.1343326383

And returns me the result as difference between the two time as hrs:mins:secs


Sorry for any confusion, to clarify my point I wrote a code that gets that gets the user’s current time execute some block of code and gets the time again. And I was wondering whether the difference could be converted into hrs:mins:sec as a string literal.

#include <sys/time.h>
#include <stdio.h>
int main(void)
{
        struct timeval tv = {0};
        gettimeofday(&tv, NULL);
        printf("%ld \n", tv.tv_sec);
        //Execte some code
         gettimeofday(&tv, NULL);
        return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:02:33+00:00Added an answer on June 8, 2026 at 4:02 pm

    First you want to get the time difference in seconds out of the timeval structures that those functions return, using something like this:

    int diff = a.tv_sec-b.tv_sec;
    

    Where a and b were the values returned by gettimeofday.

    Next you want to break that down into units of hours, minutes and seconds.

    int hours=diff/3600;
    int minutes=(diff/60)%60;
    int seconds=diff%60;
    

    Finally we want to get that data into a string, using the snprintf function from

    #include <stdio.h>
    char output[10];
    snprintf(output, 10, "%d:%d:%d", hours, minutes, seconds);
    

    sprintf words exactly like printf, except the output goes into a string, not onto stdout, and snprintf is the same except it won’t write more than n characters into the string, to prevent buffer overflows.
    Stitch those together and you’ve got the job done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering how to approach writing a function that would return whether there
I intend to change certain features/functions in Emacs. I was wondering whether there is
I am wondering whether there is a vi like editor under windows command line?
I'm wondering whether there is a way to poll a socket in c# when
I was wondering whether there is a simple PHP library to test the loading
I was wondering whether there is a way to check when the database was
I'm using TFS 2010 and am wondering whether there is an easy way, either
A recent problem* left me wondering whether there is a text editor out there
I am taking my first steps with Node.js and I was wondering whether there
Possible Duplicate: Android ImageButton with a selected state? I was wondering whether there is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.