Difference between local time and UTC

  • Thread starter Generic Usenet Account
  • Start date
G

Generic Usenet Account

I am trying to find the best way to get the difference between UTC and
the local time? Here's a small code snippet that I have written for
this purpose. Is there a better way to do this?

Thanks,
Bala

/*************************************************************************/
time_t getTimeOffset()
{
struct tm *gmTime;

time_t localEpoch, gmEpoch;

/*First get local epoch time*/
localEpoch = time(NULL);

/* Using local time epoch get the GM Time */
gmTime = gmtime(&localEpoch);

/* Convert gm time in to epoch format */
gmEpoch = mktime(gmTime);

/* get the absolute different between them */
return abs(gmEpoch - localEpoch);
}
 
E

Eric Sosman

Generic Usenet Account wrote On 05/22/06 15:42,:
I am trying to find the best way to get the difference between UTC and
the local time? Here's a small code snippet that I have written for
this purpose. Is there a better way to do this?

Thanks,
Bala

/*************************************************************************/
time_t getTimeOffset()
{
struct tm *gmTime;

time_t localEpoch, gmEpoch;

/*First get local epoch time*/
localEpoch = time(NULL);

/* Using local time epoch get the GM Time */
gmTime = gmtime(&localEpoch);

/* Convert gm time in to epoch format */
gmEpoch = mktime(gmTime);

/* get the absolute different between them */
return abs(gmEpoch - localEpoch);

Use difftime(gmEpoch, localEpoch) instead, and have
the function return a double. This makes things work
even on systems where time_t may be encoded in some other
way than a count of seconds elapsed since a Zero time.

Some error-checking would be a good idea, too. Yes,
there are systems that don't have calendar clocks, and
even among those that do there are some that simply run
their clocks in local time and have no notion of other
time zones.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top