Maybe I'm being too simplistic, but given the OP's restrictions, "usually start within a 6 hour window" and "handles times over midnight", I think it's doable. If the 6 hours ended just after midnight, you would need to handle times from 18:01 - 00:01; if it started just before midnight, you would need to handle 23:59 - 5:59. For simplicity's sake, I'd make my window 18:00 - 5:59. Thus,
$n = 0; while ( ... ) { $hour += 24 if( $hour < 6 ); next if( $hour < 18 ); # ignore times outside the window $sum += 3600*$hour + 60*$min + $sec; $avg = $sum / ++$n; }
For the $hour < 18 rule: if the time is before 6am, it's $hour would have already been adjusted to somewhere in range 24 to 29, so wouldn't trigger the <18 condition; if the time was after 6pm, it wouldn't trigger the condition; else, it will trigger the condition, and the time would be ignored.
If OP wants a narrower or wider 'accept-range', just adjust the two comparisons appropriately.
Other options would be to clamp times between 6am and noon to 05:59:59, and between noon and 6pm to 18:00:00, which will not ignore points, but manipulate them to fall within the "normal window".
I understand there are issues in the general case, but with the restrictions given, I think things are well-defined enough, and this algorithm should give a mean closer to midnight than to noon, which seems to be what the OP wants.
In reply to Re^2: Average start time handling midnight
by pryrt
in thread Average start time handling midnight
by chrisjej
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |