Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   time till the resolution of milliseconds (http://www.go4expert.com/forums/time-till-resolution-milliseconds-t8075/)

technosavvy 2Jan2008 15:12

time till the resolution of milliseconds
 
hi ...

i am completely stuck with this ...i am working on a C (and only C) project in which i have to handle the dates till the resolution of milliseconds..

the problem is that using the current ANSI/C89 standard functions i can only get the time till the resolution of seconds...

eg: i want the time to be displayed like 10:12:23:11
but with the set of functions available i can only get the time as 10:12:23

there are few functions like _ftime(windows) and gettimeofday(linux) which gives the answer i desire but these functions do not conform to the ANSI/C89 standard.hence i cannot use them.

expecting a quick help from all of u out there :goofy: :)

Salem 2Jan2008 17:06

Re: time till the resolution of milliseconds
 
You're out of luck if you want total ANSI/C89 AND sub-second accuracy.

technosavvy 2Jan2008 17:15

Re: time till the resolution of milliseconds
 
there must be some way through which i can straight away get the time from the system...
may be using some assembly..

and i am sure that system do returns the time with a millisecond resolution ..that is why function like ftime works..

Salem 2Jan2008 18:57

Re: time till the resolution of milliseconds
 
For sure there IS a way if you resort to IMPLEMENTATION SPECIFIC approaches.

But you're also saying that it also has to be ANSI-C89, and there is no standard function which is guaranteed to be available on all systems.

The question is, do you want to relax your C89 only requirement and use a platform specific API or not?

technosavvy 2Jan2008 20:02

Re: time till the resolution of milliseconds
 
what i am looking for must work both on windows and linux..and should conform to ANSI-C89 standard..

1) there are interpreters called spidermonkey (used by Mozilla) and SEE ...these guys have implemented this thing using C and conforming to standards...even though the source is OPEN i am not able to make out how they have done it .. :worried:

the interpreters are for JavaScript and they have done the above asked things to implementing Date object for javaScript


2) is there any way i can know how these standard C function (time()) get the system time ..i might be able to do something in that case...because what i know is that the system returns a value with a sub second resolution and this function discards the value and only use the value till the seconds resolution..

Salem 2Jan2008 22:35

Re: time till the resolution of milliseconds
 
They do it by having something like

Code:

#ifdef WIN32
int myTime ( void ) {
  int result = ftime();  // or whatever
  return result;
}
#elif POSIX
int myTime ( void ) {
  int result = gettimeofday();  // or whatever
  return result;
}
#else
#error bad platform for myTime
#endif

Except there are better ways of doing it, but that's the idea.

You wrap the implementation specific parts of the code up in a consistent internal API call (in this case, myTime() ), which you can call from the rest of your ANSI-C89 code.

> even though the source is OPEN i am not able to make out how they have done it
Dig deeper, and look for the platform specific API calls. They will be there somewhere for sure.

technosavvy 3Jan2008 10:14

Re: time till the resolution of milliseconds
 
thanks a ton for the help ...

i looked into the OPEN source again and this time i have found that they have done a platform specific implementation ...

the problem is that i have to follow the standard calls because i dont know in which platform the final application will be run ..!! :)
so if i continue with the [B] ifdef [\B] way ..i have to consider quite a lot of platoforms before i am assured that my application will run smoothly..

thanks again for the help..

Salem 3Jan2008 13:31

Re: time till the resolution of milliseconds
 
> because i dont know in which platform the final application will be run
And neither do the applications you've looked at.

You don't just "run" a C program on a machine, you have to compile it first (or 'make' in larger programs). This act of creating a runnable program is where the choice of actual implementation takes place.

> i have to consider quite a lot of platoforms before i am assured that my application will run smoothly..
That's the way it is. Grab any portable library (say http://www.zlib.net/) and you'll see all sorts of portability hackery.

technosavvy 3Jan2008 14:59

Re: time till the resolution of milliseconds
 
>> And neither do the applications you've looked at.
I completely agree :-)

>>You don't just "run" a C program on a machine, you have to compile it first (or 'make' in larger programs). This act of creating a runnable program is where the choice of actual implementation takes place.
I know this and by run i mean the platform on which the application with its library will be compiled and then run

>>That's the way it is. Grab any portable library (say http://www.zlib.net/) and you'll see all sorts of portability hackery.

Thanks ..I will definitely look into it.. :-)


All times are GMT +5.5. The time now is 19:33.