ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/BBClone/branches/0.6.2-Cust/lib/timecalc.php
Revision: 16
Committed: Thu Nov 21 13:07:39 2013 UTC (11 years ago) by matthys
Original Path: trunk/lib/timecalc.php
File size: 6821 byte(s)
Log Message:
Cleanup code.

File Contents

# User Rev Content
1 matthys 15 <?php
2     /* This file is part of BBClone (A PHP based Web Counter on Steroids)
3     *
4 matthys 16 * SVN FILE $Id$
5 matthys 15 *
6 matthys 16 * Copyright (C) 2001-2013, the BBClone Team (see doc/authors.txt for details)
7 matthys 15 *
8     * This program is free software: you can redistribute it and/or modify
9     * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation, either version 3 of the License, or
11     * (at your option) any later version.
12     *
13     * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     * GNU General Public License for more details.
17     *
18     * See doc/copying.txt for details
19     */
20    
21     function bbc_time_offset() {
22     global $BBC_TIMESTAMP, $BBC_TIME_OFFSET, $access, $last;
23    
24     $cfg_offset = !empty($BBC_TIME_OFFSET) ? $BBC_TIME_OFFSET : 0;
25     $sav_offset = isset($access['time']['offset']) ? $access['time']['offset'] : 0;
26     $diff_offset = $cfg_offset - $sav_offset;
27    
28     if (empty($diff_offset)) return;
29    
30     $diff_hours = -round($diff_offset / 60);
31     $new_time = $BBC_TIMESTAMP + ($cfg_offset * 60);
32     $old_time = $BBC_TIMESTAMP + ($sav_offset * 60);
33     $access['time']['last'] += $diff_offset * 60;
34     $access['time']['offset'] = $cfg_offset;
35    
36     // Shift the last reset marker as well if available
37     if (!empty($access['time']['reset'])) $access['time']['reset'] += $diff_offset * 60;
38    
39     // shifting the detailed stats
40     for ($i = 0, $max = count($last['traffic']); $i < $max; $i++) $last['traffic'][$i]['time'] += $diff_offset * 60;
41    
42     $last_day_in_month = $access['time']['day'][(count($access['time']['day']) - 1)];
43     $today = date("w", $new_time);
44     $yesterday = empty($today) ? ($today + 6) : ($today - 1);
45     $days_diff = ($today != date("w", $old_time)) ? 1 : 0;
46    
47     $this_day = date("j", $new_time) - 1;
48     $last_day = empty($this_day) ? $last_day_in_month : ($this_day - 1);
49    
50     $this_month = date("n", $new_time) - 1;
51     $last_month = empty($this_month) ? ($this_month + 11) : ($this_month - 1);
52    
53     // shifting hourly stats to get the amount "this day - previous day" we need to modify
54     $shift = array_splice($access['time']['hour'], $diff_hours, 24);
55     $diff = ($diff_hours < 0) ? array_sum($shift) : array_sum($access['time']['hour']);
56     $access['time']['hour'] = array_merge($shift, $access['time']['hour']);
57    
58     for ($i = 0, $hits = 0, $max = date("G", $new_time); $i <= $max; $i++) $hits += $access['time']['hour'][$i];
59     for ($i = ($max + 1), $rest = 0; $i < 24; $i++) $rest += $access['time']['hour'][$i];
60    
61     if ($diff_hours < 0) {
62     //set time forth
63     $access['time']['wday'][$today] = $hits;
64     $access['time']['day'][$this_day] = $hits;
65     $access['time']['wday'][$yesterday] -= ($access['time']['wday'][$yesterday] < $diff) ? $hits : $diff;
66     $access['time']['day'][$last_day] -= ($access['time']['day'][$last_day] < $diff) ? $hits : $diff;
67    
68     if ($access['time']['day'][$this_day] == $last_day_in_month) {
69     $access['time']['month'][$this_month] = $hits;
70     $access['time']['month'][$last_month] -= ($access['time']['month'][$last_month] < $diff) ? $hits : $diff;
71     }
72     }
73     else {
74     // set time back
75     $access['time']['wday'][$yesterday] += $days_diff ? $rest : $diff;
76     $access['time']['day'][$last_day] += $days_diff ? $rest : $diff;
77     $access['time']['wday'][$today] = $hits;
78     $access['time']['day'][$this_day] = $hits;
79    
80     if ($access['time']['day'][$this_day] == $access['time']['day'][0]) {
81     $access['time']['month'][$last_month] += $days_diff ? $rest : $diff;
82     $access['time']['month'][$this_month] = $hits;
83     }
84     }
85     }
86    
87     // Update the time stats in $access['time']
88     function bbc_update_time_stat($new_time, $last_time) {
89     global $access;
90    
91     // Independent variables
92     static $nb_seconds_in_day = 86400;
93     static $nb_seconds_in_week = 604800;
94    
95     // Variables for the new time
96     $new_hour = date("G", $new_time);
97     $new_wday = date("w", $new_time);
98     $new_day = date("j", $new_time) - 1;
99     $new_month = date("n", $new_time) - 1;
100    
101     // Variables for the last time
102     $last_hour = date("G", $last_time);
103     $last_wday = date("w", $last_time);
104     $last_day = date("j", $last_time) - 1;
105     $last_month = date("n", $last_time) - 1;
106     $last_lgmonth = date("t", $last_time);
107    
108     $nb_seconds_in_last_month = $last_lgmonth * $nb_seconds_in_day;
109     $nb_seconds_in_last_year = (date("L", $last_time) ? 366 : 365) * $nb_seconds_in_day;
110    
111     // Updating the last connection time to the new one
112     // for the next call of bbc_update_time_stat
113     $access['time']['last'] = $new_time;
114    
115     // Updating the hourly time stats (in a day)
116     // Setting to zero the time-counters present between the last connections
117     // and the new one, and incrementing the new time-counter
118     // Last day, there are 24 hours = 86400 seconds
119     if (($new_time - $last_time) > $nb_seconds_in_day) {
120     for ($k = 0; $k < 24; $k++) $access['time']['hour'][$k] = 0;
121     }
122     else {
123     $elapsed = $new_hour - $last_hour;
124     $elapsed = ($elapsed < 0) ? $elapsed + 24 : $elapsed;
125     for ($k = 1; $k <= $elapsed; $k++) $access['time']['hour'][($last_hour + $k) % 24] = 0;
126     }
127     $access['time']['hour'][$new_hour]++;
128    
129     // Updating the daily time stats (in a week)
130     // Last week, there are 7 days = 604800 seconds
131     if (($new_time - $last_time) > $nb_seconds_in_week) {
132     for ($k = 0; $k < 7; $k++) $access['time']['wday'][$k] = 0;
133     }
134     else {
135     $elapsed = $new_wday - $last_wday;
136     $elapsed = ($elapsed < 0) ? $elapsed + 7 : $elapsed;
137     for ($k = 1; $k <= $elapsed; $k++) $access['time']['wday'][($last_wday + $k) % 7] = 0;
138     }
139    
140     $access['time']['wday'][$new_wday]++;
141    
142     // Updating the daily time stats (in a month)
143     // Last month, there are 28, 29, 30 or 31 days
144     // Note: we have to reset 31 days even if we are a month with less days
145     if (($new_time - $last_time) > $nb_seconds_in_last_month) {
146     for ($k = 0; $k < 31; $k++) $access['time']['day'][$k] = 0;
147     }
148     else {
149     $elapsed = $new_day - $last_day;
150     $elapsed = ($elapsed < 0) ? $elapsed + $last_lgmonth : $elapsed;
151     for ($k = 1; $k <= $elapsed; $k++) $access['time']['day'][($last_day + $k) % $last_lgmonth] = 0;
152     }
153    
154     $access['time']['day'][$new_day]++;
155    
156     // Updating the monthly time stats (in a year)
157     // Last year, there are 12 months
158     if (($new_time - $last_time) > $nb_seconds_in_last_year) {
159     for ($k = 0; $k < 12; $k++) $access['time']['month'][$k] = 0;
160     }
161     else {
162     $elapsed = $new_month - $last_month;
163     $elapsed = ($elapsed < 0) ? $elapsed + 12 : $elapsed;
164     for ($k = 1; $k <= $elapsed; $k++) $access['time']['month'][($last_month + $k) % 12] = 0;
165     }
166     $access['time']['month'][$new_month]++;
167     }
168     ?>

Properties

Name Value
svn:keywords Id