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. Advertisements

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. Maxim S. Lee

    How can I control UTC offset?

    Maxim S. Lee, Oct 16, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    627
    Dino Chiesa [Microsoft]
    Oct 27, 2004
  2. Buddy Ackerman

    Getting local client UTC offset

    Buddy Ackerman, May 11, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    760
    Buddy Ackerman
    May 11, 2005
  3. Lance Riedel

    Translated Offset to Source Offset

    Lance Riedel, Oct 14, 2003, in forum: XML
    Replies:
    2
    Views:
    692
    Patrick TJ McPhee
    Oct 15, 2003
  4. =?Utf-8?B?aGVuaw==?=

    Covert UTC String to UTC Datetime

    =?Utf-8?B?aGVuaw==?=, May 7, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    19,087
    Egbert Nierop \(MVP for IIS\)
    May 7, 2006
  5. __jakal__

    obtaining the time offset from UTC

    __jakal__, May 10, 2005, in forum: C Programming
    Replies:
    4
    Views:
    646
    Dave Thompson
    May 16, 2005
  6. Replies:
    1
    Views:
    1,013
    Paul Boddie
    Mar 11, 2007
  7. Replies:
    0
    Views:
    695
  8. BjörnHolmberg

    Want to get rid of UTC offset in serialization of DateTime

    BjörnHolmberg, Aug 5, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    251
    BjörnHolmberg
    Aug 5, 2004
Loading...