Difference between local time and UTC

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Generic Usenet Account

    Difference between local time and UTC

    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(&localEp och);

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

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

  • Eric Sosman

    #2
    Re: Difference between local time and UTC



    Generic Usenet Account wrote On 05/22/06 15:42,:[color=blue]
    > 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(&localEp och);
    >
    > /* Convert gm time in to epoch format */
    > gmEpoch = mktime(gmTime);
    >
    > /* get the absolute different between them */
    > return abs(gmEpoch - localEpoch);[/color]

    Use difftime(gmEpoc h, 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.

    --
    Eric.Sosman@sun .com

    Comment

    Working...