Time offset from UTC

Discussion in 'C Programming' started by sags5495@gmail.com, Nov 14, 2005.

  1. Guest

    Hello all,

    For a program I want to fix, I need to find a portable way to get
    the offset from local time to UTC (coming from the Time Zone plus
    any daylight savings in effect).

    At first glance, the following seems to be OK:

    time_t t;
    int timeoffset;
    t = time (NULL);
    timeoffset = (int) difftime (t, mktime(gmtime(&t))); /* seconds */

    My concern is the above code's portability. I'm thinking at:

    (a) Is mktime() always the inverse of localtime(), or it may use
    implementation-dependent fields in struct tm, that may specify
    TZ info, for it's calculations? (In such a case, gmtime() may store

    UTC+00:00 in the structure, and I get timeoffset always 0.)
    (b) Assuming localtime() succeeds, is there any good reason for
    gmtime()
    to fail? (like a system and C run-time that has absolutely no
    knowledge about time-zones, not even an arbitray default.)

    Any comments, suggestions? Is there a better way?

    Thanks in advance,
    sags
     
    , Nov 14, 2005
    #1
    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,815
    Egbert Nierop \(MVP for IIS\)
    May 7, 2006
  2. __jakal__

    obtaining the time offset from UTC

    __jakal__, May 10, 2005, in forum: C Programming
    Replies:
    4
    Views:
    511
    Dave Thompson
    May 16, 2005
  3. Replies:
    1
    Views:
    831
    Paul Boddie
    Mar 11, 2007
  4. Replies:
    0
    Views:
    613
  5. Skip Montanaro
    Replies:
    0
    Views:
    478
    Skip Montanaro
    Jan 31, 2010
Loading...

Share This Page