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

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

index eb08cb53ad81d90cda3699c0b8b5ddaeece94f9a..556404e2c806e28e33722a8c19fc284d740a5743 100644 (file)
@@ -1627,6 +1627,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;
@@ -1659,6 +1672,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)
@@ -2585,6 +2614,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.
 */
@@ -2593,6 +2637,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;
@@ -2601,7 +2650,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