tags:

views:

304

answers:

5

Is there any way to get miliseconds and its fraction part from 1970 using time.h in c language??

+1  A: 
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t seconds;

  seconds = time (NULL);
  printf ("%ld milliseconds since January 1, 1970", seconds*1000);

  return 0;
}
codaddict
Thank bzabhi for your answer. Can you tell me that I there any way to get the friction part of the seconds? Means I execute your code I give me some thing like this 56464654. I want to get exact milisecond like 4646464654.68764 this. I think you understand what I want to say you.
Arman
Guys, be careful with UTC. It has leap second and stuff like that.. Make sure your API is good enough for what you are doing.
Pavel Radzivilovsky
@Arman: As pavium clearly mentiones in the other post it is not possible, this is the best you can do and the ms value will always be a whole number and a multiple of 1000.
codaddict
@Bzabhi, any other library which make it possible??
Arman
Actually time_t is not required by the standard to be an integer type. It could be a double (and so if you're going to print it with %ld, in theory you should cast it). I don't know whether any implementations actually use a non-integral time_t, though.
Steve Jessop
Also, `time()` function returns "the current calendar time. The encoding of the value is unspecified." Posix guarantees that it's number of seconds since the epoch, but C doesn't.
Alok
+1  A: 

Unix time or Posix time is the time in seconds since the epoch you mentioned.

bzabhi's answer is correct: you simply multiply the Unix timestamp by 1000 to get milliseconds.

Be aware that all millisecond values returned by relying on the Unix timestamp will be multiples of 1000 (like 12345678000). The resolution is still only 1 second.

You can't get the fraction part

The comment from Pavel is correct also. The Unix timestamp does not take into account leap seconds. This makes it even less wise to rely on a conversion to milliseconds.

pavium
Any other library which can get the exact milisecond including its fraction part?????
Arman
The Unix timestamp is about as fundamental as we can go. It must be that the designers of Unix thought one second resolution was enough. Then again the overhead of maintaining a 1ms resolution was probably beyond early Unix systems.
pavium
+1  A: 

For Unix and Linux you could use gettimeofday.

For Win32 you could use GetSystemTimeAsFileTime and then convert it to time_t + milliseconds.

dalle
gettimeofday() does what is needed. Here's a code example: http://www.docs.hp.com/en/B9106-90009/gettimeofday.2.htmlSo what OSes is Aman trying to support?
tpgould
A: 

It's not standard C, but gettimeofday() is present in both SysV and BSD derived systems, and is in POSIX. It returns the time since the epoch in a struct timeval:

struct timeval {
    time_t      tv_sec;     /* seconds */
    suseconds_t tv_usec;    /* microseconds */
};
caf
+1  A: 

If you want millisecond resolution, you can use gettimeofday() in Posix. For a Windows implementation see gettimeofday function for windows.

#include <sys/time.h>

...

struct timeval tp;
gettimeofday(&tp);
long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
Plow