~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Date/DateObject.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @category   Zend
 
16
 * @package    Zend_Date
 
17
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
18
 * @version    $Id: DateObject.php 11954 2008-10-14 17:27:50Z thomas $
 
19
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
20
 */
 
21
 
 
22
/**
 
23
 * @category   Zend
 
24
 * @package    Zend_Date
 
25
 * @subpackage Zend_Date_DateObject
 
26
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
27
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
28
 */
 
29
abstract class Zend_Date_DateObject {
 
30
 
 
31
    /**
 
32
     * UNIX Timestamp
 
33
     */
 
34
    private   $_unixTimestamp;
 
35
    protected static $_cache         = null;
 
36
    protected static $_defaultOffset = 0;
 
37
 
 
38
    /**
 
39
     * active timezone
 
40
     */
 
41
    private   $_timezone    = 'UTC';
 
42
    private   $_offset      = 0;
 
43
    private   $_syncronised = 0;
 
44
 
 
45
    // turn off DST correction if UTC or GMT
 
46
    protected $_dst         = true;
 
47
 
 
48
    /**
 
49
     * Table of Monthdays
 
50
     */
 
51
    private static $_monthTable = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
 
52
 
 
53
    /**
 
54
     * Table of Years
 
55
     */
 
56
    private static $_yearTable = array(
 
57
        1970 => 0,            1960 => -315619200,   1950 => -631152000,
 
58
        1940 => -946771200,   1930 => -1262304000,  1920 => -1577923200,
 
59
        1910 => -1893456000,  1900 => -2208988800,  1890 => -2524521600,
 
60
        1880 => -2840140800,  1870 => -3155673600,  1860 => -3471292800,
 
61
        1850 => -3786825600,  1840 => -4102444800,  1830 => -4417977600,
 
62
        1820 => -4733596800,  1810 => -5049129600,  1800 => -5364662400,
 
63
        1790 => -5680195200,  1780 => -5995814400,  1770 => -6311347200,
 
64
        1760 => -6626966400,  1750 => -6942499200,  1740 => -7258118400,
 
65
        1730 => -7573651200,  1720 => -7889270400,  1710 => -8204803200,
 
66
        1700 => -8520336000,  1690 => -8835868800,  1680 => -9151488000,
 
67
        1670 => -9467020800,  1660 => -9782640000,  1650 => -10098172800,
 
68
        1640 => -10413792000, 1630 => -10729324800, 1620 => -11044944000,
 
69
        1610 => -11360476800, 1600 => -11676096000);
 
70
 
 
71
    /**
 
72
     * Set this object to have a new UNIX timestamp.
 
73
     *
 
74
     * @param  string|integer  $timestamp  OPTIONAL timestamp; defaults to local time using time()
 
75
     * @return string|integer  old timestamp
 
76
     * @throws Zend_Date_Exception
 
77
     */
 
78
    protected function setUnixTimestamp($timestamp = null)
 
79
    {
 
80
        $old = $this->_unixTimestamp;
 
81
 
 
82
        if (is_numeric($timestamp)) {
 
83
            $this->_unixTimestamp = $timestamp;
 
84
        } else if ($timestamp === null) {
 
85
            $this->_unixTimestamp = time();
 
86
        } else {
 
87
            require_once 'Zend/Date/Exception.php';
 
88
            throw new Zend_Date_Exception('\'' . $timestamp . '\' is not a valid UNIX timestamp', $timestamp);
 
89
        }
 
90
 
 
91
        return $old;
 
92
    }
 
93
 
 
94
    /**
 
95
     * Returns this object's UNIX timestamp
 
96
     * A timestamp greater then the integer range will be returned as string
 
97
     * This function does not return the timestamp as object. Use copy() instead.
 
98
     *
 
99
     * @return  integer|string  timestamp
 
100
     */
 
101
    protected function getUnixTimestamp()
 
102
    {
 
103
        if ($this->_unixTimestamp === intval($this->_unixTimestamp)) {
 
104
            return (int) $this->_unixTimestamp;
 
105
        } else {
 
106
            return (string) $this->_unixTimestamp;
 
107
        }
 
108
    }
 
109
 
 
110
    /**
 
111
     * Internal function.
 
112
     * Returns time().  This method exists to allow unit tests to work-around methods that might otherwise
 
113
     * be hard-coded to use time().  For example, this makes it possible to test isYesterday() in Date.php.
 
114
     *
 
115
     * @param   integer  $sync      OPTIONAL time syncronisation value
 
116
     * @return  integer  timestamp
 
117
     */
 
118
    protected function _getTime($sync = null)
 
119
    {
 
120
        if ($sync !== null) {
 
121
            $this->_syncronised = round($sync);
 
122
        }
 
123
        return (time() + $this->_syncronised);
 
124
    }
 
125
 
 
126
    /**
 
127
     * Internal mktime function used by Zend_Date.
 
128
     * The timestamp returned by mktime() can exceed the precision of traditional UNIX timestamps,
 
129
     * by allowing PHP to auto-convert to using a float value.
 
130
     *
 
131
     * Returns a timestamp relative to 1970/01/01 00:00:00 GMT/UTC.
 
132
     * DST (Summer/Winter) is depriciated since php 5.1.0.
 
133
     * Year has to be 4 digits otherwise it would be recognised as
 
134
     * year 70 AD instead of 1970 AD as expected !!
 
135
     *
 
136
     * @param  integer  $hour
 
137
     * @param  integer  $minute
 
138
     * @param  integer  $second
 
139
     * @param  integer  $month
 
140
     * @param  integer  $day
 
141
     * @param  integer  $year
 
142
     * @param  boolean  $gmt     OPTIONAL true = other arguments are for UTC time, false = arguments are for local time/date
 
143
     * @return  integer|float  timestamp (number of seconds elapsed relative to 1970/01/01 00:00:00 GMT/UTC)
 
144
     */
 
145
    protected function mktime($hour, $minute, $second, $month, $day, $year, $gmt = false)
 
146
    {
 
147
        
 
148
        // complete date but in 32bit timestamp - use PHP internal
 
149
        if ((1901 < $year) and ($year < 2038)) {
 
150
 
 
151
            $oldzone = @date_default_timezone_get();
 
152
            // Timezone also includes DST settings, therefor substracting the GMT offset is not enough
 
153
            // We have to set the correct timezone to get the right value
 
154
            if (($this->_timezone != $oldzone) and ($gmt === false)) {
 
155
                date_default_timezone_set($this->_timezone);
 
156
            }
 
157
            $result = ($gmt) ? @gmmktime($hour, $minute, $second, $month, $day, $year)
 
158
                             :   @mktime($hour, $minute, $second, $month, $day, $year);
 
159
            date_default_timezone_set($oldzone);
 
160
 
 
161
            return $result;
 
162
        }
 
163
 
 
164
        if ($gmt !== true) {
 
165
            $second += $this->_offset;
 
166
        }
 
167
 
 
168
        if (isset(self::$_cache)) {
 
169
            $id = strtr('Zend_DateObject_mkTime_' . $this->_offset . '_' . $year.$month.$day.'_'.$hour.$minute.$second . '_'.(int)$gmt, '-','_');
 
170
            if ($result = self::$_cache->load($id)) {
 
171
                return unserialize($result);
 
172
            }
 
173
        }
 
174
 
 
175
        // date to integer
 
176
        $day   = intval($day);
 
177
        $month = intval($month);
 
178
        $year  = intval($year);
 
179
 
 
180
        // correct months > 12 and months < 1
 
181
        if ($month > 12) {
 
182
            $overlap = floor($month / 12);
 
183
            $year   += $overlap;
 
184
            $month  -= $overlap * 12;
 
185
        } else {
 
186
            $overlap = ceil((1 - $month) / 12);
 
187
            $year   -= $overlap;
 
188
            $month  += $overlap * 12;
 
189
        }
 
190
 
 
191
        $date = 0;
 
192
        if ($year >= 1970) {
 
193
 
 
194
            // Date is after UNIX epoch
 
195
            // go through leapyears
 
196
            // add months from latest given year
 
197
            for ($count = 1970; $count <= $year; $count++) {
 
198
 
 
199
                $leapyear = self::isYearLeapYear($count);
 
200
                if ($count < $year) {
 
201
 
 
202
                    $date += 365;
 
203
                    if ($leapyear === true) {
 
204
                        $date++;
 
205
                    }
 
206
 
 
207
                } else {
 
208
 
 
209
                    for ($mcount = 0; $mcount < ($month - 1); $mcount++) {
 
210
                        $date += self::$_monthTable[$mcount];
 
211
                        if (($leapyear === true) and ($mcount == 1)) {
 
212
                            $date++;
 
213
                        }
 
214
 
 
215
                    }
 
216
                }
 
217
            }
 
218
 
 
219
            $date += $day - 1;
 
220
            $date = (($date * 86400) + ($hour * 3600) + ($minute * 60) + $second);
 
221
        } else {
 
222
 
 
223
            // Date is before UNIX epoch
 
224
            // go through leapyears
 
225
            // add months from latest given year
 
226
            for ($count = 1969; $count >= $year; $count--) {
 
227
 
 
228
                $leapyear = self::isYearLeapYear($count);
 
229
                if ($count > $year)
 
230
                {
 
231
                    $date += 365;
 
232
                    if ($leapyear === true)
 
233
                        $date++;
 
234
                } else {
 
235
 
 
236
                    for ($mcount = 11; $mcount > ($month - 1); $mcount--) {
 
237
                        $date += self::$_monthTable[$mcount];
 
238
                        if (($leapyear === true) and ($mcount == 1)) {
 
239
                            $date++;
 
240
                        }
 
241
 
 
242
                    }
 
243
                }
 
244
            }
 
245
 
 
246
            $date += (self::$_monthTable[$month - 1] - $day);
 
247
            $date = -(($date * 86400) + (86400 - (($hour * 3600) + ($minute * 60) + $second)));
 
248
 
 
249
            // gregorian correction for 5.Oct.1582
 
250
            if ($date < -12220185600) {
 
251
                $date += 864000;
 
252
            } else if ($date < -12219321600) {
 
253
                $date  = -12219321600;
 
254
            }
 
255
        }
 
256
 
 
257
        if (isset(self::$_cache)) {
 
258
            self::$_cache->save( serialize($date), $id);
 
259
        }
 
260
 
 
261
        return $date;
 
262
    }
 
263
 
 
264
    /**
 
265
     * Returns true, if given $year is a leap year.
 
266
     *
 
267
     * @param  integer  $year
 
268
     * @return boolean  true, if year is leap year
 
269
     */
 
270
    protected static function isYearLeapYear($year)
 
271
    {
 
272
        // all leapyears can be divided through 4
 
273
        if (($year % 4) != 0) {
 
274
            return false;
 
275
        }
 
276
 
 
277
        // all leapyears can be divided through 400
 
278
        if ($year % 400 == 0) {
 
279
            return true;
 
280
        } else if (($year > 1582) and ($year % 100 == 0)) {
 
281
            return false;
 
282
        }
 
283
 
 
284
        return true;
 
285
    }
 
286
 
 
287
    /**
 
288
     * Internal mktime function used by Zend_Date for handling 64bit timestamps.
 
289
     *
 
290
     * Returns a formatted date for a given timestamp.
 
291
     *
 
292
     * @param  string   $format     format for output
 
293
     * @param  mixed    $timestamp
 
294
     * @param  boolean  $gmt        OPTIONAL true = other arguments are for UTC time, false = arguments are for local time/date
 
295
     * @return string
 
296
     */
 
297
    protected function date($format, $timestamp = null, $gmt = false)
 
298
    {
 
299
        $oldzone = @date_default_timezone_get();
 
300
        if ($this->_timezone != $oldzone) {
 
301
            date_default_timezone_set($this->_timezone);
 
302
        }
 
303
        if ($timestamp === null) {
 
304
            $result = ($gmt) ? @gmdate($format) : @date($format);
 
305
            date_default_timezone_set($oldzone);
 
306
            return $result;
 
307
        }
 
308
 
 
309
        if (abs($timestamp) <= 0x7FFFFFFF) {
 
310
            $result = ($gmt) ? @gmdate($format, $timestamp) : @date($format, $timestamp);
 
311
            date_default_timezone_set($oldzone);
 
312
            return $result;
 
313
        }
 
314
 
 
315
        $jump = false;
 
316
        if (isset(self::$_cache)) {
 
317
            $idstamp = strtr('Zend_DateObject_date_' . $this->_offset . '_'. $timestamp . '_'.(int)$gmt, '-','_');
 
318
            if ($result2 = self::$_cache->load($idstamp)) {
 
319
                $timestamp = unserialize($result2);
 
320
                $jump = true;
 
321
            }
 
322
        }
 
323
 
 
324
        // check on false or null alone failes
 
325
        if (empty($gmt) and empty($jump)) {
 
326
            $tempstamp = $timestamp;
 
327
            if ($tempstamp > 0) {
 
328
                while (abs($tempstamp) > 0x7FFFFFFF) {
 
329
                    $tempstamp -= (86400 * 23376);
 
330
                }
 
331
                $dst = date("I", $tempstamp);
 
332
                if ($dst === 1) {
 
333
                    $timestamp += 3600;
 
334
                }
 
335
                $temp = date('Z', $tempstamp);
 
336
                $timestamp += $temp;
 
337
            }
 
338
 
 
339
            if (isset(self::$_cache)) {
 
340
                self::$_cache->save( serialize($timestamp), $idstamp);
 
341
            }
 
342
        }
 
343
 
 
344
 
 
345
        if (($timestamp < 0) and ($gmt !== true)) {
 
346
            $timestamp -= $this->_offset;
 
347
        }
 
348
        date_default_timezone_set($oldzone);
 
349
 
 
350
        $date = $this->getDateParts($timestamp, true);
 
351
        $length = strlen($format);
 
352
        $output = '';
 
353
 
 
354
        for ($i = 0; $i < $length; $i++) {
 
355
 
 
356
            switch($format[$i]) {
 
357
 
 
358
                // day formats
 
359
                case 'd':  // day of month, 2 digits, with leading zero, 01 - 31
 
360
                    $output .= (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']);
 
361
                    break;
 
362
 
 
363
                case 'D':  // day of week, 3 letters, Mon - Sun
 
364
                    $output .= date('D', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday'])));
 
365
                    break;
 
366
 
 
367
                case 'j':  // day of month, without leading zero, 1 - 31
 
368
                    $output .= $date['mday'];
 
369
                    break;
 
370
 
 
371
                case 'l':  // day of week, full string name, Sunday - Saturday
 
372
                    $output .= date('l', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday'])));
 
373
                    break;
 
374
 
 
375
                case 'N':  // ISO 8601 numeric day of week, 1 - 7
 
376
                    $day = self::dayOfWeek($date['year'], $date['mon'], $date['mday']);
 
377
                    if ($day == 0) {
 
378
                        $day = 7;
 
379
                    }
 
380
                    $output .= $day;
 
381
                    break;
 
382
 
 
383
                case 'S':  // english suffix for day of month, st nd rd th
 
384
                    if (($date['mday'] % 10) == 1) {
 
385
                        $output .= 'st';
 
386
                    } else if ((($date['mday'] % 10) == 2) and ($date['mday'] != 12)) {
 
387
                        $output .= 'nd';
 
388
                    } else if (($date['mday'] % 10) == 3) {
 
389
                        $output .= 'rd';
 
390
                    } else {
 
391
                        $output .= 'th';
 
392
                    }
 
393
                    break;
 
394
 
 
395
                case 'w':  // numeric day of week, 0 - 6
 
396
                    $output .= self::dayOfWeek($date['year'], $date['mon'], $date['mday']);
 
397
                    break;
 
398
 
 
399
                case 'z':  // day of year, 0 - 365
 
400
                    $output .= $date['yday'];
 
401
                    break;
 
402
 
 
403
 
 
404
                // week formats
 
405
                case 'W':  // ISO 8601, week number of year
 
406
                    $output .= $this->weekNumber($date['year'], $date['mon'], $date['mday']);
 
407
                    break;
 
408
 
 
409
 
 
410
                // month formats
 
411
                case 'F':  // string month name, january - december
 
412
                    $output .= date('F', mktime(0, 0, 0, $date['mon'], 2, 1971));
 
413
                    break;
 
414
 
 
415
                case 'm':  // number of month, with leading zeros, 01 - 12
 
416
                    $output .= (($date['mon'] < 10) ? '0' . $date['mon'] : $date['mon']);
 
417
                    break;
 
418
 
 
419
                case 'M':  // 3 letter month name, Jan - Dec
 
420
                    $output .= date('M',mktime(0, 0, 0, $date['mon'], 2, 1971));
 
421
                    break;
 
422
 
 
423
                case 'n':  // number of month, without leading zeros, 1 - 12
 
424
                    $output .= $date['mon'];
 
425
                    break;
 
426
 
 
427
                case 't':  // number of day in month
 
428
                    $output .= self::$_monthTable[$date['mon'] - 1];
 
429
                    break;
 
430
 
 
431
 
 
432
                // year formats
 
433
                case 'L':  // is leap year ?
 
434
                    $output .= (self::isYearLeapYear($date['year'])) ? '1' : '0';
 
435
                    break;
 
436
 
 
437
                case 'o':  // ISO 8601 year number
 
438
                    $week = $this->weekNumber($date['year'], $date['mon'], $date['mday']);
 
439
                    if (($week > 50) and ($date['mon'] == 1)) {
 
440
                        $output .= ($date['year'] - 1);
 
441
                    } else {
 
442
                        $output .= $date['year'];
 
443
                    }
 
444
                    break;
 
445
 
 
446
                case 'Y':  // year number, 4 digits
 
447
                    $output .= $date['year'];
 
448
                    break;
 
449
 
 
450
                case 'y':  // year number, 2 digits
 
451
                    $output .= substr($date['year'], strlen($date['year']) - 2, 2);
 
452
                    break;
 
453
 
 
454
 
 
455
                // time formats
 
456
                case 'a':  // lower case am/pm
 
457
                    $output .= (($date['hours'] >= 12) ? 'pm' : 'am');
 
458
                    break;
 
459
 
 
460
                case 'A':  // upper case am/pm
 
461
                    $output .= (($date['hours'] >= 12) ? 'PM' : 'AM');
 
462
                    break;
 
463
 
 
464
                case 'B':  // swatch internet time
 
465
                    $dayseconds = ($date['hours'] * 3600) + ($date['minutes'] * 60) + $date['seconds'];
 
466
                    if ($gmt === true) {
 
467
                        $dayseconds += 3600;
 
468
                    }
 
469
                    $output .= (int) (($dayseconds % 86400) / 86.4);
 
470
                    break;
 
471
 
 
472
                case 'g':  // hours without leading zeros, 12h format
 
473
                    if ($date['hours'] > 12) {
 
474
                        $hour = $date['hours'] - 12;
 
475
                    } else {
 
476
                        if ($date['hours'] == 0) {
 
477
                            $hour = '12';
 
478
                        } else {
 
479
                            $hour = $date['hours'];
 
480
                        }
 
481
                    }
 
482
                    $output .= $hour;
 
483
                    break;
 
484
 
 
485
                case 'G':  // hours without leading zeros, 24h format
 
486
                    $output .= $date['hours'];
 
487
                    break;
 
488
 
 
489
                case 'h':  // hours with leading zeros, 12h format
 
490
                    if ($date['hours'] > 12) {
 
491
                        $hour = $date['hours'] - 12;
 
492
                    } else {
 
493
                        if ($date['hours'] == 0) {
 
494
                            $hour = '12';
 
495
                        } else {
 
496
                            $hour = $date['hours'];
 
497
                        }
 
498
                    }
 
499
                    $output .= (($hour < 10) ? '0'.$hour : $hour);
 
500
                    break;
 
501
 
 
502
                case 'H':  // hours with leading zeros, 24h format
 
503
                    $output .= (($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours']);
 
504
                    break;
 
505
 
 
506
                case 'i':  // minutes with leading zeros
 
507
                    $output .= (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']);
 
508
                    break;
 
509
 
 
510
                case 's':  // seconds with leading zeros
 
511
                    $output .= (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']);
 
512
                    break;
 
513
 
 
514
 
 
515
                // timezone formats
 
516
                case 'e':  // timezone identifier
 
517
                    if ($gmt === true) {
 
518
                        $output .= gmdate('e', mktime($date['hours'], $date['minutes'], $date['seconds'],
 
519
                                                      $date['mon'], $date['mday'], 2000));
 
520
                    } else {
 
521
                        $output .=   date('e', mktime($date['hours'], $date['minutes'], $date['seconds'],
 
522
                                                      $date['mon'], $date['mday'], 2000));
 
523
                    }
 
524
                    break;
 
525
 
 
526
                case 'I':  // daylight saving time or not
 
527
                    if ($gmt === true) {
 
528
                        $output .= gmdate('I', mktime($date['hours'], $date['minutes'], $date['seconds'],
 
529
                                                      $date['mon'], $date['mday'], 2000));
 
530
                    } else {
 
531
                        $output .=   date('I', mktime($date['hours'], $date['minutes'], $date['seconds'],
 
532
                                                      $date['mon'], $date['mday'], 2000));
 
533
                    }
 
534
                    break;
 
535
 
 
536
                case 'O':  // difference to GMT in hours
 
537
                    $gmtstr = ($gmt === true) ? 0 : $this->_offset;
 
538
                    $output .= sprintf('%s%04d', ($gmtstr <= 0) ? '+' : '-', abs($gmtstr) / 36);
 
539
                    break;
 
540
 
 
541
                case 'P':  // difference to GMT with colon
 
542
                    $gmtstr = ($gmt === true) ? 0 : $this->_offset;
 
543
                    $gmtstr = sprintf('%s%04d', ($gmtstr <= 0) ? '+' : '-', abs($gmtstr) / 36);
 
544
                    $output = $output . substr($gmtstr, 0, 3) . ':' . substr($gmtstr, 3);
 
545
                    break;
 
546
 
 
547
                case 'T':  // timezone settings
 
548
                    if ($gmt === true) {
 
549
                        $output .= gmdate('T', mktime($date['hours'], $date['minutes'], $date['seconds'],
 
550
                                                      $date['mon'], $date['mday'], 2000));
 
551
                    } else {
 
552
                        $output .=   date('T', mktime($date['hours'], $date['minutes'], $date['seconds'],
 
553
                                                      $date['mon'], $date['mday'], 2000));
 
554
                    }
 
555
                    break;
 
556
 
 
557
                case 'Z':  // timezone offset in seconds
 
558
                    $output .= ($gmt === true) ? 0 : -$this->_offset;
 
559
                    break;
 
560
 
 
561
 
 
562
                // complete time formats
 
563
                case 'c':  // ISO 8601 date format
 
564
                    $difference = $this->_offset;
 
565
                    $difference = sprintf('%s%04d', ($difference <= 0) ? '+' : '-', abs($difference) / 36);
 
566
                    $output .= $date['year'] . '-'
 
567
                             . (($date['mon']     < 10) ? '0' . $date['mon']     : $date['mon'])     . '-'
 
568
                             . (($date['mday']    < 10) ? '0' . $date['mday']    : $date['mday'])    . 'T'
 
569
                             . (($date['hours']   < 10) ? '0' . $date['hours']   : $date['hours'])   . ':'
 
570
                             . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']) . ':'
 
571
                             . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds'])
 
572
                             . $difference;
 
573
                    break;
 
574
 
 
575
                case 'r':  // RFC 2822 date format
 
576
                    $difference = $this->_offset;
 
577
                    $difference = sprintf('%s%04d', ($difference <= 0) ? '+' : '-', abs($difference) / 36);
 
578
                    $output .= gmdate('D', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday']))) . ', '
 
579
                             . (($date['mday']    < 10) ? '0' . $date['mday']    : $date['mday'])    . ' '
 
580
                             . date('M', mktime(0, 0, 0, $date['mon'], 2, 1971)) . ' '
 
581
                             . $date['year'] . ' '
 
582
                             . (($date['hours']   < 10) ? '0' . $date['hours']   : $date['hours'])   . ':'
 
583
                             . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']) . ':'
 
584
                             . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']) . ' '
 
585
                             . $difference;
 
586
                    break;
 
587
 
 
588
                case 'U':  // Unix timestamp
 
589
                    $output .= $timestamp;
 
590
                    break;
 
591
 
 
592
 
 
593
                // special formats
 
594
                case "\\":  // next letter to print with no format
 
595
                    $i++;
 
596
                    if ($i < $length) {
 
597
                        $output .= $format[$i];
 
598
                    }
 
599
                    break;
 
600
 
 
601
                default:  // letter is no format so add it direct
 
602
                    $output .= $format[$i];
 
603
                    break;
 
604
            }
 
605
        }
 
606
 
 
607
        return (string) $output;
 
608
    }
 
609
 
 
610
    /**
 
611
     * Returns the day of week for a Gregorian calendar date.
 
612
     * 0 = sunday, 6 = saturday
 
613
     *
 
614
     * @param  integer  $year
 
615
     * @param  integer  $month
 
616
     * @param  integer  $day
 
617
     * @return integer  dayOfWeek
 
618
     */
 
619
    protected static function dayOfWeek($year, $month, $day)
 
620
    {
 
621
        if ((1901 < $year) and ($year < 2038)) {
 
622
            return (int) date('w', mktime(0, 0, 0, $month, $day, $year));
 
623
        }
 
624
 
 
625
        // gregorian correction
 
626
        $correction = 0;
 
627
        if (($year < 1582) or (($year == 1582) and (($month < 10) or (($month == 10) && ($day < 15))))) {
 
628
            $correction = 3;
 
629
        }
 
630
 
 
631
        if ($month > 2) {
 
632
            $month -= 2;
 
633
        } else {
 
634
            $month += 10;
 
635
            $year--;
 
636
        }
 
637
 
 
638
        $day  = floor((13 * $month - 1) / 5) + $day + ($year % 100) + floor(($year % 100) / 4);
 
639
        $day += floor(($year / 100) / 4) - 2 * floor($year / 100) + 77 + $correction;
 
640
 
 
641
        return (int) ($day - 7 * floor($day / 7));
 
642
    }
 
643
 
 
644
    /**
 
645
     * Internal getDateParts function for handling 64bit timestamps, similar to:
 
646
     * http://www.php.net/getdate
 
647
     *
 
648
     * Returns an array of date parts for $timestamp, relative to 1970/01/01 00:00:00 GMT/UTC.
 
649
     *
 
650
     * $fast specifies ALL date parts should be returned (slower)
 
651
     * Default is false, and excludes $dayofweek, weekday, month and timestamp from parts returned.
 
652
     *
 
653
     * @param   mixed    $timestamp
 
654
     * @param   boolean  $fast   OPTIONAL defaults to fast (false), resulting in fewer date parts
 
655
     * @return  array
 
656
     */
 
657
    protected function getDateParts($timestamp = null, $fast = null)
 
658
    {
 
659
 
 
660
        // actual timestamp
 
661
        if ($timestamp === null) {
 
662
            return getdate();
 
663
        }
 
664
 
 
665
        // 32bit timestamp
 
666
        if (abs($timestamp) <= 0x7FFFFFFF) {
 
667
            return @getdate($timestamp);
 
668
        }
 
669
 
 
670
        if (isset(self::$_cache)) {
 
671
            $id = strtr('Zend_DateObject_getDateParts_' . $timestamp.'_'.(int)$fast, '-','_');
 
672
            if ($result = self::$_cache->load($id)) {
 
673
                return unserialize($result);
 
674
            }
 
675
        }
 
676
 
 
677
        $otimestamp = $timestamp;
 
678
        $numday = 0;
 
679
        $month = 0;
 
680
        // gregorian correction
 
681
        if ($timestamp < -12219321600) {
 
682
            $timestamp -= 864000;
 
683
        }
 
684
 
 
685
        // timestamp lower 0
 
686
        if ($timestamp < 0) {
 
687
            $sec = 0;
 
688
            $act = 1970;
 
689
 
 
690
            // iterate through 10 years table, increasing speed
 
691
            foreach(self::$_yearTable as $year => $seconds) {
 
692
                if ($timestamp >= $seconds) {
 
693
                    $i = $act;
 
694
                    break;
 
695
                }
 
696
                $sec = $seconds;
 
697
                $act = $year;
 
698
            }
 
699
 
 
700
            $timestamp -= $sec;
 
701
            if (!isset($i)) {
 
702
                $i = $act;
 
703
            }
 
704
 
 
705
            // iterate the max last 10 years
 
706
            do {
 
707
                --$i;
 
708
                $day = $timestamp;
 
709
 
 
710
                $timestamp += 31536000;
 
711
                $leapyear = self::isYearLeapYear($i);
 
712
                if ($leapyear === true) {
 
713
                    $timestamp += 86400;
 
714
                }
 
715
 
 
716
                if ($timestamp >= 0) {
 
717
                    $year = $i;
 
718
                    break;
 
719
                }
 
720
            } while ($timestamp < 0);
 
721
 
 
722
            $secondsPerYear = 86400 * ($leapyear ? 366 : 365) + $day;
 
723
 
 
724
            $timestamp = $day;
 
725
            // iterate through months
 
726
            for ($i = 12; --$i >= 0;) {
 
727
                $day = $timestamp;
 
728
 
 
729
                $timestamp += self::$_monthTable[$i] * 86400;
 
730
                if (($leapyear === true) and ($i == 1)) {
 
731
                    $timestamp += 86400;
 
732
                }
 
733
 
 
734
                if ($timestamp >= 0) {
 
735
                    $month  = $i;
 
736
                    $numday = self::$_monthTable[$i];
 
737
                    if (($leapyear === true) and ($i == 1)) {
 
738
                        ++$numday;
 
739
                    }
 
740
                    break;
 
741
                }
 
742
            }
 
743
 
 
744
            $timestamp  = $day;
 
745
            $numberdays = $numday + ceil(($timestamp + 1) / 86400);
 
746
 
 
747
            $timestamp += ($numday - $numberdays + 1) * 86400;
 
748
            $hours      = floor($timestamp / 3600);
 
749
        } else {
 
750
 
 
751
            // iterate through years
 
752
            for ($i = 1970;;$i++) {
 
753
                $day = $timestamp;
 
754
 
 
755
                $timestamp -= 31536000;
 
756
                $leapyear = self::isYearLeapYear($i);
 
757
                if ($leapyear === true) {
 
758
                    $timestamp -= 86400;
 
759
                }
 
760
 
 
761
                if ($timestamp < 0) {
 
762
                    $year = $i;
 
763
                    break;
 
764
                }
 
765
            }
 
766
 
 
767
            $secondsPerYear = $day;
 
768
 
 
769
            $timestamp = $day;
 
770
            // iterate through months
 
771
            for ($i = 0; $i <= 11; $i++) {
 
772
                $day = $timestamp;
 
773
                $timestamp -= self::$_monthTable[$i] * 86400;
 
774
 
 
775
                if (($leapyear === true) and ($i == 1)) {
 
776
                    $timestamp -= 86400;
 
777
                }
 
778
 
 
779
                if ($timestamp < 0) {
 
780
                    $month  = $i;
 
781
                    $numday = self::$_monthTable[$i];
 
782
                    if (($leapyear === true) and ($i == 1)) {
 
783
                        ++$numday;
 
784
                    }
 
785
                    break;
 
786
                }
 
787
            }
 
788
 
 
789
            $timestamp  = $day;
 
790
            $numberdays = ceil(($timestamp + 1) / 86400);
 
791
            $timestamp  = $timestamp - ($numberdays - 1) * 86400;
 
792
            $hours = floor($timestamp / 3600);
 
793
        }
 
794
 
 
795
        $timestamp -= $hours * 3600;
 
796
 
 
797
        $month  += 1;
 
798
        $minutes = floor($timestamp / 60);
 
799
        $seconds = $timestamp - $minutes * 60;
 
800
 
 
801
        if ($fast === true) {
 
802
            $array = array(
 
803
                'seconds' => $seconds,
 
804
                'minutes' => $minutes,
 
805
                'hours'   => $hours,
 
806
                'mday'    => $numberdays,
 
807
                'mon'     => $month,
 
808
                'year'    => $year,
 
809
                'yday'    => floor($secondsPerYear / 86400),
 
810
            );
 
811
        } else {
 
812
 
 
813
            $dayofweek = self::dayOfWeek($year, $month, $numberdays);
 
814
            $array = array(
 
815
                    'seconds' => $seconds,
 
816
                    'minutes' => $minutes,
 
817
                    'hours'   => $hours,
 
818
                    'mday'    => $numberdays,
 
819
                    'wday'    => $dayofweek,
 
820
                    'mon'     => $month,
 
821
                    'year'    => $year,
 
822
                    'yday'    => floor($secondsPerYear / 86400),
 
823
                    'weekday' => gmdate('l', 86400 * (3 + $dayofweek)),
 
824
                    'month'   => gmdate('F', mktime(0, 0, 0, $month, 1, 1971)),
 
825
                    0         => $otimestamp
 
826
            );
 
827
        }
 
828
 
 
829
        if (isset(self::$_cache)) {
 
830
            self::$_cache->save( serialize($array), $id);
 
831
        }
 
832
 
 
833
        return $array;
 
834
    }
 
835
 
 
836
    /**
 
837
     * Internal getWeekNumber function for handling 64bit timestamps
 
838
     *
 
839
     * Returns the ISO 8601 week number of a given date
 
840
     *
 
841
     * @param  integer  $year
 
842
     * @param  integer  $month
 
843
     * @param  integer  $day
 
844
     * @return integer
 
845
     */
 
846
    protected function weekNumber($year, $month, $day)
 
847
    {
 
848
        if ((1901 < $year) and ($year < 2038)) {
 
849
            return (int) date('W', mktime(0, 0, 0, $month, $day, $year));
 
850
        }
 
851
 
 
852
        $dayofweek = self::dayOfWeek($year, $month, $day);
 
853
        $firstday  = self::dayOfWeek($year, 1, 1);
 
854
        if (($month == 1) and (($firstday < 1) or ($firstday > 4)) and ($day < 4)) {
 
855
            $firstday  = self::dayOfWeek($year - 1, 1, 1);
 
856
            $month     = 12;
 
857
            $day       = 31;
 
858
 
 
859
        } else if (($month == 12) and ((self::dayOfWeek($year + 1, 1, 1) < 5) and
 
860
                   (self::dayOfWeek($year + 1, 1, 1) > 0))) {
 
861
            return 1;
 
862
        }
 
863
 
 
864
        return intval (((self::dayOfWeek($year, 1, 1) < 5) and (self::dayOfWeek($year, 1, 1) > 0)) +
 
865
               4 * ($month - 1) + (2 * ($month - 1) + ($day - 1) + $firstday - $dayofweek + 6) * 36 / 256);
 
866
    }
 
867
 
 
868
    /**
 
869
     * Internal _range function
 
870
     * Sets the value $a to be in the range of [0, $b]
 
871
     *
 
872
     * @param float $a - value to correct
 
873
     * @param float $b - maximum range to set
 
874
     */
 
875
    private function _range($a, $b) {
 
876
        while ($a < 0) {
 
877
            $a += $b;
 
878
        }
 
879
        while ($a >= $b) {
 
880
            $a -= $b;
 
881
        }
 
882
        return $a;
 
883
    }
 
884
 
 
885
    /**
 
886
     * Calculates the sunrise or sunset based on a location
 
887
     *
 
888
     * @param  array  $location  Location for calculation MUST include 'latitude', 'longitude', 'horizon'
 
889
     * @param  bool   $horizon   true: sunrise; false: sunset
 
890
     * @return mixed  - false: midnight sun, integer:
 
891
     */
 
892
    protected function calcSun($location, $horizon, $rise = false)
 
893
    {
 
894
        // timestamp within 32bit
 
895
        if (abs($this->_unixTimestamp) <= 0x7FFFFFFF) {
 
896
            if ($rise === false) {
 
897
                return date_sunset($this->_unixTimestamp, SUNFUNCS_RET_TIMESTAMP, $location['latitude'],
 
898
                                   $location['longitude'], 90 + $horizon, $this->_offset / 3600);
 
899
            }
 
900
            return date_sunrise($this->_unixTimestamp, SUNFUNCS_RET_TIMESTAMP, $location['latitude'],
 
901
                                $location['longitude'], 90 + $horizon, $this->_offset / 3600);
 
902
        }
 
903
 
 
904
        // self calculation - timestamp bigger than 32bit
 
905
        // fix circle values
 
906
        $quarterCircle      = 0.5 * M_PI;
 
907
        $halfCircle         =       M_PI;
 
908
        $threeQuarterCircle = 1.5 * M_PI;
 
909
        $fullCircle         = 2   * M_PI;
 
910
 
 
911
        // radiant conversion for coordinates
 
912
        $radLatitude  = $location['latitude']   * $halfCircle / 180;
 
913
        $radLongitude = $location['longitude']  * $halfCircle / 180;
 
914
 
 
915
        // get solar coordinates
 
916
        $tmpRise       = $rise ? $quarterCircle : $threeQuarterCircle;
 
917
        $radDay        = $this->date('z',$this->_unixTimestamp) + ($tmpRise - $radLongitude) / $fullCircle;
 
918
 
 
919
        // solar anomoly and longitude
 
920
        $solAnomoly    = $radDay * 0.017202 - 0.0574039;
 
921
        $solLongitude  = $solAnomoly + 0.0334405 * sin($solAnomoly);
 
922
        $solLongitude += 4.93289 + 3.49066E-4 * sin(2 * $solAnomoly);
 
923
 
 
924
        // get quadrant
 
925
        $solLongitude = $this->_range($solLongitude, $fullCircle);
 
926
 
 
927
        if (($solLongitude / $quarterCircle) - intval($solLongitude / $quarterCircle) == 0) {
 
928
            $solLongitude += 4.84814E-6;
 
929
        }
 
930
 
 
931
        // solar ascension
 
932
        $solAscension = sin($solLongitude) / cos($solLongitude);
 
933
        $solAscension = atan2(0.91746 * $solAscension, 1);
 
934
 
 
935
        // adjust quadrant
 
936
        if ($solLongitude > $threeQuarterCircle) {
 
937
            $solAscension += $fullCircle;
 
938
        } else if ($solLongitude > $quarterCircle) {
 
939
            $solAscension += $halfCircle;
 
940
        }
 
941
 
 
942
        // solar declination
 
943
        $solDeclination  = 0.39782 * sin($solLongitude);
 
944
        $solDeclination /=  sqrt(-$solDeclination * $solDeclination + 1);
 
945
        $solDeclination  = atan2($solDeclination, 1);
 
946
 
 
947
        $solHorizon = $horizon - sin($solDeclination) * sin($radLatitude);
 
948
        $solHorizon /= cos($solDeclination) * cos($radLatitude);
 
949
 
 
950
        // midnight sun, always night
 
951
        if (abs($solHorizon) > 1) {
 
952
            return false;
 
953
        }
 
954
 
 
955
        $solHorizon /= sqrt(-$solHorizon * $solHorizon + 1);
 
956
        $solHorizon  = $quarterCircle - atan2($solHorizon, 1);
 
957
 
 
958
        if ($rise) {
 
959
            $solHorizon = $fullCircle - $solHorizon;
 
960
        }
 
961
 
 
962
        // time calculation
 
963
        $localTime     = $solHorizon + $solAscension - 0.0172028 * $radDay - 1.73364;
 
964
        $universalTime = $localTime - $radLongitude;
 
965
 
 
966
        // determinate quadrant
 
967
        $universalTime = $this->_range($universalTime, $fullCircle);
 
968
 
 
969
        // radiant to hours
 
970
        $universalTime *= 24 / $fullCircle;
 
971
 
 
972
        // convert to time
 
973
        $hour = intval($universalTime);
 
974
        $universalTime    = ($universalTime - $hour) * 60;
 
975
        $min  = intval($universalTime);
 
976
        $universalTime    = ($universalTime - $min) * 60;
 
977
        $sec  = intval($universalTime);
 
978
 
 
979
        return $this->mktime($hour, $min, $sec, $this->date('m', $this->_unixTimestamp),
 
980
                             $this->date('j', $this->_unixTimestamp), $this->date('Y', $this->_unixTimestamp),
 
981
                             -1, true);
 
982
    }
 
983
 
 
984
    /**
 
985
     * Sets a new timezone for calculation of $this object's gmt offset.
 
986
     * For a list of supported timezones look here: http://php.net/timezones
 
987
     * If no timezone can be detected or the given timezone is wrong UTC will be set.
 
988
     *
 
989
     * @param  string  $zone      OPTIONAL timezone for date calculation; defaults to date_default_timezone_get()
 
990
     * @return Zend_Date_DateObject Provides fluent interface
 
991
     * @throws Zend_Date_Exception
 
992
     */
 
993
    public function setTimezone($zone = null)
 
994
    {
 
995
        $oldzone = @date_default_timezone_get();
 
996
        if ($zone === null) {
 
997
            $zone = $oldzone;
 
998
        }
 
999
 
 
1000
        // throw an error on false input, but only if the new date extension is available
 
1001
        if (function_exists('timezone_open')) {
 
1002
            if (!@timezone_open($zone)) {
 
1003
                require_once 'Zend/Date/Exception.php';
 
1004
                throw new Zend_Date_Exception("timezone ($zone) is not a known timezone", $zone);
 
1005
            }
 
1006
        }
 
1007
        // this can generate an error if the date extension is not available and a false timezone is given
 
1008
        $result = @date_default_timezone_set($zone);
 
1009
        if ($result === true) {
 
1010
            $this->_offset   = mktime(0, 0, 0, 1, 2, 1970) - gmmktime(0, 0, 0, 1, 2, 1970);
 
1011
            $this->_timezone = $zone;
 
1012
        }
 
1013
        date_default_timezone_set($oldzone);
 
1014
 
 
1015
        if (($zone == 'UTC') or ($zone == 'GMT')) {
 
1016
            $this->_dst = false;
 
1017
        }
 
1018
 
 
1019
        return $this;
 
1020
    }
 
1021
 
 
1022
    /**
 
1023
     * Return the timezone of $this object.
 
1024
     * The timezone is initially set when the object is instantiated.
 
1025
     *
 
1026
     * @return  string  actual set timezone string
 
1027
     */
 
1028
    public function getTimezone()
 
1029
    {
 
1030
        return $this->_timezone;
 
1031
    }
 
1032
 
 
1033
    /**
 
1034
     * Return the offset to GMT of $this object's timezone.
 
1035
     * The offset to GMT is initially set when the object is instantiated using the currently,
 
1036
     * in effect, default timezone for PHP functions.
 
1037
     *
 
1038
     * @return  integer  seconds difference between GMT timezone and timezone when object was instantiated
 
1039
     */
 
1040
    public function getGmtOffset()
 
1041
    {
 
1042
        return $this->_offset;
 
1043
    }
 
1044
}