Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
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);
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
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.
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
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).
 

sord

macrumors 6502
Jun 16, 2004
352
0
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)
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
Thanks for all the replies its appreciated,

I used the information Eraserhead linked to, and I managed to solve my problem.
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
can you post your solution as clock_gettime is not available for mac osx. thanks!

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);

	

		
	
}
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
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.

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.
 

ramzez

macrumors member
Feb 25, 2008
31
0
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);

	

		
	
}

thanks, would be ok to use it with pthread_cond_timedwait() ? if not what would be the best one to use?
 

laprej

macrumors regular
Oct 14, 2005
108
2
Troy, NY
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.