forked from dotproject/dotProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.class.php
More file actions
405 lines (340 loc) · 11.6 KB
/
date.class.php
File metadata and controls
405 lines (340 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php /* CLASSES $Id$ */
/**
* @package dotproject
* @subpackage utilites
*/
if (!defined('DP_BASE_DIR')) {
die('You should not access this file directly.');
}
define('DATE_CALC_BEGIN_WEEKDAY', LOCALE_FIRST_DAY);
require_once $AppUI->getLibraryClass('PEAR/Date');
define('FMT_DATEISO', '%Y%m%dT%H%M%S');
define('FMT_DATELDAP', '%Y%m%d%H%M%SZ');
define('FMT_DATETIME_MYSQL', '%Y-%m-%d %H:%M:%S');
define('FMT_DATERFC822', '%a, %d %b %Y %H:%M:%S');
define('FMT_TIMESTAMP', '%Y%m%d%H%M%S');
define('FMT_TIMESTAMP_DATE', '%Y%m%d');
define('FMT_TIMESTAMP_TIME', '%H%M%S');
define('FMT_UNIX', '3');
define('WDAY_SUNDAY', 0);
define('WDAY_MONDAY', 1);
define('WDAY_TUESDAY', 2);
define('WDAY_WEDNESDAY', 3);
define('WDAY_THURSDAY', 4);
define('WDAY_FRIDAY', 5);
define('WDAY_SATURDAY', 6);
define('SEC_MINUTE', 60);
define('SEC_HOUR', 3600);
define('SEC_DAY', 86400);
/**
* dotProject implementation of the Pear Date class
*
* This provides customised extensions to the Date class to leave the
* Date package as 'pure' as possible
*/
class CDate extends Date {
function CDate($date = null)
{
if (!is_object($date) && strlen($date) == 12 && strpos($date, '-') === false && strpos($date, '/') === false) {
$date = $date.'00';
}
return parent::Date($date);
}
/**
* extend PEAR Date's format() meet to translation needs
*/
function format($format) {
global $AppUI;
$AppUI->setBaseLocale();
$output = parent::format($format);
setlocale(LC_ALL, $AppUI->user_lang);
return $output;
}
function after($when)
{
if (!is_object($when)) {
$when = new CDate($when);
}
return parent::after($when);
}
/**
* Overloaded compare method
*
* The convertTZ calls are time intensive calls. When a compare call is
* made in a recussive loop the lag can be significant.
*/
function compare($d1, $d2, $convertTZ=false)
{
if (!is_object($d1)) {
$d1 = new CDate($d1);
} else if (!is_object($d2)) {
$d2 = new CDate($d2);
}
if ($convertTZ) {
$d1->convertTZ(new Date_TimeZone('UTC'));
$d2->convertTZ(new Date_TimeZone('UTC'));
}
$days1 = Date_Calc::dateToDays($d1->day, $d1->month, $d1->year);
$days2 = Date_Calc::dateToDays($d2->day, $d2->month, $d2->year);
$comp_value = 0;
if ($days1 - $days2) {
$comp_value = $days1 - $days2;
} else if ($d1->hour - $d2->hour) {
$comp_value = dPsgn($d1->hour - $d2->hour);
} else if ($d1->minute - $d2->minute) {
$comp_value = dPsgn($d1->minute - $d2->minute);
} else if ($d1->second - $d2->second) {
$comp_value = dPsgn($d1->second - $d2->second);
}
return dPsgn($comp_value);
}
/**
* Adds (+/-) a number of days to the current date.
* @param int Positive or negative number of days
* @author J. Christopher Pereira <kripper@users.sf.net>
*/
function addDays($n) {
$timeStamp = $this->getTime();
$oldHour = $this->getHour();
$this->setDate($timeStamp + SEC_DAY * ceil($n), DATE_FORMAT_UNIXTIME);
if (($oldHour - $this->getHour()) || !is_int($n)) {
$timeStamp += ($oldHour - $this->getHour()) * SEC_HOUR;
$this->setDate($timeStamp + SEC_DAY * $n, DATE_FORMAT_UNIXTIME);
}
}
/**
* Adds (+/-) a number of months to the current date.
* @param int Positive or negative number of months
* @author Andrew Eddie <eddieajau@users.sourceforge.net>
*/
function addMonths($n) {
$an = abs($n);
$years = floor($an / 12);
$months = $an % 12;
if ($n < 0) {
$this->year -= $years;
$this->month -= $months;
if ($this->month < 1) {
$this->year--;
$this->month = 12 + $this->month;
}
} else {
$this->year += $years;
$this->month += $months;
if ($this->month > 12) {
$this->year++;
$this->month -= 12;
}
}
}
/**
* New method to get the difference in days the stored date
* @param Date The date to compare to
* @author Andrew Eddie <eddieajau@users.sourceforge.net>
*/
function dateDiff($when, $ignoretime = false) {
return Date_calc::dateDiff($this->getDay(), $this->getMonth(), $this->getYear(),
$when->getDay(), $when->getMonth(), $when->getYear());
}
/**
* New method that sets hour, minute and second in a single call
* @param int hour
* @param int minute
* @param int second
* @author Andrew Eddie <eddieajau@users.sourceforge.net>
*/
function setTime($h=0, $m=0, $s=0) {
$this->setHour($h);
$this->setMinute($m);
$this->setSecond($s);
}
function isWorkingDay() {
global $AppUI;
$working_days = dPgetConfig("cal_working_days");
$working_days = ((is_null($working_days)) ? array('1','2','3','4','5') : explode(",", $working_days));
return in_array($this->getDayOfWeek(), $working_days);
}
function getAMPM() {
return (($this->getHour() > 11) ? 'pm' : 'am');
}
/* Return date obj for the end of the next working day
** @param bool Determine whether to set time to start of day or preserve the time of the given object
*/
function next_working_day($preserveHours = false) {
global $AppUI;
$do = $this;
$end = intval(dPgetConfig('cal_day_end'));
$start = intval(dPgetConfig('cal_day_start'));
while (! $this->isWorkingDay() || $this->getHour() > $end ||
($preserveHours == false && $this->getHour() == $end && $this->getMinute() == '0')) {
$this->addDays(1);
$this->setTime($start, '0', '0');
}
if ($preserveHours) {
$this->setTime($do->getHour(), '0', '0');
}
return $this;
}
/* Return date obj for the end of the previous working day
** @param bool Determine whether to set time to end of day or preserve the time of the given object
*/
function prev_working_day($preserveHours = false) {
global $AppUI;
$do = $this;
$end = intval(dPgetConfig('cal_day_end'));
$start = intval(dPgetConfig('cal_day_start'));
while (! $this->isWorkingDay() || ($this->getHour() < $start) ||
($this->getHour() == $start && $this->getMinute() == '0')) {
$this->addDays(-1);
$this->setTime($end, '0', '0');
}
if ($preserveHours) {
$this->setTime($do->getHour(), '0', '0');
}
return $this;
}
/* Calculating _robustly_ a date from a given date and duration
** Works in both directions: forwards/prospective and backwards/retrospective
** Respects non-working days
** @param int duration (positive = forward, negative = backward)
** @param int durationType; 1 = hour; 24 = day;
** @return obj Shifted DateObj
*/
function addDuration($duration = '8', $durationType ='1') {
// using a sgn function lets us easily cover
// prospective and retrospective calcs at the same time
// get signum of the duration
$sgn = dPsgn($duration);
// make duration positive
$duration = abs($duration);
if ($durationType == '24') { // duration type is 24, full days, we're finished very quickly
$full_working_days = $duration;
} else if ($durationType == '1') { // durationType is 1 hour
// get dP time constants
$cal_day_start = intval(dPgetConfig('cal_day_start'));
$cal_day_end = intval(dPgetConfig('cal_day_end'));
$dwh = intval(dPgetConfig('daily_working_hours'));
// move to the next working day if the first day is a non-working day
($sgn > 0) ? $this->next_working_day() : $this->prev_working_day();
// calculate the hours spent on the first day
$firstDay = ($sgn > 0) ? min($cal_day_end - $this->hour, $dwh) : min($this->hour - $cal_day_start, $dwh);
/*
** Catch some possible inconsistencies:
** If we're later than cal_end_day or sooner than cal_start_day then we don't need to
** subtract any time from duration. The difference is greater than the # of daily working hours
*/
if ($firstDay < 0) {
$firstDay = 0;
}
// Intraday additions are handled easily by just changing the hour value
if ($duration <= $firstDay) {
($sgn > 0) ? $this->setHour($this->hour+$duration) : $this->setHour($this->hour-$duration);
return $this;
}
// the effective first day hours value
$firstAdj = min($dwh, $firstDay);
// subtract the first day hours from the total duration
$duration -= $firstAdj;
// we've already processed the first day; move by one day!
$this->addDays(1 * $sgn);
// make sure that we didn't move to a non-working day
($sgn > 0) ? $this->next_working_day() : $this->prev_working_day();
// end of proceeding the first day
// calc the remaining time and the full working days part of this residual
$hoursRemaining = ($duration > $dwh) ? ($duration % $dwh) : $duration;
$full_working_days = round(($duration - $hoursRemaining) / $dwh);
// (proceed the full days later)
// proceed the last day now
// we prefer wed 16:00 over thu 08:00 as end date :)
if ($hoursRemaining == 0 && $full_working_day > 0) {
$full_working_days--;
($sgn > 0) ? $this->setHour($cal_day_start+$dwh) : $this->setHour($cal_day_end-$dwh);
} else {
($sgn > 0) ? $this->setHour($cal_day_start+$hoursRemaining) : $this->setHour($cal_day_end-$hoursRemaining);
}
//end of proceeding the last day
}
// proceeding the fulldays finally which is easy
// Full days
for ($i = 0 ; $i < $full_working_days ; $i++) {
$this->addDays(1 * $sgn);
if (!$this->isWorkingDay()) {
// just 'ignore' this non-working day
$full_working_days++;
}
}
//end of proceeding the fulldays
return $this->next_working_day();
}
/* Calculating _robustly_ the working duration between two dates
**
** Works in both directions: forwards/prospective and backwards/retrospective
** Respects non-working days
**
**
** @param obj DateObject may be viewed as end date
** @return int working duration in hours
*/
function calcDuration($e) {
// since one will alter the date ($this) one better copies it to a new instance
$s = new CDate();
$s->copy($this);
// get dP time constants
$cal_day_start = intval(dPgetConfig('cal_day_start'));
$cal_day_end = intval(dPgetConfig('cal_day_end'));
$dwh = intval(dPgetConfig('daily_working_hours'));
// assume start is before end and set a default signum for the duration
$sgn = 1;
// check whether start before end, interchange otherwise
if ($e->before($s)) {
// calculated duration must be negative, set signum appropriately
$sgn = -1;
$dummy = $s;
$s->copy($e);
$e = $dummy;
}
// determine the (working + non-working) day difference between the two dates
$days = abs($e->dateDiff($s));
// if it is an intraday difference one is finished very easily
if ($days == 0) {
return min($dwh, abs($e->hour - $s->hour)) * $sgn;
}
// initialize the duration var
$duration = 0;
// process the first day
// take into account the first day if it is a working day!
$day_endpoint = (($sgn > 0) ? $cal_day_end : $cal_day_start);
$duration += $s->isWorkingDay() ? min($dwh, abs($day_endpoint - $s->hour)) : 0;
$s->addDays(1 * $sgn);
// end of processing the first day
// calc workingdays between start and end
for ($i=1; $i < $days; $i++) {
$duration += $s->isWorkingDay() ? $dwh : 0;
$s->addDays(1 * $sgn);
}
// take into account the last day in span only if it is a working day!
$day_endpoint = (($sgn > 0) ? $cal_day_start : $cal_day_end);
$duration += $s->isWorkingDay() ? min($dwh, abs($e->hour - $day_endpoint)) : 0;
return $duration * $sgn;
}
function workingDaysInSpan($e) {
global $AppUI;
// assume start is before end and set a default signum for the duration
$sgn = 1;
// check whether start before end, interchange otherwise
if ($e->before($this)) {
// duration is negative, set signum appropriately
$sgn = -1;
}
$wd = 0;
$days = $e->dateDiff($this);
$start = $this;
for ($i = 0 ; $i <= $days ; $i++) {
if ($start->isWorkingDay()) {
$wd++;
}
$start->addDays(1 * $sgn);
}
return $wd;
}
}