Ansi C, getting the current time

Discussion in 'Mac Programming' started by MacDonaldsd, Jan 4, 2008.

  1. MacDonaldsd macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #1
    I need to get the time at certain intervals of my programme.

    The problem is I need to get the time a lot, so I also the amount of nano seconds, or all the times are the same.

    Im currently using the method below which prints the value in hh:mm:ss

    Code:
    time_t     now;
        struct tm  ts;
        char       buf[80];
    
        // Get the current time
        time(&now);
    
    
        // Format and print the time, "hh:mm:ss "
        ts = *localtime(&now);
        strftime(buf, sizeof(buf), "%H:%M:%S", &ts);
        printf("%s\n", buf);
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Stick it in a funtion which returns the time and then just call that funtion when you want the time?
     
  3. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #3
    Sorry my mistake if I didn't make it clear,

    My problem is the accuracy is not enough, I already have it in a function.
     
  4. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
  5. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #5
    I don't think the standard C library includes time fucntions with nanosecond or microsecond accuracy. It does have some functions that deal with "ticks" but the actual length of a tick is platform dependent. In my experience they are usually on the order of 1/60th sec which probably won't work for you.

    I know you said you wanted an ANSI C solution, but if you can wided your requirements a little, look at the timer functions available in the C POSIX library: <sys/time.h>. Much of the POSIX library is implemented on many platfroms. POSIX's implemented in glibc, for example (I think).
     
  6. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #6
    How about clock_gettime?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, char *argv[])
    {
    	struct timespec *thetime = (struct timespec *)malloc(sizeof(struct timespec));
    	if (clock_gettime(CLOCK_REALTIME, thetime))
    		printf("Hmm...it failed...\n");
    	else
    		printf("Seconds: %ld, nano: %ld\n", thetime->tv_sec, thetime->tv_nsec);
    	free(thetime);
    	return 0;
    }
    
    Make sure you link with librt (gcc -o file file.c -lrt)
     
  7. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #7
    Thanks for all the replies its appreciated,

    I used the information Eraserhead linked to, and I managed to solve my problem.
     
  8. ramzez macrumors member

    Joined:
    Feb 25, 2008
    #8
    can you post your solution as clock_gettime is not available for mac osx. thanks!
     
  9. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #9
    Hi its been a long time since I actually looked at this code but you can have it all the same.

    The variable current time buffer is char array.

    char currentTimeBuffer[30];



    Code:
    
    
    void getTheTime()
    {
    	/*Global variable currentTimeBuffer and is over written when this function is called
    	 with the current time*/
     	time_t     now;
        struct tm  ts;
    	
        // Get the current time
        time(&now);
        ts = *localtime(&now);
    	
    	/*strftime taken from "The C programming language"
    	Formats the time HH:MM:SS*/
        strftime(currentTimeBuffer, sizeof(currentTimeBuffer), "%H:%M:%S", &ts);
    
    	
    
    		
    	
    }
    
     
  10. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    ANSI C will probably not be good enough for what you want. On MacOS X (whether you are using Carbon or Cocoa) you can use the three CoreFoundation functions

    CFAbsoluteTimeGetCurrent
    CFTimeZoneCopySystem
    CFAbsoluteTimeGetGregorianDate

    CFAbsoluteTimeGetCurrent gets the current time in seconds since some fixed starting data, with about nanoseconds resolution. That may be good enough for many purposes; for example if you want to know what was the time between two mouse clicks. The time is in GMT (Greenwhich Mean Time), so it is independent of your time zone.

    CFAbsoluteTimeGetGregorianDate converts an absolute time into years, months, days, hours, minutes, seconds (seconds with nanosecond resolution). You can pass a time zone to this function, otherwise the time is in GMT again.

    CFTimeZoneCopySystem gives you the system time zone to pass to CFAbsoluteTimeGetGregorianDate; so you get the time where _you_ are. That's what you would use to display a clock.
     
  11. ramzez macrumors member

    Joined:
    Feb 25, 2008
    #11
    thanks, would be ok to use it with pthread_cond_timedwait() ? if not what would be the best one to use?
     
  12. laprej macrumors regular

    Joined:
    Oct 14, 2005
    Location:
    Troy, NY
    #12
    You can use the time stamp counter. Intel has an instruction for it - rdtsc. There's some inline assembly on the Wikipedia page for it, too.

    RDTSC

    Basically the TSC is incremented at every cycle so, ideally, no two should ever be the same. If you need to know how long that actually is, you can divide the 64-bit return value by your core clock rate.
     

Share This Page