Can anyone suggest a way to convert the result from gettimeofday() which is number of seconds to a UNIX integer timestamp that looks like for example, 20030411021539, on April 11, 2003 at 2:15:39am?
Thanks!
Printable View
Can anyone suggest a way to convert the result from gettimeofday() which is number of seconds to a UNIX integer timestamp that looks like for example, 20030411021539, on April 11, 2003 at 2:15:39am?
Thanks!
Thanks..I actually wanted the timestamp to look a UNIX integer, i.e, 20030411021539.
Any other ideas are appreciated!
Thanks! One more thing, how would I add milliseconds to that?
:-)
Thanks again.
I can't get the milliseconds to be part of the long long double, i.e 20030411110933.456, where 456 is the milliseconds. Maybe I am making this more complicated than it needs to be...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
int main( ) {
struct timeval tstr;
struct tm *tmstr;
char buf[128];
long long double milli;
long long double ll,num;
if( gettimeofday( &tstr, NULL ) != 0 ) {
perror( "gettimeofday" );
return 1;
}
tmstr = localtime( &tstr.tv_sec);
strftime( buf, sizeof( buf ), "%Y%m%d%I%M%S", tmstr );
milli=(long long double)(tstr.tv_usec/1000.0);
num=(long long double)strtoll(buf,NULL,10);
ll =(long long double)(0.0+num);
ll=(long long double)(ll+milli/1000.0);
printf( "%.3llf\n",(long long double)ll );
return 0;
}
AFAIK, there is no long long double, just long double. Try that.
I don't know why they're bothering with floating point values anyway. A millisecond is 1/1000th. As such, since it itself is never a floating point value, just use an integer for it. Use an integer for minutes, one for seconds, one for milliseconds. Why bother with floats?
Quzah.
vVv: I ran the code, but I need the stamp to show milliseconds as well. long long is supported, but I thought since I want the milliseconds to appear after the decimal point, I have to make it a double as well...since it won't work with just long long.
quzah: I want the output to look like: 20030411110933.456.
How do I get that unless I make the type some sort of float?
Thanks guys.