]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Util/TDateTimeStamp.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Util / TDateTimeStamp.php
1 <?php
2 /**
3  * TDateTimeStamp class file.
4
5  * @author Fabio Bas ctrlaltca[AT]gmail[DOT]com
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Util
10  */
11
12 /**
13  * TDateTimeStamp Class
14  *
15  * TDateTimeStamp Class is a wrapper to DateTime: http://php.net/manual/book.datetime.php
16  * Prado implemented this class before php started shipping the DateTime extension.
17  * This class is deprecated and left here only for compatibility for legacy code.
18  * Please note that this class doesn't support automatic conversion from gregorian to
19  * julian dates anymore.
20  *
21  * @author Fabio Bas ctrlaltca[AT]gmail[DOT]com
22  * @package System.Util
23  * @since 3.0.4
24  * @deprecated since 3.2.1
25  */
26 class TDateTimeStamp
27 {
28         protected static $_month_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31);
29         protected static $_month_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31);
30
31         /**
32          * Returns the day of the week (0=Sunday, 1=Monday, .. 6=Saturday)
33          * @param int year
34          * @param int month
35          * @param int day
36          */
37         public function getDayofWeek($year, $month, $day)
38         {
39                 $dt = new DateTime();
40                 $dt->setDate($year, $month, $day);
41                 return (int) $dt->format('w');
42         }
43
44         /**
45          * Checks for leap year, returns true if it is. No 2-digit year check. Also
46          * handles julian calendar correctly.
47          * @param float year to check
48          * @return boolean true if is leap year
49          */
50         public function isLeapYear($year)
51         {
52                 $year = $this->digitCheck($year);
53                 $dt = new DateTime();
54                 $dt->setDate($year, 1, 1);
55                 return (bool) $dt->format('L');
56         }
57
58         /**
59          * Fix 2-digit years. Works for any century.
60          * Assumes that if 2-digit is more than 30 years in future, then previous century.
61          * @return integer change two digit year into multiple digits
62          */
63         protected function digitCheck($y)
64         {
65                 if ($y < 100){
66                         $yr = (integer) date("Y");
67                         $century = (integer) ($yr /100);
68
69                         if ($yr%100 > 50) {
70                                 $c1 = $century + 1;
71                                 $c0 = $century;
72                         } else {
73                                 $c1 = $century;
74                                 $c0 = $century - 1;
75                         }
76                         $c1 *= 100;
77                         // if 2-digit year is less than 30 years in future, set it to this century
78                         // otherwise if more than 30 years in future, then we set 2-digit year to the prev century.
79                         if (($y + $c1) < $yr+30) $y = $y + $c1;
80                         else $y = $y + $c0*100;
81                 }
82                 return $y;
83         }
84
85         public function get4DigitYear($y)
86         {
87                 return $this->digitCheck($y);
88         }
89
90         /**
91          * @return integer get local time zone offset from GMT
92          */
93         public function getGMTDiff($ts=false)
94         {
95                 $dt = new DateTime();
96                 if($ts)
97                         $dt->setTimeStamp($ts);
98                 else
99                         $dt->setDate(1970, 1, 2);
100
101                 return (int) $dt->format('Z');
102         }
103
104         /**
105          * @return array an array with date info.
106          */
107         function parseDate($txt=false)
108         {
109                 if ($txt === false) return getdate();
110
111                 $dt = new DateTime($txt);
112
113                 return array(
114                         'seconds' => (int) $dt->format('s'),
115                         'minutes' => (int) $dt->format('i'),
116                         'hours' => (int) $dt->format('G'),
117                         'mday' => (int) $dt->format('j'),
118                         'wday' => (int) $dt->format('w'),
119                         'mon' => (int) $dt->format('n'),
120                         'year' => (int) $dt->format('Y'),
121                         'yday' => (int) $dt->format('z'),
122                         'weekday' => $dt->format('l'),
123                         'month' => $dt->format('F'),
124                         0 => (int) $dt->format('U'),
125                         );
126         }
127
128         /**
129          * @return array an array with date info.
130          */
131         function getDate($d=false,$fast=false)
132         {
133                 if ($d === false) return getdate();
134
135                 $dt = new DateTime();
136                 $dt->setTimestamp($d);
137
138                 return array(
139                         'seconds' => (int) $dt->format('s'),
140                         'minutes' => (int) $dt->format('i'),
141                         'hours' => (int) $dt->format('G'),
142                         'mday' => (int) $dt->format('j'),
143                         'wday' => (int) $dt->format('w'),
144                         'mon' => (int) $dt->format('n'),
145                         'year' => (int) $dt->format('Y'),
146                         'yday' => (int) $dt->format('z'),
147                         'weekday' => $dt->format('l'),
148                         'month' => $dt->format('F'),
149                         0 => (int) $dt->format('U'),
150                         );
151         }
152
153         /**
154          * @return boolean true if valid date, semantic check only.
155          */
156         public function isValidDate($y,$m,$d)
157         {
158                 if ($this->isLeapYear($y))
159                         $marr =& self::$_month_leaf;
160                 else
161                         $marr =& self::$_month_normal;
162
163                 if ($m > 12 || $m < 1) return false;
164
165                 if ($d > 31 || $d < 1) return false;
166
167                 if ($marr[$m] < $d) return false;
168
169                 if ($y < 1000 && $y > 3000) return false;
170
171                 return true;
172         }
173
174         /**
175          * @return string formatted date based on timestamp $d
176          */
177         function formatDate($fmt,$ts=false,$is_gmt=false)
178         {
179                 $dt = new DateTime();
180                 if($is_gmt)
181                         $dt->setTimeZone(new DateTimeZone('UTC'));
182                 $dt->setTimestamp($ts);
183
184                 return $dt->format($fmt);
185         }
186
187         /**
188          * @return integer|float a timestamp given a local time
189      */
190         function getTimeStamp($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_gmt=false)
191         {
192                 $dt = new DateTime();
193                 if($is_gmt)
194                         $dt->setTimeZone(new DateTimeZone('UTC'));
195                 $dt->setDate($year!==false ? $year : date('Y'),
196                         $mon!==false ? $mon : date('m'),
197                         $day!==false ? $day : date('d'));
198                 $dt->setTime($hr, $min, $sec);
199                 return (int) $dt->format('U');
200         }
201 }
202