diff options
Diffstat (limited to 'ext/intl/calendar')
| -rw-r--r-- | ext/intl/calendar/calendar_class.cpp | 555 | ||||
| -rw-r--r-- | ext/intl/calendar/calendar_class.h | 70 | ||||
| -rw-r--r-- | ext/intl/calendar/calendar_methods.cpp | 1353 | ||||
| -rw-r--r-- | ext/intl/calendar/calendar_methods.h | 114 | ||||
| -rw-r--r-- | ext/intl/calendar/gregoriancalendar_methods.cpp | 255 | ||||
| -rw-r--r-- | ext/intl/calendar/gregoriancalendar_methods.h | 32 |
6 files changed, 2379 insertions, 0 deletions
diff --git a/ext/intl/calendar/calendar_class.cpp b/ext/intl/calendar/calendar_class.cpp new file mode 100644 index 0000000000..e13425097d --- /dev/null +++ b/ext/intl/calendar/calendar_class.cpp @@ -0,0 +1,555 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | 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: | + | http://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: Gustavo Lopes <cataphract@php.net> | + +----------------------------------------------------------------------+ +*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "../intl_cppshims.h" + +#include <unicode/calendar.h> +#include <unicode/gregocal.h> + +extern "C" { +#define USE_TIMEZONE_POINTER 1 +#include "../timezone/timezone_class.h" +#define USE_CALENDAR_POINTER 1 +#include "calendar_class.h" +#include "calendar_methods.h" +#include "gregoriancalendar_methods.h" +#include <zend_exceptions.h> +#include <assert.h> +} + +/* {{{ Global variables */ +zend_class_entry *Calendar_ce_ptr; +zend_class_entry *GregorianCalendar_ce_ptr; +zend_object_handlers Calendar_handlers; +/* }}} */ + +U_CFUNC void calendar_object_create(zval *object, + Calendar *calendar TSRMLS_DC) +{ + UClassID classId = calendar->getDynamicClassID(); + zend_class_entry *ce; + + //if (dynamic_cast<GregorianCalendar*>(calendar) != NULL) { + if (classId == GregorianCalendar::getStaticClassID()) { + ce = GregorianCalendar_ce_ptr; + } else { + ce = Calendar_ce_ptr; + } + + object_init_ex(object, ce); + calendar_object_construct(object, calendar TSRMLS_CC); +} + +U_CFUNC Calendar *calendar_fetch_native_calendar(zval *object TSRMLS_DC) +{ + Calendar_object *co = (Calendar_object*) + zend_object_store_get_object(object TSRMLS_CC); + + return co->ucal; +} + +U_CFUNC void calendar_object_construct(zval *object, + Calendar *calendar TSRMLS_DC) +{ + Calendar_object *co; + + CALENDAR_METHOD_FETCH_OBJECT_NO_CHECK; //populate to from object + assert(co->ucal == NULL); + co->ucal = (Calendar*)calendar; +} + +/* {{{ clone handler for Calendar */ +static zend_object_value Calendar_clone_obj(zval *object TSRMLS_DC) +{ + Calendar_object *co_orig, + *co_new; + zend_object_value ret_val; + intl_error_reset(NULL TSRMLS_CC); + + co_orig = (Calendar_object*)zend_object_store_get_object(object TSRMLS_CC); + intl_error_reset(INTL_DATA_ERROR_P(co_orig) TSRMLS_CC); + + ret_val = Calendar_ce_ptr->create_object(Z_OBJCE_P(object) TSRMLS_CC); + co_new = (Calendar_object*)zend_object_store_get_object_by_handle(ret_val.handle TSRMLS_CC); + + zend_objects_clone_members(&co_new->zo, ret_val, + &co_orig->zo, Z_OBJ_HANDLE_P(object) TSRMLS_CC); + + if (co_orig->ucal != NULL) { + Calendar *newCalendar; + + newCalendar = co_orig->ucal->clone(); + if (!newCalendar) { + char *err_msg; + intl_errors_set_code(CALENDAR_ERROR_P(co_orig), + U_MEMORY_ALLOCATION_ERROR TSRMLS_CC); + intl_errors_set_custom_msg(CALENDAR_ERROR_P(co_orig), + "Could not clone IntlCalendar", 0 TSRMLS_CC); + err_msg = intl_error_get_message(CALENDAR_ERROR_P(co_orig) TSRMLS_CC); + zend_throw_exception(NULL, err_msg, 0 TSRMLS_CC); + efree(err_msg); + } else { + co_new->ucal = newCalendar; + } + } else { + zend_throw_exception(NULL, "Cannot clone unconstructed IntlCalendar", 0 TSRMLS_CC); + } + + return ret_val; +} +/* }}} */ + +static const struct { + UCalendarDateFields field; + const char *name; +} debug_info_fields[] = { + {UCAL_ERA, "era"}, + {UCAL_YEAR, "year"}, + {UCAL_MONTH, "month"}, + {UCAL_WEEK_OF_YEAR, "week of year"}, + {UCAL_WEEK_OF_MONTH, "week of month"}, + {UCAL_DAY_OF_YEAR, "day of year"}, + {UCAL_DAY_OF_MONTH, "day of month"}, + {UCAL_DAY_OF_WEEK, "day of week"}, + {UCAL_DAY_OF_WEEK_IN_MONTH, "day of week in month"}, + {UCAL_AM_PM, "AM/PM"}, + {UCAL_HOUR, "hour"}, + {UCAL_HOUR_OF_DAY, "hour of day"}, + {UCAL_MINUTE, "minute"}, + {UCAL_SECOND, "second"}, + {UCAL_MILLISECOND, "millisecond"}, + {UCAL_ZONE_OFFSET, "zone offset"}, + {UCAL_DST_OFFSET, "DST offset"}, + {UCAL_YEAR_WOY, "year for week of year"}, + {UCAL_DOW_LOCAL, "localized day of week"}, + {UCAL_EXTENDED_YEAR, "extended year"}, + {UCAL_JULIAN_DAY, "julian day"}, + {UCAL_MILLISECONDS_IN_DAY, "milliseconds in day"}, + {UCAL_IS_LEAP_MONTH, "is leap month"}, +}; + +/* {{{ get_debug_info handler for Calendar */ +static HashTable *Calendar_get_debug_info(zval *object, int *is_temp TSRMLS_DC) +{ + zval zv = zval_used_for_init, + *zfields; + Calendar_object *co; + const Calendar *cal; + + *is_temp = 1; + + array_init_size(&zv, 8); + + co = (Calendar_object*)zend_object_store_get_object(object TSRMLS_CC); + cal = co->ucal; + + if (cal == NULL) { + add_assoc_bool_ex(&zv, "valid", sizeof("valid"), 0); + return Z_ARRVAL(zv); + } + + add_assoc_bool_ex(&zv, "valid", sizeof("valid"), 1); + + add_assoc_string_ex(&zv, "type", sizeof("type"), + const_cast<char*>(cal->getType()), 1); + + { + zval ztz = zval_used_for_init, + *ztz_debug; + int is_tmp; + HashTable *debug_info; + + timezone_object_construct(&cal->getTimeZone(), &ztz , 0 TSRMLS_CC); + debug_info = Z_OBJ_HANDLER(ztz, get_debug_info)(&ztz, &is_tmp TSRMLS_CC); + assert(is_tmp == 1); + + ALLOC_INIT_ZVAL(ztz_debug); + Z_TYPE_P(ztz_debug) = IS_ARRAY; + Z_ARRVAL_P(ztz_debug) = debug_info; + add_assoc_zval_ex(&zv, "timeZone", sizeof("timeZone"), ztz_debug); + } + + { + UErrorCode uec = U_ZERO_ERROR; + Locale locale = cal->getLocale(ULOC_VALID_LOCALE, uec); + if (U_SUCCESS(uec)) { + add_assoc_string_ex(&zv, "locale", sizeof("locale"), + const_cast<char*>(locale.getName()), 1); + } else { + add_assoc_string_ex(&zv, "locale", sizeof("locale"), + const_cast<char*>(u_errorName(uec)), 1); + } + } + + ALLOC_INIT_ZVAL(zfields); + array_init_size(zfields, UCAL_FIELD_COUNT); + + for (int i = 0; + i < sizeof(debug_info_fields) / sizeof(*debug_info_fields); + i++) { + UErrorCode uec = U_ZERO_ERROR; + const char *name = debug_info_fields[i].name; + int32_t res = cal->get(debug_info_fields[i].field, uec); + if (U_SUCCESS(uec)) { + add_assoc_long(zfields, name, (long)res); + } else { + add_assoc_string(zfields, name, const_cast<char*>(u_errorName(uec)), 1); + } + } + + add_assoc_zval_ex(&zv, "fields", sizeof("fields"), zfields); + + return Z_ARRVAL(zv); +} +/* }}} */ + +/* {{{ void calendar_object_init(Calendar_object* to) + * Initialize internals of Calendar_object not specific to zend standard objects. + */ +static void calendar_object_init(Calendar_object *co TSRMLS_DC) +{ + intl_error_init(CALENDAR_ERROR_P(co) TSRMLS_CC); + co->ucal = NULL; +} +/* }}} */ + +/* {{{ Calendar_objects_dtor */ +static void Calendar_objects_dtor(void *object, + zend_object_handle handle TSRMLS_DC) +{ + zend_objects_destroy_object((zend_object*)object, handle TSRMLS_CC); +} +/* }}} */ + +/* {{{ Calendar_objects_free */ +static void Calendar_objects_free(zend_object *object TSRMLS_DC) +{ + Calendar_object* co = (Calendar_object*) object; + + if (co->ucal) { + delete co->ucal; + co->ucal = NULL; + } + intl_error_reset(CALENDAR_ERROR_P(co) TSRMLS_CC); + + zend_object_std_dtor(&co->zo TSRMLS_CC); + + efree(co); +} +/* }}} */ + +/* {{{ Calendar_object_create */ +static zend_object_value Calendar_object_create(zend_class_entry *ce TSRMLS_DC) +{ + zend_object_value retval; + Calendar_object* intern; + + intern = (Calendar_object*)ecalloc(1, sizeof(Calendar_object)); + + zend_object_std_init(&intern->zo, ce TSRMLS_CC); +#if PHP_VERSION_ID < 50399 + zend_hash_copy(intern->zo.properties, &(ce->default_properties), + (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval*)); +#else + object_properties_init((zend_object*) intern, ce); +#endif + calendar_object_init(intern TSRMLS_CC); + + retval.handle = zend_objects_store_put( + intern, + Calendar_objects_dtor, + (zend_objects_free_object_storage_t) Calendar_objects_free, + NULL TSRMLS_CC); + + retval.handlers = &Calendar_handlers; + + return retval; +} +/* }}} */ + +/* {{{ Calendar methods arguments info */ + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_void, 0, 0, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_field, 0, 0, 1) + ZEND_ARG_INFO(0, field) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_dow, 0, 0, 1) + ZEND_ARG_INFO(0, dayOfWeek) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_other_cal, 0, 0, 1) + ZEND_ARG_OBJ_INFO(0, calendar, IntlCalendar, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_date, 0, 0, 1) + ZEND_ARG_INFO(0, date) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_date_optional, 0, 0, 0) + ZEND_ARG_INFO(0, date) +ZEND_END_ARG_INFO() + + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_createInstance, 0, 0, 0) + ZEND_ARG_INFO(0, timeZone) + ZEND_ARG_INFO(0, locale) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_get_keyword_values_for_locale, 0, 0, 3) + ZEND_ARG_INFO(0, key) + ZEND_ARG_INFO(0, locale) + ZEND_ARG_INFO(0, commonlyUsed) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_add, 0, 0, 2) + ZEND_ARG_INFO(0, field) + ZEND_ARG_INFO(0, amount) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_setTimeZone, 0, 0, 1) + ZEND_ARG_INFO(0, timeZone) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_set, 0, 0, 2) + ZEND_ARG_INFO(0, fieldOrYear) + ZEND_ARG_INFO(0, valueOrMonth) + ZEND_ARG_INFO(0, dayOfMonth) + ZEND_ARG_INFO(0, hour) + ZEND_ARG_INFO(0, minute) + ZEND_ARG_INFO(0, second) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_roll, 0, 0, 2) + ZEND_ARG_INFO(0, field) + ZEND_ARG_INFO(0, amountOrUpOrDown) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_clear, 0, 0, 0) + ZEND_ARG_INFO(0, field) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_field_difference, 0, 0, 2) + ZEND_ARG_INFO(0, when) + ZEND_ARG_INFO(0, field) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_get_locale, 0, 0, 1) + ZEND_ARG_INFO(0, localeType) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_setLenient, 0, 0, 1) + ZEND_ARG_INFO(0, isLenient) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_set_minimal_days_in_first_week, 0, 0, 1) + ZEND_ARG_INFO(0, numberOfDays) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_from_date_time, 0, 0, 1) + ZEND_ARG_INFO(0, dateTime) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_cal_wall_time_option, 0, 0, 1) + ZEND_ARG_INFO(0, wallTimeOption) +ZEND_END_ARG_INFO() + +/* Gregorian Calendar */ +ZEND_BEGIN_ARG_INFO_EX(ainfo_gregcal___construct, 0, 0, 0) + ZEND_ARG_INFO(0, timeZoneOrYear) + ZEND_ARG_INFO(0, localeOrMonth) + ZEND_ARG_INFO(0, dayOfMonth) + ZEND_ARG_INFO(0, hour) + ZEND_ARG_INFO(0, minute) + ZEND_ARG_INFO(0, second) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(ainfo_gregcal_isLeapYear, 0, 0, 1) + ZEND_ARG_INFO(0, year) +ZEND_END_ARG_INFO() + +/* }}} */ + +/* {{{ Calendar_class_functions + * Every 'IntlCalendar' class method has an entry in this table + */ +static const zend_function_entry Calendar_class_functions[] = { + PHP_ME(IntlCalendar, __construct, ainfo_cal_void, ZEND_ACC_PRIVATE) + PHP_ME_MAPPING(createInstance, intlcal_create_instance, ainfo_cal_createInstance, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC) +#if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 42 + PHP_ME_MAPPING(getKeywordValuesForLocale, intlcal_get_keyword_values_for_locale, ainfo_cal_get_keyword_values_for_locale, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC) +#endif + PHP_ME_MAPPING(getNow, intlcal_get_now, ainfo_cal_void, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getAvailableLocales, intlcal_get_available_locales, ainfo_cal_void, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(get, intlcal_get, ainfo_cal_field, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getTime, intlcal_get_time, ainfo_cal_void, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(setTime, intlcal_set_time, ainfo_cal_date, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(add, intlcal_add, ainfo_cal_add, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(setTimeZone, intlcal_set_time_zone, ainfo_cal_setTimeZone, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(after, intlcal_after, ainfo_cal_other_cal, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(before, intlcal_before, ainfo_cal_other_cal, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(set, intlcal_set, ainfo_cal_set, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(roll, intlcal_roll, ainfo_cal_roll, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(clear, intlcal_clear, ainfo_cal_clear, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(fieldDifference, intlcal_field_difference, ainfo_cal_field_difference, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getActualMaximum, intlcal_get_actual_maximum, ainfo_cal_field, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getActualMinimum, intlcal_get_actual_minimum, ainfo_cal_field, ZEND_ACC_PUBLIC) +#if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 44 + PHP_ME_MAPPING(getDayOfWeekType, intlcal_get_day_of_week_type, ainfo_cal_dow, ZEND_ACC_PUBLIC) +#endif + PHP_ME_MAPPING(getFirstDayOfWeek, intlcal_get_first_day_of_week, ainfo_cal_void, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getGreatestMinimum, intlcal_get_greatest_minimum, ainfo_cal_field, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getLeastMaximum, intlcal_get_least_maximum, ainfo_cal_field, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getLocale, intlcal_get_locale, ainfo_cal_get_locale, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getMaximum, intlcal_get_maximum, ainfo_cal_field, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getMinimalDaysInFirstWeek, intlcal_get_minimal_days_in_first_week, ainfo_cal_void, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getMinimum, intlcal_get_minimum, ainfo_cal_field, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getTimeZone, intlcal_get_time_zone, ainfo_cal_void, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getType, intlcal_get_type, ainfo_cal_void, ZEND_ACC_PUBLIC) +#if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 44 + PHP_ME_MAPPING(getWeekendTransition,intlcal_get_weekend_transition, ainfo_cal_dow, ZEND_ACC_PUBLIC) +#endif + PHP_ME_MAPPING(inDaylightTime, intlcal_in_daylight_time, ainfo_cal_void, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(isEquivalentTo, intlcal_is_equivalent_to, ainfo_cal_other_cal, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(isLenient, intlcal_is_lenient, ainfo_cal_void, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(isSet, intlcal_is_set, ainfo_cal_field, ZEND_ACC_PUBLIC) +#if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 44 + PHP_ME_MAPPING(isWeekend, intlcal_is_weekend, ainfo_cal_date_optional, ZEND_ACC_PUBLIC) +#endif + PHP_ME_MAPPING(setFirstDayOfWeek, intlcal_set_first_day_of_week, ainfo_cal_dow, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(setLenient, intlcal_set_lenient, ainfo_cal_setLenient, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(setMinimalDaysInFirstWeek,intlcal_set_minimal_days_in_first_week,ainfo_cal_set_minimal_days_in_first_week,ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(equals, intlcal_equals, ainfo_cal_other_cal, ZEND_ACC_PUBLIC) +#if U_ICU_VERSION_MAJOR_NUM >= 49 + PHP_ME_MAPPING(getRepeatedWallTimeOption,intlcal_get_repeated_wall_time_option,ainfo_cal_void, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getSkippedWallTimeOption,intlcal_get_skipped_wall_time_option,ainfo_cal_void, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(setRepeatedWallTimeOption,intlcal_set_repeated_wall_time_option,ainfo_cal_wall_time_option,ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(setSkippedWallTimeOption,intlcal_set_skipped_wall_time_option,ainfo_cal_wall_time_option,ZEND_ACC_PUBLIC) +#endif + PHP_ME_MAPPING(fromDateTime, intlcal_from_date_time, ainfo_cal_from_date_time, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(toDateTime, intlcal_to_date_time, ainfo_cal_void, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getErrorCode, intlcal_get_error_code, ainfo_cal_void, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getErrorMessage, intlcal_get_error_message, ainfo_cal_void, ZEND_ACC_PUBLIC) + PHP_FE_END +}; +/* }}} */ + +/* {{{ GregorianCalendar_class_functions + */ +static const zend_function_entry GregorianCalendar_class_functions[] = { + PHP_ME(IntlGregorianCalendar, __construct, ainfo_gregcal___construct, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(setGregorianChange, intlgregcal_set_gregorian_change, ainfo_cal_date, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(getGregorianChange, intlgregcal_get_gregorian_change, ainfo_cal_void, ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(isLeapYear, intlgregcal_is_leap_year, ainfo_gregcal_isLeapYear, ZEND_ACC_PUBLIC) + PHP_FE_END +}; +/* }}} */ + + +/* {{{ calendar_register_IntlCalendar_class + * Initialize 'IntlCalendar' class + */ +void calendar_register_IntlCalendar_class(TSRMLS_D) +{ + zend_class_entry ce; + + /* Create and register 'IntlCalendar' class. */ + INIT_CLASS_ENTRY(ce, "IntlCalendar", Calendar_class_functions); + ce.create_object = Calendar_object_create; + Calendar_ce_ptr = zend_register_internal_class(&ce TSRMLS_CC); + if (!Calendar_ce_ptr) { + //can't happen now without bigger problems before + php_error_docref0(NULL TSRMLS_CC, E_ERROR, + "IntlCalendar: class registration has failed."); + return; + } + memcpy( &Calendar_handlers, zend_get_std_object_handlers(), + sizeof Calendar_handlers); + Calendar_handlers.clone_obj = Calendar_clone_obj; + Calendar_handlers.get_debug_info = Calendar_get_debug_info; + + /* Create and register 'IntlGregorianCalendar' class. */ + INIT_CLASS_ENTRY(ce, "IntlGregorianCalendar", GregorianCalendar_class_functions); + GregorianCalendar_ce_ptr = zend_register_internal_class_ex(&ce, + Calendar_ce_ptr, NULL TSRMLS_CC); + if (!GregorianCalendar_ce_ptr) { + //can't happen know without bigger problems before + php_error_docref0(NULL TSRMLS_CC, E_ERROR, + "IntlGregorianCalendar: class registration has failed."); + return; + } + + /* Declare 'IntlCalendar' class constants */ +#define CALENDAR_DECL_LONG_CONST(name, val) \ + zend_declare_class_constant_long(Calendar_ce_ptr, name, sizeof(name) - 1, \ + val TSRMLS_CC) + + CALENDAR_DECL_LONG_CONST("FIELD_ERA", UCAL_ERA); + CALENDAR_DECL_LONG_CONST("FIELD_YEAR", UCAL_YEAR); + CALENDAR_DECL_LONG_CONST("FIELD_MONTH", UCAL_MONTH); + CALENDAR_DECL_LONG_CONST("FIELD_WEEK_OF_YEAR", UCAL_WEEK_OF_YEAR); + CALENDAR_DECL_LONG_CONST("FIELD_WEEK_OF_MONTH", UCAL_WEEK_OF_MONTH); + CALENDAR_DECL_LONG_CONST("FIELD_DATE", UCAL_DATE); + CALENDAR_DECL_LONG_CONST("FIELD_DAY_OF_YEAR", UCAL_DAY_OF_YEAR); + CALENDAR_DECL_LONG_CONST("FIELD_DAY_OF_WEEK", UCAL_DAY_OF_WEEK); + CALENDAR_DECL_LONG_CONST("FIELD_DAY_OF_WEEK_IN_MONTH", UCAL_DAY_OF_WEEK_IN_MONTH); + CALENDAR_DECL_LONG_CONST("FIELD_AM_PM", UCAL_AM_PM); + CALENDAR_DECL_LONG_CONST("FIELD_HOUR", UCAL_HOUR); + CALENDAR_DECL_LONG_CONST("FIELD_HOUR_OF_DAY", UCAL_HOUR_OF_DAY); + CALENDAR_DECL_LONG_CONST("FIELD_HOUR", UCAL_HOUR); + CALENDAR_DECL_LONG_CONST("FIELD_HOUR_OF_DAY", UCAL_HOUR_OF_DAY); + CALENDAR_DECL_LONG_CONST("FIELD_MINUTE", UCAL_MINUTE); + CALENDAR_DECL_LONG_CONST("FIELD_SECOND", UCAL_SECOND); + CALENDAR_DECL_LONG_CONST("FIELD_MILLISECOND", UCAL_MILLISECOND); + CALENDAR_DECL_LONG_CONST("FIELD_ZONE_OFFSET", UCAL_ZONE_OFFSET); + CALENDAR_DECL_LONG_CONST("FIELD_DST_OFFSET", UCAL_DST_OFFSET); + CALENDAR_DECL_LONG_CONST("FIELD_YEAR_WOY", UCAL_YEAR_WOY); + CALENDAR_DECL_LONG_CONST("FIELD_DOW_LOCAL", UCAL_DOW_LOCAL); + CALENDAR_DECL_LONG_CONST("FIELD_EXTENDED_YEAR", UCAL_EXTENDED_YEAR); + CALENDAR_DECL_LONG_CONST("FIELD_JULIAN_DAY", UCAL_JULIAN_DAY); + CALENDAR_DECL_LONG_CONST("FIELD_MILLISECONDS_IN_DAY", UCAL_MILLISECONDS_IN_DAY); + CALENDAR_DECL_LONG_CONST("FIELD_IS_LEAP_MONTH", UCAL_IS_LEAP_MONTH); + CALENDAR_DECL_LONG_CONST("FIELD_FIELD_COUNT", UCAL_FIELD_COUNT); + CALENDAR_DECL_LONG_CONST("FIELD_DAY_OF_MONTH", UCAL_DAY_OF_MONTH); + + CALENDAR_DECL_LONG_CONST("DOW_SUNDAY", UCAL_SUNDAY); + CALENDAR_DECL_LONG_CONST("DOW_MONDAY", UCAL_MONDAY); + CALENDAR_DECL_LONG_CONST("DOW_TUESDAY", UCAL_TUESDAY); + CALENDAR_DECL_LONG_CONST("DOW_WEDNESDAY", UCAL_WEDNESDAY); + CALENDAR_DECL_LONG_CONST("DOW_THURSDAY", UCAL_THURSDAY); + CALENDAR_DECL_LONG_CONST("DOW_FRIDAY", UCAL_FRIDAY); + CALENDAR_DECL_LONG_CONST("DOW_SATURDAY", UCAL_SATURDAY); + +#if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 44 + CALENDAR_DECL_LONG_CONST("DOW_TYPE_WEEKDAY", UCAL_WEEKDAY); + CALENDAR_DECL_LONG_CONST("DOW_TYPE_WEEKEND", UCAL_WEEKEND); + CALENDAR_DECL_LONG_CONST("DOW_TYPE_WEEKEND_OFFSET", UCAL_WEEKEND_ONSET); + CALENDAR_DECL_LONG_CONST("DOW_TYPE_WEEKEND_CEASE", UCAL_WEEKEND_CEASE); +#endif + +#if U_ICU_VERSION_MAJOR_NUM >= 49 + CALENDAR_DECL_LONG_CONST("WALLTIME_FIRST", UCAL_WALLTIME_FIRST); + CALENDAR_DECL_LONG_CONST("WALLTIME_LAST", UCAL_WALLTIME_LAST); + CALENDAR_DECL_LONG_CONST("WALLTIME_NEXT_VALID", UCAL_WALLTIME_NEXT_VALID); +#endif +} +/* }}} */ diff --git a/ext/intl/calendar/calendar_class.h b/ext/intl/calendar/calendar_class.h new file mode 100644 index 0000000000..140389b639 --- /dev/null +++ b/ext/intl/calendar/calendar_class.h @@ -0,0 +1,70 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | 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: | + | http://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: Gustavo Lopes <cataphract@php.net> | + +----------------------------------------------------------------------+ + */ + +#ifndef CALENDAR_CLASS_H +#define CALENDAR_CLASS_H + +//redefinition of inline in PHP headers causes problems, so include this before +#include <math.h> + +#include <php.h> +#include "intl_error.h" +#include "intl_data.h" + +#ifndef USE_CALENDAR_POINTER +typedef void Calendar; +#endif + +typedef struct { + zend_object zo; + + // error handling + intl_error err; + + // ICU calendar + Calendar* ucal; +} Calendar_object; + +#define CALENDAR_ERROR(co) (co)->err +#define CALENDAR_ERROR_P(co) &(CALENDAR_ERROR(co)) + +#define CALENDAR_ERROR_CODE(co) INTL_ERROR_CODE(CALENDAR_ERROR(co)) +#define CALENDAR_ERROR_CODE_P(co) &(INTL_ERROR_CODE(CALENDAR_ERROR(co))) + +#define CALENDAR_METHOD_INIT_VARS INTL_METHOD_INIT_VARS(Calendar, co) +#define CALENDAR_METHOD_FETCH_OBJECT_NO_CHECK INTL_METHOD_FETCH_OBJECT(Calendar, co) +#define CALENDAR_METHOD_FETCH_OBJECT \ + CALENDAR_METHOD_FETCH_OBJECT_NO_CHECK; \ + if (co->ucal == NULL) \ + { \ + intl_errors_set(&co->err, U_ILLEGAL_ARGUMENT_ERROR, "Found unconstructed IntlCalendar", 0 TSRMLS_CC); \ + RETURN_FALSE; \ + } + +void calendar_object_create(zval *object, Calendar *calendar TSRMLS_DC); + +Calendar *calendar_fetch_native_calendar(zval *object TSRMLS_DC); + +void calendar_object_construct(zval *object, Calendar *calendar TSRMLS_DC); + +void calendar_register_IntlCalendar_class(TSRMLS_D); + +extern zend_class_entry *Calendar_ce_ptr, + *GregorianCalendar_ce_ptr; + +extern zend_object_handlers Calendar_handlers; + +#endif /* #ifndef CALENDAR_CLASS_H */ diff --git a/ext/intl/calendar/calendar_methods.cpp b/ext/intl/calendar/calendar_methods.cpp new file mode 100644 index 0000000000..ae7d0093f7 --- /dev/null +++ b/ext/intl/calendar/calendar_methods.cpp @@ -0,0 +1,1353 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | 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: | + | http://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: Gustavo Lopes <cataphract@php.net> | + +----------------------------------------------------------------------+ +*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "../intl_cppshims.h" + +#include <unicode/locid.h> +#include <unicode/calendar.h> +#include <unicode/ustring.h> + +#include "../intl_convertcpp.h" +#include "../common/common_date.h" + +extern "C" { +#include "../php_intl.h" +#define USE_TIMEZONE_POINTER 1 +#include "../timezone/timezone_class.h" +#define USE_CALENDAR_POINTER 1 +#include "calendar_class.h" +#include "../intl_convert.h" +#include <zend_exceptions.h> +#include <zend_interfaces.h> +#include <ext/date/php_date.h> +} +#include "../common/common_enum.h" + +U_CFUNC PHP_METHOD(IntlCalendar, __construct) +{ + zend_throw_exception( NULL, + "An object of this type cannot be created with the new operator", + 0 TSRMLS_CC ); +} + +U_CFUNC PHP_FUNCTION(intlcal_create_instance) +{ + zval **zv_timezone = NULL; + const char *locale_str = NULL; + int dummy; + TimeZone *timeZone; + UErrorCode status = U_ZERO_ERROR; + intl_error_reset(NULL TSRMLS_CC); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|Zs!", + &zv_timezone, &locale_str, &dummy) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_create_calendar: bad arguments", 0 TSRMLS_CC); + RETURN_NULL(); + } + + timeZone = timezone_process_timezone_argument(zv_timezone, NULL, + "intlcal_create_instance" TSRMLS_CC); + if (timeZone == NULL) { + RETURN_NULL(); + } + + if (!locale_str) { + locale_str = intl_locale_get_default(TSRMLS_C); + } + + Calendar *cal = Calendar::createInstance(timeZone, + Locale::createFromName(locale_str), status); + if (cal == NULL) { + delete timeZone; + intl_error_set(NULL, status, "Error creating ICU Calendar object", 0 TSRMLS_CC); + RETURN_NULL(); + } + + calendar_object_create(return_value, cal TSRMLS_CC); +} + +#if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 42 +class BugStringCharEnumeration : public StringEnumeration +{ +public: + BugStringCharEnumeration(UEnumeration* _uenum) : uenum(_uenum) {} + + ~BugStringCharEnumeration() + { + uenum_close(uenum); + } + + int32_t count(UErrorCode& status) const { + return uenum_count(uenum, &status); + } + + virtual const UnicodeString* snext(UErrorCode& status) + { + int32_t length; + const UChar* str = uenum_unext(uenum, &length, &status); + if (str == 0 || U_FAILURE(status)) { + return 0; + } + return &unistr.setTo(str, length); + } + + virtual const char* next(int32_t *resultLength, UErrorCode &status) + { + int32_t length = -1; + const char* str = uenum_next(uenum, &length, &status); + if (str == 0 || U_FAILURE(status)) { + return 0; + } + if (resultLength) { + //the bug is that uenum_next doesn't set the length + *resultLength = (length == -1) ? strlen(str) : length; + } + + return str; + } + + void reset(UErrorCode& status) + { + uenum_reset(uenum, &status); + } + + virtual UClassID getDynamicClassID() const; + + static UClassID U_EXPORT2 getStaticClassID(); + +private: + UEnumeration *uenum; +}; +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(BugStringCharEnumeration) + +U_CFUNC PHP_FUNCTION(intlcal_get_keyword_values_for_locale) +{ + UErrorCode status = U_ZERO_ERROR; + char *key, + *locale; + int key_len, + locale_len; + zend_bool commonly_used; + intl_error_reset(NULL TSRMLS_CC); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssb", + &key, &key_len, &locale, &locale_len, &commonly_used) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_keyword_values_for_locale: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + //does not work; see ICU bug 9194 +#if 0 + StringEnumeration *se = Calendar::getKeywordValuesForLocale(key, + Locale::createFromName(locale), (UBool)commonly_used, + status); + if (se == NULL) { + intl_error_set(NULL, status, "intlcal_get_keyword_values_for_locale: " + "error calling underlying method", 0 TSRMLS_CC); + RETURN_FALSE; + } +#else + UEnumeration *uenum = ucal_getKeywordValuesForLocale( + key, locale, !!commonly_used, &status); + if (U_FAILURE(status)) { + uenum_close(uenum); + intl_error_set(NULL, status, "intlcal_get_keyword_values_for_locale: " + "error calling underlying method", 0 TSRMLS_CC); + RETURN_FALSE; + } + + StringEnumeration *se = new BugStringCharEnumeration(uenum); +#endif + + IntlIterator_from_StringEnumeration(se, return_value TSRMLS_CC); +} +#endif //ICU 4.2 only + +U_CFUNC PHP_FUNCTION(intlcal_get_now) +{ + intl_error_reset(NULL TSRMLS_CC); + + if (zend_parse_parameters_none() == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_now: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + RETURN_DOUBLE((double)Calendar::getNow()); +} + +U_CFUNC PHP_FUNCTION(intlcal_get_available_locales) +{ + intl_error_reset(NULL TSRMLS_CC); + + if (zend_parse_parameters_none() == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_available_locales: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + int32_t count; + const Locale *availLocales = Calendar::getAvailableLocales(count); + array_init(return_value); + for (int i = 0; i < count; i++) { + Locale locale = availLocales[i]; + add_next_index_string(return_value, locale.getName(), 1); + } +} + +static void _php_intlcal_field_uec_ret_in32t_method( + int32_t (Calendar::*func)(UCalendarDateFields, UErrorCode&) const, + const char *method_name, + INTERNAL_FUNCTION_PARAMETERS) +{ + long field; + char *message; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Ol", &object, Calendar_ce_ptr, &field) == FAILURE) { + spprintf(&message, 0, "%s: bad arguments", method_name); + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, message, 1 TSRMLS_CC); + efree(message); + RETURN_FALSE; + } + + if (field < 0 || field >= UCAL_FIELD_COUNT) { + spprintf(&message, 0, "%s: invalid field", method_name); + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, message, 1 TSRMLS_CC); + efree(message); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + int32_t result = (co->ucal->*func)( + (UCalendarDateFields)field, CALENDAR_ERROR_CODE(co)); + INTL_METHOD_CHECK_STATUS(co, "Call to ICU method has failed"); + + RETURN_LONG((long)result); +} + +U_CFUNC PHP_FUNCTION(intlcal_get) +{ + _php_intlcal_field_uec_ret_in32t_method(&Calendar::get, + "intlcal_get", INTERNAL_FUNCTION_PARAM_PASSTHRU); +} + +U_CFUNC PHP_FUNCTION(intlcal_get_time) +{ + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", + &object, Calendar_ce_ptr) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_time: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + UDate result = co->ucal->getTime(CALENDAR_ERROR_CODE(co)); + INTL_METHOD_CHECK_STATUS(co, + "intlcal_get_time: error calling ICU Calendar::getTime"); + + RETURN_DOUBLE((double)result); +} + +U_CFUNC PHP_FUNCTION(intlcal_set_time) +{ + double time_arg; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Od", + &object, Calendar_ce_ptr, &time_arg) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set_time: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + co->ucal->setTime((UDate)time_arg, CALENDAR_ERROR_CODE(co)); + INTL_METHOD_CHECK_STATUS(co, "Call to underlying method failed"); + + RETURN_TRUE; +} + +U_CFUNC PHP_FUNCTION(intlcal_add) +{ + long field, + amount; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Oll", &object, Calendar_ce_ptr, &field, &amount) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_add: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + if (field < 0 || field >= UCAL_FIELD_COUNT) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_add: invalid field", 0 TSRMLS_CC); + RETURN_FALSE; + } + if (amount < INT32_MIN || amount > INT32_MAX) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_add: amount out of bounds", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + co->ucal->add((UCalendarDateFields)field, (int32_t)amount, CALENDAR_ERROR_CODE(co)); + INTL_METHOD_CHECK_STATUS(co, "intlcal_add: Call to underlying method failed"); + + RETURN_TRUE; +} + +U_CFUNC PHP_FUNCTION(intlcal_set_time_zone) +{ + zval *zv_timezone; + TimeZone *timeZone; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Oz!", &object, Calendar_ce_ptr, &zv_timezone) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set_time_zone: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + if (zv_timezone == NULL) { + RETURN_TRUE; /* the method does nothing if passed null */ + } + + timeZone = timezone_process_timezone_argument(&zv_timezone, + CALENDAR_ERROR_P(co), "intlcal_set_time_zone" TSRMLS_CC); + if (timeZone == NULL) { + RETURN_FALSE; + } + + co->ucal->adoptTimeZone(timeZone); + + RETURN_TRUE; +} + + +static void _php_intlcal_before_after( + UBool (Calendar::*func)(const Calendar&, UErrorCode&) const, + INTERNAL_FUNCTION_PARAMETERS) +{ + zval *when_object; + Calendar_object *when_co; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "OO", &object, Calendar_ce_ptr, &when_object, Calendar_ce_ptr) + == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_before/after: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + when_co = static_cast<Calendar_object*>( + zend_object_store_get_object(when_object TSRMLS_CC)); + if (when_co->ucal == NULL) { + intl_errors_set(&co->err, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_before/after: Other IntlCalendar was unconstructed", 0 TSRMLS_CC); + RETURN_FALSE; + } + + UBool res = (co->ucal->*func)(*when_co->ucal, CALENDAR_ERROR_CODE(co)); + INTL_METHOD_CHECK_STATUS(co, "intlcal_before/after: Error calling ICU method"); + + RETURN_BOOL((int)res); +} + +U_CFUNC PHP_FUNCTION(intlcal_after) +{ + _php_intlcal_before_after(&Calendar::after, INTERNAL_FUNCTION_PARAM_PASSTHRU); +} + +U_CFUNC PHP_FUNCTION(intlcal_before) +{ + _php_intlcal_before_after(&Calendar::before, INTERNAL_FUNCTION_PARAM_PASSTHRU); +} + +U_CFUNC PHP_FUNCTION(intlcal_set) +{ + long arg1, arg2, arg3, arg4, arg5, arg6; + zval **args_a[7] = {0}, + ***args = &args_a[0]; + int i; + int variant; /* number of args of the set() overload */ + CALENDAR_METHOD_INIT_VARS; + + /* must come before zpp because zpp would convert the args in the stack to 0 */ + if (ZEND_NUM_ARGS() > (getThis() ? 6 : 7) || + zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set: too many arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + if (!getThis()) { + args++; + } + variant = ZEND_NUM_ARGS() - (getThis() ? 0 : 1); + while (variant > 2 && Z_TYPE_PP(args[variant - 1]) == IS_NULL) { + variant--; + } + + if (variant == 4 || + zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Oll|llll", &object, Calendar_ce_ptr, &arg1, &arg2, &arg3, &arg4, + &arg5, &arg6) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + for (i = 0; i < variant; i++) { + if (Z_LVAL_PP(args[i]) < INT32_MIN || Z_LVAL_PP(args[i]) > INT32_MAX) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set: at least one of the arguments has an absolute " + "value that is too large", 0 TSRMLS_CC); + RETURN_FALSE; + } + } + + if (variant == 2 && (arg1 < 0 || arg1 >= UCAL_FIELD_COUNT)) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set: invalid field", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + if (variant == 2) { + co->ucal->set((UCalendarDateFields)arg1, (int32_t)arg2); + } else if (variant == 3) { + co->ucal->set((int32_t)arg1, (int32_t)arg2, (int32_t)arg3); + } else if (variant == 5) { + co->ucal->set((int32_t)arg1, (int32_t)arg2, (int32_t)arg3, (int32_t)arg4, (int32_t)arg5); + } else if (variant == 6) { + co->ucal->set((int32_t)arg1, (int32_t)arg2, (int32_t)arg3, (int32_t)arg4, (int32_t)arg5, (int32_t)arg6); + } + + RETURN_TRUE; +} + +U_CFUNC PHP_FUNCTION(intlcal_roll) +{ + long field, + value; + zval **args_a[3] = {0}, + ***args = &args_a[0]; + zend_bool bool_variant_val = (zend_bool)-1; + CALENDAR_METHOD_INIT_VARS; + + if (ZEND_NUM_ARGS() > (getThis() ? 2 :3) || + zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set: too many arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + if (!getThis()) { + args++; + } + if (args[1] != NULL && Z_TYPE_PP(args[1]) == IS_BOOL) { + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Olb", &object, Calendar_ce_ptr, &field, &bool_variant_val) + == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_roll: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + bool_variant_val = Z_BVAL_PP(args[1]); + } else if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Oll", &object, Calendar_ce_ptr, &field, &value) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_roll: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + if (field < 0 || field >= UCAL_FIELD_COUNT) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_roll: invalid field", 0 TSRMLS_CC); + RETURN_FALSE; + } + if (bool_variant_val == (zend_bool)-1 && + (value < INT32_MIN || value > INT32_MAX)) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_roll: value out of bounds", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + if (bool_variant_val != (zend_bool)-1) { + co->ucal->roll((UCalendarDateFields)field, (UBool)bool_variant_val, + CALENDAR_ERROR_CODE(co)); + } else { + co->ucal->roll((UCalendarDateFields)field, (int32_t)value, + CALENDAR_ERROR_CODE(co)); + } + INTL_METHOD_CHECK_STATUS(co, "intlcal_roll: Error calling ICU Calendar::roll"); + + RETURN_TRUE; +} + +U_CFUNC PHP_FUNCTION(intlcal_clear) +{ + zval **args_a[2] = {0}, + ***args = &args_a[0]; + long field; + int variant; + CALENDAR_METHOD_INIT_VARS; + + if (ZEND_NUM_ARGS() > (getThis() ? 1 : 2) || + zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_clear: too many arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + if (!getThis()) { + args++; + } + if (args[0] == NULL || Z_TYPE_PP(args[0]) == IS_NULL) { + zval *dummy; /* we know it's null */ + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, + getThis(), "O|z", &object, Calendar_ce_ptr, &dummy) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_clear: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + variant = 0; + } else if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, + getThis(), "Ol", &object, Calendar_ce_ptr, &field) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_clear: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } else if (field < 0 || field >= UCAL_FIELD_COUNT) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_clear: invalid field", 0 TSRMLS_CC); + RETURN_FALSE; + } else { + variant = 1; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + if (variant == 0) { + co->ucal->clear(); + } else { + co->ucal->clear((UCalendarDateFields)field); + } + + RETURN_TRUE; +} + +U_CFUNC PHP_FUNCTION(intlcal_field_difference) +{ + long field; + double when; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Odl", &object, Calendar_ce_ptr, &when, &field) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_field_difference: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + if (field < 0 || field >= UCAL_FIELD_COUNT) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_field_difference: invalid field", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + int32_t result = co->ucal->fieldDifference((UDate)when, + (UCalendarDateFields)field, CALENDAR_ERROR_CODE(co)); + INTL_METHOD_CHECK_STATUS(co, + "intlcal_field_difference: Call to ICU method has failed"); + + RETURN_LONG((long)result); +} + +U_CFUNC PHP_FUNCTION(intlcal_get_actual_maximum) +{ + _php_intlcal_field_uec_ret_in32t_method(&Calendar::getActualMaximum, + "intlcal_get_actual_maximum", INTERNAL_FUNCTION_PARAM_PASSTHRU); +} + +U_CFUNC PHP_FUNCTION(intlcal_get_actual_minimum) +{ + _php_intlcal_field_uec_ret_in32t_method(&Calendar::getActualMinimum, + "intlcal_get_actual_minimum", INTERNAL_FUNCTION_PARAM_PASSTHRU); +} + +#if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 44 +U_CFUNC PHP_FUNCTION(intlcal_get_day_of_week_type) +{ + long dow; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Ol", &object, Calendar_ce_ptr, &dow) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_day_of_week_type: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + if (dow < UCAL_SUNDAY || dow > UCAL_SATURDAY) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_day_of_week_type: invalid day of week", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + int32_t result = co->ucal->getDayOfWeekType( + (UCalendarDaysOfWeek)dow, CALENDAR_ERROR_CODE(co)); + INTL_METHOD_CHECK_STATUS(co, + "intlcal_get_day_of_week_type: Call to ICU method has failed"); + + RETURN_LONG((long)result); +} +#endif + +U_CFUNC PHP_FUNCTION(intlcal_get_first_day_of_week) +{ + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "O", &object, Calendar_ce_ptr) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_first_day_of_week: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + int32_t result = co->ucal->getFirstDayOfWeek(CALENDAR_ERROR_CODE(co)); + INTL_METHOD_CHECK_STATUS(co, + "intlcal_get_first_day_of_week: Call to ICU method has failed"); + + RETURN_LONG((long)result); +} + +static void _php_intlcal_field_ret_in32t_method( + int32_t (Calendar::*func)(UCalendarDateFields) const, + const char *method_name, + INTERNAL_FUNCTION_PARAMETERS) +{ + long field; + char *message; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Ol", &object, Calendar_ce_ptr, &field) == FAILURE) { + spprintf(&message, 0, "%s: bad arguments", method_name); + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, message, 1 TSRMLS_CC); + efree(message); + RETURN_FALSE; + } + + if (field < 0 || field >= UCAL_FIELD_COUNT) { + spprintf(&message, 0, "%s: invalid field", method_name); + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, message, 1 TSRMLS_CC); + efree(message); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + int32_t result = (co->ucal->*func)((UCalendarDateFields)field); + INTL_METHOD_CHECK_STATUS(co, "Call to ICU method has failed"); + + RETURN_LONG((long)result); +} + +U_CFUNC PHP_FUNCTION(intlcal_get_greatest_minimum) +{ + _php_intlcal_field_ret_in32t_method(&Calendar::getGreatestMinimum, + "intlcal_get_greatest_minimum", INTERNAL_FUNCTION_PARAM_PASSTHRU); +} + +U_CFUNC PHP_FUNCTION(intlcal_get_least_maximum) +{ + _php_intlcal_field_ret_in32t_method(&Calendar::getLeastMaximum, + "intlcal_get_least_maximum", INTERNAL_FUNCTION_PARAM_PASSTHRU); +} + +U_CFUNC PHP_FUNCTION(intlcal_get_locale) +{ + long locale_type; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Ol", &object, Calendar_ce_ptr, &locale_type) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_locale: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + if (locale_type != ULOC_ACTUAL_LOCALE && locale_type != ULOC_VALID_LOCALE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_locale: invalid locale type", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + Locale locale = co->ucal->getLocale((ULocDataLocaleType)locale_type, + CALENDAR_ERROR_CODE(co)); + INTL_METHOD_CHECK_STATUS(co, + "intlcal_get_locale: Call to ICU method has failed"); + + RETURN_STRING(locale.getName(), 1); +} + +U_CFUNC PHP_FUNCTION(intlcal_get_maximum) +{ + _php_intlcal_field_ret_in32t_method(&Calendar::getMaximum, + "intlcal_get_maximum", INTERNAL_FUNCTION_PARAM_PASSTHRU); +} + +U_CFUNC PHP_FUNCTION(intlcal_get_minimal_days_in_first_week) +{ + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "O", &object, Calendar_ce_ptr) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_minimal_days_in_first_week: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + uint8_t result = co->ucal->getMinimalDaysInFirstWeek(); + INTL_METHOD_CHECK_STATUS(co, + "intlcal_get_first_day_of_week: Call to ICU method has failed"); + + RETURN_LONG((long)result); +} + +U_CFUNC PHP_FUNCTION(intlcal_get_minimum) +{ + _php_intlcal_field_ret_in32t_method(&Calendar::getMinimum, + "intlcal_get_minimum", INTERNAL_FUNCTION_PARAM_PASSTHRU); +} + +U_CFUNC PHP_FUNCTION(intlcal_get_time_zone) +{ + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "O", &object, Calendar_ce_ptr) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_time_zone: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + TimeZone *tz = co->ucal->getTimeZone().clone(); + if (tz == NULL) { + intl_errors_set(CALENDAR_ERROR_P(co), U_MEMORY_ALLOCATION_ERROR, + "intlcal_get_time_zone: could not clone TimeZone", 0 TSRMLS_CC); + RETURN_FALSE; + } + + timezone_object_construct(tz, return_value, 1 TSRMLS_CC); +} + +U_CFUNC PHP_FUNCTION(intlcal_get_type) +{ + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "O", &object, Calendar_ce_ptr) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_type: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + RETURN_STRING(co->ucal->getType(), 1); +} + +#if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 44 +U_CFUNC PHP_FUNCTION(intlcal_get_weekend_transition) +{ + long dow; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Ol", &object, Calendar_ce_ptr, &dow) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_weekend_transition: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + if (dow < UCAL_SUNDAY || dow > UCAL_SATURDAY) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_weekend_transition: invalid day of week", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + int32_t res = co->ucal->getWeekendTransition((UCalendarDaysOfWeek)dow, + CALENDAR_ERROR_CODE(co)); + INTL_METHOD_CHECK_STATUS(co, "intlcal_get_weekend_transition: " + "Error calling ICU method"); + + RETURN_LONG((long)res); +} +#endif + +U_CFUNC PHP_FUNCTION(intlcal_in_daylight_time) +{ + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "O", &object, Calendar_ce_ptr) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_in_daylight_time: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + UBool ret = co->ucal->inDaylightTime(CALENDAR_ERROR_CODE(co)); + INTL_METHOD_CHECK_STATUS(co, "intlcal_in_daylight_time: " + "Error calling ICU method"); + + RETURN_BOOL((int)ret); +} + +U_CFUNC PHP_FUNCTION(intlcal_is_equivalent_to) +{ + zval *other_object; + Calendar_object *other_co; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "OO", &object, Calendar_ce_ptr, &other_object, Calendar_ce_ptr) + == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_is_equivalent_to: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + other_co = (Calendar_object*)zend_object_store_get_object(other_object TSRMLS_CC); + if (other_co->ucal == NULL) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intlcal_is_equivalent_to:" + " Other IntlCalendar is unconstructed", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + RETURN_BOOL((int)co->ucal->isEquivalentTo(*other_co->ucal)); +} + +U_CFUNC PHP_FUNCTION(intlcal_is_lenient) +{ + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "O", &object, Calendar_ce_ptr) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_is_lenient: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + RETURN_BOOL((int)co->ucal->isLenient()); +} + +U_CFUNC PHP_FUNCTION(intlcal_is_set) +{ + long field; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Ol", &object, Calendar_ce_ptr, &field) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_is_set: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + if (field < 0 || field >= UCAL_FIELD_COUNT) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_is_set: invalid field", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + RETURN_BOOL((int)co->ucal->isSet((UCalendarDateFields)field)); +} + +#if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 44 +U_CFUNC PHP_FUNCTION(intlcal_is_weekend) +{ + double date; + zval *rawDate = NULL; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters_ex(ZEND_PARSE_PARAMS_QUIET, + ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "O|z!", &object, Calendar_ce_ptr, &rawDate) == FAILURE + || (rawDate != NULL && + zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "O|d", &object, Calendar_ce_ptr, &date) == FAILURE)) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_is_weekend: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + if (rawDate == NULL) { + RETURN_BOOL((int)co->ucal->isWeekend()); + } else { + UBool ret = co->ucal->isWeekend((UDate)date, CALENDAR_ERROR_CODE(co)); + INTL_METHOD_CHECK_STATUS(co, "intlcal_is_weekend: " + "Error calling ICU method"); + RETURN_BOOL((int)ret); + } +} +#endif + + +U_CFUNC PHP_FUNCTION(intlcal_set_first_day_of_week) +{ + long dow; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Ol", &object, Calendar_ce_ptr, &dow) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set_first_day_of_week: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + if (dow < UCAL_SUNDAY || dow > UCAL_SATURDAY) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set_first_day_of_week: invalid day of week", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + co->ucal->setFirstDayOfWeek((UCalendarDaysOfWeek)dow); + + RETURN_TRUE; +} + +U_CFUNC PHP_FUNCTION(intlcal_set_lenient) +{ + zend_bool is_lenient; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Ob", &object, Calendar_ce_ptr, &is_lenient) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set_lenient: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + co->ucal->setLenient((UBool) is_lenient); + + RETURN_TRUE; +} + +U_CFUNC PHP_FUNCTION(intlcal_set_minimal_days_in_first_week) +{ + long num_days; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Ol", &object, Calendar_ce_ptr, &num_days) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set_minimal_days_in_first_week: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + if (num_days < 1 || num_days > 7) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set_minimal_days_in_first_week: invalid number of days; " + "must be between 1 and 7", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + co->ucal->setMinimalDaysInFirstWeek((uint8_t)num_days); + + RETURN_TRUE; +} + +U_CFUNC PHP_FUNCTION(intlcal_equals) +{ + zval *other_object; + Calendar_object *other_co; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "OO", &object, Calendar_ce_ptr, &other_object, Calendar_ce_ptr) + == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_equals: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + other_co = (Calendar_object *) zend_object_store_get_object(other_object TSRMLS_CC); + if (other_co->ucal == NULL) { + intl_errors_set(&co->err, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_equals: The second IntlCalendar is unconstructed", 0 TSRMLS_CC); + RETURN_FALSE; + } + + UBool result = co->ucal->equals(*other_co->ucal, CALENDAR_ERROR_CODE(co)); + INTL_METHOD_CHECK_STATUS(co, "intlcal_equals: error calling ICU Calendar::equals"); + + RETURN_BOOL((int)result); +} + +#if U_ICU_VERSION_MAJOR_NUM >= 49 + +U_CFUNC PHP_FUNCTION(intlcal_get_repeated_wall_time_option) +{ + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "O", &object, Calendar_ce_ptr) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_repeated_wall_time_option: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + RETURN_LONG(co->ucal->getRepeatedWallTimeOption()); +} + +U_CFUNC PHP_FUNCTION(intlcal_get_skipped_wall_time_option) +{ + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "O", &object, Calendar_ce_ptr) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_skipped_wall_time_option: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + RETURN_LONG(co->ucal->getSkippedWallTimeOption()); +} + +U_CFUNC PHP_FUNCTION(intlcal_set_repeated_wall_time_option) +{ + long option; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Ol", &object, Calendar_ce_ptr, &option) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set_repeated_wall_time_option: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + if (option != UCAL_WALLTIME_FIRST && option != UCAL_WALLTIME_LAST) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set_repeated_wall_time_option: invalid option", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + co->ucal->setRepeatedWallTimeOption((UCalendarWallTimeOption)option); + + RETURN_TRUE; +} + +U_CFUNC PHP_FUNCTION(intlcal_set_skipped_wall_time_option) +{ + long option; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Ol", &object, Calendar_ce_ptr, &option) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set_skipped_wall_time_option: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + if (option != UCAL_WALLTIME_FIRST && option != UCAL_WALLTIME_LAST + && option != UCAL_WALLTIME_NEXT_VALID) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_set_skipped_wall_time_option: invalid option", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + co->ucal->setSkippedWallTimeOption((UCalendarWallTimeOption)option); + + RETURN_TRUE; +} + +#endif + +U_CFUNC PHP_FUNCTION(intlcal_from_date_time) +{ + zval **zv_arg, + *zv_datetime = NULL, + *zv_timestamp = NULL; + php_date_obj *datetime; + char *locale_str = NULL; + int locale_str_len; + TimeZone *timeZone; + UErrorCode status = U_ZERO_ERROR; + Calendar *cal; + intl_error_reset(NULL TSRMLS_CC); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z|s!", + &zv_arg, &locale_str, &locale_str_len) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_from_date_time: bad arguments", 0 TSRMLS_CC); + RETURN_NULL(); + } + + if (!(Z_TYPE_PP(zv_arg) == IS_OBJECT && instanceof_function( + Z_OBJCE_PP(zv_arg), php_date_get_date_ce() TSRMLS_CC))) { + ALLOC_INIT_ZVAL(zv_datetime); + object_init_ex(zv_datetime, php_date_get_date_ce()); + zend_call_method_with_1_params(&zv_datetime, NULL, NULL, "__construct", + NULL, *zv_arg); + if (EG(exception)) { + zend_object_store_ctor_failed(zv_datetime TSRMLS_CC); + goto error; + } + } else { + zv_datetime = *zv_arg; + } + + datetime = (php_date_obj*)zend_object_store_get_object(zv_datetime TSRMLS_CC); + if (!datetime->time) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_from_date_time: DateTime object is unconstructed", + 0 TSRMLS_CC); + goto error; + } + + zend_call_method_with_0_params(&zv_datetime, php_date_get_date_ce(), + NULL, "gettimestamp", &zv_timestamp); + if (!zv_timestamp || Z_TYPE_P(zv_timestamp) != IS_LONG) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_from_date_time: bad DateTime; call to " + "DateTime::getTimestamp() failed", 0 TSRMLS_CC); + goto error; + } + + if (!datetime->time->is_localtime) { + timeZone = TimeZone::getGMT()->clone(); + } else { + timeZone = timezone_convert_datetimezone(datetime->time->zone_type, + datetime, 1, NULL, "intlcal_from_date_time" TSRMLS_CC); + if (timeZone == NULL) { + goto error; + } + } + + if (!locale_str) { + locale_str = const_cast<char*>(intl_locale_get_default(TSRMLS_C)); + } + + cal = Calendar::createInstance(timeZone, + Locale::createFromName(locale_str), status); + if (cal == NULL) { + delete timeZone; + intl_error_set(NULL, status, "intlcal_from_date_time: " + "error creating ICU Calendar object", 0 TSRMLS_CC); + goto error; + } + cal->setTime(((UDate)Z_LVAL_P(zv_timestamp)) * 1000., status); + if (U_FAILURE(status)) { + /* time zone was adopted by cal; should not be deleted here */ + delete cal; + intl_error_set(NULL, status, "intlcal_from_date_time: " + "error creating ICU Calendar::setTime()", 0 TSRMLS_CC); + goto error; + } + + calendar_object_create(return_value, cal TSRMLS_CC); + +error: + if (zv_datetime != *zv_arg) { + zval_ptr_dtor(&zv_datetime); + } + if (zv_timestamp) { + zval_ptr_dtor(&zv_timestamp); + } +} + +U_CFUNC PHP_FUNCTION(intlcal_to_date_time) +{ + zval *retval = NULL; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", + &object, Calendar_ce_ptr) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_to_date_time: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + /* There are no exported functions in ext/date to this + * in a more native fashion */ + double date = co->ucal->getTime(CALENDAR_ERROR_CODE(co)) / 1000.; + int64_t ts; + char ts_str[sizeof("@-9223372036854775808")]; + int ts_str_len; + zval ts_zval = zval_used_for_init; + + INTL_METHOD_CHECK_STATUS(co, "Call to ICU method has failed"); + + if (date > (double)U_INT64_MAX || date < (double)U_INT64_MIN) { + intl_errors_set(CALENDAR_ERROR_P(co), U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_to_date_time: The calendar date is out of the " + "range for a 64-bit integer", 0 TSRMLS_CC); + RETURN_FALSE; + } + + ts = (int64_t)date; + + ts_str_len = slprintf(ts_str, sizeof(ts_str), "@%I64d", ts); + ZVAL_STRINGL(&ts_zval, ts_str, ts_str_len, 0); + + /* Now get the time zone */ + const TimeZone& tz = co->ucal->getTimeZone(); + zval *timezone_zval = timezone_convert_to_datetimezone( + &tz, CALENDAR_ERROR_P(co), "intlcal_to_date_time" TSRMLS_CC); + if (timezone_zval == NULL) { + RETURN_FALSE; + } + + /* resources allocated from now on */ + + /* Finally, instantiate object and call constructor */ + object_init_ex(return_value, php_date_get_date_ce()); + zend_call_method_with_2_params(&return_value, NULL, NULL, "__construct", + NULL, &ts_zval, timezone_zval); + if (EG(exception)) { + intl_errors_set(CALENDAR_ERROR_P(co), U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_to_date_time: DateTime constructor has thrown exception", + 1 TSRMLS_CC); + zend_object_store_ctor_failed(return_value TSRMLS_CC); + zval_ptr_dtor(&return_value); + + RETVAL_FALSE; + goto error; + } + + /* due to bug #40743, we have to set the time zone again */ + zend_call_method_with_1_params(&return_value, NULL, NULL, "settimezone", + &retval, timezone_zval); + if (retval == NULL || Z_TYPE_P(retval) == IS_BOOL) { + intl_errors_set(CALENDAR_ERROR_P(co), U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_to_date_time: call to DateTime::setTimeZone has failed", + 1 TSRMLS_CC); + zval_ptr_dtor(&return_value); + RETVAL_FALSE; + goto error; + } + +error: + zval_ptr_dtor(&timezone_zval); + if (retval != NULL) { + zval_ptr_dtor(&retval); + } +} + +U_CFUNC PHP_FUNCTION(intlcal_get_error_code) +{ + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", + &object, Calendar_ce_ptr) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_error_code: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + /* Fetch the object (without resetting its last error code ). */ + co = (Calendar_object*)zend_object_store_get_object(object TSRMLS_CC); + if (co == NULL) + RETURN_FALSE; + + RETURN_LONG((long)CALENDAR_ERROR_CODE(co)); +} + +U_CFUNC PHP_FUNCTION(intlcal_get_error_message) +{ + const char* message = NULL; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", + &object, Calendar_ce_ptr) == FAILURE) { + intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlcal_get_error_message: bad arguments", 0 TSRMLS_CC ); + RETURN_FALSE; + } + + + /* Fetch the object (without resetting its last error code ). */ + co = (Calendar_object*)zend_object_store_get_object(object TSRMLS_CC); + if (co == NULL) + RETURN_FALSE; + + /* Return last error message. */ + message = intl_error_get_message(CALENDAR_ERROR_P(co) TSRMLS_CC); + RETURN_STRING(message, 0); +} diff --git a/ext/intl/calendar/calendar_methods.h b/ext/intl/calendar/calendar_methods.h new file mode 100644 index 0000000000..dfd0bbeeaf --- /dev/null +++ b/ext/intl/calendar/calendar_methods.h @@ -0,0 +1,114 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | 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: | + | http://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: Gustavo Lopes <cataphract@netcabo.pt> | + +----------------------------------------------------------------------+ + */ + +#ifndef CALENDAR_METHODS_H +#define CALENDAR_METHODS_H + +#include <php.h> + +PHP_METHOD(IntlCalendar, __construct); + +PHP_FUNCTION(intlcal_create_instance); + +PHP_FUNCTION(intlcal_get_keyword_values_for_locale); + +PHP_FUNCTION(intlcal_get_now); + +PHP_FUNCTION(intlcal_get_available_locales); + +PHP_FUNCTION(intlcal_get); + +PHP_FUNCTION(intlcal_get_time); + +PHP_FUNCTION(intlcal_set_time); + +PHP_FUNCTION(intlcal_add); + +PHP_FUNCTION(intlcal_set_time_zone); + +PHP_FUNCTION(intlcal_after); + +PHP_FUNCTION(intlcal_before); + +PHP_FUNCTION(intlcal_set); + +PHP_FUNCTION(intlcal_roll); + +PHP_FUNCTION(intlcal_clear); + +PHP_FUNCTION(intlcal_field_difference); + +PHP_FUNCTION(intlcal_get_actual_maximum); + +PHP_FUNCTION(intlcal_get_actual_minimum); + +PHP_FUNCTION(intlcal_get_day_of_week_type); + +PHP_FUNCTION(intlcal_get_first_day_of_week); + +PHP_FUNCTION(intlcal_get_greatest_minimum); + +PHP_FUNCTION(intlcal_get_least_maximum); + +PHP_FUNCTION(intlcal_get_locale); + +PHP_FUNCTION(intlcal_get_maximum); + +PHP_FUNCTION(intlcal_get_minimal_days_in_first_week); + +PHP_FUNCTION(intlcal_get_minimum); + +PHP_FUNCTION(intlcal_get_time_zone); + +PHP_FUNCTION(intlcal_get_type); + +PHP_FUNCTION(intlcal_get_weekend_transition); + +PHP_FUNCTION(intlcal_in_daylight_time); + +PHP_FUNCTION(intlcal_is_equivalent_to); + +PHP_FUNCTION(intlcal_is_lenient); + +PHP_FUNCTION(intlcal_is_set); + +PHP_FUNCTION(intlcal_is_weekend); + +PHP_FUNCTION(intlcal_set_first_day_of_week); + +PHP_FUNCTION(intlcal_set_lenient); + +PHP_FUNCTION(intlcal_set_minimal_days_in_first_week); + +PHP_FUNCTION(intlcal_equals); + +PHP_FUNCTION(intlcal_get_repeated_wall_time_option); + +PHP_FUNCTION(intlcal_get_skipped_wall_time_option); + +PHP_FUNCTION(intlcal_set_repeated_wall_time_option); + +PHP_FUNCTION(intlcal_set_skipped_wall_time_option); + +PHP_FUNCTION(intlcal_from_date_time); + +PHP_FUNCTION(intlcal_to_date_time); + +PHP_FUNCTION(intlcal_get_error_code); + +PHP_FUNCTION(intlcal_get_error_message); + +#endif /* #ifndef CALENDAR_METHODS_H */ diff --git a/ext/intl/calendar/gregoriancalendar_methods.cpp b/ext/intl/calendar/gregoriancalendar_methods.cpp new file mode 100644 index 0000000000..08b894964c --- /dev/null +++ b/ext/intl/calendar/gregoriancalendar_methods.cpp @@ -0,0 +1,255 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | 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: | + | http://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: Gustavo Lopes <cataphract@php.net> | + +----------------------------------------------------------------------+ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "../intl_cppshims.h" + +#include <unicode/locid.h> +#include <unicode/calendar.h> +#include <unicode/gregocal.h> +extern "C" { +#include "../php_intl.h" +#define USE_TIMEZONE_POINTER 1 +#include "../timezone/timezone_class.h" +#define USE_CALENDAR_POINTER 1 +#include "calendar_class.h" +#include <ext/date/php_date.h> +} + +static inline GregorianCalendar *fetch_greg(Calendar_object *co) { + return (GregorianCalendar*)co->ucal; +} + +static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS) +{ + zval **tz_object = NULL; + zval **args_a[6] = {0}, + ***args = &args_a[0]; + char *locale = NULL; + int locale_len; + long largs[6]; + UErrorCode status = U_ZERO_ERROR; + int variant; + intl_error_reset(NULL TSRMLS_CC); + + // parameter number validation / variant determination + if (ZEND_NUM_ARGS() > 6 || + zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlgregcal_create_instance: too many arguments", 0 TSRMLS_CC); + RETURN_NULL(); + } + for (variant = ZEND_NUM_ARGS(); + variant > 0 && Z_TYPE_PP(args[variant - 1]) == IS_NULL; + variant--) {} + if (variant == 4) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlgregcal_create_instance: no variant with 4 arguments " + "(excluding trailing NULLs)", 0 TSRMLS_CC); + RETURN_NULL(); + } + + // argument parsing + if (variant <= 2) { + if (zend_parse_parameters(MIN(ZEND_NUM_ARGS(), 2) TSRMLS_CC, + "|Z!s!", &tz_object, &locale, &locale_len) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlgregcal_create_instance: bad arguments", 0 TSRMLS_CC); + RETURN_NULL(); + } + } + if (variant > 2 && zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, + "lll|lll", &largs[0], &largs[1], &largs[2], &largs[3], &largs[4], + &largs[5]) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlgregcal_create_instance: bad arguments", 0 TSRMLS_CC); + RETURN_NULL(); + } + + // instantion of ICU object + GregorianCalendar *gcal = NULL; + + if (variant <= 2) { + // From timezone and locale (0 to 2 arguments) + TimeZone *tz = timezone_process_timezone_argument(tz_object, NULL, + "intlgregcal_create_instance" TSRMLS_CC); + if (tz == NULL) { + RETURN_NULL(); + } + if (!locale) { + locale = const_cast<char*>(intl_locale_get_default(TSRMLS_C)); + } + + gcal = new GregorianCalendar(tz, Locale::createFromName(locale), + status); + if (U_FAILURE(status)) { + intl_error_set(NULL, status, "intlgregcal_create_instance: error " + "creating ICU GregorianCalendar from time zone and locale", 0 TSRMLS_CC); + if (gcal) { + delete gcal; + } + delete tz; + RETURN_NULL(); + } + } else { + // From date/time (3, 5 or 6 arguments) + for (int i = 0; i < variant; i++) { + if (largs[i] < INT32_MIN || largs[i] > INT32_MAX) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlgregcal_create_instance: at least one of the arguments" + " has an absolute value that is too large", 0 TSRMLS_CC); + RETURN_NULL(); + } + } + + if (variant == 3) { + gcal = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1], + (int32_t)largs[2], status); + } else if (variant == 5) { + gcal = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1], + (int32_t)largs[2], (int32_t)largs[3], (int32_t)largs[4], status); + } else if (variant == 6) { + gcal = new GregorianCalendar((int32_t)largs[0], (int32_t)largs[1], + (int32_t)largs[2], (int32_t)largs[3], (int32_t)largs[4], (int32_t)largs[5], + status); + } + if (U_FAILURE(status)) { + intl_error_set(NULL, status, "intlgregcal_create_instance: error " + "creating ICU GregorianCalendar from date", 0 TSRMLS_CC); + if (gcal) { + delete gcal; + } + RETURN_NULL(); + } + + timelib_tzinfo *tzinfo = get_timezone_info(TSRMLS_C); +#if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 42 + UnicodeString tzstr = UnicodeString::fromUTF8(StringPiece(tzinfo->name)); +#else + UnicodeString tzstr = UnicodeString(tzinfo->name, + strlen(tzinfo->name), US_INV); +#endif + if (tzstr.isBogus()) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlgregcal_create_instance: could not create UTF-8 string " + "from PHP's default timezone name (see date_default_timezone_get())", + 0 TSRMLS_CC); + delete gcal; + RETURN_NULL(); + } + + TimeZone *tz = TimeZone::createTimeZone(tzstr); + gcal->adoptTimeZone(tz); + } + + Calendar_object *co = (Calendar_object*)zend_object_store_get_object( + return_value TSRMLS_CC); + co->ucal = gcal; +} + +U_CFUNC PHP_FUNCTION(intlgregcal_create_instance) +{ + zval orig; + intl_error_reset(NULL TSRMLS_CC); + + object_init_ex(return_value, GregorianCalendar_ce_ptr); + orig = *return_value; + + _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAM_PASSTHRU); + + if (Z_TYPE_P(return_value) == IS_NULL) { + zend_object_store_ctor_failed(&orig TSRMLS_CC); + zval_dtor(&orig); + } +} + +U_CFUNC PHP_METHOD(IntlGregorianCalendar, __construct) +{ + zval orig_this = *getThis(); + intl_error_reset(NULL TSRMLS_CC); + + return_value = getThis(); + //changes this to IS_NULL (without first destroying) if there's an error + _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAM_PASSTHRU); + + if (Z_TYPE_P(return_value) == IS_NULL) { + zend_object_store_ctor_failed(&orig_this TSRMLS_CC); + zval_dtor(&orig_this); + } +} + +U_CFUNC PHP_FUNCTION(intlgregcal_set_gregorian_change) +{ + double date; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Od", &object, GregorianCalendar_ce_ptr, &date) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlgregcal_set_gregorian_change: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + fetch_greg(co)->setGregorianChange(date, CALENDAR_ERROR_CODE(co)); + INTL_METHOD_CHECK_STATUS(co, "intlgregcal_set_gregorian_change: error " + "calling ICU method"); + + RETURN_TRUE; +} + +U_CFUNC PHP_FUNCTION(intlgregcal_get_gregorian_change) +{ + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "O", &object, GregorianCalendar_ce_ptr) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlgregcal_get_gregorian_change: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + RETURN_DOUBLE((double)fetch_greg(co)->getGregorianChange()); +} + +U_CFUNC PHP_FUNCTION(intlgregcal_is_leap_year) +{ + long year; + CALENDAR_METHOD_INIT_VARS; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Ol", &object, GregorianCalendar_ce_ptr, &year) == FAILURE) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlgregcal_is_leap_year: bad arguments", 0 TSRMLS_CC); + RETURN_FALSE; + } + + if (year < INT32_MIN || year > INT32_MAX) { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlgregcal_is_leap_year: year out of bounds", 0 TSRMLS_CC); + RETURN_FALSE; + } + + CALENDAR_METHOD_FETCH_OBJECT; + + RETURN_BOOL((int)fetch_greg(co)->isLeapYear((int32_t)year)); +} diff --git a/ext/intl/calendar/gregoriancalendar_methods.h b/ext/intl/calendar/gregoriancalendar_methods.h new file mode 100644 index 0000000000..f911752cc7 --- /dev/null +++ b/ext/intl/calendar/gregoriancalendar_methods.h @@ -0,0 +1,32 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | 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: | + | http://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: Gustavo Lopes <cataphract@php.net> | + +----------------------------------------------------------------------+ + */ + +#ifndef GREORIANCALENDAR_METHODS_H +#define GREORIANCALENDAR_METHODS_H + +#include <php.h> + +PHP_FUNCTION(intlgregcal_create_instance); + +PHP_METHOD(IntlGregorianCalendar, __construct); + +PHP_FUNCTION(intlgregcal_set_gregorian_change); + +PHP_FUNCTION(intlgregcal_get_gregorian_change); + +PHP_FUNCTION(intlgregcal_is_leap_year); + +#endif |
