1 /*
2 +----------------------------------------------------------------------+
3 | This source file is subject to version 3.01 of the PHP license, |
4 | that is bundled with this package in the file LICENSE, and is |
5 | available through the world-wide-web at the following url: |
6 | http://www.php.net/license/3_01.txt |
7 | If you did not receive a copy of the PHP license and are unable to |
8 | obtain it through the world-wide-web, please send a note to |
9 | license@php.net so we can mail you a copy immediately. |
10 +----------------------------------------------------------------------+
11 | Authors: Kirti Velankar <kirtig@yahoo-inc.com> |
12 +----------------------------------------------------------------------+
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18
19 #include "../php_intl.h"
20
21 #include <unicode/ustring.h>
22 #include <unicode/ucal.h>
23
24 #include "../intl_convert.h"
25 #include "../common/common_date.h"
26 #include "dateformat.h"
27 #include "dateformat_class.h"
28 #include "dateformat_data.h"
29
30 /* {{{ Internal function which calls the udat_format */
internal_format(IntlDateFormatter_object * dfo,UDate timestamp,zval * return_value)31 static void internal_format(IntlDateFormatter_object *dfo, UDate timestamp, zval *return_value)
32 {
33 UChar* formatted = NULL;
34 int32_t resultlengthneeded =0 ;
35
36 resultlengthneeded=udat_format( DATE_FORMAT_OBJECT(dfo), timestamp, NULL, resultlengthneeded, NULL, &INTL_DATA_ERROR_CODE(dfo));
37 if(INTL_DATA_ERROR_CODE(dfo)==U_BUFFER_OVERFLOW_ERROR)
38 {
39 INTL_DATA_ERROR_CODE(dfo)=U_ZERO_ERROR;
40 formatted=(UChar*)emalloc(sizeof(UChar) * resultlengthneeded);
41 udat_format( DATE_FORMAT_OBJECT(dfo), timestamp, formatted, resultlengthneeded, NULL, &INTL_DATA_ERROR_CODE(dfo));
42 }
43
44 if (formatted && U_FAILURE( INTL_DATA_ERROR_CODE(dfo) ) ) {
45 efree(formatted);
46 }
47
48 INTL_METHOD_CHECK_STATUS( dfo, "Date formatting failed" );
49 INTL_METHOD_RETVAL_UTF8( dfo, formatted, resultlengthneeded, 1 );
50
51 }
52 /* }}} */
53
54
55 /* {{{ Internal function which fetches an element from the passed array for the key_name passed */
internal_get_arr_ele(IntlDateFormatter_object * dfo,HashTable * hash_arr,char * key_name,intl_error * err)56 static int32_t internal_get_arr_ele(IntlDateFormatter_object *dfo,
57 HashTable* hash_arr, char* key_name, intl_error *err)
58 {
59 zval *ele_value = NULL;
60 int32_t result = 0;
61 char *message;
62
63 if (U_FAILURE(err->code)) {
64 return result;
65 }
66
67 if ((ele_value = zend_hash_str_find(hash_arr, key_name, strlen(key_name))) != NULL) {
68 if(Z_TYPE_P(ele_value) != IS_LONG) {
69 spprintf(&message, 0, "datefmt_format: parameter array contains "
70 "a non-integer element for key '%s'", key_name);
71 intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR, message, 1);
72 efree(message);
73 } else {
74 if (Z_LVAL_P(ele_value) > INT32_MAX ||
75 Z_LVAL_P(ele_value) < INT32_MIN) {
76 spprintf(&message, 0, "datefmt_format: value " ZEND_LONG_FMT " is out of "
77 "bounds for a 32-bit integer in key '%s'",
78 Z_LVAL_P(ele_value), key_name);
79 intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR, message, 1);
80 efree(message);
81 } else {
82 result = Z_LVAL_P(ele_value);
83 }
84 }
85 }
86
87 return result;
88 }
89 /* }}} */
90
91 /* {{{ Internal function which sets UCalendar from the passed array and retrieves timestamp */
internal_get_timestamp(IntlDateFormatter_object * dfo,HashTable * hash_arr)92 static UDate internal_get_timestamp(IntlDateFormatter_object *dfo,
93 HashTable *hash_arr)
94 {
95 int32_t year,
96 month,
97 hour,
98 minute,
99 second,
100 mday;
101 UCalendar *pcal;
102 UDate result;
103 intl_error *err = &dfo->datef_data.error;
104
105 #define INTL_GET_ELEM(elem) \
106 internal_get_arr_ele(dfo, hash_arr, (elem), err)
107
108 /* Fetch values from the incoming array */
109 year = INTL_GET_ELEM(CALENDAR_YEAR) + 1900; /* tm_year is years since 1900 */
110 /* Month in ICU and PHP starts from January =0 */
111 month = INTL_GET_ELEM(CALENDAR_MON);
112 hour = INTL_GET_ELEM(CALENDAR_HOUR);
113 minute = INTL_GET_ELEM(CALENDAR_MIN);
114 second = INTL_GET_ELEM(CALENDAR_SEC);
115 /* For the ucal_setDateTime() function, this is the 'date' value */
116 mday = INTL_GET_ELEM(CALENDAR_MDAY);
117
118 #undef INTL_GET_ELEM
119
120 pcal = ucal_clone(udat_getCalendar(DATE_FORMAT_OBJECT(dfo)),
121 &INTL_DATA_ERROR_CODE(dfo));
122
123 if (INTL_DATA_ERROR_CODE(dfo) != U_ZERO_ERROR) {
124 intl_errors_set(err, INTL_DATA_ERROR_CODE(dfo), "datefmt_format: "
125 "error cloning calendar", 0);
126 return 0;
127 }
128
129 /* set the incoming values for the calendar */
130 ucal_setDateTime(pcal, year, month, mday, hour, minute, second, &INTL_DATA_ERROR_CODE(dfo));
131 /* actually, ucal_setDateTime cannot fail */
132
133 /* Fetch the timestamp from the UCalendar */
134 result = ucal_getMillis(pcal, &INTL_DATA_ERROR_CODE(dfo));
135 ucal_close(pcal);
136 return result;
137 }
138
139
140 /* {{{ Format the time value as a string. */
PHP_FUNCTION(datefmt_format)141 PHP_FUNCTION(datefmt_format)
142 {
143 UDate timestamp = 0;
144 HashTable *hash_arr = NULL;
145 zval *zarg = NULL;
146
147 DATE_FORMAT_METHOD_INIT_VARS;
148
149 /* Parse parameters. */
150 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oz",
151 &object, IntlDateFormatter_ce_ptr, &zarg) == FAILURE) {
152 intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: unable "
153 "to parse input params", 0 );
154 RETURN_THROWS();
155 }
156
157 DATE_FORMAT_METHOD_FETCH_OBJECT;
158
159 if (Z_TYPE_P(zarg) == IS_ARRAY) {
160 hash_arr = Z_ARRVAL_P(zarg);
161 if (!hash_arr || zend_hash_num_elements(hash_arr) == 0) {
162 RETURN_FALSE;
163 }
164
165 timestamp = internal_get_timestamp(dfo, hash_arr);
166 INTL_METHOD_CHECK_STATUS(dfo, "datefmt_format: date formatting failed")
167 } else {
168 timestamp = intl_zval_to_millis(zarg, INTL_DATA_ERROR_P(dfo),
169 "datefmt_format");
170 if (U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
171 RETURN_FALSE;
172 }
173 }
174
175 internal_format( dfo, timestamp, return_value);
176 }
177
178 /* }}} */
179