1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/*
+----------------------------------------------------------------------+
| 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> |
+----------------------------------------------------------------------+
*/
#include "../intl_cppshims.h"
#include <unicode/calendar.h>
#include <unicode/gregocal.h>
#include <unicode/datefmt.h>
#include <unicode/smpdtfmt.h>
#include <unicode/locid.h>
#include "../intl_convertcpp.h"
extern "C" {
#include "../php_intl.h"
#include "../locale/locale.h"
#define USE_CALENDAR_POINTER 1
#include "../calendar/calendar_class.h"
#include <ext/date/php_date.h>
#include "../common/common_date.h"
}
static const DateFormat::EStyle valid_styles[] = {
DateFormat::kNone,
DateFormat::kFull,
DateFormat::kLong,
DateFormat::kMedium,
DateFormat::kShort,
DateFormat::kFullRelative,
DateFormat::kLongRelative,
DateFormat::kMediumRelative,
DateFormat::kShortRelative,
};
static bool valid_format(zval **z) {
if (Z_TYPE_PP(z) == IS_LONG) {
long lval = Z_LVAL_PP(z);
for (int i = 0; i < sizeof(valid_styles) / sizeof(*valid_styles); i++) {
if ((long)valid_styles[i] == lval) {
return true;
}
}
}
return false;
}
U_CFUNC PHP_FUNCTION(datefmt_format_object)
{
zval *object,
**format = NULL;
const char *locale_str = NULL;
int locale_len;
bool pattern = false;
UDate date;
TimeZone *timeZone = NULL;
UErrorCode status = U_ZERO_ERROR;
DateFormat *df = NULL;
Calendar *cal = NULL;
DateFormat::EStyle dateStyle = DateFormat::kDefault,
timeStyle = DateFormat::kDefault;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o|Zs!",
&object, &format, &locale_str, &locale_len) == FAILURE) {
RETURN_FALSE;
}
if (!locale_str) {
locale_str = intl_locale_get_default(TSRMLS_C);
}
if (format == NULL || Z_TYPE_PP(format) == IS_NULL) {
//nothing
} else if (Z_TYPE_PP(format) == IS_ARRAY) {
HashTable *ht = Z_ARRVAL_PP(format);
HashPosition pos = {0};
zval **z;
if (zend_hash_num_elements(ht) != 2) {
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
"datefmt_format_object: bad format; if array, it must have "
"two elements", 0 TSRMLS_CC);
RETURN_FALSE;
}
zend_hash_internal_pointer_reset_ex(ht, &pos);
zend_hash_get_current_data_ex(ht, (void**)&z, &pos);
if (!valid_format(z)) {
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
"datefmt_format_object: bad format; the date format (first "
"element of the array) is not valid", 0 TSRMLS_CC);
RETURN_FALSE;
}
dateStyle = (DateFormat::EStyle)Z_LVAL_PP(z);
zend_hash_move_forward_ex(ht, &pos);
zend_hash_get_current_data_ex(ht, (void**)&z, &pos);
if (!valid_format(z)) {
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
"datefmt_format_object: bad format; the time format ("
"second element of the array) is not valid", 0 TSRMLS_CC);
RETURN_FALSE;
}
timeStyle = (DateFormat::EStyle)Z_LVAL_PP(z);
} else if (Z_TYPE_PP(format) == IS_LONG) {
if (!valid_format(format)) {
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
"datefmt_format_object: the date/time format type is invalid",
0 TSRMLS_CC);
RETURN_FALSE;
}
dateStyle = timeStyle = (DateFormat::EStyle)Z_LVAL_PP(format);
} else {
convert_to_string_ex(format);
if (Z_STRLEN_PP(format) == 0) {
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
"datefmt_format_object: the format is empty", 0 TSRMLS_CC);
RETURN_FALSE;
}
pattern = true;
}
//there's no support for relative time in ICU yet
timeStyle = (DateFormat::EStyle)(timeStyle & ~DateFormat::kRelative);
zend_class_entry *instance_ce = Z_OBJCE_P(object);
if (instanceof_function(instance_ce, Calendar_ce_ptr TSRMLS_CC)) {
Calendar *obj_cal = calendar_fetch_native_calendar(object TSRMLS_CC);
if (obj_cal == NULL) {
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
"datefmt_format_object: bad IntlCalendar instance: "
"not initialized properly", 0 TSRMLS_CC);
RETURN_FALSE;
}
timeZone = obj_cal->getTimeZone().clone();
date = obj_cal->getTime(status);
if (U_FAILURE(status)) {
intl_error_set(NULL, status,
"datefmt_format_object: error obtaining instant from "
"IntlCalendar", 0 TSRMLS_CC);
RETVAL_FALSE;
goto cleanup;
}
cal = obj_cal->clone();
} else if (instanceof_function(instance_ce, php_date_get_date_ce() TSRMLS_CC)) {
if (intl_datetime_decompose(object, &date, &timeZone, NULL,
"datefmt_format_object" TSRMLS_CC) == FAILURE) {
RETURN_FALSE;
}
cal = new GregorianCalendar(Locale::createFromName(locale_str), status);
if (U_FAILURE(status)) {
intl_error_set(NULL, status,
"datefmt_format_object: could not create GregorianCalendar",
0 TSRMLS_CC);
RETVAL_FALSE;
goto cleanup;
}
} else {
intl_error_set(NULL, status, "datefmt_format_object: the passed object "
"must be an instance of either IntlCalendar or DateTime",
0 TSRMLS_CC);
RETURN_FALSE;
}
if (pattern) {
df = new SimpleDateFormat(
UnicodeString(Z_STRVAL_PP(format), Z_STRLEN_PP(format),
UnicodeString::kInvariant),
Locale::createFromName(locale_str),
status);
if (U_FAILURE(status)) {
intl_error_set(NULL, status,
"datefmt_format_object: could not create SimpleDateFormat",
0 TSRMLS_CC);
RETVAL_FALSE;
goto cleanup;
}
} else {
df = DateFormat::createDateTimeInstance(dateStyle, timeStyle,
Locale::createFromName(locale_str));
if (df == NULL) { /* according to ICU sources, this should never happen */
intl_error_set(NULL, status,
"datefmt_format_object: could not create DateFormat",
0 TSRMLS_CC);
RETVAL_FALSE;
goto cleanup;
}
}
//must be in this order (or have the cal adopt the tz)
df->adoptCalendar(cal);
cal = NULL;
df->adoptTimeZone(timeZone);
timeZone = NULL;
{
UnicodeString result = UnicodeString();
df->format(date, result);
Z_TYPE_P(return_value) = IS_STRING;
if (intl_charFromString(result, &Z_STRVAL_P(return_value),
&Z_STRLEN_P(return_value), &status) == FAILURE) {
intl_error_set(NULL, status,
"datefmt_format_object: error converting result to UTF-8",
0 TSRMLS_CC);
RETVAL_FALSE;
goto cleanup;
}
}
cleanup:
delete df;
delete timeZone;
delete cal;
}
|