1 |
<?php
|
2 |
//$Header: /hl/cvsroots/gpl01/gpl01/webprojs/fboprime/sw/phplib/tod.inc,v 1.3 2006/04/20 04:07:50 dashley Exp $
|
3 |
//--------------------------------------------------------------------------------------------------------------
|
4 |
//tod.inc--FboPrime Time of Day Functions
|
5 |
//Copyright (C) 2006 David T. Ashley
|
6 |
//
|
7 |
//This program is free software; you can redistribute it and/or
|
8 |
//modify it under the terms of the GNU General Public License
|
9 |
//as published by the Free Software Foundation; either version 2
|
10 |
//of the License, or (at your option) any later version.
|
11 |
//
|
12 |
//This program is distributed in the hope that it will be useful,
|
13 |
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
//GNU General Public License for more details.
|
16 |
//
|
17 |
//You should have received a copy of the GNU General Public License
|
18 |
//along with this program; if not, write to the Free Software
|
19 |
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
20 |
//********************************************************************************
|
21 |
//This contains functions that deal with time of day (as opposed to date).
|
22 |
//--------------------------------------------------------------------------------------------------------------
|
23 |
require_once("config.inc");
|
24 |
//--------------------------------------------------------------------------------------------------------------
|
25 |
//
|
26 |
//Determines, for a given time of day, which panel in an array is a best fit.
|
27 |
//The method used is to find in which of any panels the given time is closest
|
28 |
//to the center.
|
29 |
// $hour : Integer hour, 0-23.
|
30 |
// $minute : Integer minute, 0-59.
|
31 |
// return value : Which panel (integer). A return value of 2 would indicate array subscripts 4 and 5
|
32 |
// in the actual array.
|
33 |
//
|
34 |
//Unit tested 20060414.
|
35 |
//
|
36 |
function TOD_best_panel($hour, $minute, $panel_array)
|
37 |
{
|
38 |
$best = 0;
|
39 |
$least_center_square = 1.0;
|
40 |
|
41 |
$nelem = count($panel_array);
|
42 |
|
43 |
for ($i=0; $i<$nelem; $i+=2) //For each panel.
|
44 |
{
|
45 |
if (($hour >= $panel_array[$i]) && ($hour <= $panel_array[$i+1])) //Must be in the panel.
|
46 |
//With integers, test above on right
|
47 |
//side of interval is correct.
|
48 |
//"a < (b+1)" is equivalent to
|
49 |
//"a <= b" when integers are involved.
|
50 |
{
|
51 |
$decimal_time = (float)$hour + ((float)$minute/60.0);
|
52 |
//Convert time to decimal hours.
|
53 |
$span = (float)($panel_array[$i+1] + 1.0 - $panel_array[$i]);
|
54 |
//Use span to normalize. 1.0 added because panel actually goes to end of hour.
|
55 |
$midpoint = ((float)$panel_array[$i] + (float)$panel_array[$i+1] + 1.0) / 2.0;
|
56 |
//Find the midpoint.
|
57 |
$center_base = ($decimal_time - $midpoint) / $span;
|
58 |
//Find the normalized distance from the midpoint.
|
59 |
$center_square = $center_base * $center_base;
|
60 |
//Square it to get a monotonic function of distance.. Perhaps cheaper than absolute value.
|
61 |
|
62 |
if ($center_square < $least_center_square) //If we have a winner.
|
63 |
{
|
64 |
$best = $i/2; //Record the subscript.
|
65 |
$least_center_square = $center_square; //Record the best square.
|
66 |
}
|
67 |
}
|
68 |
}
|
69 |
|
70 |
return($best);
|
71 |
}
|
72 |
//
|
73 |
//--------------------------------------------------------------------------------------------------------------
|
74 |
//For a given panel (numbered starting at 0), returns the time midpoint expressed as a T4.
|
75 |
//
|
76 |
function TOD_panel_center_t4($panel)
|
77 |
{
|
78 |
global $CONFIG_SCHED_DAY_PANELS;
|
79 |
|
80 |
$ll = $CONFIG_SCHED_DAY_PANELS[$panel * 2];
|
81 |
$ul = $CONFIG_SCHED_DAY_PANELS[$panel * 2 + 1];
|
82 |
|
83 |
$midpoint_whole = (int)(($ll + $ul + 1) / 2);
|
84 |
$midpoint_frac = ($ll + $ul + 1) % 2; //0 if whole, 1 if half.
|
85 |
|
86 |
$whole_string = sprintf("%02d", $midpoint_whole);
|
87 |
|
88 |
if ($midpoint_frac)
|
89 |
$frac_string = "30";
|
90 |
else
|
91 |
$frac_string = "00";
|
92 |
|
93 |
return($whole_string . $frac_string);
|
94 |
}
|
95 |
//
|
96 |
//
|
97 |
//--------------------------------------------------------------------------------------------------------------
|
98 |
//End of $RCSfile: tod.inc,v $.
|
99 |
//--------------------------------------------------------------------------------------------------------------
|
100 |
?>
|