Skip to contentSkip to conversation

time conversion

DISCUSSION Lejanson C. Go 3 participants 3 messages Aug 23, 2005

lore
Share & tools
lore
#0 lore raw

Hi,

I had searched the net on how to convert unsigned long timestamp
to human readable format or maybe in struct timeval format. Any sample code that you can share would be greatly appreciated. Thanks.

Lejanson

#1 lore raw

#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

void print_time ()

{
        struct timeval tv;
        struct tm* ptm;
        char time_string[40];
        long milliseconds;

        gettimeofday (&tv, NULL);
        ptm = localtime (&tv.tv_sec);
        strftime (time_string, sizeof (time_string), "%Y-%m-%d %H:%M:%S",
ptm);
        milliseconds = tv.tv_usec / 1000;
        printf ("%s.%03ld\n", time_string, milliseconds);
}

main()

{
        print_time();
}

I hope this helps.

#2 lore raw
In reply to Lejanson C. Go

I think this may serve u r purpose

#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

int main()
{
time_t t; //Number in Unsigned format

 	printf("% u \n ",t);
	time(&t);
	printf("%s",ctime(&t)); //  Time in Human readable format
	return 0;
}
Quoted 13 lines · Lejanson C. Go

Hi,

I had searched the net on how to convert unsigned long timestamp
to human readable format or maybe in struct timeval format. Any sample code that you can share would be greatly appreciated. Thanks.

Lejanson

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 1 line
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/