time till the resolution of milliseconds

technosavvy's Avatar
Contributor
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
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
You're out of luck if you want total ANSI/C89 AND sub-second accuracy.
technosavvy's Avatar
Contributor
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's Avatar, Join Date: Nov 2007
Ambitious contributor
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's Avatar
Contributor
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 ..

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's Avatar, Join Date: Nov 2007
Ambitious contributor
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's Avatar
Contributor
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's Avatar, Join Date: Nov 2007
Ambitious contributor
> 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's Avatar
Contributor
>> 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.. :-)