obtaining the time offset from UTC

Discussion in 'C Programming' started by __jakal__, May 10, 2005.

  1. __jakal__

    __jakal__ Guest

    Hello,
    I need to find out the time difference between UTC and local time.
    I am doing it the following way

    #include <sys/timeb.h>
    #include <stdio.h>
    int main()
    {
    struct timeb tp;
    ftime(&tp);
    printf("%d", tp.timezone);
    }

    But the problem is that the timezone value is not adjusted for daylight
    saving and always returns local time zone offset.
    My TZ environment variable is set to MET
    Now the DST rules apply and the offset should be +2 hours
    When daylight saving is not in effect, then the offset should be +1 hour.

    Is there any way to find out the correct offset . i.e. it should return +2
    when DST is in effect and +1 otherwise

    Regards,
    deep
    __jakal__, May 10, 2005
    #1
    1. Advertising

  2. __jakal__

    Eric Sosman Guest

    __jakal__ wrote:
    > Hello,
    > I need to find out the time difference between UTC and local time.
    > I am doing it the following way
    >
    > #include <sys/timeb.h>
    > #include <stdio.h>
    > int main()
    > {
    > struct timeb tp;
    > ftime(&tp);
    > printf("%d", tp.timezone);
    > }
    >
    > But the problem is that the timezone value is not adjusted for daylight
    > saving and always returns local time zone offset.
    > My TZ environment variable is set to MET
    > Now the DST rules apply and the offset should be +2 hours
    > When daylight saving is not in effect, then the offset should be +1 hour.
    >
    > Is there any way to find out the correct offset . i.e. it should return +2
    > when DST is in effect and +1 otherwise


    First, the C Standard makes no guarantees about how
    good the system's timekeeping is. Some systems keep track
    of time zones and seasonal adjustments, while others do not
    and must have their clocks adjusted manually. (A very few
    systems have no clocks at all.) From C's point of view,
    then, your problem may not be solvable.

    However, if you are using a system that keeps track of
    such things, you can determine the current UTC offset with
    something like (error-checking omitted):

    time_t now = time(NULL);
    struct tm lcl = *localtime(&now);
    struct tm gmt = *gmtime(&now);
    printf ("%s\t%d\t%d\n", "year", lcl.tm_year, gmt.tm_year);
    printf ("%s\t%d\t%d\n", "yday", lcl.tm_yday, gmt.tm_yday);
    printf ("%s\t%d\t%d\n", "hour", lcl.tm_hour, gmt.tm_hour);
    printf ("%s\t%d\t%d\n", "min", lcl.tm_min, gmt.tm_min);
    printf ("%s\t%d\t%d\n", "sec", lcl.tm_sec, gmt.tm_sec);

    Examine the corresponding elements of `lcl' and `gmt' to
    determine your UTC offset.

    --
    Eric Sosman, May 10, 2005
    #2
    1. Advertising

  3. __jakal__

    CBFalconer Guest

    __jakal__ wrote:
    >
    > I need to find out the time difference between UTC and local time.
    > I am doing it the following way
    >
    > #include <sys/timeb.h>


    No such standard header.

    > #include <stdio.h>
    > int main()
    > {
    > struct timeb tp;


    No such standard type

    > ftime(&tp);


    No such standard function.

    > printf("%d", tp.timezone);
    > }
    >
    > But the problem is that the timezone value is not adjusted for
    > daylight saving and always returns local time zone offset.


    Which makes your code entirely non-portable. The things available
    to you via <time.h> are detailed below, in an excerpt from N869:

    B.22 Date and time <time.h>
    NULL size_t *time_t
    CLOCKS_PER_SEC clock_t struct tm
    clock_t clock(void);
    double difftime(time_t time1, time_t time0);
    time_t mktime(struct tm *timeptr);
    time_t time(time_t *timer);
    char *asctime(const struct tm *timeptr);
    char *ctime(const time_t *timer);
    struct tm *gmtime(const time_t *timer);
    struct tm *localtime(const time_t *timer);
    size_t strftime(char * restrict s,
    size_t maxsize,
    const char * restrict format,
    const struct tm * restrict timeptr);

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, May 10, 2005
    #3
  4. CBFalconer wrote:
    >
    > __jakal__ wrote:
    > >
    > > I need to find out the time difference between UTC and local time.
    > > I am doing it the following way
    > >
    > > #include <sys/timeb.h>

    >
    > No such standard header.
    >
    > > #include <stdio.h>
    > > int main()
    > > {
    > > struct timeb tp;

    >
    > No such standard type
    >
    > > ftime(&tp);

    >
    > No such standard function.
    >

    "return to sender...no such number, no such home" -- Elvis

    --
    +----------------------------------------------------------------+
    | Charles and Francis Richmond It is moral cowardice to leave |
    | undone what one perceives right |
    | richmond at plano dot net to do. -- Confucius |
    +----------------------------------------------------------------+
    Charles Richmond, May 11, 2005
    #4
  5. On Tue, 10 May 2005 10:24:39 -0400, Eric Sosman <>
    wrote:

    >
    >
    > __jakal__ wrote:
    > > Hello,
    > > I need to find out the time difference between UTC and local time.
    > > I am [using (POSIX) struct timeb and ftime()]
    > > But the problem is that the timezone value is not adjusted for daylight
    > > saving and always returns local time zone offset.
    > > My TZ environment variable is set to MET
    > > Now the DST rules apply and the offset should be +2 hours
    > > When daylight saving is not in effect, then the offset should be +1 hour.
    > >
    > > Is there any way to find out the correct offset . i.e. it should return +2
    > > when DST is in effect and +1 otherwise

    >
    > First, the C Standard makes no guarantees about how
    > good the system's timekeeping is. Some systems keep track
    > of time zones and seasonal adjustments, while others do not
    > and must have their clocks adjusted manually. (A very few
    > systems have no clocks at all.) From C's point of view,
    > then, your problem may not be solvable.
    >

    And if it does, standard C doesn't require it to use env var TZ.
    POSIX/SUS does, and provides ftime() and struct timeb, which also
    includes member dstflag, which is nonzero when (for a time that) DST
    is in effect (assuming that the daylight/summer change is always +1hr,
    which is very common but probably not universal).

    > However, if you are using a system that keeps track of
    > such things, you can determine the current UTC offset with
    > something like (error-checking omitted):
    >
    > time_t now = time(NULL);
    > struct tm lcl = *localtime(&now);
    > struct tm gmt = *gmtime(&now);

    <snip>
    > Examine the corresponding elements of `lcl' and `gmt' to
    > determine your UTC offset.


    Or just look at lcl.tm_isdst to determine if there is _any_
    daylight/summer in effect and if so assume +1hr.

    These are I believe the only fully-standard-C ways. Note that
    localtime(), and its difference from gmtime(), can be applied to times
    other than the current time, if you wish.

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, May 16, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?aGVuaw==?=

    Covert UTC String to UTC Datetime

    =?Utf-8?B?aGVuaw==?=, May 7, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    18,700
    Egbert Nierop \(MVP for IIS\)
    May 7, 2006
  2. Time offset from UTC

    , Nov 14, 2005, in forum: C Programming
    Replies:
    0
    Views:
    370
  3. Replies:
    1
    Views:
    771
    Paul Boddie
    Mar 11, 2007
  4. Replies:
    0
    Views:
    582
  5. Skip Montanaro
    Replies:
    0
    Views:
    454
    Skip Montanaro
    Jan 31, 2010
Loading...

Share This Page