Time in milliseconds
I've searched the topic on google and this website, but i could not find the exact answer what i was looking for.
For a certain scenario in my application (timestamps related) i need time in milliseconds. What i am currently doing is to get time in seconds using time() function and convert it into milliseconds by multiplying it with 1000.
But the problem is that time_t is signed long (32 bit). When i multiply time_in_seconds with 1000, it overflows 32 bits and the resulting time in milliseconds becomes -ve.
Please tell me how can i get time in milliseconds ? Also, i don't want processor time taken, i want wallclock time.
If you only need time(), then cast it to unsigned long long, then multiply it by 1000. That way it won't overflow.
But... it seems you might need more precision if you're targeting milliseconds...
Perhaps you can use long-long. In at least Linux, you have gettimeofday - it may exist in other Unix variants too. It gives you time in microseconds (although it may jump in steps of several microseconds at a time).
Isn't there any function on Unix or Linux that gives time from start of the process rather than the epoch? Such a long time period from 1970 to now makes it so big in magnitude.
Just take the time at the start of main, and then show the difference between that and "current time" when you want to show how long you've been running [or some such].
Originally Posted by coder_009