stas                                     Sat, 20 Nov 2010 02:09:04 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=305580

Log:
Add support for DateTime in datefmt_format

Changed paths:
    U   php/php-src/branches/PHP_5_3/NEWS
    U   php/php-src/branches/PHP_5_3/ext/intl/dateformat/dateformat_format.c
    U   php/php-src/branches/PHP_5_3/ext/intl/tests/dateformat_format.phpt
    U   php/php-src/trunk/ext/intl/dateformat/dateformat_format.c
    U   php/php-src/trunk/ext/intl/tests/dateformat_format.phpt

Modified: php/php-src/branches/PHP_5_3/NEWS
===================================================================
--- php/php-src/branches/PHP_5_3/NEWS	2010-11-20 01:55:30 UTC (rev 305579)
+++ php/php-src/branches/PHP_5_3/NEWS	2010-11-20 02:09:04 UTC (rev 305580)
@@ -1,6 +1,8 @@
 ´╗┐PHP                                                                        NEWS
 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 ?? ??? 2010, PHP 5.3.4
+- Added DateTime support for intl date formatting. (Stas)
+
 - Fixed extract() to do not overwrite $GLOBALS and $this when using
   EXTR_OVERWRITE. (jorto at redhat dot com)
 - Fixed crashes on invalid parameters in intl extension (Stas, Maksymilian

Modified: php/php-src/branches/PHP_5_3/ext/intl/dateformat/dateformat_format.c
===================================================================
--- php/php-src/branches/PHP_5_3/ext/intl/dateformat/dateformat_format.c	2010-11-20 01:55:30 UTC (rev 305579)
+++ php/php-src/branches/PHP_5_3/ext/intl/dateformat/dateformat_format.c	2010-11-20 02:09:04 UTC (rev 305580)
@@ -27,6 +27,7 @@
 #include "dateformat_class.h"
 #include "dateformat_format.h"
 #include "dateformat_data.h"
+#include "ext/date/php_date.h"

 /* {{{
  * Internal function which calls the udat_format
@@ -158,9 +159,30 @@
 			timestamp = internal_get_timestamp(dfo, hash_arr TSRMLS_CC);
 			INTL_METHOD_CHECK_STATUS( dfo, "datefmt_format: Date formatting failed" )
 			break;
+		case IS_OBJECT: {
+			zend_class_entry *date_ce = php_date_get_date_ce();
+			zval retval;
+			zval *zfuncname;
+			if(!instanceof_function(Z_OBJCE_P(zarg), date_ce TSRMLS_CC)) {
+				intl_errors_set(INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: object must be an instance of DateTime", 0 TSRMLS_CC );
+				RETURN_FALSE;
+			}
+			INIT_ZVAL(retval);
+			MAKE_STD_ZVAL(zfuncname);
+			ZVAL_STRING(zfuncname, "getTimestamp", 1);
+			if(call_user_function(NULL, &zarg, zfuncname, &retval, 0, NULL) != SUCCESS || Z_TYPE(retval) != IS_LONG) {
+				intl_errors_set(INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: cannot get timestamp", 0 TSRMLS_CC );
+				zval_ptr_dtor(&zfuncname);
+				RETURN_FALSE;
+			}
+			zval_ptr_dtor(&zfuncname);
+			p_timestamp = Z_LVAL(retval);
+			timestamp = p_timestamp*1000;
+		}
+			break;
 		default:
-			intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
-				"datefmt_format: takes either an array  or an integer timestamp value ", 0 TSRMLS_CC );
+			intl_errors_set( INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR,
+				"datefmt_format: takes either an array or an integer timestamp value or a DateTime object", 0 TSRMLS_CC );
 			RETURN_FALSE;
 	}


Modified: php/php-src/branches/PHP_5_3/ext/intl/tests/dateformat_format.phpt
===================================================================
--- php/php-src/branches/PHP_5_3/ext/intl/tests/dateformat_format.phpt	2010-11-20 01:55:30 UTC (rev 305579)
+++ php/php-src/branches/PHP_5_3/ext/intl/tests/dateformat_format.phpt	2010-11-20 02:09:04 UTC (rev 305580)
@@ -33,8 +33,8 @@
 		0,
 		-1200000,
 		1200000,
-		2200000000,
-		-2200000000,
+		2200000000.0,
+		-2200000000.0,
 		90099999,
 		3600,
 		-3600
@@ -70,6 +70,15 @@
 		$localtime_arr2,
 		$localtime_arr3
 	);
+
+	$d1 = new DateTime("2010-01-01 01:02:03", new DateTimeZone("UTC"));
+	$d2 = new DateTime("2000-12-31 03:04:05", new DateTimeZone("UTC"));
+	$d2->setTimezone(new DateTimeZone("PDT"));
+	$dates = array(
+		$d1,
+		$d2,
+		new StdClass(),
+	);

 	//Test format with input as a timestamp : integer
 	foreach( $time_arr as $timestamp_entry){
@@ -111,6 +120,24 @@
 	}
 	}

+	foreach($dates as $date_entry) {
+		foreach( $locale_arr as $locale_entry ){
+			foreach( $datetype_arr as $datetype_entry ) {
+				$res_str .= "\n------------";
+				$res_str .= "\nDate is: ".var_export($date_entry, true);
+				$res_str .= "\n------------";
+
+				$fmt = ut_datefmt_create( $locale_entry , $datetype_entry ,$datetype_entry, $timezone, IntlDateFormatter::GREGORIAN );
+				$formatted1 = ut_datefmt_format( $fmt , $date_entry);
+				if( intl_get_error_code() == U_ZERO_ERROR){
+					$res_str .= "\nFormatted DateTime is : $formatted1";
+				}else{
+					$res_str .= "\nError while formatting as: '".intl_get_error_message()."'";
+				}
+			}
+		}
+	}
+
 	return $res_str;

 }
@@ -285,4 +312,109 @@
 IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
 Formatted localtime_array is : 12/17/95 12:13 AM
 IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
-Formatted localtime_array is : 18951217 12:13 AM
\ No newline at end of file
+Formatted localtime_array is : 18951217 12:13 AM
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2010-01-01 01:02:03',
+   'timezone_type' => 3,
+   'timezone' => 'UTC',
+))
+------------
+Formatted DateTime is : Thursday, December 31, 2009 3:02:03 PM GMT-10:00
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2010-01-01 01:02:03',
+   'timezone_type' => 3,
+   'timezone' => 'UTC',
+))
+------------
+Formatted DateTime is : December 31, 2009 3:02:03 PM GMT-10:00
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2010-01-01 01:02:03',
+   'timezone_type' => 3,
+   'timezone' => 'UTC',
+))
+------------
+Formatted DateTime is : Dec 31, 2009 3:02:03 PM
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2010-01-01 01:02:03',
+   'timezone_type' => 3,
+   'timezone' => 'UTC',
+))
+------------
+Formatted DateTime is : 12/31/09 3:02 PM
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2010-01-01 01:02:03',
+   'timezone_type' => 3,
+   'timezone' => 'UTC',
+))
+------------
+Formatted DateTime is : 20091231 03:02 PM
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2000-12-30 19:04:05',
+   'timezone_type' => 3,
+   'timezone' => 'America/Los_Angeles',
+))
+------------
+Formatted DateTime is : Saturday, December 30, 2000 5:04:05 PM GMT-10:00
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2000-12-30 19:04:05',
+   'timezone_type' => 3,
+   'timezone' => 'America/Los_Angeles',
+))
+------------
+Formatted DateTime is : December 30, 2000 5:04:05 PM GMT-10:00
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2000-12-30 19:04:05',
+   'timezone_type' => 3,
+   'timezone' => 'America/Los_Angeles',
+))
+------------
+Formatted DateTime is : Dec 30, 2000 5:04:05 PM
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2000-12-30 19:04:05',
+   'timezone_type' => 3,
+   'timezone' => 'America/Los_Angeles',
+))
+------------
+Formatted DateTime is : 12/30/00 5:04 PM
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2000-12-30 19:04:05',
+   'timezone_type' => 3,
+   'timezone' => 'America/Los_Angeles',
+))
+------------
+Formatted DateTime is : 20001230 05:04 PM
+------------
+Date is: stdClass::__set_state(array(
+))
+------------
+Error while formatting as: 'datefmt_format: object must be an instance of DateTime: U_ILLEGAL_ARGUMENT_ERROR'
+------------
+Date is: stdClass::__set_state(array(
+))
+------------
+Error while formatting as: 'datefmt_format: object must be an instance of DateTime: U_ILLEGAL_ARGUMENT_ERROR'
+------------
+Date is: stdClass::__set_state(array(
+))
+------------
+Error while formatting as: 'datefmt_format: object must be an instance of DateTime: U_ILLEGAL_ARGUMENT_ERROR'
+------------
+Date is: stdClass::__set_state(array(
+))
+------------
+Error while formatting as: 'datefmt_format: object must be an instance of DateTime: U_ILLEGAL_ARGUMENT_ERROR'
+------------
+Date is: stdClass::__set_state(array(
+))
+------------
+Error while formatting as: 'datefmt_format: object must be an instance of DateTime: U_ILLEGAL_ARGUMENT_ERROR'

Modified: php/php-src/trunk/ext/intl/dateformat/dateformat_format.c
===================================================================
--- php/php-src/trunk/ext/intl/dateformat/dateformat_format.c	2010-11-20 01:55:30 UTC (rev 305579)
+++ php/php-src/trunk/ext/intl/dateformat/dateformat_format.c	2010-11-20 02:09:04 UTC (rev 305580)
@@ -27,6 +27,7 @@
 #include "dateformat_class.h"
 #include "dateformat_format.h"
 #include "dateformat_data.h"
+#include "ext/date/php_date.h"

 /* {{{
  * Internal function which calls the udat_format
@@ -158,9 +159,30 @@
 			timestamp = internal_get_timestamp(dfo, hash_arr TSRMLS_CC);
 			INTL_METHOD_CHECK_STATUS( dfo, "datefmt_format: Date formatting failed" )
 			break;
+		case IS_OBJECT: {
+			zend_class_entry *date_ce = php_date_get_date_ce();
+			zval retval;
+			zval *zfuncname;
+			if(!instanceof_function(Z_OBJCE_P(zarg), date_ce TSRMLS_CC)) {
+				intl_errors_set(INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: object must be an instance of DateTime", 0 TSRMLS_CC );
+				RETURN_FALSE;
+			}
+			INIT_ZVAL(retval);
+			MAKE_STD_ZVAL(zfuncname);
+			ZVAL_STRING(zfuncname, "getTimestamp", 1);
+			if(call_user_function(NULL, &zarg, zfuncname, &retval, 0, NULL) != SUCCESS || Z_TYPE(retval) != IS_LONG) {
+				intl_errors_set(INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format: cannot get timestamp", 0 TSRMLS_CC );
+				zval_ptr_dtor(&zfuncname);
+				RETURN_FALSE;
+			}
+			zval_ptr_dtor(&zfuncname);
+			p_timestamp = Z_LVAL(retval);
+			timestamp = p_timestamp*1000;
+		}
+			break;
 		default:
-			intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
-				"datefmt_format: takes either an array  or an integer timestamp value ", 0 TSRMLS_CC );
+			intl_errors_set( INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR,
+				"datefmt_format: takes either an array or an integer timestamp value or a DateTime object", 0 TSRMLS_CC );
 			RETURN_FALSE;
 	}


Modified: php/php-src/trunk/ext/intl/tests/dateformat_format.phpt
===================================================================
--- php/php-src/trunk/ext/intl/tests/dateformat_format.phpt	2010-11-20 01:55:30 UTC (rev 305579)
+++ php/php-src/trunk/ext/intl/tests/dateformat_format.phpt	2010-11-20 02:09:04 UTC (rev 305580)
@@ -33,8 +33,8 @@
 		0,
 		-1200000,
 		1200000,
-		2200000000,
-		-2200000000,
+		2200000000.0,
+		-2200000000.0,
 		90099999,
 		3600,
 		-3600
@@ -70,6 +70,15 @@
 		$localtime_arr2,
 		$localtime_arr3
 	);
+
+	$d1 = new DateTime("2010-01-01 01:02:03", new DateTimeZone("UTC"));
+	$d2 = new DateTime("2000-12-31 03:04:05", new DateTimeZone("UTC"));
+	$d2->setTimezone(new DateTimeZone("PDT"));
+	$dates = array(
+		$d1,
+		$d2,
+		new StdClass(),
+	);

 	//Test format with input as a timestamp : integer
 	foreach( $time_arr as $timestamp_entry){
@@ -111,6 +120,24 @@
 	}
 	}

+	foreach($dates as $date_entry) {
+		foreach( $locale_arr as $locale_entry ){
+			foreach( $datetype_arr as $datetype_entry ) {
+				$res_str .= "\n------------";
+				$res_str .= "\nDate is: ".var_export($date_entry, true);
+				$res_str .= "\n------------";
+
+				$fmt = ut_datefmt_create( $locale_entry , $datetype_entry ,$datetype_entry, $timezone, IntlDateFormatter::GREGORIAN );
+				$formatted1 = ut_datefmt_format( $fmt , $date_entry);
+				if( intl_get_error_code() == U_ZERO_ERROR){
+					$res_str .= "\nFormatted DateTime is : $formatted1";
+				}else{
+					$res_str .= "\nError while formatting as: '".intl_get_error_message()."'";
+				}
+			}
+		}
+	}
+
 	return $res_str;

 }
@@ -285,4 +312,109 @@
 IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
 Formatted localtime_array is : 12/17/95 12:13 AM
 IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
-Formatted localtime_array is : 18951217 12:13 AM
\ No newline at end of file
+Formatted localtime_array is : 18951217 12:13 AM
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2010-01-01 01:02:03',
+   'timezone_type' => 3,
+   'timezone' => 'UTC',
+))
+------------
+Formatted DateTime is : Thursday, December 31, 2009 3:02:03 PM GMT-10:00
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2010-01-01 01:02:03',
+   'timezone_type' => 3,
+   'timezone' => 'UTC',
+))
+------------
+Formatted DateTime is : December 31, 2009 3:02:03 PM GMT-10:00
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2010-01-01 01:02:03',
+   'timezone_type' => 3,
+   'timezone' => 'UTC',
+))
+------------
+Formatted DateTime is : Dec 31, 2009 3:02:03 PM
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2010-01-01 01:02:03',
+   'timezone_type' => 3,
+   'timezone' => 'UTC',
+))
+------------
+Formatted DateTime is : 12/31/09 3:02 PM
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2010-01-01 01:02:03',
+   'timezone_type' => 3,
+   'timezone' => 'UTC',
+))
+------------
+Formatted DateTime is : 20091231 03:02 PM
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2000-12-30 19:04:05',
+   'timezone_type' => 3,
+   'timezone' => 'America/Los_Angeles',
+))
+------------
+Formatted DateTime is : Saturday, December 30, 2000 5:04:05 PM GMT-10:00
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2000-12-30 19:04:05',
+   'timezone_type' => 3,
+   'timezone' => 'America/Los_Angeles',
+))
+------------
+Formatted DateTime is : December 30, 2000 5:04:05 PM GMT-10:00
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2000-12-30 19:04:05',
+   'timezone_type' => 3,
+   'timezone' => 'America/Los_Angeles',
+))
+------------
+Formatted DateTime is : Dec 30, 2000 5:04:05 PM
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2000-12-30 19:04:05',
+   'timezone_type' => 3,
+   'timezone' => 'America/Los_Angeles',
+))
+------------
+Formatted DateTime is : 12/30/00 5:04 PM
+------------
+Date is: DateTime::__set_state(array(
+   'date' => '2000-12-30 19:04:05',
+   'timezone_type' => 3,
+   'timezone' => 'America/Los_Angeles',
+))
+------------
+Formatted DateTime is : 20001230 05:04 PM
+------------
+Date is: stdClass::__set_state(array(
+))
+------------
+Error while formatting as: 'datefmt_format: object must be an instance of DateTime: U_ILLEGAL_ARGUMENT_ERROR'
+------------
+Date is: stdClass::__set_state(array(
+))
+------------
+Error while formatting as: 'datefmt_format: object must be an instance of DateTime: U_ILLEGAL_ARGUMENT_ERROR'
+------------
+Date is: stdClass::__set_state(array(
+))
+------------
+Error while formatting as: 'datefmt_format: object must be an instance of DateTime: U_ILLEGAL_ARGUMENT_ERROR'
+------------
+Date is: stdClass::__set_state(array(
+))
+------------
+Error while formatting as: 'datefmt_format: object must be an instance of DateTime: U_ILLEGAL_ARGUMENT_ERROR'
+------------
+Date is: stdClass::__set_state(array(
+))
+------------
+Error while formatting as: 'datefmt_format: object must be an instance of DateTime: U_ILLEGAL_ARGUMENT_ERROR'
-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to