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