mktime() doesn't return meaningful value!

This is a discussion on mktime() doesn't return meaningful value! within the C Programming forums, part of the General Programming Boards category; Dear there, mktime() didn't work? please take a close look at the outpout, which shows that "tt=-1", while "ft" has ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    mktime() doesn't return meaningful value!

    Dear there,

    mktime() didn't work? please take a close look at the outpout, which shows that "tt=-1", while "ft" has been assigned value by mktime. And how could both says in year 2000? Apprantly I want one year in 2000, another in 2050. Any ideas? Thanks!

    Code:
    int main(int argc, char **argv){
    
    struct tm f;
    struct tm t;
    time_t ft;
    time_t tt;
    
    //2000-1-1 00:00:00
    f.tm_sec = 0;
    f.tm_min = 0;
    f.tm_hour = 0;
    f.tm_mday = 1;
    f.tm_mon = 0;
    f.tm_year = 100;
    f.tm_gmtoff = 0;
    
    //2050-1-1 00:00:00
    t.tm_sec = 0;
    t.tm_min = 0;
    t.tm_hour = 0;
    t.tm_mday = 1;
    t.tm_mon = 0;
    t.tm_year = 150;
    t.tm_gmtoff = 0;
    
    ft = mktime(&f);
    tt = mktime(&t);
    
    printf("get all records from %s\t%ld \n to %s\t%ld\n", asctime(&f), ft, asctime(&t), tt);
    }
    output:-------------------------------------------------------------------
    get all records from Sat Jan 1 00:00:00 2000
    946702800
    to Sat Jan 1 00:00:00 2000
    -1

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You may need to cast time_t into (unsigned long int) -- or half way to 2038 your program will break. It is guaranteed to be some sort of integer value just exactly what is upto the implementation.

    Also the year 2050 is past the 2038 problem so it probably won't work on most implementations which implement the number of seconds since the unix epoch.

    You'll need to probably implement your own time functions perhaps wrapping the old ones -- with your own epoch or a larger datatype (64bit?).
    Last edited by zacs7; 12-19-2008 at 06:12 AM.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    reply

    thanks, I changed time to 2009/1/1, however the output still doesn't seems right. Please pay attention that 2000 appeared twice.

    get all records from Sat Jan 1 00:00:00 2000
    946702800
    to Sat Jan 1 00:00:00 2000
    1230786000

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    That's because asctime() holds a static array with the contents... it doesn't return a "dynamic string" or anything like that.

    ie (this is just an example, it's not taken from the real one ):
    Code:
    char * asctime(const struct tm * timeptr)
    {
       static char timeString[256];
       /* ... */
       return timeString;
    }
    And you call it twice within the same statement (the order its called is undefined) so it overwrites the previous values. You need to copy the value it returns into a string if you want to print more than one at once.

    Also you may want to initialize and set the other "struct tm" members, being tm_wday, tm_yday & tm_isdst. Currently their value is rubbish.
    Last edited by zacs7; 12-19-2008 at 06:49 AM.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    reply

    thanks! it works pretty well. Except that after I pass time_t tt=1230786000 to a fuction, which expect parameter of type time_t, the value changed to a negative number! Is this a 32/64-bit problem? I kind of recall there is a gcc compiler option to let me use longer bytes (64bit) on a 32bit machine, but don't exactly remember it. please let me know how would you cope with these large epoch times, thanks!

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    That's because it's a "signed constant", you need to tell the compiler you want it unsigned (and long)

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       time_t tt = (time_t) 1230786000UL;
       printf("&#37;lu\n", (unsigned long int) tt);
       return 0;
    }
    The "UL" standing for "unsigned long"

    > please let me know how would you cope with these large epoch times, thanks!
    Either
    * 64bit type (on a 64bit machine)
    * a bignum library
    * Pick a epoch of my own from the oldest time the program needs to represent (ie today)

    Option 1 & 2 won't work on a 32-bit machine "stock". Option 2 will, but you still have to implement your own time stuff since the time_t type will still be 32-bit.
    Last edited by zacs7; 12-19-2008 at 07:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 11:00 PM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 02:08 AM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21