Awight has uploaded a new change for review.
https://gerrit.wikimedia.org/r/305186
Change subject: [WIP] Make another DateTime conversion more robust
......................................................................
[WIP] Make another DateTime conversion more robust
I missed this usage in @I6333bf7d07d335fe93de03e1513db6931831bfc4
Bug: T140667
Change-Id: Id9260c69245a27fa5e2d0ec600a783889a3f3bef
---
M sites/all/modules/wmf_common/tests/phpunit/WmfDatesTest.php
M sites/all/modules/wmf_common/wmf_dates.php
2 files changed, 35 insertions(+), 13 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm
refs/changes/86/305186/1
diff --git a/sites/all/modules/wmf_common/tests/phpunit/WmfDatesTest.php
b/sites/all/modules/wmf_common/tests/phpunit/WmfDatesTest.php
index 915c055..67ea3aa 100644
--- a/sites/all/modules/wmf_common/tests/phpunit/WmfDatesTest.php
+++ b/sites/all/modules/wmf_common/tests/phpunit/WmfDatesTest.php
@@ -3,20 +3,33 @@
* @group WmfCommon
*/
class WmfDatesTest extends BaseWmfDrupalPhpUnitTestCase {
+ /**
+ * @return [text, expectedSeconds, expectedUtc]
+ */
public static function dateProvider() {
return array(
- array( '@1470333044', 1470333044 ),
- array( '@1470333045.63', 1470333045 ),
+ array( '@1470333044', 1470333044, '' ),
+ array( '@1470333045.63', 1470333045, '' ),
);
}
/**
* @dataProvider WmfDatesTest::dateProvider
*/
- public function testDateParse( $text, $expectedSeconds ) {
+ public function testDateParseString( $text, $expectedSeconds,
$expectedUtc ) {
$actual = wmf_common_date_parse_string( $text );
- $this->assertEquals( $expectedSeconds, $actual,
- 'Date parsed as expected' );
- }
+ $this->assertEquals( $expectedSeconds, $actual,
+ 'Date parsed as expected' );
+ }
+
+ /**
+ * @dataProvider WmfDatesTest::dateProvider
+ */
+ public function testDateFormatUsingUtc( $text, $expectedSeconds,
$expectedUtc ) {
+ $actual = wmf_common_date_format_using_utc( $text );
+
+ $this->assertEquals( $expectedUtc, $actual,
+ 'Date formatted as expected' );
+ }
}
diff --git a/sites/all/modules/wmf_common/wmf_dates.php
b/sites/all/modules/wmf_common/wmf_dates.php
index 4e12bd2..7a70d9a 100644
--- a/sites/all/modules/wmf_common/wmf_dates.php
+++ b/sites/all/modules/wmf_common/wmf_dates.php
@@ -25,13 +25,10 @@
*/
function wmf_common_date_parse_string( $date ){
try {
- // Funky hack to trim decimal timestamp
- $date = preg_replace( '/^(@\d+)\.\d+$/', '$1', $date );
-
- $obj = new DateTime( $date, new DateTimeZone( 'UTC' ) );
+ $obj = wmf_common_make_datetime( $date );
return $obj->getTimestamp();
} catch ( Exception $ex ) {
- watchdog( 'wmf_common', t( "Caught date exception: " ) .
$ex->getMessage(), NULL, WATCHDOG_ERROR );
+ watchdog( 'wmf_common', t( 'Caught date exception in ' . __METHOD __ .
': ' ) . $ex->getMessage(), NULL, WATCHDOG_ERROR );
return null;
}
}
@@ -89,10 +86,10 @@
*/
function wmf_common_date_format_using_utc( $format, $unixtime ) {
try {
- $obj = new DateTime( '@' . $unixtime, new DateTimeZone( 'UTC' ) );
+ $obj = wmf_common_make_datetime( '@' . $unixtime );
$formatted = $obj->format( $format );
} catch ( Exception $ex ) {
- watchdog( 'wmf_common', t( "Caught date exception: " ) .
$ex->getMessage(), NULL, WATCHDOG_ERROR );
+ watchdog( 'wmf_common', t( 'Caught date exception in ' . __METHOD __ .
': ' ) . $ex->getMessage(), NULL, WATCHDOG_ERROR );
return '';
}
@@ -100,6 +97,18 @@
}
/**
+ * Normalize a date string and attempt to parse into a DateTime object.
+ * @throws Exception when the string is unparsable.
+ * @return DateTime
+ */
+function wmf_common_make_datetime( $text ) {
+ // Funky hack to trim decimal timestamp. More normalizations may
follow.
+ $date = preg_replace( '/^(@\d+)\.\d+$/', '$1', $date );
+
+ return new DateTime( $date, new DateTimeZone( 'UTC' ) );
+}
+
+/**
* Used to format dates for the CiviCRM API.
*
* @param string $unixtime unix timestamp in seconds since epoch
--
To view, visit https://gerrit.wikimedia.org/r/305186
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id9260c69245a27fa5e2d0ec600a783889a3f3bef
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits