/[phpgwapi]/phpgwapi/inc/class.datetime.inc.php
ViewVC logotype

Contents of /phpgwapi/inc/class.datetime.inc.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5.2.12.2.8 - (show annotations) (download)
Sat May 1 09:32:26 2004 UTC (19 years, 11 months ago) by skwashd
Branch: Version-0_9_16-branch
Changes since 1.5.2.12.2.7: +18 -20 lines
fix for bug #8315

1 <?php
2 /**
3 * Datetime class that contains common date/time functions
4 * @author Joseph Engo <jengo@phpgroupware.org>
5 * @author Mark Peters <skeeter@phpgroupware.org>
6 * @copyright Copyright (C) 2000,2001 Joseph Engo, Mark Peters
7 * @copyright Portions Copyright (C) 2000-2004 Free Software Foundation, Inc. http://www.fsf.org/
8 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
9 * @package phpgwapi
10 * @subpackage utilities
11 * @version $Id: class.datetime.inc.php,v 1.5.2.12.2.7 2004/02/24 22:10:57 alexbsa Exp $
12 */
13
14 $d1 = strtolower(@substr(PHPGW_API_INC,0,3));
15 $d2 = strtolower(@substr(PHPGW_SERVER_ROOT,0,3));
16 $d3 = strtolower(@substr(PHPGW_APP_INC,0,3));
17 if($d1 == 'htt' || $d1 == 'ftp' || $d2 == 'htt' || $d2 == 'ftp' || $d3 == 'htt' || $d3 == 'ftp')
18 {
19 echo 'Failed attempt to break in via an old Security Hole!<br>'."\n";
20 exit;
21 }
22 unset($d1);
23 unset($d2);
24 unset($d3);
25
26
27 /**
28 * Datetime class that contains common date/time functions
29 *
30 * @package phpgwapi
31 * @subpackage utilities
32 */
33 class datetime
34 {
35 var $tz_offset;
36 var $days = Array();
37 var $gmtnow = 0;
38 var $users_localtime;
39 var $cv_gmtdate;
40
41 function datetime()
42 {
43 $this->tz_offset = 3600 * intval(@$GLOBALS['phpgw_info']['user']['preferences']['common']['tz_offset']);
44 print_debug('datetime::datetime::gmtnow',$this->gmtnow,'api');
45
46 $error_occured = True;
47 // If we already have a GMT time, no need to do this again.
48 if(!$this->gmtnow)
49 {
50 if(isset($GLOBALS['phpgw_info']['server']['tz_offset']))
51 {
52 $this->gmtnow = time() - (intval($GLOBALS['phpgw_info']['server']['tz_offset']) * 3600);
53 //echo "<p>set via tz_offset=".$GLOBALS['phpgw_info']['server']['tz_offset'].": gmtnow=".date('Y/m/d H:i',$this->gmtnow)."</p>\n";
54 }
55 else
56 {
57 $this->gmtnow = time() - ($this->getbestguess() * 3600);
58 //echo "<p>set via bestguess=".$this->getbestguess().": gmtnow=".date('Y/m/d H:i',$this->gmtnow)."</p>\n";
59 }
60 }
61 $this->users_localtime = time() + $this->tz_offset;
62 }
63
64 function getntpoffset()
65 {
66 $error_occured = False;
67 if(!@is_object($GLOBALS['phpgw']->network))
68 {
69 $GLOBALS['phpgw']->network = createobject('phpgwapi.network');
70 }
71 $server_time = time();
72
73 if($GLOBALS['phpgw']->network->open_port('129.6.15.28',13,5))
74 {
75 $line = $GLOBALS['phpgw']->network->bs_read_port(64);
76 $GLOBALS['phpgw']->network->close_port();
77
78 $array = explode(' ',$line);
79 // host: 129.6.15.28
80 // Value returned is 52384 02-04-20 13:55:29 50 0 0 9.2 UTC(NIST) *
81 print_debug('Server datetime',time(),'api');
82 print_debug('Temporary NTP datetime',$line,'api');
83 if ($array[5] == 4)
84 {
85 $error_occured = True;
86 }
87 else
88 {
89 $date = explode('-',$array[1]);
90 $time = explode(':',$array[2]);
91 $this->gmtnow = mktime(intval($time[0]),intval($time[1]),intval($time[2]),intval($date[1]),intval($date[2]),intval($date[0]) + 2000);
92 print_debug('Temporary RFC epoch',$this->gmtnow,'api');
93 print_debug('GMT',date('Ymd H:i:s',$this->gmtnow),'api');
94 }
95 }
96 else
97 {
98 $error_occured = True;
99 }
100
101 if($error_occured == True)
102 {
103 return $this->getbestguess();
104 }
105 else
106 {
107 return intval(($server_time - $this->gmtnow) / 3600);
108 }
109 }
110
111 function gethttpoffset()
112 {
113 $error_occured = False;
114 if(!@is_object($GLOBALS['phpgw']->network))
115 {
116 $GLOBALS['phpgw']->network = createobject('phpgwapi.network');
117 }
118 $server_time = time();
119
120 $filename = 'http://132.163.4.213/timezone.cgi?GMT';
121 $file = $GLOBALS['phpgw']->network->gethttpsocketfile($filename);
122 if(!$file)
123 {
124 return $this->getbestguess();
125 }
126 $time = strip_tags($file[55]);
127 $date = strip_tags($file[56]);
128
129 print_debug('GMT DateTime',$date.' '.$time,'api');
130 $dt_array = explode(' ',$date);
131 $temp_datetime = $dt_array[0].' '.substr($dt_array[2],0,-1).' '.substr($dt_array[1],0,3).' '.$dt_array[3].' '.$time.' GMT';
132 print_debug('Reformulated GMT DateTime',$temp_datetime,'api');
133 $this->gmtnow = $this->convert_rfc_to_epoch($temp_datetime);
134 print_debug('this->gmtnow',$this->gmtnow,'api');
135 print_debug('server time',$server_time,'api');
136 print_debug('server DateTime',date('D, d M Y H:i:s',$server_time),'api');
137 return intval(($server_time - $this->gmtnow) / 3600);
138 }
139
140 function getbestguess()
141 {
142 print_debug('datetime::datetime::debug: Inside getting from local server','api');
143 $server_time = time();
144 // Calculate GMT time...
145 // If DST, add 1 hour...
146 // - (date('I') == 1?3600:0)
147 $this->gmtnow = $this->convert_rfc_to_epoch(gmdate('D, d M Y H:i:s',$server_time).' GMT');
148 return intval(($server_time - $this->gmtnow) / 3600);
149 }
150
151 function convert_rfc_to_epoch($date_str)
152 {
153 $comma_pos = strpos($date_str,',');
154 if($comma_pos)
155 {
156 $date_str = substr($date_str,$comma_pos+1);
157 }
158
159 // This may need to be a reference to the different months in native tongue....
160 $month= array(
161 'Jan' => 1,
162 'Feb' => 2,
163 'Mar' => 3,
164 'Apr' => 4,
165 'May' => 5,
166 'Jun' => 6,
167 'Jul' => 7,
168 'Aug' => 8,
169 'Sep' => 9,
170 'Oct' => 10,
171 'Nov' => 11,
172 'Dec' => 12
173 );
174 $dta = array();
175 $ta = array();
176
177 // Convert "15 Jul 2000 20:50:22 +0200" to unixtime
178 $dta = explode(' ',$date_str);
179 $ta = explode(':',$dta[4]);
180
181 if(substr($dta[5],0,3) <> 'GMT')
182 {
183 $tzoffset = substr($dta[5],0,1);
184 $tzhours = intval(substr($dta[5],1,2));
185 $tzmins = intval(substr($dta[5],3,2));
186 switch ($tzoffset)
187 {
188 case '-':
189 (int)$ta[0] += $tzhours;
190 (int)$ta[1] += $tzmins;
191 break;
192 case '+':
193 (int)$ta[0] -= $tzhours;
194 (int)$ta[1] -= $tzmins;
195 break;
196 }
197 }
198 return mktime($ta[0],$ta[1],$ta[2],$month[$dta[2]],$dta[1],$dta[3]);
199 }
200
201 function get_weekday_start($year,$month,$day)
202 {
203 $weekday = $this->day_of_week($year,$month,$day);
204 switch($GLOBALS['phpgw_info']['user']['preferences']['calendar']['weekdaystarts'])
205 {
206 // Saturday is for arabic support
207 case 'Saturday':
208 $this->days = Array(
209 0 => 'Sat',
210 1 => 'Sun',
211 2 => 'Mon',
212 3 => 'Tue',
213 4 => 'Wed',
214 5 => 'Thu',
215 6 => 'Fri'
216 );
217 switch($weekday)
218 {
219 case 0:
220 $sday = mktime(2,0,0,$month,$day - 1,$year);
221 break;
222 case 6:
223 $sday = mktime(2,0,0,$month,$day,$year);
224 break;
225 default:
226 $sday = mktime(2,0,0,$month,$day - ($weekday + 1),$year);
227 break;
228 }
229 break;
230 case 'Monday':
231 $this->days = Array(
232 0 => 'Mon',
233 1 => 'Tue',
234 2 => 'Wed',
235 3 => 'Thu',
236 4 => 'Fri',
237 5 => 'Sat',
238 6 => 'Sun'
239 );
240 switch($weekday)
241 {
242 case 0:
243 $sday = mktime(2,0,0,$month,$day - 6,$year);
244 break;
245 case 1:
246 $sday = mktime(2,0,0,$month,$day,$year);
247 break;
248 default:
249 $sday = mktime(2,0,0,$month,$day - ($weekday - 1),$year);
250 break;
251 }
252 break;
253 case 'Sunday':
254 default:
255 $this->days = Array(
256 0 => 'Sun',
257 1 => 'Mon',
258 2 => 'Tue',
259 3 => 'Wed',
260 4 => 'Thu',
261 5 => 'Fri',
262 6 => 'Sat'
263 );
264 $sday = mktime(2,0,0,$month,$day - $weekday,$year);
265 break;
266 }
267 return $sday - 7200;
268 }
269
270 function is_leap_year($year)
271 {
272 if ((intval($year) % 4 == 0) && (intval($year) % 100 != 0) || (intval($year) % 400 == 0))
273 {
274 return 1;
275 }
276 else
277 {
278 return 0;
279 }
280 }
281
282 function days_in_month($month,$year)
283 {
284 $days = Array(
285 1 => 31,
286 2 => 28 + $this->is_leap_year(intval($year)),
287 3 => 31,
288 4 => 30,
289 5 => 31,
290 6 => 30,
291 7 => 31,
292 8 => 31,
293 9 => 30,
294 10 => 31,
295 11 => 30,
296 12 => 31
297 );
298 return $days[intval($month)];
299 }
300
301 function date_valid($year,$month,$day)
302 {
303 return checkdate(intval($month),intval($day),intval($year));
304 }
305
306 function time_valid($hour,$minutes,$seconds)
307 {
308 if(intval($hour) < 0 || intval($hour) > 24)
309 {
310 return False;
311 }
312 if(intval($minutes) < 0 || intval($minutes) > 59)
313 {
314 return False;
315 }
316 if(intval($seconds) < 0 || intval($seconds) > 59)
317 {
318 return False;
319 }
320
321 return True;
322 }
323
324 function day_of_week($year,$month,$day)
325 {
326 if($month > 2)
327 {
328 $month -= 2;
329 }
330 else
331 {
332 $month += 10;
333 $year--;
334 }
335 $day = (floor((13 * $month - 1) / 5) + $day + ($year % 100) + floor(($year % 100) / 4) + floor(($year / 100) / 4) - 2 * floor($year / 100) + 77);
336 return (($day - 7 * floor($day / 7)));
337 }
338
339 function day_of_year($year,$month,$day)
340 {
341 $days = array(0,31,59,90,120,151,181,212,243,273,304,334);
342
343 $julian = ($days[$month - 1] + $day);
344
345 if($month > 2 && $this->is_leap_year($year))
346 {
347 $julian++;
348 }
349 return($julian);
350 }
351
352 /*!
353 @function days_between
354 @abstract Get the number of days between two dates
355 @author Steven Cramer/Ralf Becker
356 @param $m1 - Month_1, $d1 - Day_1, $y1 - Year_1, $m2 - Month_2, $d2 - Day_2, $y2 - Year_2
357 @note the last param == 0, ensures that the calculation is always done without daylight-saveing
358 */
359 function days_between($m1,$d1,$y1,$m2,$d2,$y2)
360 {
361 return intval((mktime(0,0,0,$m2,$d2,$y2,0) - mktime(0,0,0,$m1,$d1,$y1,0)) / 86400);
362 }
363
364 function date_compare($a_year,$a_month,$a_day,$b_year,$b_month,$b_day)
365 {
366 $a_date = mktime(0,0,0,intval($a_month),intval($a_day),intval($a_year));
367 $b_date = mktime(0,0,0,intval($b_month),intval($b_day),intval($b_year));
368 if($a_date == $b_date)
369 {
370 return 0;
371 }
372 elseif($a_date > $b_date)
373 {
374 return 1;
375 }
376 elseif($a_date < $b_date)
377 {
378 return -1;
379 }
380 }
381
382 function time_compare($a_hour,$a_minute,$a_second,$b_hour,$b_minute,$b_second)
383 {
384 // I use the 1970/1/2 to compare the times, as the 1. can get via TZ-offest still
385 // before 1970/1/1, which is the earliest date allowed on windows
386 $a_time = mktime(intval($a_hour),intval($a_minute),intval($a_second),1,2,1970);
387 $b_time = mktime(intval($b_hour),intval($b_minute),intval($b_second),1,2,1970);
388 if($a_time == $b_time)
389 {
390 return 0;
391 }
392 elseif($a_time > $b_time)
393 {
394 return 1;
395 }
396 elseif($a_time < $b_time)
397 {
398 return -1;
399 }
400 }
401
402 function makegmttime($hour,$minute,$second,$month,$day,$year)
403 {
404 return $this->gmtdate(mktime($hour, $minute, $second, $month, $day, $year));
405 }
406
407 function localdates($localtime)
408 {
409 $date = Array('raw','day','month','year','full','dow','dm','bd');
410 $date['raw'] = $localtime;
411 $date['year'] = intval($GLOBALS['phpgw']->common->show_date($date['raw'],'Y'));
412 $date['month'] = intval($GLOBALS['phpgw']->common->show_date($date['raw'],'m'));
413 $date['day'] = intval($GLOBALS['phpgw']->common->show_date($date['raw'],'d'));
414 $date['full'] = intval($GLOBALS['phpgw']->common->show_date($date['raw'],'Ymd'));
415 $date['bd'] = mktime(0,0,0,$date['month'],$date['day'],$date['year']);
416 $date['dm'] = intval($GLOBALS['phpgw']->common->show_date($date['raw'],'dm'));
417 $date['dow'] = $this->day_of_week($date['year'],$date['month'],$date['day']);
418 $date['hour'] = intval($GLOBALS['phpgw']->common->show_date($date['raw'],'H'));
419 $date['minute'] = intval($GLOBALS['phpgw']->common->show_date($date['raw'],'i'));
420 $date['second'] = intval($GLOBALS['phpgw']->common->show_date($date['raw'],'s'));
421
422 return $date;
423 }
424
425 function gmtdate($localtime)
426 {
427 return $this->localdates($localtime - $this->tz_offset);
428 }
429
430 /**
431 * Convert a date from one format to another
432 *
433 * @param string date date in source format representation
434 * @param string formatSource format of the passed date
435 * @param string formatTarget target date format
436 * @return string date in target format representation
437 */
438 function convertDate($date, $formatSource, $formatTarget)
439 {
440 // get format separator character
441 $formatSourceSepChar = substr($formatSource,1,1);
442 $formatSourceArray = explode($formatSourceSepChar, $formatSource);
443 $dateSourceArray = explode($formatSourceSepChar, $date);
444
445 $keyNum = count($formatSourceArray);
446 $valNum = count($dateSourceArray);
447 if($keyNum != $valNum)
448 {
449 return false;
450 }
451
452 $map_date = array();
453 for($i=0; $i<$keyNum; $i++)
454 {
455 $key = $formatSourceArray[$i];
456 $val = $dateSourceArray[$i];
457
458 if($key == 'M')
459 {
460 for($j=1; $j <=12; $j++)
461 {
462 if(date('M',mktime(0,0,0,$j,1,2000)) == $val)
463 {
464 $map_date['m'] = $j;
465 }
466 }
467 }
468 else
469 {
470 $map_date[strtolower($key)] = intval($val);
471 }
472 }
473 return date($formatTarget, mktime(0,0,0,$map_date['m'], $map_date['d'], $map_date['y']));
474 }
475
476 }
477 ?>

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26