Like Tree1Likes
  • 1 Post By cyberfish

Makes Time() to return the time in microseconds

This is a discussion on Makes Time() to return the time in microseconds within the C Programming forums, part of the General Programming Boards category; is it possible to get the number of microseconds that passed since 1.1.1970? I need it to recored input off ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    8

    Makes Time() to return the time in microseconds

    is it possible to get the number of microseconds that passed since 1.1.1970?
    I need it to recored input off parallel port.

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    Not in a portable fashion. The standard time() function can return the time in any resolution (seconds, microseconds, hours, whatever), and needn't be from the epoch.

    You'll have to look at platform-specific functions. See gettimeofday() if you're on a POSIX system.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    8
    I'm running ubuntu linux

  4. #4
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    time_t is the number of seconds since the epoch on ubuntu. With the number of seconds, it is easy to get the microseconds by multiplying that value by 1,000,000. Just be careful about overflowing whatever int type you are using.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    8
    I tried this, but this returns "1306944180000000" (it just second times million, not microseconds)

    Code:
    while(1){
    
        time_t current_time = time(0);
        printf("%ld \n",current_time*1000000);
    
    
    }

  6. #6
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    There are 1 million microseconds in 1 second. And I do not think time_t can hold that result. You probably need a bigger type.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    8
    yes, i know that there are 1 million microseconds in 1 second, I tried to use LONG but this didn't work as well...

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It would probably be a better idea to maintain two variables... the number of seconds since the epoch and a separate for the number of microseconds in *this* second.

    It can be artificially represented for display with ... printf("%d.%d",seconds,microseconds);
    Last edited by CommonTater; 06-01-2011 at 11:34 AM.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    8
    Found a solution

    Code:
    #include <sys/time.h>
    #include <stdio.h>
    #include <unistd.h>
    int main()
    {
        struct timeval start, end;
    
        long mtime, seconds, useconds;    
    
        gettimeofday(&start, NULL);
        usleep(2000);
        gettimeofday(&end, NULL);
    
        seconds  = end.tv_sec  - start.tv_sec;
        useconds = end.tv_usec - start.tv_usec;
    
        mtime = seconds + useconds;
    
        printf("Elapsed time: %ld microsecons\n", mtime);
    
        return 0;
    }
    Last edited by kirill578; 06-01-2011 at 12:21 PM.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Code:
    mtime = seconds + useconds;
    Doesn't look right to me...
    stahta01 likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-21-2009, 01:02 AM
  2. Determine process time in microseconds
    By kenkoh in forum C Programming
    Replies: 9
    Last Post: 02-18-2008, 02:16 PM
  3. how to convert return type of function at run time ?
    By vinod_mrd in forum C++ Programming
    Replies: 26
    Last Post: 01-26-2008, 11:49 AM
  4. Execution Time in Microseconds?
    By thetinman in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2007, 02:32 PM
  5. Return the system time!
    By Arkanos in forum C++ Programming
    Replies: 5
    Last Post: 04-14-2006, 08:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21