]> granicus.if.org Git - php/commitdiff
- MFH: Added support for selectively listing timezone identifiers through
authorDerick Rethans <derick@php.net>
Sun, 27 Jan 2008 17:29:14 +0000 (17:29 +0000)
committerDerick Rethans <derick@php.net>
Sun, 27 Jan 2008 17:29:14 +0000 (17:29 +0000)
  timezone_identifiers_list() / DateTimezone::listIdentifiers().

NEWS
ext/date/php_date.c
ext/date/tests/timezones-list.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 2a3689a79785e36ba0a63d09d9cd13719981e0f4..76872801e87ab463bac56c9e060a7b107dabc8fd 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -2,7 +2,7 @@ PHP                                                                        NEWS
 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 ?? ??? 20??, PHP 5.3.0
 - Added garbage collector. (David Wang, Dmitry).
-- Added new date/time functionality:
+- Added new date/time functionality: (Derick)
 
   * date_parse_from_format(): Parse date/time strings according to a format.
   * date_create_from_format()/DateTime::createFromFormat(): Create a date/time
@@ -14,6 +14,10 @@ PHP                                                                        NEWS
        - date_parse_from_format()
   * support for abbreviation and offset based timezone specifiers for
     DateTime::getOffset() and DateTime::getName().
+  * support for selectively listing timezone identifiers through
+    timezone_identifiers_list() / DateTimezone::listIdentifiers().
+  * date_timestamp_set() / DateTime::setTimestamp() to set a Unix timestamp
+    without invoking the date parser. (Scott)
 
 - Added ability to use Traversable objects instead of plain arrays in ext/soap.
   (Joshua Reese, Dmitry)
@@ -40,8 +44,6 @@ PHP                                                                        NEWS
   (Etienne Kneuss)
 - Added "compact" handler for Zend MM storage. (Dmitry)
 - Added "+" and "*" specifiers to zend_parse_parameters(). (Andrei)
-- Added DateTime::setTimestamp() to set a unix timestamp without
-  invoking the date parser. (Scott)
 - Added SplDoublyLinkedList, SplStack, SplQueue classes. (Etienne)
 
 - Removed the experimental RPL (master/slave) functions from mysqli. (Andrey)
index 598ee7948702b58494b9814468cd76851db6c5cb..f5f3551f0378f1b6b9cef00f3f6819c9515333d4 100644 (file)
@@ -1497,6 +1497,19 @@ PHP_FUNCTION(getdate)
 }
 /* }}} */
 
+#define PHP_DATE_TIMEZONE_GROUP_AMERICA    0x0001
+#define PHP_DATE_TIMEZONE_GROUP_ANTARCTICA 0x0002
+#define PHP_DATE_TIMEZONE_GROUP_ARCTIC     0x0004
+#define PHP_DATE_TIMEZONE_GROUP_ASIA       0x0008
+#define PHP_DATE_TIMEZONE_GROUP_ATLANTIC   0x0010
+#define PHP_DATE_TIMEZONE_GROUP_AUSTRALIA  0x0020
+#define PHP_DATE_TIMEZONE_GROUP_EUROPE     0x0040
+#define PHP_DATE_TIMEZONE_GROUP_INDIAN     0x0080
+#define PHP_DATE_TIMEZONE_GROUP_PACIFIC    0x0100
+#define PHP_DATE_TIMEZONE_GROUP_UTC        0x0200
+#define PHP_DATE_TIMEZONE_GROUP_ALL        0x03FF
+#define PHP_DATE_TIMEZONE_GROUP_ALL_W_BC   0x07FF
+
 static void date_register_classes(TSRMLS_D)
 {
        zend_class_entry ce_date, ce_timezone;
@@ -1529,6 +1542,22 @@ static void date_register_classes(TSRMLS_D)
        date_ce_timezone = zend_register_internal_class_ex(&ce_timezone, NULL, NULL TSRMLS_CC);
        memcpy(&date_object_handlers_timezone, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
        date_object_handlers_timezone.clone_obj = date_object_clone_timezone;
+
+#define REGISTER_TIMEZONE_CLASS_CONST_STRING(const_name, value) \
+       zend_declare_class_constant_long(date_ce_timezone, const_name, sizeof(const_name)-1, value TSRMLS_CC);
+
+       REGISTER_TIMEZONE_CLASS_CONST_STRING("AMERICA",     PHP_DATE_TIMEZONE_GROUP_AMERICA);
+       REGISTER_TIMEZONE_CLASS_CONST_STRING("ANTARCTICA",  PHP_DATE_TIMEZONE_GROUP_ANTARCTICA);
+       REGISTER_TIMEZONE_CLASS_CONST_STRING("ARCTIC",      PHP_DATE_TIMEZONE_GROUP_ARCTIC);
+       REGISTER_TIMEZONE_CLASS_CONST_STRING("ASIA",        PHP_DATE_TIMEZONE_GROUP_ASIA);
+       REGISTER_TIMEZONE_CLASS_CONST_STRING("ATLANTIC",    PHP_DATE_TIMEZONE_GROUP_ATLANTIC);
+       REGISTER_TIMEZONE_CLASS_CONST_STRING("AUSTRALIA",   PHP_DATE_TIMEZONE_GROUP_AUSTRALIA);
+       REGISTER_TIMEZONE_CLASS_CONST_STRING("EUROPE",      PHP_DATE_TIMEZONE_GROUP_EUROPE);
+       REGISTER_TIMEZONE_CLASS_CONST_STRING("INDIAN",      PHP_DATE_TIMEZONE_GROUP_INDIAN);
+       REGISTER_TIMEZONE_CLASS_CONST_STRING("PACIFIC",     PHP_DATE_TIMEZONE_GROUP_PACIFIC);
+       REGISTER_TIMEZONE_CLASS_CONST_STRING("UTC",         PHP_DATE_TIMEZONE_GROUP_UTC);
+       REGISTER_TIMEZONE_CLASS_CONST_STRING("ALL",         PHP_DATE_TIMEZONE_GROUP_ALL);
+       REGISTER_TIMEZONE_CLASS_CONST_STRING("ALL_WITH_BC", PHP_DATE_TIMEZONE_GROUP_ALL_W_BC);
 }
 
 static inline zend_object_value date_object_new_date_ex(zend_class_entry *class_type, php_date_obj **ptr TSRMLS_DC)
@@ -2423,6 +2452,21 @@ PHP_FUNCTION(timezone_transitions_get)
 }
 /* }}} */
 
