|
1
|
|
|
<?php |
|
2
|
|
|
/********************************************************************************* |
|
3
|
|
|
* SugarCRM Community Edition is a customer relationship management program developed by |
|
4
|
|
|
* SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc. |
|
5
|
|
|
|
|
6
|
|
|
* SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd. |
|
7
|
|
|
* Copyright (C) 2011 - 2014 Salesagility Ltd. |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software; you can redistribute it and/or modify it under |
|
10
|
|
|
* the terms of the GNU Affero General Public License version 3 as published by the |
|
11
|
|
|
* Free Software Foundation with the addition of the following permission added |
|
12
|
|
|
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK |
|
13
|
|
|
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY |
|
14
|
|
|
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. |
|
15
|
|
|
* |
|
16
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
17
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
18
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
|
19
|
|
|
* details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU Affero General Public License along with |
|
22
|
|
|
* this program; if not, see http://www.gnu.org/licenses or write to the Free |
|
23
|
|
|
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
24
|
|
|
* 02110-1301 USA. |
|
25
|
|
|
* |
|
26
|
|
|
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road, |
|
27
|
|
|
* SW2-130, Cupertino, CA 95014, USA. or at email address [email protected]. |
|
28
|
|
|
* |
|
29
|
|
|
* The interactive user interfaces in modified source and object code versions |
|
30
|
|
|
* of this program must display Appropriate Legal Notices, as required under |
|
31
|
|
|
* Section 5 of the GNU Affero General Public License version 3. |
|
32
|
|
|
* |
|
33
|
|
|
* In accordance with Section 7(b) of the GNU Affero General Public License version 3, |
|
34
|
|
|
* these Appropriate Legal Notices must retain the display of the "Powered by |
|
35
|
|
|
* SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not |
|
36
|
|
|
* reasonably feasible for technical reasons, the Appropriate Legal Notices must |
|
37
|
|
|
* display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM". |
|
38
|
|
|
********************************************************************************/ |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Sugar DateTime container |
|
43
|
|
|
* Extends regular PHP DateTime with useful services |
|
44
|
|
|
* @api |
|
45
|
|
|
*/ |
|
46
|
|
|
class SugarDateTime extends DateTime |
|
47
|
|
|
{ |
|
48
|
|
|
// Recognized properties and their formats |
|
49
|
|
|
protected $formats = array( |
|
50
|
|
|
"sec" => "s", |
|
51
|
|
|
"min" => "i", |
|
52
|
|
|
"hour" => "G", |
|
53
|
|
|
"zhour" => "H", |
|
54
|
|
|
"day" => "j", |
|
55
|
|
|
"zday" => "d", |
|
56
|
|
|
"days_in_month" => "t", |
|
57
|
|
|
"day_of_week" => "w", |
|
58
|
|
|
"day_of_year" => "z", |
|
59
|
|
|
"week" => "W", |
|
60
|
|
|
"month" => "n", |
|
61
|
|
|
"zmonth" => "m", |
|
62
|
|
|
"year" => "Y", |
|
63
|
|
|
"am_pm" => "A", |
|
64
|
|
|
"hour_12" => "g", |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
// Property aliases |
|
68
|
|
|
protected $var_gets = array( |
|
69
|
|
|
"24_hour" => "hour", |
|
70
|
|
|
"day_of_week" => "day_of_week_long", |
|
71
|
|
|
"day_of_week_short" => "day_of_week_short", |
|
72
|
|
|
"month_name" => "month_long", |
|
73
|
|
|
"hour" => "hour_12", |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var DateTimeZone |
|
78
|
|
|
*/ |
|
79
|
|
|
protected static $_gmt; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Calendar strings |
|
83
|
|
|
* @var array |
|
84
|
|
|
*/ |
|
85
|
|
|
protected $_strings; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* For testing - if we allowed to use PHP date parse |
|
89
|
|
|
* @var bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public static $use_php_parser = true; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* For testing - if we allowed to use strptime() |
|
95
|
|
|
* @var bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public static $use_strptime = true; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Copy of DateTime::createFromFormat |
|
101
|
|
|
* |
|
102
|
|
|
* Needed to return right type of the object |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $format Format like in date() |
|
105
|
|
|
* @param string $time Time to parse |
|
106
|
|
|
* @param DateTimeZone $timezone |
|
107
|
|
|
* @return SugarDateTime |
|
108
|
|
|
* @see DateTime::createFromFormat |
|
109
|
|
|
*/ |
|
110
|
117 |
|
public static function createFromFormat($format, $time, $timezone = null) |
|
111
|
|
|
{ |
|
112
|
117 |
|
if(empty($time) || empty($format)) { |
|
113
|
2 |
|
return false; |
|
114
|
|
|
} |
|
115
|
115 |
|
if(self::$use_php_parser && is_callable(array("DateTime", "createFromFormat"))) { |
|
116
|
|
|
// 5.3, hurray! |
|
117
|
115 |
|
if(!empty($timezone)) { |
|
118
|
111 |
|
$d = parent::createFromFormat($format, $time, $timezone); |
|
119
|
|
|
} else { |
|
120
|
115 |
|
$d = parent::createFromFormat($format, $time); |
|
121
|
|
|
} |
|
122
|
|
|
} else { |
|
123
|
|
|
// doh, 5.2, will have to simulate |
|
124
|
|
|
$d = self::_createFromFormat($format, $time, $timezone); |
|
125
|
|
|
} |
|
126
|
115 |
|
if(!$d) { |
|
127
|
6 |
|
return false; |
|
128
|
|
|
} |
|
129
|
113 |
|
$sd = new self($d->format(DateTime::ISO8601)); |
|
130
|
113 |
|
$sd->setTimezone($d->getTimezone()); |
|
131
|
113 |
|
return $sd; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Internal _createFromFormat implementation for 5.2 |
|
136
|
|
|
* @internal |
|
137
|
|
|
* @param string $format Format like in date() |
|
138
|
|
|
* @param string $time Time string to parse |
|
139
|
|
|
* @param DateTimeZone $timezone TZ |
|
140
|
|
|
* @return SugarDateTime |
|
141
|
|
|
* @see DateTime::createFromFormat |
|
142
|
|
|
*/ |
|
143
|
|
|
protected static function _createFromFormat($format, $time, DateTimeZone $timezone = null) |
|
144
|
|
|
{ |
|
145
|
|
|
$res = new self(); |
|
146
|
|
|
if(!empty($timezone)) { |
|
147
|
|
|
$res->setTimezone($timezone); |
|
148
|
|
|
} |
|
149
|
|
|
if(self::$use_strptime && function_exists("strptime")) { |
|
150
|
|
|
$str_format = str_replace(array_keys(TimeDate::$format_to_str), array_values(TimeDate::$format_to_str), $format); |
|
151
|
|
|
// for a reason unknown to modern science, %P doesn't work in strptime |
|
152
|
|
|
$str_format = str_replace("%P", "%p", $str_format); |
|
153
|
|
|
// strip spaces before am/pm as our formats don't have them |
|
154
|
|
|
$time = preg_replace('/\s+(AM|PM)/i', '\1', $time); |
|
155
|
|
|
// TODO: better way to not risk locale stuff problems? |
|
156
|
|
|
$data = strptime($time, $str_format); |
|
157
|
|
|
if(empty($data)) { |
|
158
|
|
|
$GLOBALS['log']->error("Cannot parse $time for format $format"); |
|
159
|
|
|
return null; |
|
160
|
|
|
} |
|
161
|
|
|
if($data["tm_year"] == 0) { |
|
162
|
|
|
unset($data["tm_year"]); |
|
163
|
|
|
} |
|
164
|
|
|
if($data["tm_mday"] == 0) { |
|
165
|
|
|
unset($data["tm_mday"]); |
|
166
|
|
|
} |
|
167
|
|
|
if(isset($data["tm_year"])) { |
|
168
|
|
|
$data["tm_year"] += 1900; |
|
169
|
|
|
} |
|
170
|
|
|
if(isset($data["tm_mon"])) { |
|
171
|
|
|
$data["tm_mon"]++; |
|
172
|
|
|
} |
|
173
|
|
|
$data += self::$data_init; // fill in missing parts |
|
174
|
|
|
} else { |
|
175
|
|
|
// Windows, etc. might not have strptime - we'd have to work harder here |
|
176
|
|
|
$data = $res->_strptime($time, $format); |
|
177
|
|
|
} |
|
178
|
|
|
if(empty($data)) { |
|
179
|
|
|
$GLOBALS['log']->error("Cannot parse $time for format $format"); |
|
180
|
|
|
return null; |
|
181
|
|
|
} |
|
182
|
|
|
if(isset($data["tm_year"])) { |
|
183
|
|
|
$res->setDate($data["tm_year"], $data["tm_mon"], $data["tm_mday"]); |
|
184
|
|
|
} |
|
185
|
|
|
$res->setTime($data["tm_hour"], $data["tm_min"], $data["tm_sec"]); |
|
186
|
|
|
return $res; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Load language Calendar strings |
|
191
|
|
|
* @internal |
|
192
|
|
|
* @param string $name string section to return |
|
193
|
|
|
* @return array |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function _getStrings($name) |
|
196
|
|
|
{ |
|
197
|
|
|
if(empty($this->_strings)) { |
|
198
|
|
|
$this->_strings = return_mod_list_strings_language($GLOBALS['current_language'],"Calendar"); |
|
199
|
|
|
} |
|
200
|
|
|
return $this->_strings[$name]; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Fetch property of the date by name |
|
205
|
|
|
* @param string $var Property name |
|
206
|
|
|
* @return mixed |
|
207
|
|
|
*/ |
|
208
|
21 |
|
public function __get($var) |
|
209
|
|
|
{ |
|
210
|
|
|
// simple formats |
|
211
|
21 |
|
if(isset($this->formats[$var])) { |
|
212
|
17 |
|
return $this->format($this->formats[$var]); |
|
213
|
|
|
} |
|
214
|
|
|
// conditional, derived and translated ones |
|
215
|
|
|
switch($var) { |
|
216
|
6 |
|
case "ts": |
|
217
|
6 |
|
return $this->format("U")+0; |
|
218
|
2 |
|
case "tz_offset": |
|
219
|
|
|
return $this->getTimezone()->getOffset($this); |
|
220
|
2 |
|
case "days_in_year": |
|
221
|
|
|
return $this->format("L") == '1'?366:365; |
|
222
|
|
|
break; |
|
|
|
|
|
|
223
|
2 |
|
case "day_of_week_short": |
|
224
|
|
|
$str = $this->_getStrings('dom_cal_weekdays'); |
|
225
|
|
|
return $str[$this->day_of_week]; |
|
|
|
|
|
|
226
|
2 |
|
case "day_of_week_long": |
|
227
|
|
|
$str = $this->_getStrings('dom_cal_weekdays_long'); |
|
228
|
|
|
return $str[$this->day_of_week]; |
|
|
|
|
|
|
229
|
2 |
|
case "month_short": |
|
230
|
|
|
$str = $this->_getStrings('dom_cal_month'); |
|
231
|
|
|
return $str[$this->month]; |
|
|
|
|
|
|
232
|
2 |
|
case "month_long": |
|
233
|
|
|
$str = $this->_getStrings('dom_cal_month_long'); |
|
234
|
|
|
return $str[$this->month]; |
|
|
|
|
|
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
2 |
|
return ''; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Implement some get_ methods that fetch variables |
|
242
|
|
|
* |
|
243
|
|
|
* @param string $name |
|
244
|
|
|
* @param array $args |
|
245
|
|
|
* @return mixed |
|
246
|
|
|
*/ |
|
247
|
|
|
public function __call($name, $args) |
|
248
|
|
|
{ |
|
249
|
|
|
// fill in 5.2.x gaps |
|
250
|
|
|
if($name == "getTimestamp") { |
|
251
|
|
|
return $this->format('U')+0; |
|
252
|
|
|
} |
|
253
|
|
|
if($name == "setTimestamp") { |
|
254
|
|
|
$sec = (int)$args[0]; |
|
255
|
|
|
$sd = new self("@$sec"); |
|
256
|
|
|
$sd->setTimezone($this->getTimezone()); |
|
257
|
|
|
return $sd; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
// getters |
|
261
|
|
|
if(substr($name, 0, 4) == "get_") { |
|
262
|
|
|
$var = substr($name, 4); |
|
263
|
|
|
|
|
264
|
|
|
if(isset($this->var_gets[$var])) { |
|
265
|
|
|
return $this->__get($this->var_gets[$var]); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
if(isset($this->formats[$var])) { |
|
269
|
|
|
return $this->__get($var); |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
$GLOBALS['log']->fatal("SugarDateTime: unknowm method $name called"); |
|
273
|
|
|
sugar_die("SugarDateTime: unknowm method $name called"); |
|
274
|
|
|
return false; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Get specific hour of today |
|
279
|
|
|
* @param int $hour_index |
|
280
|
|
|
* @return SugarDateTime |
|
281
|
|
|
*/ |
|
282
|
|
|
public function get_datetime_by_index_today($hour_index) |
|
283
|
|
|
{ |
|
284
|
|
|
if ( $hour_index < 0 || $hour_index > 23 ) |
|
285
|
|
|
{ |
|
286
|
|
|
sugar_die("hour is outside of range"); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
$newdate = clone $this; |
|
290
|
|
|
$newdate->setTime($hour_index, 0, 0); |
|
291
|
|
|
return $newdate; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Get the last second of current hour |
|
296
|
|
|
* @return SugarDateTime |
|
297
|
|
|
*/ |
|
298
|
|
|
function get_hour_end_time() |
|
299
|
|
|
{ |
|
300
|
|
|
$newdate = clone $this; |
|
301
|
|
|
$newdate->setTime($this->hour, 59, 59); |
|
|
|
|
|
|
302
|
|
|
return $newdate; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Get the last second of the current day |
|
307
|
|
|
* @return SugarDateTime |
|
308
|
|
|
*/ |
|
309
|
|
|
function get_day_end_time() |
|
310
|
|
|
{ |
|
311
|
|
|
$newdate = clone $this; |
|
312
|
|
|
return $newdate->setTime(23, 59, 59); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Get the beginning of i's day of the week |
|
317
|
|
|
* @param int $day_index Day, 0 is Sunday, 1 is Monday, etc. |
|
318
|
|
|
* @return SugarDateTime |
|
319
|
|
|
*/ |
|
320
|
|
|
function get_day_by_index_this_week($day_index) |
|
321
|
|
|
{ |
|
322
|
|
|
$newdate = clone $this; |
|
323
|
|
|
$newdate->setDate($this->year, $this->month, $this->day + |
|
|
|
|
|
|
324
|
|
|
($day_index - $this->day_of_week))->setTime(0,0); |
|
|
|
|
|
|
325
|
|
|
return $newdate; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* Get the beginning of the last day of i's the month |
|
330
|
|
|
* @deprecated |
|
331
|
|
|
* FIXME: no idea why this function exists and what's the use of it |
|
332
|
|
|
* @param int $month_index Month, January is 0 |
|
333
|
|
|
* @return SugarDateTime |
|
334
|
|
|
*/ |
|
335
|
|
|
function get_day_by_index_this_year($month_index) |
|
336
|
|
|
{ |
|
337
|
|
|
$newdate = clone $this; |
|
338
|
|
|
$newdate->setDate($this->year, $month_index+1, 1); |
|
|
|
|
|
|
339
|
|
|
$newdate->setDate($newdate->year, $newdate->month, $newdate->days_in_month); |
|
|
|
|
|
|
340
|
|
|
$newdate->setTime(0, 0); |
|
341
|
|
|
return $newdate; |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* Get the beginning of i's day of the month |
|
346
|
|
|
* @param int $day_index 0 is the first day of the month (sic!) |
|
347
|
|
|
* @return SugarDateTime |
|
348
|
|
|
*/ |
|
349
|
|
|
function get_day_by_index_this_month($day_index) |
|
350
|
|
|
{ |
|
351
|
|
|
$newdate = clone $this; |
|
352
|
|
|
return $newdate->setDate($this->year, $this->month, $day_index+1)->setTime(0, 0); |
|
|
|
|
|
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* Get new date, modified by date expression |
|
357
|
|
|
* |
|
358
|
|
|
* @example $yesterday = $today->get("yesterday"); |
|
359
|
|
|
* |
|
360
|
|
|
* @param string $expression |
|
361
|
|
|
* @return SugarDateTime |
|
362
|
|
|
*/ |
|
363
|
12 |
|
function get($expression) |
|
364
|
|
|
{ |
|
365
|
12 |
|
$newdate = clone $this; |
|
366
|
12 |
|
$newdate->modify($expression); |
|
367
|
12 |
|
return $newdate; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* Create from ISO 8601 datetime |
|
372
|
|
|
* @param string $str |
|
373
|
|
|
* @return SugarDateTime |
|
374
|
|
|
*/ |
|
375
|
|
|
static public function parse_utc_date_time($str) |
|
|
|
|
|
|
376
|
|
|
{ |
|
377
|
|
|
return new self($str); |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* Create a list of time slots for calendar view |
|
382
|
|
|
* Times must be in user TZ |
|
383
|
|
|
* @param string $view Which view we are using - day, week, month |
|
384
|
|
|
* @param SugarDateTime $start_time Start time |
|
385
|
|
|
* @param SugarDateTime $end_time End time |
|
386
|
|
|
* @return array |
|
387
|
|
|
*/ |
|
388
|
|
|
static function getHashList($view, $start_time, $end_time) |
|
389
|
|
|
{ |
|
390
|
|
|
$hash_list = array(); |
|
391
|
|
|
|
|
392
|
|
|
if ( $view != 'day') |
|
393
|
|
|
{ |
|
394
|
|
|
$end_time = $end_time->get_day_end_time(); |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
$end = $end_time->ts; |
|
398
|
|
|
if($end <= $start_time->ts) { |
|
399
|
|
|
$end = $start_time->ts+1; |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
$new_time = clone $start_time; |
|
403
|
|
|
$new_time->setTime($new_time->hour, 0, 0); |
|
|
|
|
|
|
404
|
|
|
|
|
405
|
|
|
while ($new_time->ts < $end) { |
|
406
|
|
|
if ($view == 'day') { |
|
407
|
|
|
$hash_list[] = $new_time->format(TimeDate::DB_DATE_FORMAT) . ":" . $new_time->hour; |
|
|
|
|
|
|
408
|
|
|
$new_time->modify("next hour"); |
|
409
|
|
|
} else { |
|
410
|
|
|
$hash_list[] = $new_time->format(TimeDate::DB_DATE_FORMAT); |
|
411
|
|
|
$new_time->modify("next day"); |
|
412
|
|
|
} |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
return $hash_list; |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* Get the beginning of the given day |
|
420
|
|
|
* @param int $day Day, starting with 1, default is current |
|
421
|
|
|
* @param int $month Month, starting with 1, default is current |
|
422
|
|
|
* @param int $year Year, default is current |
|
423
|
|
|
* @return SugarDateTime |
|
424
|
|
|
*/ |
|
425
|
1 |
|
function get_day_begin($day = null, $month = null, $year = null) |
|
426
|
|
|
{ |
|
427
|
1 |
|
$newdate = clone $this; |
|
428
|
1 |
|
$newdate->setDate( |
|
429
|
1 |
|
$year?$year:$this->year, |
|
|
|
|
|
|
430
|
1 |
|
$month?$month:$this->month, |
|
|
|
|
|
|
431
|
1 |
|
$day?$day:$this->day); |
|
|
|
|
|
|
432
|
1 |
|
$newdate->setTime(0, 0); |
|
433
|
1 |
|
return $newdate; |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
/** |
|
437
|
|
|
* Get the last second of the given day |
|
438
|
|
|
* @param int $day Day, starting with 1, default is current |
|
439
|
|
|
* @param int $month Month, starting with 1, default is current |
|
440
|
|
|
* @param int $year Year, default is current |
|
441
|
|
|
* @return SugarDateTime |
|
442
|
|
|
*/ |
|
443
|
|
|
function get_day_end($day = null, $month = null, $year = null) |
|
444
|
|
|
{ |
|
445
|
|
|
$newdate = clone $this; |
|
446
|
|
|
$newdate->setDate( |
|
447
|
|
|
$year?$year:$this->year, |
|
|
|
|
|
|
448
|
|
|
$month?$month:$this->month, |
|
|
|
|
|
|
449
|
|
|
$day?$day:$this->day); |
|
|
|
|
|
|
450
|
|
|
$newdate->setTime(23, 59, 59); |
|
451
|
|
|
return $newdate; |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
/** |
|
455
|
|
|
* Get the beginning of the first day of the year |
|
456
|
|
|
* @param int $year |
|
457
|
|
|
* @return SugarDateTime |
|
458
|
|
|
*/ |
|
459
|
|
|
function get_year_begin($year) |
|
460
|
|
|
{ |
|
461
|
|
|
$newdate = clone $this; |
|
462
|
|
|
$newdate->setDate($year, 1, 1); |
|
463
|
|
|
$newdate->setTime(0,0); |
|
464
|
|
|
return $newdate; |
|
465
|
|
|
} |
|
466
|
|
|
|
|
467
|
|
|
/** |
|
468
|
|
|
* Print datetime in standard DB format |
|
469
|
|
|
* |
|
470
|
|
|
* Set $tz parameter to false if you are sure that the date is in UTC. |
|
471
|
|
|
* |
|
472
|
|
|
* @param bool $tz do conversion to UTC |
|
473
|
|
|
* @return string |
|
474
|
|
|
*/ |
|
475
|
20 |
|
function asDb($tz = true) |
|
476
|
|
|
{ |
|
477
|
20 |
|
if($tz) { |
|
478
|
20 |
|
if(empty(self::$_gmt)) { |
|
479
|
1 |
|
self::$_gmt = new DateTimeZone("UTC"); |
|
480
|
|
|
} |
|
481
|
20 |
|
$this->setTimezone(self::$_gmt); |
|
482
|
|
|
} |
|
483
|
20 |
|
return $this->format(TimeDate::DB_DATETIME_FORMAT); |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
|
|
/** |
|
487
|
|
|
* Print date in standard DB format |
|
488
|
|
|
* |
|
489
|
|
|
* Set $tz parameter to false if you are sure that the date is in UTC. |
|
490
|
|
|
* |
|
491
|
|
|
* @param bool $tz do conversion to UTC |
|
492
|
|
|
* @return string |
|
493
|
|
|
*/ |
|
494
|
|
|
function asDbDate($tz = true) |
|
495
|
|
|
{ |
|
496
|
|
|
if($tz) { |
|
497
|
|
|
if(empty(self::$_gmt)) { |
|
498
|
|
|
self::$_gmt = new DateTimeZone("UTC"); |
|
499
|
|
|
} |
|
500
|
|
|
$this->setTimezone(self::$_gmt); |
|
501
|
|
|
} |
|
502
|
|
|
return $this->format(TimeDate::DB_DATE_FORMAT); |
|
503
|
|
|
} |
|
504
|
|
|
|
|
505
|
|
|
/** |
|
506
|
|
|
* Get query string for the date, year=%d&month=%d&day=%d&hour=%d |
|
507
|
|
|
* @return string |
|
508
|
|
|
*/ |
|
509
|
|
|
function get_date_str() |
|
510
|
|
|
{ |
|
511
|
|
|
return sprintf("&year=%d&month=%d&day=%d&hour=%d", $this->year, $this->month, $this->day, $this->hour); |
|
|
|
|
|
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
/** |
|
515
|
|
|
* Convert date to string - 'r' format, like: Thu, 21 Dec 2000 16:01:07 +0200 |
|
516
|
|
|
* @return string |
|
517
|
|
|
*/ |
|
518
|
|
|
function __toString() |
|
519
|
|
|
{ |
|
520
|
|
|
return $this->format('r'); |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
/** |
|
524
|
|
|
* Match between tm_ parts and date() format strings |
|
525
|
|
|
* @var array |
|
526
|
|
|
*/ |
|
527
|
|
|
protected static $parts_match = array( |
|
528
|
|
|
'Y' => 'tm_year', |
|
529
|
|
|
'm' => 'tm_mon', |
|
530
|
|
|
'n' => 'tm_mon', |
|
531
|
|
|
'd' => 'tm_mday', |
|
532
|
|
|
'H' => 'tm_hour', |
|
533
|
|
|
'h' => 'tm_hour', |
|
534
|
|
|
'i' => 'tm_min', |
|
535
|
|
|
's' => 'tm_sec', |
|
536
|
|
|
); |
|
537
|
|
|
|
|
538
|
|
|
protected static $data_init = array( |
|
539
|
|
|
"tm_hour" => 0, |
|
540
|
|
|
"tm_min" => 0, |
|
541
|
|
|
"tm_sec" => 0, |
|
542
|
|
|
); |
|
543
|
|
|
|
|
544
|
|
|
protected static $strptime_short_mon, $strptime_long_mon; |
|
|
|
|
|
|
545
|
|
|
/** |
|
546
|
|
|
* DateTime homebrew parser |
|
547
|
|
|
* |
|
548
|
|
|
* Since some OSes and PHP versions (please upgrade to 5.3!) do not support built-in parsing functions, |
|
549
|
|
|
* we have to restort to this ugliness. |
|
550
|
|
|
* @internal |
|
551
|
|
|
* @param string $time Time formatted string |
|
552
|
|
|
* @param string $format Format, as accepted by strptime() |
|
553
|
|
|
* @return array Parsed parts |
|
554
|
|
|
*/ |
|
555
|
|
|
protected function _strptime($time, $format) |
|
556
|
|
|
{ |
|
557
|
|
|
$data = self::$data_init; |
|
558
|
|
|
if(empty(self::$strptime_short_mon)) { |
|
559
|
|
|
self::$strptime_short_mon = array_flip($this->_getStrings('dom_cal_month')); |
|
560
|
|
|
unset(self::$strptime_short_mon[""]); |
|
561
|
|
|
} |
|
562
|
|
|
if(empty(self::$strptime_long_mon)) { |
|
563
|
|
|
self::$strptime_long_mon = array_flip($this->_getStrings('dom_cal_month_long')); |
|
564
|
|
|
unset(self::$strptime_long_mon[""]); |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
$regexp = TimeDate::get_regular_expression($format); |
|
568
|
|
|
if(!preg_match('@'.$regexp['format'].'@', $time, $dateparts)) { |
|
569
|
|
|
return false; |
|
570
|
|
|
} |
|
571
|
|
|
|
|
572
|
|
|
foreach(self::$parts_match as $part => $datapart) { |
|
573
|
|
|
if (isset($regexp['positions'][$part]) && isset($dateparts[$regexp['positions'][$part]])) { |
|
574
|
|
|
$data[$datapart] = (int)$dateparts[$regexp['positions'][$part]]; |
|
575
|
|
|
} |
|
576
|
|
|
} |
|
577
|
|
|
// now process non-numeric ones |
|
578
|
|
|
if ( isset($regexp['positions']['F']) && !empty($dateparts[$regexp['positions']['F']])) { |
|
579
|
|
|
// FIXME: locale? |
|
580
|
|
|
$mon = $dateparts[$regexp['positions']['F']]; |
|
581
|
|
|
if(isset(self::$sugar_strptime_long_mon[$mon])) { |
|
582
|
|
|
$data["tm_mon"] = self::$sugar_strptime_long_mon[$mon]; |
|
583
|
|
|
} else { |
|
584
|
|
|
return false; |
|
585
|
|
|
} |
|
586
|
|
|
} |
|
587
|
|
|
if ( isset($regexp['positions']['M']) && !empty($dateparts[$regexp['positions']['M']])) { |
|
588
|
|
|
// FIXME: locale? |
|
589
|
|
|
$mon = $dateparts[$regexp['positions']['M']]; |
|
590
|
|
|
if(isset(self::$sugar_strptime_short_mon[$mon])) { |
|
591
|
|
|
$data["tm_mon"] = self::$sugar_strptime_short_mon[$mon]; |
|
592
|
|
|
} else { |
|
593
|
|
|
return false; |
|
594
|
|
|
} |
|
595
|
|
|
} |
|
596
|
|
|
if ( isset($regexp['positions']['a']) && !empty($dateparts[$regexp['positions']['a']])) { |
|
597
|
|
|
$ampm = trim($dateparts[$regexp['positions']['a']]); |
|
598
|
|
|
if($ampm == 'pm') { |
|
599
|
|
|
if($data["tm_hour"] != 12) $data["tm_hour"] += 12; |
|
600
|
|
|
} else if($ampm == 'am') { |
|
601
|
|
|
if($data["tm_hour"] == 12) { |
|
602
|
|
|
// 12:00am is 00:00 |
|
603
|
|
|
$data["tm_hour"] = 0; |
|
604
|
|
|
} |
|
605
|
|
|
} else { |
|
606
|
|
|
return false; |
|
607
|
|
|
} |
|
608
|
|
|
} |
|
609
|
|
|
|
|
610
|
|
|
if ( isset($regexp['positions']['A']) && !empty($dateparts[$regexp['positions']['A']])) { |
|
611
|
|
|
$ampm = trim($dateparts[$regexp['positions']['A']]); |
|
612
|
|
|
if($ampm == 'PM') { |
|
613
|
|
|
if($data["tm_hour"] != 12) $data["tm_hour"] += 12; |
|
614
|
|
|
} else if($ampm == 'AM') { |
|
615
|
|
|
if($data["tm_hour"] == 12) { |
|
616
|
|
|
// 12:00am is 00:00 |
|
617
|
|
|
$data["tm_hour"] = 0; |
|
618
|
|
|
} |
|
619
|
|
|
} else { |
|
620
|
|
|
return false; |
|
621
|
|
|
} |
|
622
|
|
|
} |
|
623
|
|
|
|
|
624
|
|
|
return $data; |
|
625
|
|
|
} |
|
626
|
|
|
|
|
627
|
|
|
// 5.2 compatibility - 5.2 functions don't return $this, let's help them |
|
628
|
|
|
|
|
629
|
|
|
/** |
|
630
|
|
|
* (non-PHPdoc) |
|
631
|
|
|
* @see DateTime::setDate() |
|
632
|
|
|
* @param $year |
|
633
|
|
|
* @param $month |
|
634
|
|
|
* @param $day |
|
635
|
|
|
* @return SugarDateTime |
|
636
|
|
|
*/ |
|
637
|
2 |
|
public function setDate ($year, $month, $day) |
|
638
|
|
|
{ |
|
639
|
2 |
|
parent::setDate($year, $month, $day); |
|
640
|
2 |
|
return $this; |
|
641
|
|
|
} |
|
642
|
|
|
|
|
643
|
|
|
/** |
|
644
|
|
|
* (non-PHPdoc) |
|
645
|
|
|
* @see DateTime::setTime() |
|
646
|
|
|
* @param $hour |
|
647
|
|
|
* @param $minute |
|
648
|
|
|
* @param int $sec |
|
649
|
|
|
* @return SugarDateTime |
|
650
|
|
|
*/ |
|
651
|
26 |
|
public function setTime($hour, $minute, $sec = 0) |
|
652
|
|
|
{ |
|
653
|
26 |
|
parent::setTime($hour, $minute, $sec); |
|
654
|
26 |
|
return $this; |
|
655
|
|
|
} |
|
656
|
|
|
|
|
657
|
|
|
/** |
|
658
|
|
|
* (non-PHPdoc) |
|
659
|
|
|
* @see DateTime::modify() |
|
660
|
|
|
* @param $modify |
|
661
|
|
|
* @return SugarDateTime |
|
662
|
|
|
*/ |
|
663
|
70 |
|
public function modify($modify) |
|
664
|
|
|
{ |
|
665
|
|
|
// We can't user PHP_VERSION_ID here because problem with yesterday and tomorrow appears in defferent versions |
|
666
|
|
|
// In that case we just set time to midnight for yesterday & tomorrow |
|
667
|
|
|
// To leave time as it is we can use -+1 day instead of yesterday & tomorrow |
|
668
|
70 |
|
if (strpos($modify, 'yesterday') !== false || strpos($modify, 'tomorrow') !== false) { |
|
669
|
8 |
|
$this->setTime(0, 0); |
|
670
|
|
|
} |
|
671
|
70 |
|
if(PHP_VERSION_ID >= 50300 || $modify != 'first day of next month') { |
|
672
|
70 |
|
parent::modify($modify); |
|
673
|
|
|
} else { |
|
674
|
|
|
/* PHP 5.2 does not understand 'first day of' and defaults need it */ |
|
675
|
|
|
$this->setDate($this->year, $this->month+1, 1); |
|
|
|
|
|
|
676
|
|
|
} |
|
677
|
70 |
|
return $this; |
|
678
|
|
|
} |
|
679
|
|
|
|
|
680
|
|
|
/** |
|
681
|
|
|
* (non-PHPdoc) |
|
682
|
|
|
* @see DateTime::setTimezone() |
|
683
|
|
|
* @param DateTimeZone $timezone |
|
684
|
|
|
* @return SugarDateTime |
|
685
|
|
|
*/ |
|
686
|
188 |
|
public function setTimezone ($timezone) |
|
687
|
|
|
{ |
|
688
|
188 |
|
parent::setTimezone($timezone); |
|
689
|
188 |
|
return $this; |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
} |
|
693
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.