Thread: mktime issues

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    13

    mktime issues

    I am doing a project and I need to be able to display a simple calendar for the month and year supplied by user input.
    To do this I have written the following to determine the first day of the month selected.
    Code:
    int getFirstDayOFMonth(unsigned year, unsigned month) {
    
       struct tm time_struct;
       
       time_struct.tm_year = year - 1900;
       time_struct.tm_mon = month - 1;
       time_struct.tm_mday = 1;
       time_struct.tm_sec = 0; 
       time_struct.tm_min = 0;
       time_struct.tm_hour = 0;
       time_struct.tm_isdst = 0;
    
       if (mktime(&time_struct) == -1)
          return -1;
       else 
          return time_struct.tm_wday;
    }
    My problem arises when I enter a year larger than 2038 and smaller than 1902. Reading through this forum I found out about the Year 2038 problem so I understand the upper limit but I cannot understand what the issue is with 1900 and 1901.

    Is there any way to get around the 2038 problem for the case above?
    Why is mktime() failing on the years 1900 and 1901? Is there a way around this?
    Is there a way you can you go below 1900?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The "time" system in Unix is defined in such a way that it's not easy to do anything about those matters - time is a 32-bit integer, and 2^32 (4 billion) seconds is the entire range - it goes backwards and forwards 2^31 (+1 backwards) seconds, which means you can go 2147 million (and a bit) seconds either side of 1st Jan 1970.-

    Sun's man-page here says:
    [quote="Sun"]
    The tm_year member must be for year 1901 or later. Calendar times before 20:45:52 UTC, December 13, 1901 or after 03:14:07 UTC, January 19, 2038 cannot be represented. Portable applications should not try to create dates before 00:00:00 UTC, January 1, 1970 or after 00:00:00 UTC, January 1, 2038.
    [/quite]

    If you need a greater range than that, you will need a different form to store your time - you could simply extend it to 64-bit, and then you would be able to go about 130 billion years or so in either direction.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    cheers for the quick response matsp

    How can I extend to 64-bit?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If your system has "mktime64", then you can use that (and the related time64, difftime64 etc). If there is no mktime64 on your system, then you have to roll your own (or change to a system that supports 64-bit time?).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mktime() doesn't return meaningful value!
    By patiobarbecue in forum C Programming
    Replies: 5
    Last Post: 12-19-2008, 07:06 AM
  2. error using mktime
    By amit_sahrawat in forum Linux Programming
    Replies: 2
    Last Post: 12-04-2007, 06:03 AM
  3. SQL Connection issues
    By jverkoey in forum Tech Board
    Replies: 4
    Last Post: 02-17-2005, 07:52 AM
  4. Major game issues
    By VOX in forum Game Programming
    Replies: 0
    Last Post: 01-18-2005, 08:40 AM
  5. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 09:01 PM