Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function in code I'm debugging seems to not take into account shifts to and from DST

I'm debugging a conference room booking calendar which was put together by someone which is no longer working with me. It's been a nightmare since there were so many things wrong with it, but I'm having trouble figuring out exactly what's causing this last bug. The calendar checks to see if rooms have already been booked at certain times and they aren't having an issue showing as booked at the right time, but if someone tries to book the same room an hour or less after the room is vacated after a shift to or from DST, it shows the room as booked still. Example:

  • User sees that a room is reserved for Nov. 29th from 9am-10am.
  • User then tries to book the room on Nov. 29th from 10:30am-12:00pm.
  • The calendar cancels this second request and informs the user that the room is already booked.

It should be noted that this doesn't happen any time before the shift to DST (Nov. 4th). Here's the function which determines if the room is available:

    function calCheck($starttime, $endtime, $cal_name, $cat_id, $myDB, $myHost, $myUser, $myPass) {
    $timezone = 'America/Denver';
    date_default_timezone_set ($timezone);
    $dset = new DateTime($odate, new DateTimeZone($timezone));
    $dset2 = $dset->getOffset();

    //$starttime = $starttime + 1;
    //$endtime = $endtime  - 1;     
    $starttime = $starttime - $dset2 + 1;
    $endtime = $endtime - $dset2 - 1;
    $starttime = $starttime;
    $endtime = $endtime; 

    //echo $starttime .'</br>'. $endtime . '</br>';
    $db = new myDB($myDB, $myHost, $myUser, $myPass);
    $db->myDB_Connect();
    //echo 'calcheck</br>';
    $ck_query = 'SELECT * FROM vw_cal_chk 
                    WHERE (stime < '. $starttime . '  AND etime > ' . $starttime . ' ) and Calendar = "' . $cal_name . '" and cat_id = "' .$cat_id . '"
                    OR (stime < ' . $endtime . ' AND etime > ' . $endtime . ') and Calendar = "' . $cal_name . '" and cat_id = "' .$cat_id . '"
                    OR (stime >= '. $starttime . ' AND etime < ' . $endtime .') and Calendar = "' . $cal_name . '" and cat_id = "' .$cat_id . '"';
    $ck_result = $db->myQuery($ck_query);
    $num = mysql_num_rows($ck_result);  
    //echo $ck_query . '</br>' . $num;
    if ($num >> 0){
        $avail = 1;
    } else {
        $avail = 0;
    }
    return $avail;
}

All of my timestamps up to this point are in UTC and I notice the $odate variable is actually never instantiated anywhere, but I haven't been able to determine what value to pass it in order for the offsetting to work correctly. If I can find out what kind of date it wants, I should be able to work the rest out.

like image 991
Kent Kingdon Avatar asked Oct 08 '12 21:10

Kent Kingdon


1 Answers

$odate is null which defaults to now giving you the current offset. What you want is the offset at the time that the schedule is being set not the offset right at this moment. It looks like your database dates are in Mountain time and that your startdate is utc and you are subtracting the offset to get back to mountain (I would think that you would add the offsets not subtract , so I might have this reversed, can you query your database and figure it out?)

In any event you should be computing your offset using $startdate not now(), try switching $startdate for $odate and see what happens. If this doesn't work try building a string from startdate and then generating a new date from that string. If nothing else outputting that string should give you a clear picture as to whether $startdate is UTC or mountain

like image 178
Bryan Waters Avatar answered Nov 03 '22 23:11

Bryan Waters