+static int check_id_allowed(char *id, long what)
+{
+       if (what & PHP_DATE_TIMEZONE_GROUP_AMERICA    && strncasecmp(id, "America/",     8) == 0) return 1;
+       if (what & PHP_DATE_TIMEZONE_GROUP_ANTARCTICA && strncasecmp(id, "Antarctica/", 11) == 0) return 1;
+       if (what & PHP_DATE_TIMEZONE_GROUP_ARCTIC     && strncasecmp(id, "Arctic/",      7) == 0) return 1;
+       if (what & PHP_DATE_TIMEZONE_GROUP_ASIA       && strncasecmp(id, "Asia/",        5) == 0) return 1;
+       if (what & PHP_DATE_TIMEZONE_GROUP_ATLANTIC   && strncasecmp(id, "Atlantic/",    9) == 0) return 1;
+       if (what & PHP_DATE_TIMEZONE_GROUP_AUSTRALIA  && strncasecmp(id, "Australia/",  10) == 0) return 1;
+       if (what & PHP_DATE_TIMEZONE_GROUP_EUROPE     && strncasecmp(id, "Europe/",      7) == 0) return 1;
+       if (what & PHP_DATE_TIMEZONE_GROUP_INDIAN     && strncasecmp(id, "Indian/",      7) == 0) return 1;
+       if (what & PHP_DATE_TIMEZONE_GROUP_PACIFIC    && strncasecmp(id, "Pacific/",     8) == 0) return 1;
+       if (what & PHP_DATE_TIMEZONE_GROUP_UTC        && strncasecmp(id, "UTC",          3) == 0) return 1;
+       return 0;
+}
+
 /* {{{ proto array timezone_identifiers_list()
    Returns numerically index array with all timezone identifiers.
 */
@@ -2431,6 +2475,11 @@ PHP_FUNCTION(timezone_identifiers_list)
        const timelib_tzdb             *tzdb;
        const timelib_tzdb_index_entry *table;
        int                             i, item_count;
+       long                            what = PHP_DATE_TIMEZONE_GROUP_ALL;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &what) == FAILURE) {
+               RETURN_FALSE;
+       }
 
        tzdb = DATE_TIMEZONEDB;
        item_count = tzdb->index_size;
@@ -2439,7 +2488,9 @@ PHP_FUNCTION(timezone_identifiers_list)
        array_init(return_value);
 
        for (i = 0; i < item_count; ++i) {
-               add_next_index_string(return_value, table[i].id, 1);
+               if (what == PHP_DATE_TIMEZONE_GROUP_ALL_W_BC || check_id_allowed(table[i].id, what)) {
+                       add_next_index_string(return_value, table[i].id, 1);
+               }
        };
 }
 /* }}} */
diff --git a/ext/date/tests/timezones-list.phpt b/ext/date/tests/timezones-list.phpt
new file mode 100644 (file)
index 0000000..5998498
--- /dev/null
@@ -0,0 +1,49 @@
+--TEST--
+timezone_identifiers_list()
+--FILE--
+<?php
+$a = timezone_identifiers_list();
+$b = timezone_identifiers_list( DateTimezone::AMERICA );
+$c = timezone_identifiers_list( DateTimezone::ALL_WITH_BC );
+$d = timezone_identifiers_list( DateTimezone::EUROPE | DateTimezone::UTC );
+
+echo in_array( "Europe/Oslo", $a ) ? "found" : "notfound", "\n";
+echo in_array( "Europe/Oslo", $b ) ? "found" : "notfound", "\n";
+echo in_array( "Europe/Oslo", $c ) ? "found" : "notfound", "\n";
+echo in_array( "Europe/Oslo", $d ) ? "found" : "notfound", "\n\n";
+
+echo in_array( "America/New_York", $a ) ? "found" : "notfound", "\n";
+echo in_array( "America/New_York", $b ) ? "found" : "notfound", "\n";
+echo in_array( "America/New_York", $c ) ? "found" : "notfound", "\n";
+echo in_array( "America/New_York", $d ) ? "found" : "notfound", "\n\n";
+
+echo in_array( "UTC", $a ) ? "found" : "notfound", "\n";
+echo in_array( "UTC", $b ) ? "found" : "notfound", "\n";
+echo in_array( "UTC", $c ) ? "found" : "notfound", "\n";
+echo in_array( "UTC", $d ) ? "found" : "notfound", "\n\n";
+
+echo in_array( "US/Eastern", $a ) ? "found" : "notfound", "\n";
+echo in_array( "US/Eastern", $b ) ? "found" : "notfound", "\n";
+echo in_array( "US/Eastern", $c ) ? "found" : "notfound", "\n";
+echo in_array( "US/Eastern", $d ) ? "found" : "notfound", "\n\n";
+?>
+--EXPECT--
+found
+notfound
+found
+found
+
+found
+found
+found
+notfound
+
+found
+notfound
+found
+found
+
+notfound
+notfound
+found
+notfound