Thread: Local time_t

  1. #1
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

    Local time_t

    I need a 'local time_t'. Currently, I've got the following code. Anyone got better code that won't crap out at dst changing time?

    Code:
    // Work out the time adjustment for this timeT...
    if ( (ptm = gmtime(&timeT)) == NULL ) return E_FAIL;
    if ( (timeTTemp = mktime(ptm)) == (time_t) -1 ) return E_FAIL;
    
    timeT = (timeT - timeTTemp);
    Thanks.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Well, I think I have a working solution. Can anybody see any situation where this will fail?
    Code:
    	localtm = *localtime(&timeT);
    	utctm = *gmtime(&timeT);
    
    	localtm.tm_isdst = 0;
    	utctm.tm_isdst = 0;
    
    	timeTLocal = mktime(&localtm);
    	timeTUtc = mktime(&utctm);
    
    	timeT += timeTLocal - timeTUtc;
    It basically gets the broken down local and gmt time and then uses mktime to get time_t's so the difference can be found. isdst is set to 0 so they both should be adjusted by the same amount in calls to mktime.

  3. #3
    root
    Join Date
    Sep 2003
    Posts
    232
    >timeT += timeTLocal - timeTUtc;
    difftime, perhaps?
    Code:
    timeT += difftime(timeTLocal, timeTUtc);
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >difftime, perhaps?

    Why?

  5. #5
    root
    Join Date
    Sep 2003
    Posts
    232
    >Why?
    Because you don't know how time_t may be represented and it describes your intentions more clearly.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Local and Roaming Profiles
    By BobS0327 in forum Tech Board
    Replies: 6
    Last Post: 01-20-2008, 09:03 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Advice: Webserver to local database
    By zackr in forum Networking/Device Communication
    Replies: 1
    Last Post: 12-07-2005, 08:16 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM