php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
dateformat_format.c
Go to the documentation of this file.
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 | https://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"
26#include "dateformat.h"
27#include "dateformat_class.h"
28#include "dateformat_data.h"
29
30/* {{{ Internal function which calls the udat_format */
31static 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));
38 {
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 */
56static 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_deref(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);
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);
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 */
92static 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;
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 */
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 */
117
118#undef INTL_GET_ELEM
119
120 pcal = ucal_clone(udat_getCalendar(DATE_FORMAT_OBJECT(dfo)),
122
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. */
142{
143 UDate timestamp = 0;
144 HashTable *hash_arr = NULL;
145 zval *zarg = NULL;
146
148
149 /* Parse parameters. */
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 );
155 }
156
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) {
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))) {
172 }
173 }
174
175 internal_format( dfo, timestamp, return_value);
176}
177
178/* }}} */
const U_BUFFER_OVERFLOW_ERROR
const U_ILLEGAL_ARGUMENT_ERROR
const U_ZERO_ERROR
U_CDECL_BEGIN U_CDECL_END U_CFUNC double intl_zval_to_millis(zval *z, intl_error *err, const char *func)
#define CALENDAR_YEAR
Definition dateformat.h:32
#define CALENDAR_MIN
Definition dateformat.h:28
#define CALENDAR_SEC
Definition dateformat.h:27
#define CALENDAR_HOUR
Definition dateformat.h:29
#define CALENDAR_MDAY
Definition dateformat.h:30
#define CALENDAR_MON
Definition dateformat.h:31
zend_class_entry * IntlDateFormatter_ce_ptr
#define DATE_FORMAT_METHOD_FETCH_OBJECT
#define DATE_FORMAT_OBJECT(dfo)
#define DATE_FORMAT_METHOD_INIT_VARS
#define INTL_GET_ELEM(elem)
char * err
Definition ffi.c:3029
#define NULL
Definition gdcache.h:45
#define INTL_DATA_ERROR_P(obj)
Definition intl_data.h:39
#define INTL_DATA_ERROR_CODE(obj)
Definition intl_data.h:40
#define INTL_METHOD_CHECK_STATUS(obj, msg)
Definition intl_data.h:66
#define INTL_METHOD_RETVAL_UTF8(obj, ustring, ulen, free_it)
Definition intl_data.h:103
void intl_errors_set(intl_error *err, UErrorCode code, const char *msg, int copyMsg)
Definition intl_error.c:169
void intl_error_set(intl_error *err, UErrorCode code, const char *msg, int copyMsg)
Definition intl_error.c:161
struct _intl_error intl_error
#define PHP_FUNCTION
Definition php.h:364
datefmt_format(IntlDateFormatter $formatter, $datetime)
#define spprintf
Definition spprintf.h:29
ZEND_API zend_result zend_parse_method_parameters(uint32_t num_args, zval *this_ptr, const char *type_spec,...)
Definition zend_API.c:1314
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
#define RETURN_FALSE
Definition zend_API.h:1058
#define RETURN_THROWS()
Definition zend_API.h:1060
#define getThis()
Definition zend_API.h:526
#define efree(ptr)
Definition zend_alloc.h:155
#define emalloc(size)
Definition zend_alloc.h:151
struct _zval_struct zval
strlen(string $string)
#define ZEND_LONG_FMT
Definition zend_long.h:87
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define Z_ARRVAL_P(zval_p)
Definition zend_types.h:987
struct _zend_array HashTable
Definition zend_types.h:386
#define IS_ARRAY
Definition zend_types.h:607
@ FAILURE
Definition zend_types.h:61
#define IS_LONG
Definition zend_types.h:604
#define Z_LVAL_P(zval_p)
Definition zend_types.h:966
zval * return_value
bool result