1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Kirti Velankar <kirtig@yahoo-inc.com> |
14 +----------------------------------------------------------------------+
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <unicode/ustring.h>
22 #include <unicode/ucal.h>
23
24 #include "php_intl.h"
25 #include "intl_convert.h"
26 #include "dateformat.h"
27 #include "dateformat_class.h"
28 #include "dateformat_format.h"
29 #include "dateformat_data.h"
30 #include "ext/date/php_date.h"
31
32 /* {{{
33 * Internal function which calls the udat_format
34 */
internal_format(IntlDateFormatter_object * dfo,UDate timestamp,zval * return_value TSRMLS_DC)35 static void internal_format(IntlDateFormatter_object *dfo, UDate timestamp, zval *return_value TSRMLS_DC)
36 {
37 UChar* formatted = NULL;
38 int32_t resultlengthneeded =0 ;
39
40 resultlengthneeded=udat_format( DATE_FORMAT_OBJECT(dfo), timestamp, NULL, resultlengthneeded, NULL, &INTL_DATA_ERROR_CODE(dfo));
41 if(INTL_DATA_ERROR_CODE(dfo)==U_BUFFER_OVERFLOW_ERROR)
42 {
43 INTL_DATA_ERROR_CODE(dfo)=U_ZERO_ERROR;
44 formatted=(UChar*)emalloc(sizeof(UChar) * resultlengthneeded);
45 udat_format( DATE_FORMAT_OBJECT(dfo), timestamp, formatted, resultlengthneeded, NULL, &INTL_DATA_ERROR_CODE(dfo));
46 }
47
48 if (formatted && U_FAILURE( INTL_DATA_ERROR_CODE(dfo) ) ) {
49 efree(formatted);
50 }
51
52 INTL_METHOD_CHECK_STATUS( dfo, "Date formatting failed" );
53 INTL_METHOD_RETVAL_UTF8( dfo, formatted, resultlengthneeded, 1 );
54
55 }
56 /* }}} */
57
58
59 /* {{{
60 * Internal function which fetches an element from the passed array for the key_name passed
61 */
internal_get_arr_ele(IntlDateFormatter_object * dfo,HashTable * hash_arr,char * key_name TSRMLS_DC)62 static double internal_get_arr_ele(IntlDateFormatter_object *dfo, HashTable* hash_arr, char* key_name TSRMLS_DC)
63 {
64 zval** ele_value = NULL;
65 UDate result = -1;
66
67 if( zend_hash_find( hash_arr, key_name, strlen(key_name) + 1, (void **)&ele_value ) == SUCCESS ){
68 if( Z_TYPE_PP(ele_value)!= IS_LONG ){
69 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
70 "datefmt_format: parameter array does not contain a long element.", 0 TSRMLS_CC );
71 }else{
72 result = Z_LVAL_PP(ele_value);
73 }
74 }
75 /* printf("\n Inside internal_get_arr_ele key_name= %s, result = %g \n", key_name, result); */
76 return result;
77 }
78 /* }}} */
79
80 /* {{{
81 * Internal function which sets UCalendar from the passed array and retrieves timestamp
82 */
internal_get_timestamp(IntlDateFormatter_object * dfo,HashTable * hash_arr TSRMLS_DC)83 static UDate internal_get_timestamp(IntlDateFormatter_object *dfo, HashTable* hash_arr TSRMLS_DC)
84 {
85 long year =0;
86 long month =0;
87 long hour =0;
88 long minute =0;
89 long second =0;
90 long wday =0;
91 long yday =0;
92 long mday =0;
93 UBool isInDST = FALSE;
94 UCalendar *pcal;
95
96 /* Fetch values from the incoming array */
97 year = internal_get_arr_ele( dfo, hash_arr, CALENDAR_YEAR TSRMLS_CC) + 1900; /* tm_year is years since 1900 */
98 /* Month in ICU and PHP starts from January =0 */
99 month = internal_get_arr_ele( dfo, hash_arr, CALENDAR_MON TSRMLS_CC);
100 hour = internal_get_arr_ele( dfo, hash_arr, CALENDAR_HOUR TSRMLS_CC);
101 minute = internal_get_arr_ele( dfo, hash_arr, CALENDAR_MIN TSRMLS_CC);
102 second = internal_get_arr_ele( dfo, hash_arr, CALENDAR_SEC TSRMLS_CC);
103 wday = internal_get_arr_ele( dfo, hash_arr, CALENDAR_WDAY TSRMLS_CC);
104 yday = internal_get_arr_ele( dfo, hash_arr, CALENDAR_YDAY TSRMLS_CC);
105 isInDST = internal_get_arr_ele( dfo, hash_arr, CALENDAR_ISDST TSRMLS_CC);
106 /* For the ucal_setDateTime() function, this is the 'date' value */
107 mday = internal_get_arr_ele( dfo, hash_arr, CALENDAR_MDAY TSRMLS_CC);
108
109 pcal = udat_getCalendar(DATE_FORMAT_OBJECT(dfo));
110 /* set the incoming values for the calendar */
111 ucal_setDateTime( pcal, year, month, mday, hour, minute, second, &INTL_DATA_ERROR_CODE(dfo));
112 if( INTL_DATA_ERROR_CODE(dfo) != U_ZERO_ERROR){
113 return 0;
114 }
115
116 /* Fetch the timestamp from the UCalendar */
117 return ucal_getMillis(pcal, &INTL_DATA_ERROR_CODE(dfo) );
118 }
119
120
121 /* {{{ proto string IntlDateFormatter::format( [mixed]int $args or array $args )
122 * Format the time value as a string. }}}*/
123 /* {{{ proto string datefmt_format( [mixed]int $args or array $args )
124 * Format the time value as a string. }}}*/
PHP_FUNCTION(datefmt_format)125 PHP_FUNCTION(datefmt_format)
126 {
127 UDate timestamp =0;
128 UDate p_timestamp =0;
129 HashTable* hash_arr = NULL;
130 zval* zarg = NULL;
131
132 DATE_FORMAT_METHOD_INIT_VARS;
133
134 /* Parse parameters. */
135 if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oz", &object, IntlDateFormatter_ce_ptr,&zarg ) == FAILURE )
136 {
137 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: unable to parse input params", 0 TSRMLS_CC );
138 RETURN_FALSE;
139 }
140
141 /* Fetch the object. */
142 DATE_FORMAT_METHOD_FETCH_OBJECT;
143
144 switch(Z_TYPE_P(zarg) ){
145 case IS_LONG:
146 p_timestamp = Z_LVAL_P(zarg) ;
147 timestamp = p_timestamp * 1000;
148 break;
149 case IS_DOUBLE:
150 /* timestamp*1000 since ICU expects it in milliseconds */
151 p_timestamp = Z_DVAL_P(zarg) ;
152 timestamp = p_timestamp * 1000;
153 break;
154 case IS_ARRAY:
155 hash_arr = Z_ARRVAL_P(zarg);
156 if( !hash_arr || zend_hash_num_elements( hash_arr ) == 0 )
157 RETURN_FALSE;
158
159 timestamp = internal_get_timestamp(dfo, hash_arr TSRMLS_CC);
160 INTL_METHOD_CHECK_STATUS( dfo, "datefmt_format: Date formatting failed" )
161 break;
162 case IS_OBJECT: {
163 zend_class_entry *date_ce = php_date_get_date_ce();
164 zval retval;
165 zval *zfuncname;
166 if(!instanceof_function(Z_OBJCE_P(zarg), date_ce TSRMLS_CC)) {
167 intl_errors_set(INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: object must be an instance of DateTime", 0 TSRMLS_CC );
168 RETURN_FALSE;
169 }
170 INIT_ZVAL(retval);
171 MAKE_STD_ZVAL(zfuncname);
172 ZVAL_STRING(zfuncname, "getTimestamp", 1);
173 if(call_user_function(NULL, &zarg, zfuncname, &retval, 0, NULL TSRMLS_CC) != SUCCESS || Z_TYPE(retval) != IS_LONG) {
174 intl_errors_set(INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: cannot get timestamp", 0 TSRMLS_CC );
175 zval_ptr_dtor(&zfuncname);
176 RETURN_FALSE;
177 }
178 zval_ptr_dtor(&zfuncname);
179 p_timestamp = Z_LVAL(retval);
180 timestamp = p_timestamp*1000;
181 }
182 break;
183 default:
184 intl_errors_set( INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR,
185 "datefmt_format: takes either an array or an integer timestamp value or a DateTime object", 0 TSRMLS_CC );
186 RETURN_FALSE;
187 }
188
189 internal_format( dfo, timestamp, return_value TSRMLS_CC);
190
191 }
192
193 /* }}} */
194
195