-
Notifications
You must be signed in to change notification settings - Fork 0
/
Date.php
6170 lines (5607 loc) · 228 KB
/
Date.php
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
// {{{ Header
/**
* Generic date handling class for PEAR
*
* Handles time zones and changes from local standard to local Summer
* time (daylight-saving time) through the {@link Date_TimeZone} class.
* Supports several operations from {@link Date_Calc} on Date objects.
*
* PHP versions 4 and 5
*
* LICENSE:
*
* Copyright (c) 1997-2008 Baba Buehler, Pierre-Alain Joye, Firman
* Wandayandi, C.A. Woodcock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted under the terms of the BSD License.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Date and Time
* @package Date
* @author Baba Buehler <baba@babaz.com>
* @author Pierre-Alain Joye <pajoye@php.net>
* @author Firman Wandayandi <firman@php.net>
* @author C.A. Woodcock <c01234@netcomuk.co.uk>
* @copyright 1997-2007 Baba Buehler, Pierre-Alain Joye, Firman Wandayandi, C.A. Woodcock
* @license http://www.opensource.org/licenses/bsd-license.php
* BSD License
* @version CVS: $Id$
* @link http://pear.php.net/package/Date
*/
// }}}
// {{{ Error constants
define('DATE_ERROR_INVALIDDATE', 1);
define('DATE_ERROR_INVALIDTIME', 2);
define('DATE_ERROR_INVALIDTIMEZONE', 3);
define('DATE_ERROR_INVALIDDATEFORMAT', 4);
define('DATE_ERROR_INVALIDFORMATSTRING', 5);
// }}}
// {{{ Includes
require_once 'PEAR.php';
/**
* Load Date_TimeZone
*/
require_once 'Date/TimeZone.php';
/**
* Load Date_Calc
*/
require_once 'Date/Calc.php';
/**
* Load Date_Span
*/
require_once 'Date/Span.php';
// }}}
// {{{ General constants
/**
* Whether to capture the micro-time (in microseconds) by default
* in calls to {@link Date::setNow()}. Note that this makes a call to
* {@link http://www.php.net/gettimeofday gettimeofday()}, which may
* not work on all systems.
*
* @since Constant available since Release 1.5.0
*/
define('DATE_CAPTURE_MICROTIME_BY_DEFAULT', false);
/**
* Whether to correct, by adding the local Summer time offset, the
* specified time if it falls in the 'skipped hour' (encountered
* when the clocks go forward).
*
* N.B. if specified as 'false', and if a time zone that adjusts
* for Summer time is specified, then an object of this class will
* be set to a semi-invalid state if an invalid time is set. That
* is, an error will not be returned, unless the user then calls
* a function, directly or indirectly, that accesses the time
* part of the object. So, for example, if the user calls:
*
* <code>$date_object->formatLikeSQL('HH.MI.SS');</code>
*
* or:
*
* <code>$date_object->addSeconds(30);</code>
*
* an error will be returned if the time is invalid. However,
* if the user calls:
*
* <code>$date_object->addDays(1);</code>
*
* for example, such that the time is no longer invalid, then the
* object will no longer be in this invalid state. This behaviour
* is intended to minimize unexpected errors when a user uses the
* class to do addition with days only, and does not intend to
* access the time.
*
* Of course, this constant will be unused if the user chooses to
* work in UTC or a time zone without Summer time, in which case
* this situation will never arise.
*
* This constant is set to 'true' by default for backwards-compatibility
* reasons, however, you are recommended to set it to 'false'. Note that the
* behaviour is not intended to match that of previous versions of the class
* in terms of ignoring the Summer time offset when making calculations which
* involve dates in both standard and Summer time - this was recognized as a
* bug - but in terms of returning a PEAR error object when the user sets the
* object to an invalid date (i.e. a time in the hour which is skipped when
* the clocks go forwards, which in Europe would be a time such as 01.30).
* Backwards compatibility here means that the behaviour is the same as it
* used to be, less the bug.
*
* Note that this problem is not an issue for the user if any of these
* conditions are satisfied:
*
* <ol>
* <li>the user uses a time zone that does not observe Summer time, e.g. UTC</li>
* <li>the user never accesses the time, that is, he never makes a call to
* {@link Date::getHour()} or {@link Date::formatLikeStrftime()} using
* format code '<b>%H</b>', for example, even if he sets the time to
* something invalid</li>
* <li>the user sets DATE_CORRECTINVALIDTIME_DEFAULT to true</li>
* </ol>
*
* @since Constant available since Release 1.5.0
* @see Date::isValidTime(), DATE_VALIDATE_DATE_BY_DEFAULT
*/
define('DATE_CORRECTINVALIDTIME_DEFAULT', true);
/**
* Whether to validate dates (i.e. day/month/year, ignoring the time) by
* disallowing invalid dates (e.g. 31st February) being set by the following
* functions:
*
* - {@link Date::setYear()}
* - {@link Date::setMonth()}
* - {@link Date::setDay()}
*
* If the constant is set to 'true', then the date will be checked (by
* default), and if invalid, an error will be returned with the Date object
* left unmodified.
*
* This constant is set to 'false' by default for backwards-compatibility
* reasons, however, you are recommended to set it to 'true'.
*
* Note that {@link Date::setHour()}, {@link Date::setMinute()},
* {@link Date::setSecond()} and {@link Date::setPartSecond()}
* allow an invalid date/time to be set regardless of the value of this
* constant.
*
* @see Date::isValidDate(), Date::isValidTime(), Date::isNull(),
* DATE_CORRECTINVALIDTIME_DEFAULT
* @since Constant available since Release 1.5.0
*/
define('DATE_VALIDATE_DATE_BY_DEFAULT', false);
/**
* Whether, by default, to accept times including leap seconds (i.e. '23.59.60')
* when setting the date/time, and whether to count leap seconds in the
* following functions:
*
* - {@link Date::addSeconds()}
* - {@link Date::subtractSeconds()}
* - {@link Date_Calc::addSeconds()}
* - {@link Date::round()}
* - {@link Date::roundSeconds()}
*
* This constant is set to 'false' by default for backwards-compatibility
* reasons, however, you are recommended to set it to 'true'.
*
* Note that this constant does not affect {@link Date::addSpan()} and
* {@link Date::subtractSpan()} which will not count leap seconds in any case.
*
* @since Constant available since Release 1.5.0
*/
define('DATE_COUNT_LEAP_SECONDS', false);
/**
* Method to call when user invokes {@link Date::format()}
*
* @since Constant available since Release 1.5.1
*/
define('DATE_FORMAT_METHOD', 'formatLikeStrftime');
// }}}
// {{{ Output format constants (used in {@link Date::getDate()})
/**
* "YYYY-MM-DD HH:MM:SS"
*/
define('DATE_FORMAT_ISO', 1);
/**
* "YYYYMMDDTHHMMSS(Z|(+/-)HHMM)?"
*/
define('DATE_FORMAT_ISO_BASIC', 2);
/**
* "YYYY-MM-DDTHH:MM:SS(Z|(+/-)HH:MM)?"
*/
define('DATE_FORMAT_ISO_EXTENDED', 3);
/**
* "YYYY-MM-DDTHH:MM:SS(.S*)?(Z|(+/-)HH:MM)?"
*/
define('DATE_FORMAT_ISO_EXTENDED_MICROTIME', 6);
/**
* "YYYYMMDDHHMMSS"
*/
define('DATE_FORMAT_TIMESTAMP', 4);
/**
* long int, seconds since the unix epoch
*/
define('DATE_FORMAT_UNIXTIME', 5);
// }}}
// {{{ Class: Date
/**
* Generic date handling class for PEAR
*
* Supports time zones with the Date_TimeZone class. Supports several
* operations from Date_Calc on Date objects.
*
* Note to developers: the class stores the local time and date in the
* local standard time. That is, it does not store the time as the
* local Summer time when and if the time zone is in Summer time. It
* is much easier to store local standard time and remember to offset
* it when the user requests it.
*
* @category Date and Time
* @package Date
* @author Baba Buehler <baba@babaz.com>
* @author Pierre-Alain Joye <pajoye@php.net>
* @author Firman Wandayandi <firman@php.net>
* @author C.A. Woodcock <c01234@netcomuk.co.uk>
* @copyright 1997-2007 Baba Buehler, Pierre-Alain Joye, Firman Wandayandi, C.A. Woodcock
* @license http://www.opensource.org/licenses/bsd-license.php
* BSD License
* @version Release: 1.5.0a1
* @link http://pear.php.net/package/Date
*/
class Date
{
// {{{ Properties
/**
* The year
*
* @var int
* @access private
* @since Property available since Release 1.0
*/
var $year;
/**
* The month
*
* @var int
* @access private
* @since Property available since Release 1.0
*/
var $month;
/**
* The day
*
* @var int
* @access private
* @since Property available since Release 1.0
*/
var $day;
/**
* The hour
*
* @var int
* @access private
* @since Property available since Release 1.0
*/
var $hour;
/**
* The minute
*
* @var int
* @access private
* @since Property available since Release 1.0
*/
var $minute;
/**
* The second
*
* @var int
* @access private
* @since Property available since Release 1.0
*/
var $second;
/**
* The parts of a second
*
* @var float
* @access private
* @since Property available since Release 1.4.3
*/
var $partsecond;
/**
* The year in local standard time
*
* @var int
* @access private
* @since Property available since Release 1.5.0
*/
var $on_standardyear;
/**
* The month in local standard time
*
* @var int
* @access private
* @since Property available since Release 1.5.0
*/
var $on_standardmonth;
/**
* The day in local standard time
*
* @var int
* @access private
* @since Property available since Release 1.5.0
*/
var $on_standardday;
/**
* The hour in local standard time
*
* @var int
* @access private
* @since Property available since Release 1.5.0
*/
var $on_standardhour;
/**
* The minute in local standard time
*
* @var int
* @access private
* @since Property available since Release 1.5.0
*/
var $on_standardminute;
/**
* The second in local standard time
*
* @var int
* @access private
* @since Property available since Release 1.5.0
*/
var $on_standardsecond;
/**
* The part-second in local standard time
*
* @var float
* @access private
* @since Property available since Release 1.5.0
*/
var $on_standardpartsecond;
/**
* Whether the object should accept and count leap seconds
*
* @var bool
* @access private
* @since Property available since Release 1.5.0
*/
var $ob_countleapseconds;
/**
* Whether the time is valid as a local time (an invalid time
* is one that lies in the 'skipped hour' at the point that
* the clocks go forward)
*
* @var bool
* @access private
* @see Date::isValidTime()
* @since Property available since Release 1.5.0
*/
var $ob_invalidtime = null;
/**
* Date_TimeZone object for this date
*
* @var object Date_TimeZone object
* @access private
* @since Property available since Release 1.0
*/
var $tz;
/**
* Defines the default weekday abbreviation length
*
* Formerly used by {@link Date::formatLikeStrftime()}, but now
* redundant - the abbreviation for the current locale of the machine
* is used.
*
* @var int
* @access private
* @since Property available since Release 1.4.4
*/
var $getWeekdayAbbrnameLength = 3;
// }}}
// {{{ Constructor
/**
* Constructor
*
* Creates a new Date Object initialized to the current date/time in the
* system-default timezone by default. A date optionally
* passed in may be in the ISO 8601, TIMESTAMP or UNIXTIME format,
* or another Date object. If no date is passed, the current date/time
* is used.
*
* If a date is passed and an exception is returned by {@link Date::setDate()}
* there is nothing that this function can do, so for this reason, it
* is advisable to pass no parameter and to make a separate call to
* Date::setDate(). A date/time should only be passed if known to be a
* valid ISO 8601 string or a valid Unix timestamp.
*
* @param mixed $date optional ISO 8601 date/time to initialize;
* or, a Unix time stamp
* @param bool $pb_countleapseconds whether to count leap seconds
* (defaults to
* {@link DATE_COUNT_LEAP_SECONDS})
*
* @return void
* @access public
* @see Date::setDate()
*/
function Date($date = null,
$pb_countleapseconds = DATE_COUNT_LEAP_SECONDS)
{
$this->ob_countleapseconds = $pb_countleapseconds;
if (is_a($date, 'Date')) {
$this->copy($date);
} else {
if (!is_null($date)) {
// 'setDate()' expects a time zone to be already set:
//
$this->_setTZToDefault();
$this->setDate($date);
} else {
$this->setNow();
}
}
}
// }}}
// {{{ copy()
/**
* Copy values from another Date object
*
* Makes this Date a copy of another Date object. This is a
* PHP4-compatible implementation of {@link Date::__clone()} in PHP5.
*
* @param object $date Date object to copy
*
* @return void
* @access public
*/
function copy($date)
{
$this->year = $date->year;
$this->month = $date->month;
$this->day = $date->day;
$this->hour = $date->hour;
$this->minute = $date->minute;
$this->second = $date->second;
$this->partsecond = $date->partsecond;
$this->on_standardyear = $date->on_standardyear;
$this->on_standardmonth = $date->on_standardmonth;
$this->on_standardday = $date->on_standardday;
$this->on_standardhour = $date->on_standardhour;
$this->on_standardminute = $date->on_standardminute;
$this->on_standardsecond = $date->on_standardsecond;
$this->on_standardpartsecond = $date->on_standardpartsecond;
$this->ob_countleapseconds = $date->ob_countleapseconds;
$this->ob_invalidtime = $date->ob_invalidtime;
$this->tz = new Date_TimeZone($date->getTZID());
$this->getWeekdayAbbrnameLength = $date->getWeekdayAbbrnameLength;
}
// }}}
// {{{ __clone()
/**
* Copy values from another Date object
*
* Makes this Date a copy of another Date object. For PHP5
* only.
*
* @return void
* @access public
* @see Date::copy()
*/
function __clone()
{
// This line of code would be preferable, but will only
// compile in PHP5:
//
// $this->tz = clone $this->tz;
$this->tz = new Date_TimeZone($this->getTZID());
}
// }}}
// {{{ isNull()
/**
* Returns whether the object is null (i.e. no date has been set)
*
* If the object is set to an invalid date, then this function will
* still return 'false'. To check whether the date is valid use
* either {@link Date::isValidDate()} (to check the day/month/year
* part of the object only) or {@link Date::isValidTime()} (to check
* the time, in addition to the day/month/year part).
*
* @return bool
* @access public
* @see Date::setDate(), Date::isValidDate(), Date::isValidTime()
* @since Method available since Release 1.5.0
*/
function isNull()
{
return is_null($this->year);
}
// }}}
// {{{ isValidDate()
/**
* Returns whether the date (i.e. day/month/year) is valid
*
* It is not possible to set the object to an invalid date using
* {@link Date::setDate()}, but it is possible to do so using the
* following functions:
*
* - {@link Date::setYear()}
* - {@link Date::setMonth()}
* - {@link Date::setDay()}
*
* However you can prevent this possibility (by default) by setting
* {@link DATE_VALIDATE_DATE_BY_DEFAULT} to 'true', in which case
* these three functions will return an error if they specify an
* invalid date, and the object will be unmodified.
*
* Note that this function only checks the day/month/year part of
* the object. Even if this is valid, it is still possible for the
* time to be invalid (see {@link DATE_CORRECTINVALIDTIME_DEFAULT}).
* To check the time as well, use {@link Date::isValidTime()}.
*
* @return bool
* @access public
* @see Date::setDate(), Date::isNull(), Date::isValidTime()
* @since Method available since Release 1.5.0
*/
function isValidDate()
{
return
!Date::isNull() &&
Date_Calc::isValidDate($this->year, $this->month, $this->day);
}
// }}}
// {{{ setDate()
/**
* Sets the date/time of the object based on the input date and format
*
* Accepts a string in three possible formats, and in this order of
* precedence:
*
* - ISO 8601 date (see {@link http://en.wikipedia.org/wiki/ISO_8601})
* - Time-Stamp (i.e. 'YYYYMMDDHHMMSS')
* - Unix time-stamp (see {@link http://en.wikipedia.org/wiki/Unix_time})
*
* Note that if you want to pass a Unix time-stamp then you need to set
* the $format parameter to {@link DATE_FORMAT_UNIXTIME}, or else use the
* method {@link Date::setFromTime()}.
*
* The input string should be a date/time representation in one of the
* following general formats:
*
* - <b><date>T<time><time-zone></b>
* - <b><date> <time><time-zone></b> (non-ISO-standard)
* - <b><date><time><time-zone></b> (non-ISO-standard)
* - <b><date>T<time></b> i.e. without optional <time-zone> representation
* - <b><date> <time></b>
* - <b><date><time></b>
* - <b><date></b> i.e. without optional <time> representation
*
* that is, the representation must be comprised of a <b><date></b> part,
* with an optional <b><time></b> part, which itself may include an optional
* <time-zone> part, each of which may consist of any one of the permitted
* formats detailed below. The <b><date></b> and <b><time</b> representations
* should be divided with the time designator <b>T</b> according to the ISO 8601
* standard, although this method also permits representations divided by a
* space, or by no delimiter at all.
*
* The <b><date></b> representation should be in one of the following formats:
*
* - <b>Calendar date</b>: <b>YYYY-MM-DD</b> (extended format) or
* <b>YYYYMMDD</b> (basic format), where [YYYY]
* indicates the four-digit year (0000-9999), [MM]
* indicates the month (01-12) and [DD] indicates the
* day of the month [01-31]
* - <b>ISO week date</b>: <b>YYYY-Www-D</b> (extended format) or
* <b>YYYYWwwD</b> (basic format), where [YYYY]
* indicates the ISO year (slightly different from the
* calendar year (see below)), [Www] indicates the ISO
* week no prefixed by the letter 'W' (W01-W53) and
* [D] indicates the ISO week-day (1-7), beginning on
* Monday and ending on Sunday. (Also see
* {@link http://en.wikipedia.org/wiki/ISO_week_date}.)
* - <b>Ordinal date</b>: <b>YYYY-DDD</b> (extended format) or
* <b>YYYYDDD</b> (basic format), where [YYYY]
* indicates the four-digit year (0000-9999) and [DDD]
* indicates the day of the year (001-366)
*
* The <b><time></b> representation should be in one of the following formats:
*
* - <b>hh:mm:ss</b> (extended format) or <b>hhmmss</b> (basic format)
* - <b>hh:mm</b> (extended format) or <b>hhmm</b> (basic format)
* - <b>hh</b> (extended format) or <b>hh</b> (basic format)
*
* where [hh] represents the hour (00-24), [mm] represents the minute (00-59)
* and [ss] represents the second (00-60)
*
* Format parameter should be one of the specified DATE_FORMAT_* constants:
*
* - <b>{@link DATE_FORMAT_ISO}</b> - 'YYYY-MM-DD HH:MI:SS'
* - <b>{@link DATE_FORMAT_ISO_BASIC}</b> - 'YYYYMMDDTHHMMSS(Z|(+/-)HHMM)?'
* - <b>{@link DATE_FORMAT_ISO_EXTENDED}</b> - 'YYYY-MM-DDTHH:MM:SS(Z|(+/-)HH:MM)?'
* - <b>{@link DATE_FORMAT_ISO_EXTENDED_MICROTIME}</b> - 'YYYY-MM-DDTHH:MM:SS(.S*)?(Z|(+/-)HH:MM)?'
* - <b>{@link DATE_FORMAT_TIMESTAMP}</b> - 'YYYYMMDDHHMMSS'
* - <b>{@link DATE_FORMAT_UNIXTIME}</b> - long integer of the no of seconds since
* the Unix Epoch
* (1st January 1970 00.00.00 GMT)
*
* @param string $date input date
* @param int $format optional format constant
* (DATE_FORMAT_*) of the input date.
* This parameter is not needed,
* except to force the setting of the
* date from a Unix time-stamp (for
* which use
* {@link DATE_FORMAT_UNIXTIME}).
* (Defaults to
* {@link DATE_FORMAT_ISO}.)
* @param bool $pb_repeatedhourdefault value to return if repeated
* hour is specified (defaults
* to false)
*
* @return void
* @access public
* @see Date::isNull(), Date::isValidDate(), Date::isValidTime(),
* Date::setFromTime()
*/
function setDate($date,
$format = DATE_FORMAT_ISO,
$pb_repeatedhourdefault = false)
{
if ($format == DATE_FORMAT_UNIXTIME) {
if (is_numeric($date)) {
// Assume Unix time-stamp:
//
$this->setFromTime((int) $date);
} else {
return PEAR::raiseError("'$date' not valid Unix time-stamp");
}
} else if (preg_match('/^([0-9]{4,4})-?(' .
'(0[1-9]|1[0-2])-?(0[1-9]|[12][0-9]|3[01])|' . // [mm]-[dd]
'W(0[1-9]|[1-4][0-9]|5[0-3])-?([1-7])|' . // ISO week date
'(0(0[1-9]|[1-9][0-9])|[12][0-9]{2,2}|3([0-5][0-9]|6[1-6]))' . // [ddd]
')([T\s]?' .
'([01][0-9]|2[0-3])(:?' . // [hh]
'([0-5][0-9])(:?' . // [mm]
'([0-5][0-9]|60)([,.][0-9]+)?)?)?' . // [ss]
'(Z|[+\-][0-9]{2,2}(:?[0-5][0-9])?)?)?$/i', // offset
$date, $regs)
) {
if (substr($regs[2], 0, 1) == "W") {
// ISO week date (YYYY-Www-D)
//
$hs_date = Date_Calc::isoWeekToDate($regs[6],
$regs[5],
$regs[1],
"%Y %m %d");
if (PEAR::isError($hs_date)) {
return $hs_date;
}
list($hs_year, $hs_month, $hs_day) = explode(" ", $hs_date);
} else if (strlen($regs[2]) == 3) {
// ISO ordinal date (YYYY-DDD)
//
$hn_jd = Date_Calc::firstDayOfYear($regs[1]) + $regs[2] - 1;
list($hs_year, $hs_month, $hs_day) =
explode(" ", Date_Calc::daysToDate($hn_jd, "%Y %m %d"));
} else {
// ISO calendar date (YYYY-MM-DD)
//
// DATE_FORMAT_ISO, ISO_BASIC, ISO_EXTENDED, and TIMESTAMP
// These formats are extremely close to each other. This regex
// is very loose and accepts almost any butchered format you could
// throw at it. e.g. 2003-10-07 19:45:15 and 2003-10071945:15
// are the same thing in the eyes of this regex, even though the
// latter is not a valid ISO 8601 date.
//
$hs_year = $regs[1];
$hs_month = $regs[3];
$hs_day = $regs[4];
if (!Date_Calc::isValidDate($hs_day, $hs_month, $hs_year)) {
return PEAR::raiseError("'" .
Date_Calc::dateFormat($hs_year,
$hs_month,
$hs_day,
"%Y-%m-%d") .
"' is invalid calendar date",
DATE_ERROR_INVALIDDATE);
}
}
if (isset($regs[17])) {
if ($regs[17] == "Z") {
$this->tz = new Date_TimeZone("UTC");
} else {
$this->tz = new Date_TimeZone("UTC" . $regs[17]);
}
}
$this->setLocalTime($hs_day,
$hs_month,
$hs_year,
isset($regs[11]) && $regs[11] != "" ?
$regs[11] : 0,
isset($regs[13]) && $regs[13] != "" ?
$regs[13] : 0,
isset($regs[15]) && $regs[15] != "" ?
$regs[15] : 0,
isset($regs[16]) && $regs[16] != "" ?
$regs[16] : 0.0,
$pb_repeatedhourdefault);
} else {
return PEAR::raiseError("Date '$date' not in ISO 8601 format",
DATE_ERROR_INVALIDDATEFORMAT);
}
}
// }}}
// {{{ setNow()
/**
* Sets to local current time and time zone
*
* @param bool $pb_setmicrotime whether to set micro-time (defaults to the
* value of the constant
* {@link DATE_CAPTURE_MICROTIME_BY_DEFAULT})
*
* @return void
* @access public
* @since Method available since Release 1.5.0
*/
function setNow($pb_setmicrotime = DATE_CAPTURE_MICROTIME_BY_DEFAULT)
{
$this->_setTZToDefault();
if ($pb_setmicrotime) {
$ha_unixtime = gettimeofday();
} else {
$ha_unixtime = array("sec" => time());
}
$this->setDate(date("Y-m-d H:i:s", $ha_unixtime["sec"]) .
(isset($ha_unixtime["usec"]) ?
"." . sprintf("%06d", $ha_unixtime["usec"]) :
""));
}
// }}}
// {{{ round()
/**
* Rounds the date according to the specified precision (defaults
* to nearest day)
*
* The precision parameter must be one of the following constants:
*
* - <b>{@link DATE_PRECISION_YEAR}</b>
* - <b>{@link DATE_PRECISION_MONTH}</b>
* - <b>{@link DATE_PRECISION_DAY}</b> (default)
* - <b>{@link DATE_PRECISION_HOUR}</b>
* - <b>{@link DATE_PRECISION_10MINUTES}</b>
* - <b>{@link DATE_PRECISION_MINUTE}</b>
* - <b>{@link DATE_PRECISION_10SECONDS}</b>
* - <b>{@link DATE_PRECISION_SECOND}</b>
*
* The precision can also be specified as an integral offset from
* one of these constants, where the offset reflects a precision
* of 10 to the power of the offset greater than the constant.
* For example:
*
* - <b>(DATE_PRECISION_YEAR - 1)</b> - rounds the date to the nearest 10 years
* - <b>(DATE_PRECISION_YEAR - 3)</b> - rounds the date to the nearest 1000
* years
* - <b>(DATE_PRECISION_SECOND + 1)</b> - rounds the date to 1 decimal
* point of a second
* - <b>(DATE_PRECISION_SECOND + 3)</b> - rounds the date to 3 decimal
* points of a second
* - <b>(DATE_PRECISION_SECOND - 1)</b> - rounds the date to the nearest 10
* seconds (thus it is equivalent to
* <b>DATE_PRECISION_10SECONDS</b>)
*
* @param int $pn_precision a 'DATE_PRECISION_*' constant (defaults to
* {@link DATE_PRECISION_DAY})
* @param bool $pb_correctinvalidtime whether to correct, by adding the
* local Summer time offset, the rounded
* time if it falls in the skipped hour
* (defaults to
* {@link DATE_CORRECTINVALIDTIME_DEFAULT})
*
* @return void
* @access public
* @since Method available since Release 1.5.0
*/
function round($pn_precision = DATE_PRECISION_DAY,
$pb_correctinvalidtime = DATE_CORRECTINVALIDTIME_DEFAULT)
{
if ($pn_precision <= DATE_PRECISION_DAY) {
list($hn_year,
$hn_month,
$hn_day,
$hn_hour,
$hn_minute,
$hn_secondraw) =
Date_Calc::round($pn_precision,
$this->day,
$this->month,
$this->year,
$this->hour,
$this->minute,
$this->partsecond == 0.0 ?
$this->second :
$this->second + $this->partsecond,
$this->ob_countleapseconds);
if (is_float($hn_secondraw)) {
$hn_second = intval($hn_secondraw);
$hn_partsecond = $hn_secondraw - $hn_second;
} else {
$hn_second = $hn_secondraw;
$hn_partsecond = 0.0;
}
$this->setLocalTime($hn_day,
$hn_month,
$hn_year,
$hn_hour,
$hn_minute,
$hn_second,
$hn_partsecond,
true, // This is unlikely anyway, but the
// day starts with the repeated hour
// the first time around
$pb_correctinvalidtime);
return;
}
// ($pn_precision >= DATE_PRECISION_HOUR)
//
if ($this->tz->getDSTSavings() % 3600000 == 0 ||
($this->tz->getDSTSavings() % 60000 == 0 &&
$pn_precision >= DATE_PRECISION_MINUTE)
) {
list($hn_year,
$hn_month,
$hn_day,
$hn_hour,
$hn_minute,
$hn_secondraw) =
Date_Calc::round($pn_precision,
$this->on_standardday,
$this->on_standardmonth,
$this->on_standardyear,
$this->on_standardhour,
$this->on_standardminute,
$this->on_standardpartsecond == 0.0 ?
$this->on_standardsecond :
$this->on_standardsecond +
$this->on_standardpartsecond,
$this->ob_countleapseconds);
if (is_float($hn_secondraw)) {
$hn_second = intval($hn_secondraw);
$hn_partsecond = $hn_secondraw - $hn_second;
} else {
$hn_second = $hn_secondraw;
$hn_partsecond = 0.0;
}
$this->setStandardTime($hn_day,
$hn_month,
$hn_year,
$hn_hour,
$hn_minute,
$hn_second,
$hn_partsecond);
return;
}
// Very unlikely anyway (as I write, the only time zone like this
// is Lord Howe Island in Australia (offset of half an hour)):
//
// (This algorithm could be better)
//
list($hn_year,
$hn_month,
$hn_day,
$hn_hour,
$hn_minute,
$hn_secondraw) =
Date_Calc::round($pn_precision,
$this->day,
$this->month,
$this->year,
$this->hour,
$this->minute,
$this->partsecond == 0.0 ?
$this->second :
$this->second + $this->partsecond,
$this->ob_countleapseconds);
if (is_float($hn_secondraw)) {
$hn_second = intval($hn_secondraw);
$hn_partsecond = $hn_secondraw - $hn_second;
} else {