GMT equivalent of mktime

R

Rajmohan

Hi All,

I need help with date and time conversions. gmtime() gives struct tm and
localtime() gives the same with local correction. Is there a GMT equivalen
of mktime() also? Here is my sample program.

***********************
time_t now, NOW;
struct tm *today;

time(&now);
printf ("now: %d\n", now);

today = localtime(&now);

NOW = mktime (today);

printf ("NOW: %d\n", NOW);
*************************
And the Output is:
now: 1058194848
NOW: 1058194848

Now if i use gmtime() instead of localtime(), the output is:
now: 1058194815
NOW: 1058175015

The time_t value is not the same. If i want to use gmtime(), then is there a
GMT equivalent api for mktime? Why is this disparity in values?

thanks in advance,
 
M

Mark McIntyre

Hi All,

I need help with date and time conversions.
***********************
time_t now, NOW;

aside: not a good idea to have variables whose names differ only in
case. Also not a good idea to use ALL CAPS for a variable -
traditionally in C they're reserved for constants.
NOW = mktime (today);
And the Output is:
now: 1058194848
NOW: 1058194848

Now if i use gmtime() instead of localtime(), the output is:
now: 1058194815
NOW: 1058175015

But the two times are different, one is 11pm and the other 10pm, or
whatever, so you'd expect them to have different time_t values.

In any events, as far as I'm aware, the precise handling of local time
conversions is implementation defined. Therefore you should use
consistent conventions. Either stick to GMT or stick to local time,
don't switch between them during your conversions or you will get
confused.
 

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,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top