From 196f2619c004a0ea5bf200df15e974f5664120ab Mon Sep 17 00:00:00 2001 From: defacer Date: Wed, 16 Feb 2005 21:50:25 +0000 Subject: [PATCH] Working for DST support. --- admin/dst.php | 25 ++++++++++----------- admin/dst_edit.html | 4 ++-- calendar/lib.php | 17 +++++++++++++-- lib/moodlelib.php | 53 ++++++++++++++------------------------------- 4 files changed, 44 insertions(+), 55 deletions(-) diff --git a/admin/dst.php b/admin/dst.php index c27c3e97c03..64bd046dd18 100644 --- a/admin/dst.php +++ b/admin/dst.php @@ -152,7 +152,7 @@ } else { $other = get_record('dst_preset', 'name', $preset->name); - if($preset->id != $other->id) { + if($other !== false && $preset->id != $other->id) { $errors[] = get_string('errordstpresetnameexists', 'admin'); } } @@ -163,18 +163,15 @@ // Are we error-free? if(empty($errors)) { // Calculate the last/next/current_offset variables - // WARNING: TODO: BUG: To calculate the timestamps, we are taking into account - // the admin's own DST setting. That means that if the admin changes his own DST - // setting and with this change his effective DST status changes, the timestamps - // will NOT be calculated correctly. Thus, the whole DST setting will be wrong - // for ALL USERS! + $preset->activate_time = sprintf('%02d:%02d', $preset->activate_hour, $preset->activate_minute); + $preset->deactivate_time = sprintf('%02d:%02d', $preset->deactivate_hour, $preset->deactivate_minute); $preset = calendar_dst_update_preset($preset); print_object("record is:"); print_object($preset); - print_object('The last change time (DST) was: '.strftime('%A, %d %B %Y %H:%M', $preset->last_change)); - print_object('The last change time (user) was: '.userdate($preset->last_change)); - print_object('The next change time (DST) is : '.strftime('%A, %d %B %Y %H:%M', $preset->next_change)); - print_object('The next change time (user) is : '.userdate($preset->next_change)); + print_object('The last change time was: '); + print_object(gmdate('M d Y H:i', $preset->last_change)); + print_object('The next change time is: '); + print_object(gmdate('M d Y H:i', $preset->next_change)); // Write it! if($preset->id) { @@ -189,14 +186,14 @@ die(); } else { - print_simple_box_start('center', '70%', '#cc0000'); - echo '
'.get_string('therewereerrors', 'admin').':
'; + echo '
'; + echo '

'.get_string('therewereerrors', 'admin').':

'; echo '
    '; foreach($errors as $error) { echo '
  • '.$error.'
  • '; } - echo '
'; - print_simple_box_end(); + echo ''; + echo '
'; } } diff --git a/admin/dst_edit.html b/admin/dst_edit.html index 1ec12d51530..038b16f12b7 100644 --- a/admin/dst_edit.html +++ b/admin/dst_edit.html @@ -25,7 +25,7 @@ get_string('first', 'calendar'), '-1' => get_string('last', 'calendar')), 'activate_index', $preset->activate_index, ''); ?> activate_day, ''); ?> activate_month); ?> - activate_hour, $preset->activate_minute), 30); ?> + activate_hour, $preset->activate_minute), 99, false); ?> activate_hour; ?> @@ -50,7 +50,7 @@ get_string('first', 'calendar'), '-1' => get_string('last', 'calendar')), 'deactivate_index', $preset->deactivate_index, ''); ?> deactivate_day, ''); ?> deactivate_month); ?> - deactivate_hour, $preset->deactivate_minute), 30); ?> + deactivate_hour, $preset->deactivate_minute), 99, false); ?> diff --git a/calendar/lib.php b/calendar/lib.php index 62b982e015e..fffae00802e 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -1304,11 +1304,24 @@ function calendar_dst_update_preset($dstpreset) { } function calendar_dst_changes_for_year($year, $dstpreset) { + $monthdayactivate = calendar_find_day_in_month($dstpreset->activate_index, $dstpreset->activate_day, $dstpreset->activate_month, $year); $monthdaydeactivate = calendar_find_day_in_month($dstpreset->deactivate_index, $dstpreset->deactivate_day, $dstpreset->deactivate_month, $year); - list($activate_hour, $activate_minute) = explode(':', $dstpreset->activate_time); - list($deactivate_hour, $deactivate_minute) = explode(':', $dstpreset->deactivate_time); + if(isset($dstpreset->activate_time)) { + list($activate_hour, $activate_minute) = explode(':', $dstpreset->activate_time); + } + else { + $activate_hour = $dstpreset->activate_hour; + $activate_minute = $dstpreset->activate_minute; + } + if(isset($dstpreset->deactivate_time)) { + list($deactivate_hour, $deactivate_minute) = explode(':', $dstpreset->deactivate_time); + } + else { + $deactivate_hour = $dstpreset->deactivate_hour; + $deactivate_minute = $dstpreset->deactivate_minute; + } $timezone = get_user_timezone(99); $timeactivate = make_timestamp($year, $dstpreset->activate_month, $monthdayactivate, $activate_hour, $activate_minute, 0, $timezone, false); diff --git a/lib/moodlelib.php b/lib/moodlelib.php index c82050cfef1..1392048f2c0 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -592,51 +592,30 @@ function make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0, $time = mktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year, 0); } else { $time = gmmktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year, 0); - $time = usertime($time, $timezone); // This is GMT + $time = usertime($time, $timezone); } if(!$applydst) { return $time; } - return $time; - - /* - // WARNING: BUG: TODO: This is buggy, but it will do for testing purposes if(($dstid = get_user_preferences('calendar_dstpreset')) !== NULL) { $preset = get_record('dst_preset', 'id', $dstid); - if($time > $preset->last_change && $time < $preset->next_change) { + + if($time >= $preset->last_change && $time < $preset->next_change) { + $time -= $preset->current_offset * 60; return $time; } - // We need to find out what's going on... - $nowuserdate = usergetdate($time); - + // It's in some other time period, we need to recalculate $changes = calendar_dst_changes_for_year($year, $preset); - if($time < $changes['activate'] || $time > $changes['deactivate']) { - // DST will be off at that time - if($preset->current_offset != 0) { - print_object('Uncompensated time was:'); - print_object(usergetdate($time)); - $time += $preset->apply_offset * 60; - print_object('Compensated time is:'); - print_object(usergetdate($time)); - } + + if($time >= $changes['activate'] && $time < $changes['deactivate']) { + // Compensation required + $time -= $preset->apply_offset * $preset->apply_offset_sign; } - else { - // DST will be on at that time - if($preset->current_offset == 0) { - print_object('Uncompensated time was:'); - print_object(usergetdate($time)); - $time -= $preset->apply_offset * 60; - print_object('Compensated time is:'); - print_object(usergetdate($time)); - } - } - - return $time; } - */ + return $time; } /** @@ -751,24 +730,24 @@ function userdate($date, $format='', $timezone=99, $fixday = true) { } /** - * Given a $date timestamp in GMT (seconds since epoch), + * Given a $time timestamp in GMT (seconds since epoch), * returns an array that represents the date in user time * * @uses HOURSECS - * @param int $date Timestamp in GMT + * @param int $time Timestamp in GMT * @param int $timezone ? * @return array An array that represents the date in user time * @todo Finish documenting this function */ -function usergetdate($date, $timezone=99) { +function usergetdate($time, $timezone=99) { $timezone = get_user_timezone($timezone); if (abs($timezone) > 13) { - return getdate($date); + return getdate($time); } //There is no gmgetdate so I have to fake it... - $date = $date + (int)($timezone * HOURSECS); + $time += (int)($timezone * HOURSECS); // This is independent of the server's TZ settings, // unlike gmstrftime. It's also a bit faster this way. @@ -783,7 +762,7 @@ function usergetdate($date, $timezone=99) { $getdate['yday'], $getdate['weekday'], $getdate['month'] - ) = explode(' ', gmdate('s i H d m Y w z l F', $date)); + ) = explode(' ', gmdate('s i H d m Y w z l F', $time)); return $getdate; }