/* {{{ compare_objects handler for TimeZone * Can't be used for >, >=, <, <= comparisons */ static int TimeZone_compare_objects(zval *object1, zval *object2) { TimeZone_object *to1, *to2; to1 = Z_INTL_TIMEZONE_P(object1); to2 = Z_INTL_TIMEZONE_P(object2); if (to1->utimezone == NULL || to2->utimezone == NULL) { zend_throw_exception(NULL, "Comparison with at least one unconstructed " "IntlTimeZone operand", 0); /* intentionally not returning */ } else { if (*to1->utimezone == *to2->utimezone) { return 0; } } return 1; }
/* {{{ get_debug_info handler for TimeZone */ static HashTable *TimeZone_get_debug_info(zval *object, int *is_temp) { zval zv; TimeZone_object *to; const TimeZone *tz; UnicodeString ustr; char *str; size_t str_len; HashTable *debug_info; UErrorCode uec = U_ZERO_ERROR; *is_temp = 1; ALLOC_HASHTABLE(debug_info); zend_hash_init(debug_info, 8, NULL, ZVAL_PTR_DTOR, 0); to = Z_INTL_TIMEZONE_P(object); tz = to->utimezone; if (tz == NULL) { ZVAL_FALSE(&zv); zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv); return debug_info; } ZVAL_TRUE(&zv); zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv); tz->getID(ustr); intl_convert_utf16_to_utf8(&str, &str_len, ustr.getBuffer(), ustr.length(), &uec); if (U_FAILURE(uec)) { return debug_info; } ZVAL_STRINGL(&zv, str, str_len); zend_hash_str_update(debug_info, "id", sizeof("id") - 1, &zv); // TODO: avoid reallocation ??? efree(str); int32_t rawOffset, dstOffset; UDate now = Calendar::getNow(); tz->getOffset(now, FALSE, rawOffset, dstOffset, uec); if (U_FAILURE(uec)) { return debug_info; } ZVAL_LONG(&zv, (zend_long)rawOffset); zend_hash_str_update(debug_info,"rawOffset", sizeof("rawOffset") - 1, &zv); ZVAL_LONG(&zv, (zend_long)(rawOffset + dstOffset)); zend_hash_str_update(debug_info,"currentOffset", sizeof("currentOffset") - 1, &zv); return debug_info; }
/* {{{ get_debug_info handler for TimeZone */ static HashTable *TimeZone_get_debug_info(zval *object, int *is_temp) { zval zv; TimeZone_object *to; const TimeZone *tz; UnicodeString ustr; zend_string *u8str; HashTable *debug_info; UErrorCode uec = U_ZERO_ERROR; *is_temp = 1; debug_info = zend_new_array(8); to = Z_INTL_TIMEZONE_P(object); tz = to->utimezone; if (tz == NULL) { ZVAL_FALSE(&zv); zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv); return debug_info; } ZVAL_TRUE(&zv); zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv); tz->getID(ustr); u8str = intl_convert_utf16_to_utf8( ustr.getBuffer(), ustr.length(), &uec); if (!u8str) { return debug_info; } ZVAL_NEW_STR(&zv, u8str); zend_hash_str_update(debug_info, "id", sizeof("id") - 1, &zv); int32_t rawOffset, dstOffset; UDate now = Calendar::getNow(); tz->getOffset(now, FALSE, rawOffset, dstOffset, uec); if (U_FAILURE(uec)) { return debug_info; } ZVAL_LONG(&zv, (zend_long)rawOffset); zend_hash_str_update(debug_info,"rawOffset", sizeof("rawOffset") - 1, &zv); ZVAL_LONG(&zv, (zend_long)(rawOffset + dstOffset)); zend_hash_str_update(debug_info,"currentOffset", sizeof("currentOffset") - 1, &zv); return debug_info; }
/* {{{ clone handler for TimeZone */ static zend_object *TimeZone_clone_obj(zval *object) { TimeZone_object *to_orig, *to_new; zend_object *ret_val; intl_error_reset(NULL); to_orig = Z_INTL_TIMEZONE_P(object); intl_error_reset(TIMEZONE_ERROR_P(to_orig)); ret_val = TimeZone_ce_ptr->create_object(Z_OBJCE_P(object)); to_new = php_intl_timezone_fetch_object(ret_val); zend_objects_clone_members(&to_new->zo, &to_orig->zo); if (to_orig->utimezone != NULL) { TimeZone *newTimeZone; newTimeZone = to_orig->utimezone->clone(); to_new->should_delete = 1; if (!newTimeZone) { zend_string *err_msg; intl_errors_set_code(TIMEZONE_ERROR_P(to_orig), U_MEMORY_ALLOCATION_ERROR); intl_errors_set_custom_msg(TIMEZONE_ERROR_P(to_orig), "Could not clone IntlTimeZone", 0); err_msg = intl_error_get_message(TIMEZONE_ERROR_P(to_orig)); zend_throw_exception(NULL, err_msg->val, 0); zend_string_free(err_msg); } else { to_new->utimezone = newTimeZone; } } else { zend_throw_exception(NULL, "Cannot clone unconstructed IntlTimeZone", 0); } return ret_val; }
U_CFUNC PHP_FUNCTION(intltz_has_same_rules) { zval *other_object; TimeZone_object *other_to; TIMEZONE_METHOD_INIT_VARS; if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, TimeZone_ce_ptr, &other_object, TimeZone_ce_ptr) == FAILURE) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_has_same_rules: bad arguments", 0); RETURN_FALSE; } TIMEZONE_METHOD_FETCH_OBJECT; other_to = Z_INTL_TIMEZONE_P(other_object); if (other_to->utimezone == NULL) { intl_errors_set(&to->err, U_ILLEGAL_ARGUMENT_ERROR, "intltz_has_same_rules: The second IntlTimeZone is unconstructed", 0); RETURN_FALSE; } RETURN_BOOL(to->utimezone->hasSameRules(*other_to->utimezone)); }
/* {{{ timezone_process_timezone_argument * TimeZone argument processor. outside_error may be NULL (for static functions/constructors) */ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone, intl_error *outside_error, const char *func) { zval local_zv_tz; char *message = NULL; TimeZone *timeZone; if (zv_timezone == NULL || Z_TYPE_P(zv_timezone) == IS_NULL) { timelib_tzinfo *tzinfo = get_timezone_info(); ZVAL_STRING(&local_zv_tz, tzinfo->name); zv_timezone = &local_zv_tz; } else { ZVAL_NULL(&local_zv_tz); } if (Z_TYPE_P(zv_timezone) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zv_timezone), TimeZone_ce_ptr)) { TimeZone_object *to = Z_INTL_TIMEZONE_P(zv_timezone); if (to->utimezone == NULL) { spprintf(&message, 0, "%s: passed IntlTimeZone is not " "properly constructed", func); if (message) { intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1); efree(message); } zval_dtor(&local_zv_tz); return NULL; } timeZone = to->utimezone->clone(); if (timeZone == NULL) { spprintf(&message, 0, "%s: could not clone TimeZone", func); if (message) { intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1); efree(message); } zval_dtor(&local_zv_tz); return NULL; } } else if (Z_TYPE_P(zv_timezone) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zv_timezone), php_date_get_timezone_ce())) { php_timezone_obj *tzobj = Z_PHPTIMEZONE_P(zv_timezone); zval_dtor(&local_zv_tz); return timezone_convert_datetimezone(tzobj->type, tzobj, 0, outside_error, func); } else { UnicodeString id, gottenId; UErrorCode status = U_ZERO_ERROR; /* outside_error may be NULL */ convert_to_string_ex(zv_timezone); if (intl_stringFromChar(id, Z_STRVAL_P(zv_timezone), Z_STRLEN_P(zv_timezone), &status) == FAILURE) { spprintf(&message, 0, "%s: Time zone identifier given is not a " "valid UTF-8 string", func); if (message) { intl_errors_set(outside_error, status, message, 1); efree(message); } zval_dtor(&local_zv_tz); return NULL; } timeZone = TimeZone::createTimeZone(id); if (timeZone == NULL) { spprintf(&message, 0, "%s: could not create time zone", func); if (message) { intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1); efree(message); } zval_dtor(&local_zv_tz); return NULL; } if (timeZone->getID(gottenId) != id) { spprintf(&message, 0, "%s: no such time zone: '%s'", func, Z_STRVAL_P(zv_timezone)); if (message) { intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1); efree(message); } zval_dtor(&local_zv_tz); delete timeZone; return NULL; } } zval_dtor(&local_zv_tz); return timeZone; }