]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Calendar/DateTimeUtil.php
Release 6.3.1
[Github/sugarcrm.git] / modules / Calendar / DateTimeUtil.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40  ********************************************************************************/
41
42 /**
43  * @deprecated Phased out, should not be used anymore
44  * Use SugarDateTime instead
45  */
46 class DateTimeUtil
47 {
48                 var $timezone;
49                 var $sec;
50                 var $min;
51                 var $hour;
52                 var $zhour;
53                 var $day;
54                 var $zday;
55                 var $day_of_week;
56                 var $day_of_week_short;
57                 var $day_of_week_long;
58                 var $day_of_year;
59                 var $week;
60                 var $month;
61                 var $zmonth;
62                 var $month_short;
63                 var $month_long;
64                 var $year;
65                 var $am_pm;
66                 var $tz_offset;
67
68                 // unix epoch time
69                 var $ts;
70     /**
71      * Convert from DB-formatted timedate to DateTimeUtil object
72      * @param string $date_start
73      * @param string $time_start
74      */
75     function get_time_start($date_start, $time_start = '')
76         {
77                 global $timedate;
78                 if(empty($time_start)) {
79                         list($date_start, $time_start) = explode(' ', $date_start);
80                 }
81                 $match=array();
82
83                 preg_match("/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/",$date_start,$match);
84                 $time_arr = array();
85                 $time_arr['year'] = $match[1];
86                 $time_arr['month'] = $match[2];
87                 $time_arr['day'] = $match[3];
88
89                 if ( empty( $time_start) )
90                 {
91                         $time_arr['hour'] = 0;
92                         $time_arr['min'] = 0;
93                 }
94                 else
95                 {
96                         if (preg_match('/^(\d\d*):(\d\d*):(\d\d*)$/',$time_start,$match))
97                         {
98                                 $time_arr['hour'] = $match[1];
99                                 $time_arr['min'] = $match[2];
100                         }
101                         else if ( preg_match('/^(\d\d*):(\d\d*)$/',$time_start,$match))
102                         {
103                                 $time_arr['hour'] = $match[1];
104                                 $time_arr['min'] = $match[2];
105                         }
106                 }
107                 $gmtdiff = date('Z')-$timedate->adjustmentForUserTimeZone()*60;
108                 $time_arr['sec'] = $gmtdiff;
109                 return new DateTimeUtil($time_arr,true);
110         }
111
112         function get_time_end( $start_time, $duration_hours,$duration_minutes)
113         {
114                 if ( empty($duration_hours))
115                 {
116                         $duration_hours = "00";
117                 }
118                 if ( empty($duration_minutes))
119                 {
120                         $duration_minutes = "00";
121                 }
122
123                 $added_seconds = ($duration_hours * 60 * 60 + $duration_minutes * 60 ) - 1;
124
125                 $time_arr = array();
126                 $time_arr['year'] = $start_time->year;
127                 $time_arr['month'] = $start_time->month;
128                 $time_arr['day'] = $start_time->day;
129                 $time_arr['hour'] = $start_time->hour;
130                 $time_arr['min'] = $start_time->min;
131                 $time_arr['sec'] = $added_seconds;
132                 return new DateTimeUtil($time_arr,true);
133
134         }
135
136         function get_date_str()
137         {
138
139                 $arr = array();
140                 if ( isset( $this->hour))
141                 {
142                  array_push( $arr, "hour=".$this->hour);
143                 }
144                 if ( isset( $this->day))
145                 {
146                  array_push( $arr, "day=".$this->day);
147                 }
148                 if ( isset( $this->month))
149                 {
150                  array_push( $arr, "month=".$this->month);
151                 }
152                 if ( isset( $this->year))
153                 {
154                  array_push( $arr, "year=".$this->year);
155                 }
156                 return  ("&".implode('&',$arr));
157         }
158
159         function get_tomorrow()
160         {
161                         $date_arr = array('day'=>($this->day + 1),
162                         'month'=>$this->month,
163                         'year'=>$this->year);
164
165                 return new DateTimeUtil($date_arr,true);
166         }
167         function get_yesterday()
168         {
169                         $date_arr = array('day'=>($this->day - 1),
170                         'month'=>$this->month,
171                         'year'=>$this->year);
172
173                 return new DateTimeUtil($date_arr,true);
174         }
175
176         function get_mysql_date()
177         {
178                 return $this->year."-".$this->zmonth."-".$this->zday;
179         }
180         function get_mysql_time()
181         {
182                 return $this->hour.":".$this->min;
183         }
184
185   function parse_utc_date_time($str)
186   {
187     preg_match('/(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/',$str,$matches);
188
189     $date_arr = array(
190       'year'=>$matches[1],
191       'month'=>$matches[2],
192       'day'=>$matches[3],
193       'hour'=>$matches[4],
194       'min'=>$matches[5]);
195
196       $date_time = new DateTimeUtil($date_arr,true);
197
198       $date_arr = array('ts'=>$date_time->ts + $date_time->tz_offset);
199
200       return new DateTimeUtil($date_arr,true);
201   }
202
203         function get_utc_date_time()
204         {
205                 return gmdate('Ymd\THi', $this->ts)."00Z";
206         }
207
208         function get_first_day_of_last_year()
209         {
210                         $date_arr = array('day'=>1,
211                         'month'=>1,
212                         'year'=>($this->year - 1));
213
214                 return new DateTimeUtil($date_arr,true);
215
216         }
217         function get_first_day_of_next_year()
218         {
219                         $date_arr = array('day'=>1,
220                         'month'=>1,
221                         'year'=>($this->year + 1));
222
223                 return new DateTimeUtil($date_arr,true);
224
225         }
226
227         function get_first_day_of_next_week()
228         {
229                 $first_day = $this->get_day_by_index_this_week(0);
230                         $date_arr = array('day'=>($first_day->day + 7),
231                         'month'=>$first_day->month,
232                         'year'=>$first_day->year);
233
234                 return new DateTimeUtil($date_arr,true);
235
236         }
237         function get_first_day_of_last_week()
238         {
239                 $first_day = $this->get_day_by_index_this_week(0);
240                         $date_arr = array('day'=>($first_day->day - 7),
241                         'month'=>$first_day->month,
242                         'year'=>$first_day->year);
243
244                 return new DateTimeUtil($date_arr,true);
245         }
246         function get_first_day_of_last_month()
247         {
248                 if ($this->month == 1)
249                 {
250                         $month = 12;
251                         $year = $this->year - 1;
252                 }
253                 else
254                 {
255                         $month = $this->month - 1;
256                         $year = $this->year ;
257                 }
258                         $date_arr = array('day'=>1,
259                         'month'=>$month,
260                         'year'=>$year);
261
262                 return new DateTimeUtil($date_arr,true);
263
264         }
265         function get_first_day_of_this_month()
266         {
267                 $month = $this->month;
268                 $year = $this->year ;
269                 $date_arr = array('day'=>1,
270                 'month'=>$month,
271                 'year'=>$year);
272
273                 return new DateTimeUtil($date_arr,true);
274
275         }
276         function get_first_day_of_next_month()
277         {
278                 $date_arr = array('day'=>1,
279                         'month'=>($this->month + 1),
280                         'year'=>$this->year);
281                 return new DateTimeUtil($date_arr,true);
282         }
283
284
285         function fill_in_details()
286         {
287                 global $mod_strings, $timedate;
288                 $hour = 0;
289                 $min = 0;
290                 $sec = 0;
291                 $day = 1;
292                 $month = 1;
293                 $year = 1970;
294
295                 if ( isset($this->sec))
296                 {
297                         $sec = $this->sec;
298                 }
299                 if ( isset($this->min))
300                 {
301                         $min = $this->min;
302                 }
303                 if ( isset($this->hour))
304                 {
305                         $hour = $this->hour;
306                 }
307                 if ( isset($this->day))
308                 {
309                         $day= $this->day;
310                 }
311                 if ( isset($this->month))
312                 {
313                         $month = $this->month;
314                 }
315                 if ( isset($this->year))
316                 {
317                         $year = $this->year;
318                 }
319                 else
320                 {
321                         sugar_die ("fill_in_details: year was not set");
322                 }
323                 $this->ts = mktime($hour,$min,$sec,$month,$day,$year)+$timedate->adjustmentForUserTimeZone()*60;
324                 $this->load_ts($this->ts);
325
326         }
327
328         function load_ts($timestamp)
329         {
330         //      global $mod_list_strings;
331                 global $current_language;
332                 $mod_list_strings = return_mod_list_strings_language($current_language,"Calendar");
333                 if ( empty($timestamp))
334                 {
335
336                         $timestamp = time();
337                 }
338
339                 $this->ts = $timestamp;
340                 global $timedate;
341
342                 $tdiff = $timedate->adjustmentForUserTimeZone();
343                 $date_str = date('i:G:H:j:d:t:w:z:L:W:n:m:Y:Z',$timestamp-$tdiff*60);
344                 list(
345                 $this->min,
346                 $this->hour,
347                 $this->zhour,
348                 $this->day,
349                 $this->zday,
350                 $this->days_in_month,
351                 $this->day_of_week,
352                 $this->day_of_year,
353                 $is_leap,
354                 $this->week,
355                 $this->month,
356                 $this->zmonth,
357                 $this->year,
358                 $this->tz_offset)
359                  = explode(':',$date_str);
360                 $this->tz_offset = date('Z') - $tdiff * 60;
361
362                 $this->day_of_week_short =$mod_list_strings['dom_cal_weekdays'][$this->day_of_week];
363                 $this->day_of_week_long=$mod_list_strings['dom_cal_weekdays_long'][$this->day_of_week];
364                 $this->month_short=$mod_list_strings['dom_cal_month'][$this->month];
365                 $this->month_long=$mod_list_strings['dom_cal_month_long'][$this->month];
366
367                 $this->days_in_year = 365;
368
369                 if ($is_leap == 1)
370                 {
371                         $this->days_in_year += 1;
372                 }
373
374
375         }
376
377         function DateTimeUtil($time,$fill_in_details)
378         {
379                 if (! isset( $time) || count($time) == 0 )
380                 {
381                         $this->load_ts(null);
382                 }
383                 else if ( isset( $time['ts']))
384                 {
385                         $this->load_ts($time['ts']);
386                 }
387                 else if ( isset( $time['date_str']))
388                 {
389                         list($this->year,$this->month,$this->day)=
390                                 explode("-",$time['date_str']);
391                         if ($fill_in_details == true)
392                         {
393                                 $this->fill_in_details();
394                         }
395                 }
396                 else
397                 {
398                         if ( isset($time['sec']))
399                         {
400                                 $this->sec = $time['sec'];
401                         }
402                         if ( isset($time['min']))
403                         {
404                                 $this->min = $time['min'];
405                         }
406                         if ( isset($time['hour']))
407                         {
408                                 $this->hour = $time['hour'];
409                         }
410                         if ( isset($time['day']))
411                         {
412                                 $this->day = $time['day'];
413                         }
414                         if ( isset($time['week']))
415                         {
416                                 $this->week = $time['week'];
417                         }
418                         if ( isset($time['month']))
419                         {
420                                 $this->month = $time['month'];
421                         }
422                         if ( isset($time['year']) && $time['year'] >= 1970)
423                         {
424                                 $this->year = $time['year'];
425                         }
426                         else
427                         {
428                                 return null;
429                         }
430
431                         if ($fill_in_details == true)
432                         {
433                                 $this->fill_in_details();
434                         }
435
436                 }
437         }
438
439         function dump_date_info()
440         {
441                 echo "min:".$this->min."<br>\n";
442                 echo "hour:".$this->hour."<br>\n";
443                 echo "day:".$this->day."<br>\n";
444                 echo "month:".$this->month."<br>\n";
445                 echo "year:".$this->year."<br>\n";
446         }
447
448         function get_hour()
449         {
450                 $hour = $this->hour;
451                 if ($this->hour > 12)
452                 {
453                         $hour -= 12;
454                 }
455                 else if ($this->hour == 0)
456                 {
457                         $hour = 12;
458                 }
459                 return $hour;
460         }
461
462         function get_24_hour()
463         {
464                 return $this->hour;
465         }
466
467         function get_am_pm()
468         {
469                 if ($this->hour >=12)
470                 {
471                         return "PM";
472                 }
473                 return "AM";
474         }
475
476         function get_day()
477         {
478                 return $this->day;
479         }
480
481         function get_month()
482         {
483                 return $this->month;
484         }
485
486         function get_day_of_week_short()
487         {
488                 return $this->day_of_week_short;
489         }
490         function get_day_of_week()
491         {
492                 return $this->day_of_week_long;
493         }
494
495
496         function get_month_name()
497         {
498                 return $this->month_long;
499         }
500
501         function get_datetime_by_index_today($hour_index)
502         {
503                 $arr = array();
504
505                 if ( $hour_index < 0 || $hour_index > 23  )
506                 {
507                         sugar_die("hour is outside of range");
508                 }
509
510                 $arr['hour'] = $hour_index;
511                 $arr['min'] = 0;
512                 $arr['day'] = $this->day;
513
514                 $arr['month'] = $this->month;
515                 $arr['year'] = $this->year;
516
517                 return new DateTimeUtil($arr,true);
518         }
519
520         function get_hour_end_time()
521         {
522                 $arr = array();
523                 $arr['hour'] = $this->hour;
524                 $arr['min'] = 59;
525                 $arr['sec'] = 59;
526                 $arr['day'] = $this->day;
527
528                 $arr['month'] = $this->month;
529                 $arr['year'] = $this->year;
530
531                 return new DateTimeUtil($arr,true);
532         }
533
534         function get_day_end_time()
535         {
536                 $arr = array();
537                 $arr['hour'] = 23;
538                 $arr['min'] = 59;
539                 $arr['sec'] = 59;
540                 $arr['day'] = $this->day;
541
542                 $arr['month'] = $this->month;
543                 $arr['year'] = $this->year;
544
545                 return new DateTimeUtil($arr,true);
546         }
547
548         function get_day_by_index_this_week($day_index)
549         {
550                 $arr = array();
551
552                 if ( $day_index < 0 || $day_index > 6  )
553                 {
554                         sugar_die("day is outside of week range");
555                 }
556
557                 $arr['day'] = $this->day +
558                         ($day_index - $this->day_of_week);
559
560                 $arr['month'] = $this->month;
561                 $arr['year'] = $this->year;
562
563                 return new DateTimeUtil($arr,true);
564         }
565         function get_day_by_index_this_year($month_index)
566         {
567                 $arr = array();
568                 $arr['month'] = $month_index+1;
569                 $arr['year'] = $this->year;
570                 // wp: Find the last day of the month requested, ensure that is the ceiling of the day param
571                 $arr['day'] = min(strftime("%d", mktime(0, 0, 0, $arr['month']+1, 0, $arr['year'])), $this->day);
572
573                 return new DateTimeUtil($arr,true);
574         }
575
576         function get_day_by_index_this_month($day_index)
577         {
578                 $arr = array();
579                 $arr['day'] = $day_index + 1;
580                 $arr['month'] = $this->month;
581                 $arr['year'] = $this->year;
582
583                 return new DateTimeUtil($arr,true);
584         }
585
586         function getHashList($view, &$start_time, &$end_time)
587         {
588                 $hash_list = array();
589
590         if (version_compare(phpversion(), '5.0') < 0)
591             $new_time = $start_time;
592         else
593             $new_time = clone($start_time);
594
595                 $arr = array();
596
597                 if ( $view != 'day')
598                 {
599                   $end_time = $end_time->get_day_end_time();
600                 }
601
602
603                 if (empty($new_time->ts))
604                 {
605                         return;
606                 }
607
608                 if ( $new_time->ts == $end_time->ts)
609                 {
610                         $end_time->ts+=1;
611                 }
612
613                  while( $new_time->ts < $end_time->ts)
614                  {
615
616                   $arr['month'] = $new_time->month;
617                   $arr['year'] = $new_time->year;
618                   $arr['day'] = $new_time->day;
619                   $arr['hour'] = $new_time->hour;
620                   if ( $view == 'day')
621                   {
622                    $hash_list[] = $new_time->get_mysql_date().":".$new_time->hour;
623                    $arr['hour'] += 1;
624                   }
625                   else
626                   {
627                    $hash_list[] = $new_time->get_mysql_date();
628                    $arr['day'] += 1;
629                   }
630                   $new_time = new DateTimeUtil($arr,true);
631     }
632                 return $hash_list;
633         }
634
635 }
636
637 ?>