-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
php_date.c
6076 lines (5105 loc) · 178 KB
/
php_date.c
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
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Derick Rethans <derick@derickrethans.nl> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "php_main.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/php_versioning.h"
#include "php_date.h"
#include "zend_attributes.h"
#include "zend_interfaces.h"
#include "zend_exceptions.h"
#include "lib/timelib.h"
#include "lib/timelib_private.h"
#ifndef PHP_WIN32
#include <time.h>
#else
#include "win32/time.h"
#endif
#ifdef PHP_WIN32
static __inline __int64 php_date_llabs( __int64 i ) { return i >= 0? i: -i; }
#elif defined(__GNUC__) && __GNUC__ < 3
static __inline __int64_t php_date_llabs( __int64_t i ) { return i >= 0 ? i : -i; }
#else
static inline long long php_date_llabs( long long i ) { return i >= 0 ? i : -i; }
#endif
#ifdef PHP_WIN32
#define DATE_I64_BUF_LEN 65
# define DATE_I64A(i, s, len) _i64toa_s(i, s, len, 10)
# define DATE_A64I(i, s) i = _atoi64(s)
#else
#define DATE_I64_BUF_LEN 65
# define DATE_I64A(i, s, len) \
do { \
int st = snprintf(s, len, "%lld", i); \
s[st] = '\0'; \
} while (0);
#define DATE_A64I(i, s) i = strtoll(s, NULL, 10)
#endif
PHPAPI time_t php_time(void)
{
#ifdef HAVE_GETTIMEOFDAY
struct timeval tm;
if (UNEXPECTED(gettimeofday(&tm, NULL) != SUCCESS)) {
/* fallback, can't reasonably happen */
return time(NULL);
}
return tm.tv_sec;
#else
return time(NULL);
#endif
}
/*
* RFC822, Section 5.1: http://www.ietf.org/rfc/rfc822.txt
* date-time = [ day "," ] date time ; dd mm yy hh:mm:ss zzz
* day = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
* date = 1*2DIGIT month 2DIGIT ; day month year e.g. 20 Jun 82
* month = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
* time = hour zone ; ANSI and Military
* hour = 2DIGIT ":" 2DIGIT [":" 2DIGIT] ; 00:00:00 - 23:59:59
* zone = "UT" / "GMT" / "EST" / "EDT" / "CST" / "CDT" / "MST" / "MDT" / "PST" / "PDT" / 1ALPHA / ( ("+" / "-") 4DIGIT )
*/
#define DATE_FORMAT_RFC822 "D, d M y H:i:s O"
/*
* RFC850, Section 2.1.4: http://www.ietf.org/rfc/rfc850.txt
* Format must be acceptable both to the ARPANET and to the getdate routine.
* One format that is acceptable to both is Weekday, DD-Mon-YY HH:MM:SS TIMEZONE
* TIMEZONE can be any timezone name (3 or more letters)
*/
#define DATE_FORMAT_RFC850 "l, d-M-y H:i:s T"
/*
* RFC1036, Section 2.1.2: http://www.ietf.org/rfc/rfc1036.txt
* Its format must be acceptable both in RFC-822 and to the getdate(3)
* Wdy, DD Mon YY HH:MM:SS TIMEZONE
* There is no hope of having a complete list of timezones. Universal
* Time (GMT), the North American timezones (PST, PDT, MST, MDT, CST,
* CDT, EST, EDT) and the +/-hhmm offset specified in RFC-822 should be supported.
*/
#define DATE_FORMAT_RFC1036 "D, d M y H:i:s O"
/*
* RFC1123, Section 5.2.14: http://www.ietf.org/rfc/rfc1123.txt
* RFC-822 Date and Time Specification: RFC-822 Section 5
* The syntax for the date is hereby changed to: date = 1*2DIGIT month 2*4DIGIT
*/
#define DATE_FORMAT_RFC1123 "D, d M Y H:i:s O"
/*
* RFC7231, Section 7.1.1: http://tools.ietf.org/html/rfc7231
*/
#define DATE_FORMAT_RFC7231 "D, d M Y H:i:s \\G\\M\\T"
/*
* RFC2822, Section 3.3: http://www.ietf.org/rfc/rfc2822.txt
* FWS = ([*WSP CRLF] 1*WSP) / ; Folding white space
* CFWS = *([FWS] comment) (([FWS] comment) / FWS)
*
* date-time = [ day-of-week "," ] date FWS time [CFWS]
* day-of-week = ([FWS] day-name)
* day-name = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
* date = day month year
* year = 4*DIGIT
* month = (FWS month-name FWS)
* month-name = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
* day = ([FWS] 1*2DIGIT)
* time = time-of-day FWS zone
* time-of-day = hour ":" minute [ ":" second ]
* hour = 2DIGIT
* minute = 2DIGIT
* second = 2DIGIT
* zone = (( "+" / "-" ) 4DIGIT)
*/
#define DATE_FORMAT_RFC2822 "D, d M Y H:i:s O"
/*
* RFC3339, Section 5.6: http://www.ietf.org/rfc/rfc3339.txt
* date-fullyear = 4DIGIT
* date-month = 2DIGIT ; 01-12
* date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on month/year
*
* time-hour = 2DIGIT ; 00-23
* time-minute = 2DIGIT ; 00-59
* time-second = 2DIGIT ; 00-58, 00-59, 00-60 based on leap second rules
*
* time-secfrac = "." 1*DIGIT
* time-numoffset = ("+" / "-") time-hour ":" time-minute
* time-offset = "Z" / time-numoffset
*
* partial-time = time-hour ":" time-minute ":" time-second [time-secfrac]
* full-date = date-fullyear "-" date-month "-" date-mday
* full-time = partial-time time-offset
*
* date-time = full-date "T" full-time
*/
#define DATE_FORMAT_RFC3339 "Y-m-d\\TH:i:sP"
/*
* This format does not technically match the ISO 8601 standard, as it does not
* use : in the UTC offset format specifier. This is kept for BC reasons. The
* DATE_FORMAT_ISO8601_EXPANDED format does correct this, as well as adding
* support for years out side of the traditional 0000-9999 range.
*/
#define DATE_FORMAT_ISO8601 "Y-m-d\\TH:i:sO"
/* ISO 8601:2004(E)
*
* Section 3.5 Expansion:
* By mutual agreement of the partners in information interchange, it is
* permitted to expand the component identifying the calendar year, which is
* otherwise limited to four digits. This enables reference to dates and times
* in calendar years outside the range supported by complete representations,
* i.e. before the start of the year [0000] or after the end of the year
* [9999]."
*
* Section 4.1.2.4 Expanded representations:
* If, by agreement, expanded representations are used, the formats shall be as
* specified below. The interchange parties shall agree the additional number of
* digits in the time element year. In the examples below it has been agreed to
* expand the time element year with two digits.
* Extended format: ±YYYYY-MM-DD
* Example: +001985-04-12
*
* PHP's year expansion digits are variable.
*/
#define DATE_FORMAT_ISO8601_EXPANDED "X-m-d\\TH:i:sP"
/* Internal Only
* This format only extends the year when needed, keeping the 'P' format with
* colon for UTC offsets
*/
#define DATE_FORMAT_ISO8601_LARGE_YEAR "x-m-d\\TH:i:sP"
/*
* RFC3339, Appendix A: http://www.ietf.org/rfc/rfc3339.txt
* ISO 8601 also requires (in section 5.3.1.3) that a decimal fraction
* be proceeded by a "0" if less than unity. Annex B.2 of ISO 8601
* gives examples where the decimal fractions are not preceded by a "0".
* This grammar assumes section 5.3.1.3 is correct and that Annex B.2 is
* in error.
*/
#define DATE_FORMAT_RFC3339_EXTENDED "Y-m-d\\TH:i:s.vP"
/*
* This comes from various sources that like to contradict. I'm going with the
* format here because of:
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa384321%28v=vs.85%29.aspx
* and http://curl.haxx.se/rfc/cookie_spec.html
*/
#define DATE_FORMAT_COOKIE "l, d-M-Y H:i:s T"
#define SUNFUNCS_RET_TIMESTAMP 0
#define SUNFUNCS_RET_STRING 1
#define SUNFUNCS_RET_DOUBLE 2
#define PHP_DATE_TIMEZONE_GROUP_AFRICA 0x0001
#define PHP_DATE_TIMEZONE_GROUP_AMERICA 0x0002
#define PHP_DATE_TIMEZONE_GROUP_ANTARCTICA 0x0004
#define PHP_DATE_TIMEZONE_GROUP_ARCTIC 0x0008
#define PHP_DATE_TIMEZONE_GROUP_ASIA 0x0010
#define PHP_DATE_TIMEZONE_GROUP_ATLANTIC 0x0020
#define PHP_DATE_TIMEZONE_GROUP_AUSTRALIA 0x0040
#define PHP_DATE_TIMEZONE_GROUP_EUROPE 0x0080
#define PHP_DATE_TIMEZONE_GROUP_INDIAN 0x0100
#define PHP_DATE_TIMEZONE_GROUP_PACIFIC 0x0200
#define PHP_DATE_TIMEZONE_GROUP_UTC 0x0400
#define PHP_DATE_TIMEZONE_GROUP_ALL 0x07FF
#define PHP_DATE_TIMEZONE_GROUP_ALL_W_BC 0x0FFF
#define PHP_DATE_TIMEZONE_PER_COUNTRY 0x1000
#define PHP_DATE_PERIOD_EXCLUDE_START_DATE 0x0001
#define PHP_DATE_PERIOD_INCLUDE_END_DATE 0x0002
#include "php_date_arginfo.h"
static const char* guess_timezone(const timelib_tzdb *tzdb);
static void date_register_classes(void);
/* }}} */
ZEND_DECLARE_MODULE_GLOBALS(date)
static PHP_GINIT_FUNCTION(date);
/* True global */
timelib_tzdb *php_date_global_timezone_db;
int php_date_global_timezone_db_enabled;
#define DATE_DEFAULT_LATITUDE "31.7667"
#define DATE_DEFAULT_LONGITUDE "35.2333"
/* on 90'50; common sunset declaration (start of sun body appear) */
#define DATE_SUNSET_ZENITH "90.833333"
/* on 90'50; common sunrise declaration (sun body disappeared) */
#define DATE_SUNRISE_ZENITH "90.833333"
static PHP_INI_MH(OnUpdate_date_timezone);
/* {{{ INI Settings */
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("date.timezone", "UTC", PHP_INI_ALL, OnUpdate_date_timezone, default_timezone, zend_date_globals, date_globals)
PHP_INI_ENTRY("date.default_latitude", DATE_DEFAULT_LATITUDE, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("date.default_longitude", DATE_DEFAULT_LONGITUDE, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("date.sunset_zenith", DATE_SUNSET_ZENITH, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("date.sunrise_zenith", DATE_SUNRISE_ZENITH, PHP_INI_ALL, NULL)
PHP_INI_END()
/* }}} */
static zend_class_entry *date_ce_date, *date_ce_timezone, *date_ce_interval, *date_ce_period;
static zend_class_entry *date_ce_immutable, *date_ce_interface;
static zend_class_entry *date_ce_date_error, *date_ce_date_object_error, *date_ce_date_range_error;
static zend_class_entry *date_ce_date_exception, *date_ce_date_invalid_timezone_exception, *date_ce_date_invalid_operation_exception, *date_ce_date_malformed_string_exception, *date_ce_date_malformed_interval_string_exception, *date_ce_date_malformed_period_string_exception;
PHPAPI zend_class_entry *php_date_get_date_ce(void)
{
return date_ce_date;
}
PHPAPI zend_class_entry *php_date_get_immutable_ce(void)
{
return date_ce_immutable;
}
PHPAPI zend_class_entry *php_date_get_interface_ce(void)
{
return date_ce_interface;
}
PHPAPI zend_class_entry *php_date_get_timezone_ce(void)
{
return date_ce_timezone;
}
PHPAPI zend_class_entry *php_date_get_interval_ce(void)
{
return date_ce_interval;
}
PHPAPI zend_class_entry *php_date_get_period_ce(void)
{
return date_ce_period;
}
static zend_object_handlers date_object_handlers_date;
static zend_object_handlers date_object_handlers_immutable;
static zend_object_handlers date_object_handlers_timezone;
static zend_object_handlers date_object_handlers_interval;
static zend_object_handlers date_object_handlers_period;
static void date_throw_uninitialized_error(zend_class_entry *ce)
{
if (ce->type == ZEND_INTERNAL_CLASS) {
zend_throw_error(date_ce_date_object_error, "Object of type %s has not been correctly initialized by calling parent::__construct() in its constructor", ZSTR_VAL(ce->name));
} else {
zend_class_entry *ce_ptr = ce;
while (ce_ptr && ce_ptr->parent && ce_ptr->type == ZEND_USER_CLASS) {
ce_ptr = ce_ptr->parent;
}
if (ce_ptr->type != ZEND_INTERNAL_CLASS) {
zend_throw_error(date_ce_date_object_error, "Object of type %s not been correctly initialized by calling parent::__construct() in its constructor", ZSTR_VAL(ce->name));
return;
}
zend_throw_error(date_ce_date_object_error, "Object of type %s (inheriting %s) has not been correctly initialized by calling parent::__construct() in its constructor", ZSTR_VAL(ce->name), ZSTR_VAL(ce_ptr->name));
}
}
#define DATE_CHECK_INITIALIZED(member, ce) \
if (UNEXPECTED(!member)) { \
date_throw_uninitialized_error(ce); \
RETURN_THROWS(); \
}
static void date_object_free_storage_date(zend_object *object);
static void date_object_free_storage_timezone(zend_object *object);
static void date_object_free_storage_interval(zend_object *object);
static void date_object_free_storage_period(zend_object *object);
static zend_object *date_object_new_date(zend_class_entry *class_type);
static zend_object *date_object_new_timezone(zend_class_entry *class_type);
static zend_object *date_object_new_interval(zend_class_entry *class_type);
static zend_object *date_object_new_period(zend_class_entry *class_type);
static zend_object *date_object_clone_date(zend_object *this_ptr);
static zend_object *date_object_clone_timezone(zend_object *this_ptr);
static zend_object *date_object_clone_interval(zend_object *this_ptr);
static zend_object *date_object_clone_period(zend_object *this_ptr);
static int date_object_compare_date(zval *d1, zval *d2);
static HashTable *date_object_get_gc(zend_object *object, zval **table, int *n);
static HashTable *date_object_get_properties_for(zend_object *object, zend_prop_purpose purpose);
static HashTable *date_object_get_gc_interval(zend_object *object, zval **table, int *n);
static HashTable *date_object_get_properties_interval(zend_object *object);
static HashTable *date_object_get_gc_period(zend_object *object, zval **table, int *n);
static HashTable *date_object_get_properties_for_timezone(zend_object *object, zend_prop_purpose purpose);
static HashTable *date_object_get_gc_timezone(zend_object *object, zval **table, int *n);
static HashTable *date_object_get_debug_info_timezone(zend_object *object, int *is_temp);
static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv);
static int date_interval_compare_objects(zval *o1, zval *o2);
static zval *date_interval_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv);
static zval *date_interval_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot);
static zval *date_interval_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot);
static int date_period_has_property(zend_object *object, zend_string *name, int type, void **cache_slot);
static zval *date_period_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv);
static zval *date_period_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot);
static zval *date_period_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot);
static void date_period_unset_property(zend_object *object, zend_string *name, void **cache_slot);
static HashTable *date_period_get_properties_for(zend_object *object, zend_prop_purpose purpose);
static int date_object_compare_timezone(zval *tz1, zval *tz2);
/* {{{ Module struct */
zend_module_entry date_module_entry = {
STANDARD_MODULE_HEADER_EX,
NULL,
NULL,
"date", /* extension name */
ext_functions, /* function list */
PHP_MINIT(date), /* process startup */
PHP_MSHUTDOWN(date), /* process shutdown */
PHP_RINIT(date), /* request startup */
PHP_RSHUTDOWN(date), /* request shutdown */
PHP_MINFO(date), /* extension info */
PHP_DATE_VERSION, /* extension version */
PHP_MODULE_GLOBALS(date), /* globals descriptor */
PHP_GINIT(date), /* globals ctor */
NULL, /* globals dtor */
ZEND_MODULE_POST_ZEND_DEACTIVATE_N(date), /* post deactivate */
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
/* {{{ PHP_GINIT_FUNCTION */
static PHP_GINIT_FUNCTION(date)
{
date_globals->default_timezone = NULL;
date_globals->timezone = NULL;
date_globals->tzcache = NULL;
}
/* }}} */
static void _php_date_tzinfo_dtor(zval *zv) /* {{{ */
{
timelib_tzinfo *tzi = (timelib_tzinfo*)Z_PTR_P(zv);
timelib_tzinfo_dtor(tzi);
} /* }}} */
/* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(date)
{
if (DATEG(timezone)) {
efree(DATEG(timezone));
}
DATEG(timezone) = NULL;
DATEG(tzcache) = NULL;
DATEG(last_errors) = NULL;
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(date)
{
if (DATEG(timezone)) {
efree(DATEG(timezone));
}
DATEG(timezone) = NULL;
return SUCCESS;
}
/* }}} */
ZEND_MODULE_POST_ZEND_DEACTIVATE_D(date)
{
if (DATEG(tzcache)) {
zend_hash_destroy(DATEG(tzcache));
FREE_HASHTABLE(DATEG(tzcache));
DATEG(tzcache) = NULL;
}
if (DATEG(last_errors)) {
timelib_error_container_dtor(DATEG(last_errors));
DATEG(last_errors) = NULL;
}
return SUCCESS;
}
#define DATE_TIMEZONEDB php_date_global_timezone_db ? php_date_global_timezone_db : timelib_builtin_db()
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(date)
{
REGISTER_INI_ENTRIES();
date_register_classes();
register_php_date_symbols(module_number);
php_date_global_timezone_db = NULL;
php_date_global_timezone_db_enabled = 0;
DATEG(last_errors) = NULL;
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(date)
{
UNREGISTER_INI_ENTRIES();
if (DATEG(last_errors)) {
timelib_error_container_dtor(DATEG(last_errors));
}
#ifndef ZTS
DATEG(default_timezone) = NULL;
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(date)
{
const timelib_tzdb *tzdb = DATE_TIMEZONEDB;
php_info_print_table_start();
php_info_print_table_row(2, "date/time support", "enabled");
php_info_print_table_row(2, "timelib version", TIMELIB_ASCII_VERSION);
php_info_print_table_row(2, "\"Olson\" Timezone Database Version", tzdb->version);
php_info_print_table_row(2, "Timezone Database", php_date_global_timezone_db_enabled ? "external" : "internal");
php_info_print_table_row(2, "Default timezone", guess_timezone(tzdb));
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ Timezone Cache functions */
static timelib_tzinfo *php_date_parse_tzfile(const char *formal_tzname, const timelib_tzdb *tzdb)
{
timelib_tzinfo *tzi;
int dummy_error_code;
if(!DATEG(tzcache)) {
ALLOC_HASHTABLE(DATEG(tzcache));
zend_hash_init(DATEG(tzcache), 4, NULL, _php_date_tzinfo_dtor, 0);
}
if ((tzi = zend_hash_str_find_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname))) != NULL) {
return tzi;
}
tzi = timelib_parse_tzfile(formal_tzname, tzdb, &dummy_error_code);
if (tzi) {
zend_hash_str_add_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname), tzi);
}
return tzi;
}
static timelib_tzinfo *php_date_parse_tzfile_wrapper(const char *formal_tzname, const timelib_tzdb *tzdb, int *dummy_error_code)
{
return php_date_parse_tzfile(formal_tzname, tzdb);
}
/* }}} */
/* Callback to check the date.timezone only when changed increases performance */
/* {{{ static PHP_INI_MH(OnUpdate_date_timezone) */
static PHP_INI_MH(OnUpdate_date_timezone)
{
if (new_value && !timelib_timezone_id_is_valid(ZSTR_VAL(new_value), DATE_TIMEZONEDB)) {
php_error_docref(
NULL, E_WARNING,
"Invalid date.timezone value '%s', using '%s' instead",
ZSTR_VAL(new_value),
DATEG(default_timezone) ? DATEG(default_timezone) : "UTC"
);
return FAILURE;
}
if (OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage) == FAILURE) {
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ Helper functions */
static const char* guess_timezone(const timelib_tzdb *tzdb)
{
/* Checking whether timezone has been set with date_default_timezone_set() */
if (DATEG(timezone) && (strlen(DATEG(timezone))) > 0) {
return DATEG(timezone);
}
/* Check config setting for default timezone */
if (!DATEG(default_timezone)) {
/* Special case: ext/date wasn't initialized yet */
zval *ztz;
if (NULL != (ztz = cfg_get_entry("date.timezone", sizeof("date.timezone")))
&& Z_TYPE_P(ztz) == IS_STRING && Z_STRLEN_P(ztz) > 0 && timelib_timezone_id_is_valid(Z_STRVAL_P(ztz), tzdb)) {
return Z_STRVAL_P(ztz);
}
} else if (*DATEG(default_timezone)) {
return DATEG(default_timezone);
}
/* Fallback to UTC */
return "UTC";
}
PHPAPI timelib_tzinfo *get_timezone_info(void)
{
timelib_tzinfo *tzi;
const char *tz = guess_timezone(DATE_TIMEZONEDB);
tzi = php_date_parse_tzfile(tz, DATE_TIMEZONEDB);
if (! tzi) {
zend_throw_error(date_ce_date_error, "Timezone database is corrupt. Please file a bug report as this should never happen");
}
return tzi;
}
static void update_property(zend_object *object, zend_string *key, zval *prop_val)
{
if (ZSTR_LEN(key) > 0 && ZSTR_VAL(key)[0] == '\0') { // not public
const char *class_name, *prop_name;
size_t prop_name_len;
if (zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len) == SUCCESS) {
if (class_name[0] != '*') { // private
zend_string *cname;
zend_class_entry *ce;
cname = zend_string_init(class_name, strlen(class_name), 0);
ce = zend_lookup_class(cname);
if (ce) {
zend_update_property(ce, object, prop_name, prop_name_len, prop_val);
}
zend_string_release_ex(cname, 0);
} else { // protected
zend_update_property(object->ce, object, prop_name, prop_name_len, prop_val);
}
}
return;
}
// public
zend_update_property(object->ce, object, ZSTR_VAL(key), ZSTR_LEN(key), prop_val);
}
/* }}} */
/* {{{ date() and gmdate() data */
#include "zend_smart_str.h"
static const char * const mon_full_names[] = {
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};
static const char * const mon_short_names[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static const char * const day_full_names[] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
static const char * const day_short_names[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const char *english_suffix(timelib_sll number)
{
if (number >= 10 && number <= 19) {
return "th";
} else {
switch (number % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
}
}
return "th";
}
/* }}} */
/* {{{ day of week helpers */
static const char *php_date_full_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
{
timelib_sll day_of_week = timelib_day_of_week(y, m, d);
if (day_of_week < 0) {
return "Unknown";
}
return day_full_names[day_of_week];
}
static const char *php_date_short_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
{
timelib_sll day_of_week = timelib_day_of_week(y, m, d);
if (day_of_week < 0) {
return "Unknown";
}
return day_short_names[day_of_week];
}
/* }}} */
/* {{{ date_format - (gm)date helper */
static zend_string *date_format(const char *format, size_t format_len, timelib_time *t, bool localtime)
{
smart_str string = {0};
size_t i;
int length = 0;
char buffer[97];
timelib_time_offset *offset = NULL;
timelib_sll isoweek, isoyear;
bool rfc_colon;
int weekYearSet = 0;
if (!format_len) {
return ZSTR_EMPTY_ALLOC();
}
if (localtime) {
if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
offset = timelib_time_offset_ctor();
offset->offset = (t->z + (t->dst * 3600));
offset->leap_secs = 0;
offset->is_dst = t->dst;
offset->abbr = timelib_strdup(t->tz_abbr);
} else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
offset = timelib_time_offset_ctor();
offset->offset = (t->z);
offset->leap_secs = 0;
offset->is_dst = 0;
offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
snprintf(offset->abbr, 9, "GMT%c%02d%02d",
(offset->offset < 0) ? '-' : '+',
abs(offset->offset / 3600),
abs((offset->offset % 3600) / 60));
} else if (t->zone_type == TIMELIB_ZONETYPE_ID) {
offset = timelib_get_time_zone_info(t->sse, t->tz_info);
} else {
/* Shouldn't happen, but code defensively */
offset = timelib_time_offset_ctor();
}
}
for (i = 0; i < format_len; i++) {
rfc_colon = 0;
switch (format[i]) {
/* day */
case 'd': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->d); break;
case 'D': length = slprintf(buffer, sizeof(buffer), "%s", php_date_short_day_name(t->y, t->m, t->d)); break;
case 'j': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->d); break;
case 'l': length = slprintf(buffer, sizeof(buffer), "%s", php_date_full_day_name(t->y, t->m, t->d)); break;
case 'S': length = slprintf(buffer, sizeof(buffer), "%s", english_suffix(t->d)); break;
case 'w': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_day_of_week(t->y, t->m, t->d)); break;
case 'N': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_iso_day_of_week(t->y, t->m, t->d)); break;
case 'z': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_day_of_year(t->y, t->m, t->d)); break;
/* week */
case 'W':
if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
length = slprintf(buffer, sizeof(buffer), "%02d", (int) isoweek); break; /* iso weeknr */
case 'o':
if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
length = slprintf(buffer, sizeof(buffer), ZEND_LONG_FMT, (zend_long) isoyear); break; /* iso year */
/* month */
case 'F': length = slprintf(buffer, sizeof(buffer), "%s", mon_full_names[t->m - 1]); break;
case 'm': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->m); break;
case 'M': length = slprintf(buffer, sizeof(buffer), "%s", mon_short_names[t->m - 1]); break;
case 'n': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->m); break;
case 't': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_days_in_month(t->y, t->m)); break;
/* year */
case 'L': length = slprintf(buffer, sizeof(buffer), "%d", timelib_is_leap((int) t->y)); break;
case 'y': length = slprintf(buffer, sizeof(buffer), "%02d", (int) (t->y % 100)); break;
case 'Y': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : "", php_date_llabs((timelib_sll) t->y)); break;
case 'x': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : (t->y >= 10000 ? "+" : ""), php_date_llabs((timelib_sll) t->y)); break;
case 'X': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : "+", php_date_llabs((timelib_sll) t->y)); break;
/* time */
case 'a': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "pm" : "am"); break;
case 'A': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "PM" : "AM"); break;
case 'B': {
int retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
if (retval < 0) {
retval += 864000;
}
/* Make sure to do this on a positive int to avoid rounding errors */
retval = (retval / 864) % 1000;
length = slprintf(buffer, sizeof(buffer), "%03d", retval);
break;
}
case 'g': length = slprintf(buffer, sizeof(buffer), "%d", (t->h % 12) ? (int) t->h % 12 : 12); break;
case 'G': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->h); break;
case 'h': length = slprintf(buffer, sizeof(buffer), "%02d", (t->h % 12) ? (int) t->h % 12 : 12); break;
case 'H': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->h); break;
case 'i': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->i); break;
case 's': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->s); break;
case 'u': length = slprintf(buffer, sizeof(buffer), "%06d", (int) floor(t->us)); break;
case 'v': length = slprintf(buffer, sizeof(buffer), "%03d", (int) floor(t->us / 1000)); break;
/* timezone */
case 'I': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->is_dst : 0); break;
case 'p':
if (!localtime || strcmp(offset->abbr, "UTC") == 0 || strcmp(offset->abbr, "Z") == 0 || strcmp(offset->abbr, "GMT+0000") == 0) {
length = slprintf(buffer, sizeof(buffer), "%s", "Z");
break;
}
ZEND_FALLTHROUGH;
case 'P': rfc_colon = 1; ZEND_FALLTHROUGH;
case 'O': length = slprintf(buffer, sizeof(buffer), "%c%02d%s%02d",
localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
localtime ? abs(offset->offset / 3600) : 0,
rfc_colon ? ":" : "",
localtime ? abs((offset->offset % 3600) / 60) : 0
);
break;
case 'T': length = slprintf(buffer, sizeof(buffer), "%s", localtime ? offset->abbr : "GMT"); break;
case 'e': if (!localtime) {
length = slprintf(buffer, sizeof(buffer), "%s", "UTC");
} else {
switch (t->zone_type) {
case TIMELIB_ZONETYPE_ID:
length = slprintf(buffer, sizeof(buffer), "%s", t->tz_info->name);
break;
case TIMELIB_ZONETYPE_ABBR:
length = slprintf(buffer, sizeof(buffer), "%s", offset->abbr);
break;
case TIMELIB_ZONETYPE_OFFSET:
length = slprintf(buffer, sizeof(buffer), "%c%02d:%02d",
((offset->offset < 0) ? '-' : '+'),
abs(offset->offset / 3600),
abs((offset->offset % 3600) / 60)
);
break;
}
}
break;
case 'Z': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->offset : 0); break;
/* full date/time */
case 'c': length = slprintf(buffer, sizeof(buffer), "%04" ZEND_LONG_FMT_SPEC "-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
(zend_long) t->y, (int) t->m, (int) t->d,
(int) t->h, (int) t->i, (int) t->s,
localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
localtime ? abs(offset->offset / 3600) : 0,
localtime ? abs((offset->offset % 3600) / 60) : 0
);
break;
case 'r': length = slprintf(buffer, sizeof(buffer), "%3s, %02d %3s %04" ZEND_LONG_FMT_SPEC " %02d:%02d:%02d %c%02d%02d",
php_date_short_day_name(t->y, t->m, t->d),
(int) t->d, mon_short_names[t->m - 1],
(zend_long) t->y, (int) t->h, (int) t->i, (int) t->s,
localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
localtime ? abs(offset->offset / 3600) : 0,
localtime ? abs((offset->offset % 3600) / 60) : 0
);
break;
case 'U': length = slprintf(buffer, sizeof(buffer), "%lld", (timelib_sll) t->sse); break;
case '\\': if (i < format_len) i++; ZEND_FALLTHROUGH;
default: buffer[0] = format[i]; buffer[1] = '\0'; length = 1; break;
}
smart_str_appendl(&string, buffer, length);
}
smart_str_0(&string);
if (localtime) {
timelib_time_offset_dtor(offset);
}
return string.s;
}
PHPAPI zend_string *php_format_date_obj(const char *format, size_t format_len, php_date_obj *date_obj)
{
if (!date_obj->time) {
return NULL;
}
return date_format(format, format_len, date_obj->time, date_obj->time->is_localtime);
}
static void php_date(INTERNAL_FUNCTION_PARAMETERS, bool localtime)
{
zend_string *format;
zend_long ts;
bool ts_is_null = 1;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(format)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(ts, ts_is_null)
ZEND_PARSE_PARAMETERS_END();
if (ts_is_null) {
ts = php_time();
}
RETURN_STR(php_format_date(ZSTR_VAL(format), ZSTR_LEN(format), ts, localtime));
}
/* }}} */
PHPAPI zend_string *php_format_date(const char *format, size_t format_len, time_t ts, bool localtime) /* {{{ */
{
timelib_time *t;
timelib_tzinfo *tzi;
zend_string *string;
t = timelib_time_ctor();
if (localtime) {
tzi = get_timezone_info();
t->tz_info = tzi;
t->zone_type = TIMELIB_ZONETYPE_ID;
timelib_unixtime2local(t, ts);
} else {
tzi = NULL;
timelib_unixtime2gmt(t, ts);
}
string = date_format(format, format_len, t, localtime);
timelib_time_dtor(t);
return string;
}
/* }}} */
/* {{{ php_idate */
PHPAPI int php_idate(char format, time_t ts, bool localtime)
{
timelib_time *t;
timelib_tzinfo *tzi;
int retval = -1;
timelib_time_offset *offset = NULL;
timelib_sll isoweek, isoyear;
t = timelib_time_ctor();
if (!localtime) {
tzi = get_timezone_info();
t->tz_info = tzi;
t->zone_type = TIMELIB_ZONETYPE_ID;
timelib_unixtime2local(t, ts);
} else {
tzi = NULL;
timelib_unixtime2gmt(t, ts);
}
if (!localtime) {
if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
offset = timelib_time_offset_ctor();
offset->offset = (t->z + (t->dst * 3600));
offset->leap_secs = 0;
offset->is_dst = t->dst;
offset->abbr = timelib_strdup(t->tz_abbr);
} else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
offset = timelib_time_offset_ctor();
offset->offset = (t->z + (t->dst * 3600));
offset->leap_secs = 0;
offset->is_dst = t->dst;
offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
snprintf(offset->abbr, 9, "GMT%c%02d%02d",
(offset->offset < 0) ? '-' : '+',
abs(offset->offset / 3600),
abs((offset->offset % 3600) / 60));
} else {
offset = timelib_get_time_zone_info(t->sse, t->tz_info);
}
}
timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear);
switch (format) {
/* day */
case 'd': case 'j': retval = (int) t->d; break;
case 'N': retval = (int) timelib_iso_day_of_week(t->y, t->m, t->d); break;
case 'w': retval = (int) timelib_day_of_week(t->y, t->m, t->d); break;
case 'z': retval = (int) timelib_day_of_year(t->y, t->m, t->d); break;
/* week */
case 'W': retval = (int) isoweek; break; /* iso weeknr */
/* month */
case 'm': case 'n': retval = (int) t->m; break;
case 't': retval = (int) timelib_days_in_month(t->y, t->m); break;
/* year */
case 'L': retval = (int) timelib_is_leap((int) t->y); break;
case 'y': retval = (int) (t->y % 100); break;
case 'Y': retval = (int) t->y; break;
case 'o': retval = (int) isoyear; break; /* iso year */
/* Swatch Beat a.k.a. Internet Time */
case 'B':
retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
if (retval < 0) {
retval += 864000;
}
/* Make sure to do this on a positive int to avoid rounding errors */
retval = (retval / 864) % 1000;
break;
/* time */
case 'g': case 'h': retval = (int) ((t->h % 12) ? (int) t->h % 12 : 12); break;
case 'H': case 'G': retval = (int) t->h; break;
case 'i': retval = (int) t->i; break;
case 's': retval = (int) t->s; break;
/* timezone */
case 'I': retval = (int) (!localtime ? offset->is_dst : 0); break;
case 'Z': retval = (int) (!localtime ? offset->offset : 0); break;
case 'U': retval = (int) t->sse; break;
}
if (!localtime) {
timelib_time_offset_dtor(offset);
}
timelib_time_dtor(t);
return retval;
}
/* }}} */
/* {{{ Format a local date/time */