hellp about local timestamp

Z

Zhaohui

hello everyone:

How can I get local timestamp use c++/c, I only know is time(), but
time() returns a UTC timestamp, I want get local timestamp, how can
I do, I am under windows system.

thanks.

Zhaohui
 
A

Alf P. Steinbach

* Zhaohui:
hello everyone:

How can I get local timestamp use c++/c, I only know is time(), but
time() returns a UTC timestamp, I want get local timestamp, how can
I do, I am under windows system.

I would check the documentation.
 
B

babi

Hi,
Have a look at this program which may help you.
I tried to print the time.

Thanks & Regds,
T.Mohanta

cat time.c

#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;
double sec;
double secs;

/* Obtain the time of day, and convert it to a tm struct.
*/
gettimeofday (&tv, NULL);
ptm = localtime (&tv.tv_sec);
/* Format the date and time, down to a single second. */
strftime (time_string, sizeof (time_string), "%Y-%m-%d
%H:%M:%S",
ptm);
/* Compute milliseconds from microseconds. */
milliseconds = tv.tv_usec / 1000;
sec = tv.tv_sec;
/* Print the formatted time, in seconds, followed by a
decimal
point
* and the milliseconds. */

printf ("The sec--- value %d\n", sec);
printf ("%s\n", time_string);
printf ("The sec value %d\n", ptm->tm_sec);
printf ("%s.%03ld\n", time_string, milliseconds);
printf ("The min value ======%d\n", ptm->tm_min);
//printf ("%s.%03ld\n", time_string, milliseconds);
printf ("%d\n", time_string, milliseconds);
sleep(2);
gettimeofday (&tv, NULL);
secs = tv.tv_sec;
ptm = localtime (&tv.tv_sec);
/* Format the date and time, down to a single second. */
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);
printf ("The sec--- value %d\n", secs);
printf ("The sec value %d\n", ptm->tm_sec);
printf ("The min value %d\n", ptm->tm_min);
printf ("The diff value %d\n", (secs-sec));
}
int main ()
{
print_time();
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top