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 <unicode/ustring.h>
20 #include <math.h>
21
22 #include "php_intl.h"
23 #include "intl_convert.h"
24 #include "dateformat.h"
25 #include "dateformat_class.h"
26 #include "dateformat_data.h"
27
28 /* {{{
29 * Internal function which calls the udat_parse
30 * param int store_error acts like a boolean
31 * if set to 1 - store any error encountered in the parameter parse_error
32 * if set to 0 - no need to store any error encountered in the parameter parse_error
33 */
internal_parse_to_timestamp(IntlDateFormatter_object * dfo,char * text_to_parse,size_t text_len,int32_t * parse_pos,zval * return_value)34 static void internal_parse_to_timestamp(IntlDateFormatter_object *dfo, char* text_to_parse, size_t text_len, int32_t *parse_pos, zval *return_value)
35 {
36 double result = 0;
37 UDate timestamp =0;
38 UChar* text_utf16 = NULL;
39 int32_t text_utf16_len = 0;
40
41 /* Convert timezone to UTF-16. */
42 intl_convert_utf8_to_utf16(&text_utf16, &text_utf16_len, text_to_parse, text_len, &INTL_DATA_ERROR_CODE(dfo));
43 INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" );
44
45 timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
46 if( text_utf16 ){
47 efree(text_utf16);
48 }
49
50 INTL_METHOD_CHECK_STATUS( dfo, "Date parsing failed" );
51
52 /* Since return is in sec. */
53 result = (double)timestamp / U_MILLIS_PER_SECOND;
54 if (result > (double)LONG_MAX || result < (double)LONG_MIN) {
55 ZVAL_DOUBLE(return_value, result<0?ceil(result):floor(result));
56 } else {
57 ZVAL_LONG(return_value, (zend_long)result);
58 }
59 }
60 /* }}} */
61
add_to_localtime_arr(IntlDateFormatter_object * dfo,zval * return_value,const UCalendar * parsed_calendar,zend_long calendar_field,char * key_name)62 static void add_to_localtime_arr( IntlDateFormatter_object *dfo, zval* return_value, const UCalendar *parsed_calendar, zend_long calendar_field, char* key_name)
63 {
64 zend_long calendar_field_val = ucal_get( parsed_calendar, calendar_field, &INTL_DATA_ERROR_CODE(dfo));
65 INTL_METHOD_CHECK_STATUS( dfo, "Date parsing - localtime failed : could not get a field from calendar" );
66
67 if( strcmp(key_name, CALENDAR_YEAR )==0 ){
68 /* since tm_year is years from 1900 */
69 add_assoc_long( return_value, key_name,( calendar_field_val-1900) );
70 }else if( strcmp(key_name, CALENDAR_WDAY )==0 ){
71 /* since tm_wday starts from 0 whereas ICU WDAY start from 1 */
72 add_assoc_long( return_value, key_name,( calendar_field_val-1) );
73 }else{
74 add_assoc_long( return_value, key_name, calendar_field_val );
75 }
76 }
77
78 /* {{{ Internal function which calls the udat_parseCalendar */
internal_parse_to_localtime(IntlDateFormatter_object * dfo,char * text_to_parse,size_t text_len,int32_t * parse_pos,zval * return_value)79 static void internal_parse_to_localtime(IntlDateFormatter_object *dfo, char* text_to_parse, size_t text_len, int32_t *parse_pos, zval *return_value)
80 {
81 UCalendar *parsed_calendar = NULL;
82 UChar* text_utf16 = NULL;
83 int32_t text_utf16_len = 0;
84 zend_long isInDST = 0;
85
86 /* Convert timezone to UTF-16. */
87 intl_convert_utf8_to_utf16(&text_utf16, &text_utf16_len, text_to_parse, text_len, &INTL_DATA_ERROR_CODE(dfo));
88 INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" );
89
90 parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo));
91 udat_parseCalendar( DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
92
93 if (text_utf16) {
94 efree(text_utf16);
95 }
96
97 INTL_METHOD_CHECK_STATUS( dfo, "Date parsing failed" );
98
99
100 array_init( return_value );
101 /* Add entries from various fields of the obtained parsed_calendar */
102 add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_SECOND, CALENDAR_SEC);
103 add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_MINUTE, CALENDAR_MIN);
104 add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_HOUR_OF_DAY, CALENDAR_HOUR);
105 add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_YEAR, CALENDAR_YEAR);
106 add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_DAY_OF_MONTH, CALENDAR_MDAY);
107 add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_DAY_OF_WEEK, CALENDAR_WDAY);
108 add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_DAY_OF_YEAR, CALENDAR_YDAY);
109 add_to_localtime_arr( dfo, return_value, parsed_calendar, UCAL_MONTH, CALENDAR_MON);
110
111 /* Is in DST? */
112 isInDST = ucal_inDaylightTime(parsed_calendar , &INTL_DATA_ERROR_CODE(dfo));
113 INTL_METHOD_CHECK_STATUS( dfo, "Date parsing - localtime failed : while checking if currently in DST." );
114 add_assoc_long( return_value, CALENDAR_ISDST,isInDST==1);
115 }
116 /* }}} */
117
118
119 /* {{{ Parse the string $value starting at parse_pos to a Unix timestamp -int */
PHP_FUNCTION(datefmt_parse)120 PHP_FUNCTION(datefmt_parse)
121 {
122 char* text_to_parse = NULL;
123 size_t text_len =0;
124 zval* z_parse_pos = NULL;
125 int32_t parse_pos = -1;
126
127 DATE_FORMAT_METHOD_INIT_VARS;
128
129 /* Parse parameters. */
130 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|z!",
131 &object, IntlDateFormatter_ce_ptr, &text_to_parse, &text_len, &z_parse_pos ) == FAILURE ){
132 RETURN_THROWS();
133 }
134
135 /* Fetch the object. */
136 DATE_FORMAT_METHOD_FETCH_OBJECT;
137
138 if (z_parse_pos) {
139 zend_long long_parse_pos;
140 ZVAL_DEREF(z_parse_pos);
141 long_parse_pos = zval_get_long(z_parse_pos);
142 if (ZEND_LONG_INT_OVFL(long_parse_pos)) {
143 intl_error_set_code(NULL, U_ILLEGAL_ARGUMENT_ERROR);
144 intl_error_set_custom_msg(NULL, "String index is out of valid range.", 0);
145 RETURN_FALSE;
146 }
147 parse_pos = (int32_t)long_parse_pos;
148 if((size_t)parse_pos > text_len) {
149 RETURN_FALSE;
150 }
151 }
152 internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, return_value);
153 if(z_parse_pos) {
154 zval_ptr_dtor(z_parse_pos);
155 ZVAL_LONG(z_parse_pos, parse_pos);
156 }
157 }
158 /* }}} */
159
160 /* {{{ Parse the string $value to a localtime array */
PHP_FUNCTION(datefmt_localtime)161 PHP_FUNCTION(datefmt_localtime)
162 {
163 char* text_to_parse = NULL;
164 size_t text_len =0;
165 zval* z_parse_pos = NULL;
166 int32_t parse_pos = -1;
167
168 DATE_FORMAT_METHOD_INIT_VARS;
169
170 /* Parse parameters. */
171 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|z!",
172 &object, IntlDateFormatter_ce_ptr, &text_to_parse, &text_len, &z_parse_pos ) == FAILURE ){
173 RETURN_THROWS();
174 }
175
176 /* Fetch the object. */
177 DATE_FORMAT_METHOD_FETCH_OBJECT;
178
179 if (z_parse_pos) {
180 zend_long long_parse_pos;
181 ZVAL_DEREF(z_parse_pos);
182 long_parse_pos = zval_get_long(z_parse_pos);
183 if (ZEND_LONG_INT_OVFL(long_parse_pos)) {
184 intl_error_set_code(NULL, U_ILLEGAL_ARGUMENT_ERROR);
185 intl_error_set_custom_msg(NULL, "String index is out of valid range.", 0);
186 RETURN_FALSE;
187 }
188 parse_pos = (int32_t)long_parse_pos;
189 if((size_t)parse_pos > text_len) {
190 RETURN_FALSE;
191 }
192 }
193 internal_parse_to_localtime( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, return_value);
194 if (z_parse_pos) {
195 zval_ptr_dtor(z_parse_pos);
196 ZVAL_LONG(z_parse_pos, parse_pos);
197 }
198 }
199 /* }}} */
200