xref: /PHP-7.1/ext/intl/dateformat/dateformat.c (revision bdeb220f)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
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 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 
20 #include <unicode/udat.h>
21 
22 #include "php_intl.h"
23 #include "dateformat_class.h"
24 #include "dateformat.h"
25 
26 /* {{{ dateformat_register_constants
27  * Register constants common for the both (OO and procedural)
28  * APIs.
29  */
dateformat_register_constants(INIT_FUNC_ARGS)30 void dateformat_register_constants( INIT_FUNC_ARGS )
31 {
32 	if( IntlDateFormatter_ce_ptr == NULL) {
33 		zend_error(E_ERROR, "DateFormat class not defined");
34 		return;
35 	}
36 
37 	#define DATEFORMATTER_EXPOSE_CONST(x) REGISTER_LONG_CONSTANT(#x, x, CONST_PERSISTENT | CONST_CS)
38 	#define DATEFORMATTER_EXPOSE_CLASS_CONST(x) zend_declare_class_constant_long( IntlDateFormatter_ce_ptr, ZEND_STRS( #x ) - 1, UDAT_##x );
39 	#define DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST(name, value) zend_declare_class_constant_long( IntlDateFormatter_ce_ptr, ZEND_STRS( name ) - 1, value );
40 
41 	#define DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST(x) zend_declare_class_constant_long( IntlDateFormatter_ce_ptr, ZEND_STRS( #x ) - 1, UCAL_##x );
42 
43 	/* UDateFormatStyle constants */
44 	DATEFORMATTER_EXPOSE_CLASS_CONST( FULL );
45 	DATEFORMATTER_EXPOSE_CLASS_CONST( LONG );
46 	DATEFORMATTER_EXPOSE_CLASS_CONST( MEDIUM );
47 	DATEFORMATTER_EXPOSE_CLASS_CONST( SHORT );
48 	DATEFORMATTER_EXPOSE_CLASS_CONST( NONE );
49 
50 /*
51 	DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "GREGORIAN", DATEF_GREGORIAN );
52 	DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "CUSTOMARY", DATEF_CUSTOMARY );
53 	DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "BUDDHIST", DATEF_BUDDHIST );
54 	DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "JAPANESE_IMPERIAL", DATEF_JAPANESE_IMPERIAL );
55 */
56 
57 	DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST( GREGORIAN );
58 	DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST( TRADITIONAL );
59 
60 	#undef DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST
61 	#undef DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST
62 	#undef DATEFORMATTER_EXPOSE_CLASS_CONST
63 	#undef DATEFORMATTER_EXPOSE_CONST
64 }
65 /* }}} */
66 
67 /* {{{ proto int IntlDateFormatter::getErrorCode()
68  * Get formatter's last error code. }}} */
69 /* {{{ proto int datefmt_get_error_code( IntlDateFormatter $nf )
70  * Get formatter's last error code.
71  */
PHP_FUNCTION(datefmt_get_error_code)72 PHP_FUNCTION( datefmt_get_error_code )
73 {
74 	DATE_FORMAT_METHOD_INIT_VARS;
75 
76 	/* Parse parameters. */
77 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
78 		&object, IntlDateFormatter_ce_ptr ) == FAILURE )
79 	{
80 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
81 			"datefmt_get_error_code: unable to parse input params", 0 );
82 		RETURN_FALSE;
83 	}
84 
85 	dfo = Z_INTL_DATEFORMATTER_P( object );
86 
87 	/* Return formatter's last error code. */
88 	RETURN_LONG( INTL_DATA_ERROR_CODE(dfo) );
89 }
90 /* }}} */
91 
92 /* {{{ proto string IntlDateFormatter::getErrorMessage( )
93  * Get text description for formatter's last error code. }}} */
94 /* {{{ proto string datefmt_get_error_message( IntlDateFormatter $coll )
95  * Get text description for formatter's last error code.
96  */
PHP_FUNCTION(datefmt_get_error_message)97 PHP_FUNCTION( datefmt_get_error_message )
98 {
99 	zend_string *message = NULL;
100 	DATE_FORMAT_METHOD_INIT_VARS;
101 
102 	/* Parse parameters. */
103 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
104 		&object, IntlDateFormatter_ce_ptr ) == FAILURE )
105 	{
106 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
107 			"datefmt_get_error_message: unable to parse input params", 0 );
108 
109 		RETURN_FALSE;
110 	}
111 
112 	dfo = Z_INTL_DATEFORMATTER_P( object );
113 
114 	/* Return last error message. */
115 	message = intl_error_get_message( INTL_DATA_ERROR_P(dfo) );
116 	RETURN_STR( message);
117 }
118 /* }}} */
119