Is there any way to get miliseconds and its fraction part from 1970 using time.h in c language??
#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;
}
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.
For Unix and Linux you could use gettimeofday.
For Win32 you could use GetSystemTimeAsFileTime and then convert it to time_t + milliseconds.
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 */
};
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;