-
Notifications
You must be signed in to change notification settings - Fork 10
/
TimeZone.php
7358 lines (7222 loc) · 263 KB
/
TimeZone.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
/**
* TimeZone representation class, along with time zone information data
*
* PHP versions 4 and 5
*
* LICENSE:
*
* Copyright (c) 1997-2007 Baba Buehler, Pierre-Alain Joye, 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 C.A. Woodcock <c01234@netcomuk.co.uk>
* @copyright 1997-2007 Baba Buehler, Pierre-Alain Joye, C.A. Woodcock
* @license http://www.opensource.org/licenses/bsd-license.php
* BSD License
* @version CVS: $Id$
* @link http://pear.php.net/package/Date
*/
// }}}
// {{{ Class Date_TimeZone
/**
* TimeZone representation class, along with time zone information data
*
* The default timezone is set from the first valid timezone id found
* in one of the following places, in this order:
* + global $_DATE_TIMEZONE_DEFAULT
* + system environment variable PHP_TZ
* + system environment variable TZ
* + the result of date('T')
*
* If no valid timezone id is found, the default timezone is set to 'UTC'.
* You may also manually set the default timezone by passing a valid id to
* {@link Date_TimeZone::setDefault()}.
*
* This class includes time zone data (from zoneinfo) in the form of a
* global array, $_DATE_TIMEZONE_DATA.
*
* @category Date and Time
* @package Date
* @author Baba Buehler <baba@babaz.com>
* @author C.A. Woodcock <c01234@netcomuk.co.uk>
* @copyright 1997-2007 Baba Buehler, Pierre-Alain Joye, 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_TimeZone
{
// {{{ Properties
/**
* Unique time Zone ID of this time zone
*
* @var string
* @access private
* @since Property available since Release 1.0
*/
var $id;
/**
* Offset, in milliseconds, of this timezone
*
* @var int
* @access private
* @since Property available since Release 1.0
*/
var $offset;
/**
* Short name of this time zone (e.g. "CST")
*
* @var string
* @access private
* @since Property available since Release 1.0
*/
var $shortname;
/**
* DST short name of this timezone (e.g. 'BST')
*
* @var string
* @access private
* @since Property available since Release 1.0
*/
var $dstshortname;
/**
* Long name of this time zone (e.g. "Central Standard Time")
*
* N.B. this is not necessarily unique
*
* @since 1.0
* @access private
* @since Property available since Release 1.0
*/
var $longname;
/**
* DST long name of this time zone (e.g. 'British Summer Time')
*
* @var string
* @access private
* @since Property available since Release 1.0
*/
var $dstlongname;
/**
* Whether this time zone observes daylight savings time
*
* @var bool
* @access private
* @since Property available since Release 1.0
*/
var $hasdst;
/**
* Additional offset of Summer time from the standard time of the
* time zone in milli-seconds
*
* The value is usually 3600000, i.e. one hour, and always positive
*
* @var int
* @access private
* @since Property available since Release 1.5.0
*/
var $on_summertimeoffset;
/**
* Month no (1-12) in which Summer time starts (the clocks go forward)
*
* @var int
* @access private
* @since Property available since Release 1.5.0
*/
var $on_summertimestartmonth;
/**
* Definition of when Summer time starts in the specified month
*
* Can take one of the following forms:
*
* 5 the fifth of the month
* lastSun the last Sunday in the month
* lastMon the last Monday in the month
* Sun>=8 first Sunday on or after the 8th
* Sun<=25 last Sunday on or before the 25th
*
* @var string
* @access private
* @since Property available since Release 1.5.0
*/
var $os_summertimestartday;
/**
* Time in milli-seconds relative to midnight UTC when
* Summer time starts (the clocks go forward)
*
* @var int
* @access private
* @since Property available since Release 1.5.0
*/
var $on_summertimestarttime;
/**
* Month no (1-12) in which Summer time ends (the clocks go back)
*
* @var int
* @access private
* @since Property available since Release 1.5.0
*/
var $on_summertimeendmonth;
/**
* Definition of when Summer time ends in the specified month
*
* @var string
* @access private
* @see Date_TimeZone::$os_summertimestartday
* @since Property available since Release 1.5.0
*/
var $os_summertimeendday;
/**
* Time in milli-seconds relative to midnight UTC when
* Summer time ends (the clocks go back)
*
* @var int
* @access private
* @since Property available since Release 1.5.0
*/
var $on_summertimeendtime;
// }}}
// {{{ Constructor
/**
* Constructor
*
* If the supplied ID is invalid, the created time zone is "UTC".
*
* A note about time zones of the form 'Etc/*' (quoted from the public
* domain 'tz' data-base (see {@link ftp://elsie.nci.nih.gov/pub/}
* [file 'etcetera']):
*
* These entries are mostly present for historical reasons, so that
* people in areas not otherwise covered by the tz files could use
* a time zone that was right for their area. These days, the
* tz files cover almost all the inhabited world, and the only practical
* need now for the entries that are not on UTC are for ships at sea
* that cannot use POSIX TZ settings.
*
* - <b>Etc/GMT</b> (GMT)
* - <b>Etc/UTC</b> (UTC)
* - <b>Etc/UCT</b> (UCT)
*
* The following link uses older naming conventions, but it belongs here.
* We want this to work even on installations that omit the other older
* names.
*
* - <b>Etc/GMT</b> (equivalent to GMT)
*
* - <b>Etc/UTC</b> (equivalent to Etc/Universal)
* - <b>Etc/UTC</b> (equivalent to Etc/Zulu)
*
* - <b>Etc/GMT</b> (equivalent to Etc/Greenwich)
* - <b>Etc/GMT</b> (equivalent to Etc/GMT-0)
* - <b>Etc/GMT</b> (equivalent to Etc/GMT+0)
* - <b>Etc/GMT</b> (equivalent to Etc/GMT0)
*
* We use POSIX-style signs in the Zone names and the output abbreviations,
* even though this is the opposite of what many people expect.
* POSIX has positive signs west of Greenwich, but many people expect
* positive signs east of Greenwich. For example, TZ='Etc/GMT+4' uses
* the abbreviation "GMT+4" and corresponds to 4 hours behind UTC
* (i.e. west of Greenwich) even though many people would expect it to
* mean 4 hours ahead of UTC (i.e. east of Greenwich).
*
* In the draft 5 of POSIX 1003.1-200x, the angle bracket notation
* (which is not yet supported by the tz code) allows for
* TZ='<GMT-4>+4'; if you want time zone abbreviations conforming to
* ISO 8601 you can use TZ='<-0400>+4'. Thus the commonly-expected
* offset is kept within the angle bracket (and is used for display)
* while the POSIX sign is kept outside the angle bracket (and is used
* for calculation).
*
* Do not use a TZ setting like TZ='GMT+4', which is four hours behind
* GMT but uses the completely misleading abbreviation "GMT".
*
* Earlier incarnations of this package were not POSIX-compliant, and
* we did not want things to change quietly if someone accustomed to the
* old way uses the codes from previous versions so we moved the names
* into the Etc subdirectory.
*
* - <b>Etc/GMT-14</b> (14 hours ahead of Greenwich)
* - <b>Etc/GMT-13</b> (13)
* - <b>Etc/GMT-12</b> (12)
* - <b>Etc/GMT-11</b> (11)
* - <b>Etc/GMT-10</b> (10)
* - <b>Etc/GMT-9</b> (9)
* - <b>Etc/GMT-8</b> (8)
* - <b>Etc/GMT-7</b> (7)
* - <b>Etc/GMT-6</b> (6)
* - <b>Etc/GMT-5</b> (5)
* - <b>Etc/GMT-4</b> (4)
* - <b>Etc/GMT-3</b> (3)
* - <b>Etc/GMT-2</b> (2)
* - <b>Etc/GMT-1</b> (1)
* - <b>Etc/GMT+1</b> (1 hour behind Greenwich)
* - <b>Etc/GMT+2</b> (2)
* - <b>Etc/GMT+3</b> (3)
* - <b>Etc/GMT+4</b> (4)
* - <b>Etc/GMT+5</b> (5)
* - <b>Etc/GMT+6</b> (6)
* - <b>Etc/GMT+7</b> (7)
* - <b>Etc/GMT+8</b> (8)
* - <b>Etc/GMT+9</b> (9)
* - <b>Etc/GMT+10</b> (10)
* - <b>Etc/GMT+11</b> (11)
* - <b>Etc/GMT+12</b> (12)
*
* @param string $ps_id the time zone ID
*
* @return void
* @access public
* @see Date::setTZ(), Date::setTZByID(), Date_TimeZone::isValidID()
*/
function Date_TimeZone($ps_id)
{
$_DATE_TIMEZONE_DATA =& $GLOBALS['_DATE_TIMEZONE_DATA'];
if (isset($GLOBALS['_DATE_TIMEZONE_DATA'][$ps_id])) {
$this->id = $ps_id;
$this->shortname = $_DATE_TIMEZONE_DATA[$ps_id]['shortname'];
$this->longname = $_DATE_TIMEZONE_DATA[$ps_id]['longname'];
$this->offset = $_DATE_TIMEZONE_DATA[$ps_id]['offset'];
$this->dstshortname =
array_key_exists("dstshortname",
$_DATE_TIMEZONE_DATA[$ps_id]) ?
$_DATE_TIMEZONE_DATA[$ps_id]['dstshortname'] :
null;
if ($this->hasdst = !is_null($this->dstshortname)) {
$this->dstlongname =
array_key_exists("dstlongname",
$_DATE_TIMEZONE_DATA[$ps_id]) ?
$_DATE_TIMEZONE_DATA[$ps_id]['dstlongname'] :
null;
if (isset($_DATE_TIMEZONE_DATA[$ps_id]["summertimeoffset"])) {
$this->on_summertimeoffset = $_DATE_TIMEZONE_DATA[$ps_id]["summertimeoffset"];
$this->on_summertimestartmonth = $_DATE_TIMEZONE_DATA[$ps_id]["summertimestartmonth"];
$this->os_summertimestartday = $_DATE_TIMEZONE_DATA[$ps_id]["summertimestartday"];
$this->on_summertimestarttime = $_DATE_TIMEZONE_DATA[$ps_id]["summertimestarttime"];
$this->on_summertimeendmonth = $_DATE_TIMEZONE_DATA[$ps_id]["summertimeendmonth"];
$this->os_summertimeendday = $_DATE_TIMEZONE_DATA[$ps_id]["summertimeendday"];
$this->on_summertimeendtime = $_DATE_TIMEZONE_DATA[$ps_id]["summertimeendtime"];
} else {
$this->on_summertimeoffset = null;
}
}
} else {
$this->hasdst = false;
if (preg_match('/^UTC([+\-])([0-9]{2,2}):?([0-5][0-9])$/',
$ps_id,
$ha_matches)) {
$this->id = $ps_id;
$this->offset = ($ha_matches[1] .
($ha_matches[2] * 3600 +
$ha_matches[3] * 60)) * 1000;
if (!($hb_isutc = $this->offset == 0)) {
$this->id = $ps_id;
$this->shortname = "UTC" .
$ha_matches[1] .
($ha_matches[3] == "00" ?
ltrim($ha_matches[2], "0") :
$ha_matches[2] . $ha_matches[3]);
$this->longname = "UTC" .
$ha_matches[1] .
$ha_matches[2] .
":" .
$ha_matches[3];
}
} else if (preg_match('/^UTC([+\-])([0-9]{1,2})$/',
$ps_id,
$ha_matches)) {
$this->id = $ps_id;
$this->offset = ($ha_matches[1] .
($ha_matches[2] * 3600)) * 1000;
if (!($hb_isutc = $this->offset == 0)) {
$this->shortname = "UTC" .
$ha_matches[1] .
ltrim($ha_matches[2], "0");
$this->longname = "UTC" .
$ha_matches[1] .
sprintf("%02d", $ha_matches[2]) .
":00";
}
} else {
$this->id = "UTC";
$hb_isutc = true;
}
if ($hb_isutc) {
$this->shortname = $_DATE_TIMEZONE_DATA["UTC"]['shortname'];
$this->longname = $_DATE_TIMEZONE_DATA["UTC"]['longname'];
$this->offset = $_DATE_TIMEZONE_DATA["UTC"]['offset'];
}
}
}
// }}}
// {{{ getDefault()
/**
* Returns a TimeZone object representing the system default time zone
*
* The system default time zone is initialized during the loading of
* this file.
*
* @return object Date_TimeZone object of the default time zone
* @access public
*/
function getDefault()
{
return new Date_TimeZone($GLOBALS['_DATE_TIMEZONE_DEFAULT']);
}
// }}}
// {{{ setDefault()
/**
* Sets the system default time zone to the time zone in $id
*
* @param string $id the time zone id to use
*
* @return void
* @access public
*/
function setDefault($id)
{
if (Date_TimeZone::isValidID($id)) {
$GLOBALS['_DATE_TIMEZONE_DEFAULT'] = $id;
} else {
return PEAR::raiseError("Invalid time zone ID '$id'");
}
}
// }}}
// {{{ isValidID()
/**
* Tests if given time zone ID (e.g. 'London/Europe') is valid and unique
*
* Checks if given ID is either represented in the $_DATE_TIMEZONE_DATA
* time zone data, or is a UTC offset in one of the following forms,
* i.e. an offset with no geographical or political base:
*
* - <b>UTC[+/-][hh]:[mm]</b> - e.g. UTC+03:00
* - <b>UTC[+/-][hh][mm]</b> - e.g. UTC-0530
* - <b>UTC[+/-][hh]</b> - e.g. UTC+03
* - <b>UTC[+/-][h]</b> - e.g. UTC-1
*
* (the last is not an ISO 8601 standard but is the preferred form)
*
* N.B. these are not sanctioned by any ISO standard, but the form of
* the offset itself, i.e. the part after the characters 'UTC', is the
* ISO 8601 standard form for representing this part.
*
* The form '<b>[+/-][h]</b>' is not ISO conformant, but ISO 8601 only
* defines the form of the time zone offset of a particular time, that
* is, it actually defines the form '<b><time>UTC[+/-][hh]</b>', and
* its purview does not apparently cover the name of the time zone
* itself. For this there is no official international standard (or
* even a non- international standard). The closest thing to a
* sanctioning body is the 'tz' database
* ({@link http://www.twinsun.com/tz/tz-link.htm})
* which is run by volunteers but which is heavily relied upon by
* various programming languages and the internet community. However
* they mainly define geographical/political time zone names of the
* form 'London/Europe' because their main aim is to collate the time
* zone definitions which are set by individual countries/states, not
* to prescribe any standard.
*
* However it seems that the de facto standard to describe time zones
* as non-geographically/politically-based areas where the local time
* on all clocks reads the same seems to be the form '<b>UTC[+/-][h]</b>'
* for integral numbers of hours, and '<b>UTC[+/-][hh]:[mm]</b>'
* otherwise.
* (See {@link http://en.wikipedia.org/wiki/List_of_time_zones})
*
* N.B. 'GMT' is also commonly used instead of 'UTC', but 'UTC' seems
* to be technically preferred. GMT-based IDs still exist in the 'tz
* data-base', but beware of POSIX-style offsets which are the opposite
* way round to what people normally expect.
*
* @param string $ps_id the time zone ID to test
*
* @return bool true if the supplied ID is valid
* @access public
* @see Date::setTZByID(), Date_TimeZone::Date_TimeZone()
*/
function isValidID($ps_id)
{
if (isset($GLOBALS['_DATE_TIMEZONE_DATA'][$ps_id])) {
return true;
} else if (preg_match('/^UTC[+\-]([0-9]{2,2}:?[0-5][0-9]|[0-9]{1,2})$/',
$ps_id)) {
return true;
} else {
return false;
}
}
// }}}
// {{{ isEqual()
/**
* Is this time zone equal to another
*
* Tests to see if this time zone is equal (ids match)
* to a given Date_TimeZone object.
*
* @param object $tz the Date_TimeZone object to test
*
* @return bool true if this time zone is equal to the supplied
* time zone
* @access public
* @see Date_TimeZone::isEquivalent()
*/
function isEqual($tz)
{
if (strcasecmp($this->id, $tz->id) == 0) {
return true;
} else {
return false;
}
}
// }}}
// {{{ isEquivalent()
/**
* Is this time zone equivalent to another
*
* Tests to see if this time zone is equivalent to a given time zone object.
* Equivalence in this context consists in the two time zones having:
*
* - an equal offset from UTC in both standard and Summer time (if
* the time zones observe Summer time)
* - the same Summer time start and end rules, that is, the two time
* zones must switch from standard time to Summer time,
* and vice versa, on the same day and at the same time
*
* @param object $pm_tz the Date_TimeZone object to test, or a valid time
* zone ID
*
* @return bool true if this time zone is equivalent to the supplied
* time zone
* @access public
* @see Date_TimeZone::isEqual(), Date::inEquivalentTimeZones()
*/
function isEquivalent($pm_tz)
{
if (is_a($pm_tz, "Date_TimeZone")) {
if ($pm_tz->getID() == $this->id) {
return true;
}
} else {
if (!Date_TimeZone::isValidID($pm_tz)) {
return PEAR::raiseError("Invalid time zone ID '$pm_tz'",
DATE_ERROR_INVALIDTIMEZONE);
}
if ($pm_tz == $this->id)
return true;
$pm_tz = new Date_TimeZone($pm_tz);
}
if ($this->getRawOffset() == $pm_tz->getRawOffset() &&
$this->hasDaylightTime() == $pm_tz->hasDaylightTime() &&
$this->getDSTSavings() == $pm_tz->getDSTSavings() &&
$this->getSummerTimeStartMonth() == $pm_tz->getSummerTimeStartMonth() &&
$this->getSummerTimeStartDay() == $pm_tz->getSummerTimeStartDay() &&
$this->getSummerTimeStartTime() == $pm_tz->getSummerTimeStartTime() &&
$this->getSummerTimeEndMonth() == $pm_tz->getSummerTimeEndMonth() &&
$this->getSummerTimeEndDay() == $pm_tz->getSummerTimeEndDay() &&
$this->getSummerTimeEndTime() == $pm_tz->getSummerTimeEndTime()
) {
return true;
} else {
return false;
}
}
// }}}
// {{{ hasDaylightTime()
/**
* Returns true if this zone observes daylight savings time
*
* @return bool true if this time zone has DST
* @access public
*/
function hasDaylightTime()
{
return $this->hasdst;
}
// }}}
// {{{ getSummerTimeLimitDay()
/**
* Returns day on which Summer time starts or ends for given year
*
* The limit (start or end) code can take the following forms:
*
* - <b>5</b> - the fifth of the month
* - <b>lastSun</b> - the last Sunday in the month
* - <b>lastMon</b> - the last Monday in the month
* - <b>Sun>=8</b> - first Sunday on or after the 8th
* - <b>Sun<=25</b> - last Sunday on or before the 25th
*
* @param string $ps_summertimelimitcode code which specifies Summer time
* limit day
* @param int $pn_month start or end month
* @param int $pn_year year for which to calculate Summer
* time limit day
*
* @return int
* @access private
* @see Date_TimeZone::getSummerTimeStartDay()
* @since Method available since Release 1.5.0
*/
function getSummerTimeLimitDay($ps_summertimelimitcode, $pn_month, $pn_year)
{
if (preg_match('/^[0-9]+$/', $ps_summertimelimitcode)) {
$hn_day = $ps_summertimelimitcode;
} else {
if (!isset($ha_daysofweek))
static $ha_daysofweek = array("Sun" => 0,
"Mon" => 1,
"Tue" => 2,
"Wed" => 3,
"Thu" => 4,
"Fri" => 5,
"Sat" => 6);
if (preg_match('/^last(Sun|Mon|Tue|Wed|Thu|Fri|Sat)$/',
$ps_summertimelimitcode,
$ha_matches)) {
list($hn_nmyear, $hn_nextmonth, $hn_nmday) =
explode(" ", Date_Calc::beginOfMonthBySpan(1,
$pn_month,
$pn_year,
"%Y %m %d"));
list($hn_year, $hn_month, $hn_day) =
explode(" ",
Date_Calc::prevDayOfWeek($ha_daysofweek[$ha_matches[1]],
$hn_nmday,
$hn_nextmonth,
$hn_nmyear,
"%Y %m %d",
false)); // not including
// this day
if ($hn_month != $pn_month) {
// This code happen legitimately if the calendar jumped some days
// e.g. in a calendar switch, or the limit day is badly defined:
//
$hn_day = Date_Calc::getFirstDayOfMonth($pn_month, $pn_year);
}
} else if (preg_match('/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)([><]=)([0-9]+)$/',
$ps_summertimelimitcode,
$ha_matches)) {
if ($ha_matches[2] == "<=") {
list($hn_year, $hn_month, $hn_day) =
explode(" ",
Date_Calc::prevDayOfWeek($ha_daysofweek[$ha_matches[1]],
$ha_matches[3],
$pn_month,
$pn_year,
"%Y %m %d",
true)); // including
// this day
if ($hn_month != $pn_month) {
$hn_day = Date_Calc::getFirstDayOfMonth($pn_month, $pn_year);
}
} else {
list($hn_year, $hn_month, $hn_day) =
explode(" ",
Date_Calc::nextDayOfWeek($ha_daysofweek[$ha_matches[1]],
$ha_matches[3],
$pn_month,
$pn_year,
"%Y %m %d",
true)); // including
// this day
if ($hn_month != $pn_month) {
$hn_day = Date_Calc::daysInMonth($pn_month, $pn_year);
}
}
}
}
return $hn_day;
}
// }}}
// {{{ inDaylightTime()
/**
* Returns whether the given date/time is in DST for this time zone
*
* Works for all years, positive and negative. Possible problems
* are that when the clocks go forward, there is an invalid hour
* which is skipped. If a time in this hour is specified, this
* function returns an error. When the clocks go back, there is an
* hour which is repeated, that is, the hour is gone through twice -
* once in Summer time and once in standard time. If this time
* is specified, then this function returns '$pb_repeatedhourdefault',
* because there is no way of knowing which is correct, and
* both possibilities are equally likely.
*
* Also bear in mind that the clocks go forward at the instant of
* the hour specified in the time-zone array below, and if this
* exact hour is specified then the clocks have actually changed,
* and this function reflects this.
*
* @param object $pm_date Date object to test or array of
* day, month, year, seconds past
* midnight
* @param bool $pb_repeatedhourdefault value to return if repeated hour is
* specified (defaults to false)
*
* @return bool true if this date is in Summer time for this time
* zone
* @access public
* @see Date_TimeZone::inDaylightTimeStandard()
*/
function inDaylightTime($pm_date, $pb_repeatedhourdefault = false)
{
if (!$this->hasdst) {
return false;
}
if (is_a($pm_date, "Date")) {
$hn_day = $pm_date->getDay();
$hn_month = $pm_date->getMonth();
$hn_year = $pm_date->getYear();
$hn_seconds = $pm_date->getSecondsPastMidnight();
} else {
$hn_day = $pm_date[0];
$hn_month = $pm_date[1];
$hn_year = $pm_date[2];
$hn_seconds = $pm_date[3]; // seconds past midnight
}
if (($this->on_summertimestartmonth < $this->on_summertimeendmonth &&
$hn_month >= $this->on_summertimestartmonth &&
$hn_month <= $this->on_summertimeendmonth) ||
($this->on_summertimestartmonth > $this->on_summertimeendmonth &&
$hn_month >= $this->on_summertimestartmonth &&
$hn_month <= $this->on_summertimeendmonth)
) {
if ($hn_month == $this->on_summertimestartmonth) {
$hn_startday =
$this->getSummerTimeLimitDay($this->os_summertimestartday,
$this->on_summertimestartmonth,
$hn_year);
if ($hn_day < $hn_startday) {
return false;
} else if ($hn_day > $hn_startday) {
return true;
} else if (($hn_gmt = $hn_seconds * 1000 - $this->offset) -
$this->on_summertimeoffset >=
$this->on_summertimestarttime) {
return true;
} else if (($hn_gmt = $hn_seconds * 1000 - $this->offset) >=
$this->on_summertimestarttime) {
return PEAR::raiseError("Invalid time specified for date '" .
Date_Calc::dateFormat($hn_day,
$hn_month,
$hn_year,
"%Y-%m-%d") .
"'",
DATE_ERROR_INVALIDTIME);
} else {
return false;
}
} else if ($hn_month == $this->on_summertimeendmonth) {
$hn_endday =
$this->getSummerTimeLimitDay($this->os_summertimeendday,
$this->on_summertimeendmonth,
$hn_year);
if ($hn_day < $hn_endday) {
return true;
} else if ($hn_day > $hn_endday) {
return false;
} else if (($hn_gmt = $hn_seconds * 1000 - $this->offset) -
$this->on_summertimeoffset >=
$this->on_summertimeendtime) {
return false;
} else if ($hn_gmt >= $this->on_summertimeendtime) {
// There is a 50:50 chance that it's Summer time, but there
// is no way of knowing (the hour is repeated), so return
// default:
//
return $pb_repeatedhourdefault;
} else {
return true;
}
}
return true;
}
return false;
}
// }}}
// {{{ inDaylightTimeStandard()
/**
* Returns whether the given date/time in local standard time is
* in Summer time
*
* For example, if the clocks go forward at 1.00 standard time,
* then if the specified date/time is at 1.00, the function will
* return true, although the correct local time will actually
* be 2.00.
*
* This function is reliable for all dates and times, unlike the
* related function '{@link Date_TimeZone::inDaylightTime()}',
* which will fail if passed
* an invalid time (the skipped hour) and will be wrong half the
* time if passed an ambiguous time (the repeated hour).
*
* @param object $pm_date Date object to test or array of day, month, year,
* seconds past midnight
*
* @return bool true if this date is in Summer time for this time
* zone
* @access public
* @see Date_TimeZone::inDaylightTime()
* @since Method available since Release 1.5.0
*/
function inDaylightTimeStandard($pm_date)
{
if (!$this->hasdst) {
return false;
}
if (is_a($pm_date, "Date")) {
$hn_day = $pm_date->getDay();
$hn_month = $pm_date->getMonth();
$hn_year = $pm_date->getYear();
$hn_seconds = $pm_date->getSecondsPastMidnight();
} else {
$hn_day = $pm_date[0];
$hn_month = $pm_date[1];
$hn_year = $pm_date[2];
$hn_seconds = $pm_date[3];
}
if (($this->on_summertimestartmonth < $this->on_summertimeendmonth &&
$hn_month >= $this->on_summertimestartmonth &&
$hn_month <= $this->on_summertimeendmonth) ||
($this->on_summertimestartmonth > $this->on_summertimeendmonth &&
$hn_month >= $this->on_summertimestartmonth &&
$hn_month <= $this->on_summertimeendmonth)
) {
if ($hn_month == $this->on_summertimestartmonth) {
$hn_startday =
$this->getSummerTimeLimitDay($this->os_summertimestartday,
$this->on_summertimestartmonth,
$hn_year);
if ($hn_day < $hn_startday) {
return false;
} else if ($hn_day > $hn_startday) {
return true;
} else if ($hn_seconds * 1000 - $this->offset >=
$this->on_summertimestarttime) {
return true;
} else {
return false;
}
} else if ($hn_month == $this->on_summertimeendmonth) {
$hn_endday =
$this->getSummerTimeLimitDay($this->os_summertimeendday,
$this->on_summertimeendmonth,
$hn_year);
if ($hn_day < $hn_endday) {
return true;
} else if ($hn_day > $hn_endday) {
return false;
} else if ($hn_seconds * 1000 - $this->offset >=
$this->on_summertimeendtime) {
return false;
} else {
return true;
}
}
return true;
}
return false;
}
// }}}
// {{{ getDSTSavings()
/**
* Get the DST offset for this time zone
*
* Returns the DST offset of this time zone, in milliseconds,
* if the zone observes DST, zero otherwise. If the offset is not
* known, the function returns one hour.
*
* @return int the DST offset, in milliseconds or nought if the
* zone does not observe DST
* @access public
*/
function getDSTSavings()
{
if ($this->hasdst) {
// If offset is not specified, guess one hour. (This is almost
// always correct anyway). This cannot be improved upon, because
// where it is unset, the offset is either unknowable because the
// time-zone covers more than one political area (which may have
// different Summer time policies), or they might all have the
// same policy, but there is no way to automatically maintain
// this data at the moment, and manually it is simply not worth
// the bother. If a user wants this functionality and refuses
// to use the standard time-zone IDs, then he can always update
// the array himself.
//
return isset($this->on_summertimeoffset) ?
$this->on_summertimeoffset :
3600000;
} else {
return 0;
}
}
// }}}
// {{{ getRawOffset()
/**
* Returns the raw (non-DST-corrected) offset from UTC/GMT for this time
* zone
*
* @return int the offset, in milliseconds
* @access public
* @see Date_TimeZone::getOffset()
*/
function getRawOffset()
{
return $this->offset;
}
// }}}
// {{{ getOffset()
/**
* Returns the DST-corrected offset from UTC for the given date
*
* Gets the offset to UTC for a given date/time, taking into
* account daylight savings time, if the time zone observes it and if
* it is in effect.
*
* N.B. that the offset is calculated historically
* and in the future according to the current Summer time rules,
* and so this function is proleptically correct, but not necessarily
* historically correct. (Although if you want to be correct about
* times in the distant past, this class is probably not for you
* because the whole notion of time zones does not apply, and
* historically there are so many time zone changes, Summer time
* rule changes, name changes, calendar changes, that calculating
* this sort of information is beyond the scope of this package
* altogether.)
*
* @param mixed $pm_insummertime a boolean specifying whether or not the
* date is in Summer time, or,
* a Date object to test for this condition
*
* @return int the corrected offset to UTC in milliseconds