Computing times with mktime()

Discussion in 'Programmer's Corner' started by dor, Jan 29, 2010.

  1. dor

    Thread Starter Active Member

    Feb 20, 2009
    62
    0
    Does the mktime() library function adds an offset of 10 days to the output value?
    I tried to implement a mktime() function myself, but noticed the lack of 10 days from the output value.
    So, if I convert a (time_t) 1234567890 value to a (struct tm) and later converts it back (with my pseudo mktime() function) to a (time_t), the result is 1233703890 ( = 1234567890 - 10*24*60*60 )

    Thank you.
     
  2. dor

    Thread Starter Active Member

    Feb 20, 2009
    62
    0
    This problem was caused by the leap years.
    From 1970 until now, there were 10 leap years. The following PHP script shows that:

    Code ( (Unknown Language)):
    1. header('content-type: text/plain; charset=ASCII');
    2. for($i=1970, $cnt=0; $i<2010; ++$i) {
    3.     if ( !( !($i%4) && $i%100 || !($i%400) ) ) continue;
    4.     echo $i, PHP_EOL;    ++$cnt;
    5. }
    6. echo PHP_EOL, 'Total leap years: ', $cnt;
    Output:
    Code ( (Unknown Language)):
    1. 1972
    2. 1976
    3. 1980
    4. 1984
    5. 1988
    6. 1992
    7. 1996
    8. 2000
    9. 2004
    10. 2008
    11.  
    12. Total leap years: 10
     
Loading...