jenkins-bot has submitted this change and it was merged. Change subject: (bug 33454) Add timezone support to Language::sprintfDate ......................................................................
(bug 33454) Add timezone support to Language::sprintfDate
Add an optional timezone parameter to Language::sprintfDate, add format
characters eIOPTZ, and correct crU.
While we're at it, remove backwards-compatability code for 'N' and then
merge the existing switch cases for cr and wNzWtLoU that are basically
identical, since all those cases need to be changed anyway.
Bug: 33454
Change-Id: Iea1f78428bc0d32d6395818311dbe4b94d776c42
---
M languages/Language.php
M tests/phpunit/languages/LanguageTest.php
2 files changed, 152 insertions(+), 66 deletions(-)
Approvals:
Tim Starling: Looks good to me, approved
jenkins-bot: Verified
diff --git a/languages/Language.php b/languages/Language.php
index 8aaaec0..b653f99 100644
--- a/languages/Language.php
+++ b/languages/Language.php
@@ -1018,8 +1018,8 @@
* internationalisation, a reduced set of format characters, and a
better
* escaping format.
*
- * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrU. See the
- * PHP manual for definitions. There are a number of extensions, which
+ * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrUeIOPTZ.
See
+ * the PHP manual for definitions. There are a number of extensions,
which
* start with "x":
*
* xn Do not translate digits of the next numeric format character
@@ -1065,22 +1065,24 @@
* Backslash escaping is also supported.
*
* Input timestamp is assumed to be pre-normalized to the desired local
- * time zone, if any.
+ * time zone, if any. Note that the format characters crUeIOPTZ will
assume
+ * $ts is UTC if $zone is not given.
*
* @param $format String
* @param $ts String: 14-character timestamp
* YYYYMMDDHHMMSS
* 01234567890123
+ * @param $zone DateTimeZone: Timezone of $ts
* @todo handling of "o" format character for Iranian, Hebrew, Hijri &
Thai?
*
* @return string
*/
- function sprintfDate( $format, $ts ) {
+ function sprintfDate( $format, $ts, DateTimeZone $zone = null ) {
$s = '';
$raw = false;
$roman = false;
$hebrewNum = false;
- $unix = false;
+ $dateTimeObj = false;
$rawToggle = false;
$iranian = false;
$hebrew = false;
@@ -1126,8 +1128,12 @@
$num = substr( $ts, 6, 2 );
break;
case 'D':
- if ( !$unix ) $unix = wfTimestamp(
TS_UNIX, $ts );
- $s .= $this->getWeekdayAbbreviation(
gmdate( 'w', $unix ) + 1 );
+ if ( !$dateTimeObj ) {
+ $dateTimeObj =
DateTime::createFromFormat(
+ 'YmdHis', $ts, $zone ?:
new DateTimeZone( 'UTC' )
+ );
+ }
+ $s .= $this->getWeekdayAbbreviation(
$dateTimeObj->format( 'w' ) + 1 );
break;
case 'j':
$num = intval( substr( $ts, 6, 2 ) );
@@ -1151,35 +1157,12 @@
$num = $hebrew[2];
break;
case 'l':
- if ( !$unix ) {
- $unix = wfTimestamp( TS_UNIX,
$ts );
+ if ( !$dateTimeObj ) {
+ $dateTimeObj =
DateTime::createFromFormat(
+ 'YmdHis', $ts, $zone ?:
new DateTimeZone( 'UTC' )
+ );
}
- $s .= $this->getWeekdayName( gmdate(
'w', $unix ) + 1 );
- break;
- case 'N':
- if ( !$unix ) {
- $unix = wfTimestamp( TS_UNIX,
$ts );
- }
- $w = gmdate( 'w', $unix );
- $num = $w ? $w : 7;
- break;
- case 'w':
- if ( !$unix ) {
- $unix = wfTimestamp( TS_UNIX,
$ts );
- }
- $num = gmdate( 'w', $unix );
- break;
- case 'z':
- if ( !$unix ) {
- $unix = wfTimestamp( TS_UNIX,
$ts );
- }
- $num = gmdate( 'z', $unix );
- break;
- case 'W':
- if ( !$unix ) {
- $unix = wfTimestamp( TS_UNIX,
$ts );
- }
- $num = gmdate( 'W', $unix );
+ $s .= $this->getWeekdayName(
$dateTimeObj->format( 'w' ) + 1 );
break;
case 'F':
$s .= $this->getMonthName( substr( $ts,
4, 2 ) );
@@ -1229,29 +1212,11 @@
}
$num = $hebrew[1];
break;
- case 't':
- if ( !$unix ) {
- $unix = wfTimestamp( TS_UNIX,
$ts );
- }
- $num = gmdate( 't', $unix );
- break;
case 'xjt':
if ( !$hebrew ) {
$hebrew = self::tsToHebrew( $ts
);
}
$num = $hebrew[3];
- break;
- case 'L':
- if ( !$unix ) {
- $unix = wfTimestamp( TS_UNIX,
$ts );
- }
- $num = gmdate( 'L', $unix );
- break;
- case 'o':
- if ( !$unix ) {
- $unix = wfTimestamp( TS_UNIX,
$ts );
- }
- $num = gmdate( 'o', $unix );
break;
case 'Y':
$num = substr( $ts, 0, 4 );
@@ -1328,22 +1293,36 @@
$num = substr( $ts, 12, 2 );
break;
case 'c':
- if ( !$unix ) {
- $unix = wfTimestamp( TS_UNIX,
$ts );
- }
- $s .= gmdate( 'c', $unix );
- break;
case 'r':
- if ( !$unix ) {
- $unix = wfTimestamp( TS_UNIX,
$ts );
+ case 'e':
+ case 'O':
+ case 'P':
+ case 'T':
+ // Pass through string from
$dateTimeObj->format()
+ if ( !$dateTimeObj ) {
+ $dateTimeObj =
DateTime::createFromFormat(
+ 'YmdHis', $ts, $zone ?:
new DateTimeZone( 'UTC' )
+ );
}
- $s .= gmdate( 'r', $unix );
+ $s .= $dateTimeObj->format( $code );
break;
+ case 'w':
+ case 'N':
+ case 'z':
+ case 'W':
+ case 't':
+ case 'L':
+ case 'o':
case 'U':
- if ( !$unix ) {
- $unix = wfTimestamp( TS_UNIX,
$ts );
+ case 'I':
+ case 'Z':
+ // Pass through number from
$dateTimeObj->format()
+ if ( !$dateTimeObj ) {
+ $dateTimeObj =
DateTime::createFromFormat(
+ 'YmdHis', $ts, $zone ?:
new DateTimeZone( 'UTC' )
+ );
}
- $num = $unix;
+ $num = $dateTimeObj->format( $code );
break;
case '\\':
# Backslash escaping
diff --git a/tests/phpunit/languages/LanguageTest.php
b/tests/phpunit/languages/LanguageTest.php
index f55684f..d5dbfb2 100644
--- a/tests/phpunit/languages/LanguageTest.php
+++ b/tests/phpunit/languages/LanguageTest.php
@@ -511,10 +511,10 @@
}
/**
- * bug 33454. sprintfDate should always use UTC.
+ * sprintfDate should always use UTC when no zone is given.
* @dataProvider provideSprintfDateSamples
*/
- function testSprintfDateTZ( $format, $ts, $expected, $msg ) {
+ function testSprintfDateNoZone( $format, $ts, $expected, $ignore, $msg
) {
$oldTZ = date_default_timezone_get();
$res = date_default_timezone_set( 'Asia/Seoul' );
if ( !$res ) {
@@ -530,17 +530,36 @@
date_default_timezone_set( $oldTZ );
}
+ /**
+ * sprintfDate should use passed timezone
+ * @dataProvider provideSprintfDateSamples
+ */
+ function testSprintfDateTZ( $format, $ts, $ignore, $expected, $msg ) {
+ $tz = new DateTimeZone( 'Asia/Seoul' );
+ if ( !$tz ) {
+ $this->markTestSkipped( "Error getting Timezone" );
+ }
+
+ $this->assertEquals(
+ $expected,
+ $this->getLang()->sprintfDate( $format, $ts, $tz ),
+ "sprintfDate('$format', '$ts', 'Asia/Seoul'): $msg"
+ );
+ }
+
public static function provideSprintfDateSamples() {
return array(
array(
'xiY',
'20111212000000',
'1390', // note because we're testing English
locale we get Latin-standard digits
+ '1390',
'Iranian calendar full year'
),
array(
'xiy',
'20111212000000',
+ '90',
'90',
'Iranian calendar short year'
),
@@ -548,11 +567,13 @@
'o',
'20120101235000',
'2011',
+ '2011',
'ISO 8601 (week) year'
),
array(
'W',
'20120101235000',
+ '52',
'52',
'Week number'
),
@@ -560,11 +581,13 @@
'W',
'20120102235000',
'1',
+ '1',
'Week number'
),
array(
'o-\\WW-N',
'20091231235000',
+ '2009-W53-4',
'2009-W53-4',
'leap week'
),
@@ -573,11 +596,13 @@
'Y',
'20120102090705',
'2012',
+ '2012',
'Full year'
),
array(
'y',
'20120102090705',
+ '12',
'12',
'2 digit year'
),
@@ -585,11 +610,13 @@
'L',
'20120102090705',
'1',
+ '1',
'Leap year'
),
array(
'n',
'20120102090705',
+ '1',
'1',
'Month index, not zero pad'
),
@@ -597,11 +624,13 @@
'N',
'20120102090705',
'01',
+ '01',
'Month index. Zero pad'
),
array(
'M',
'20120102090705',
+ 'Jan',
'Jan',
'Month abbrev'
),
@@ -609,11 +638,13 @@
'F',
'20120102090705',
'January',
+ 'January',
'Full month'
),
array(
'xg',
'20120102090705',
+ 'January',
'January',
'Genitive month name (same in EN)'
),
@@ -621,11 +652,13 @@
'j',
'20120102090705',
'2',
+ '2',
'Day of month (not zero pad)'
),
array(
'd',
'20120102090705',
+ '02',
'02',
'Day of month (zero-pad)'
),
@@ -633,11 +666,13 @@
'z',
'20120102090705',
'1',
+ '1',
'Day of year (zero-indexed)'
),
array(
'D',
'20120102090705',
+ 'Mon',
'Mon',
'Day of week (abbrev)'
),
@@ -645,11 +680,13 @@
'l',
'20120102090705',
'Monday',
+ 'Monday',
'Full day of week'
),
array(
'N',
'20120101090705',
+ '7',
'7',
'Day of week (Mon=1, Sun=7)'
),
@@ -657,11 +694,13 @@
'w',
'20120101090705',
'0',
+ '0',
'Day of week (Sun=0, Sat=6)'
),
array(
'N',
'20120102090705',
+ '1',
'1',
'Day of week'
),
@@ -669,11 +708,13 @@
'a',
'20120102090705',
'am',
+ 'am',
'am vs pm'
),
array(
'A',
'20120102120000',
+ 'PM',
'PM',
'AM vs PM'
),
@@ -681,11 +722,13 @@
'a',
'20120102000000',
'am',
+ 'am',
'AM vs PM'
),
array(
'g',
'20120102090705',
+ '9',
'9',
'12 hour, not Zero'
),
@@ -693,11 +736,13 @@
'h',
'20120102090705',
'09',
+ '09',
'12 hour, zero padded'
),
array(
'G',
'20120102090705',
+ '9',
'9',
'24 hour, not zero'
),
@@ -705,11 +750,13 @@
'H',
'20120102090705',
'09',
+ '09',
'24 hour, zero'
),
array(
'H',
'20120102110705',
+ '11',
'11',
'24 hour, zero'
),
@@ -717,11 +764,13 @@
'i',
'20120102090705',
'07',
+ '07',
'Minutes'
),
array(
's',
'20120102090705',
+ '05',
'05',
'seconds'
),
@@ -729,11 +778,13 @@
'U',
'20120102090705',
'1325495225',
+ '1325462825',
'unix time'
),
array(
't',
'20120102090705',
+ '31',
'31',
'Days in current month'
),
@@ -741,17 +792,62 @@
'c',
'20120102090705',
'2012-01-02T09:07:05+00:00',
+ '2012-01-02T09:07:05+09:00',
'ISO 8601 timestamp'
),
array(
'r',
'20120102090705',
'Mon, 02 Jan 2012 09:07:05 +0000',
+ 'Mon, 02 Jan 2012 09:07:05 +0900',
'RFC 5322'
+ ),
+ array(
+ 'e',
+ '20120102090705',
+ 'UTC',
+ 'Asia/Seoul',
+ 'Timezone identifier'
+ ),
+ array(
+ 'I',
+ '19880602090705',
+ '0',
+ '1',
+ 'DST indicator'
+ ),
+ array(
+ 'O',
+ '20120102090705',
+ '+0000',
+ '+0900',
+ 'Timezone offset'
+ ),
+ array(
+ 'P',
+ '20120102090705',
+ '+00:00',
+ '+09:00',
+ 'Timezone offset with colon'
+ ),
+ array(
+ 'T',
+ '20120102090705',
+ 'UTC',
+ 'KST',
+ 'Timezone abbreviation'
+ ),
+ array(
+ 'Z',
+ '20120102090705',
+ '0',
+ '32400',
+ 'Timezone offset in seconds'
),
array(
'xmj xmF xmn xmY',
'20120102090705',
+ '7 Safar 2 1433',
'7 Safar 2 1433',
'Islamic'
),
@@ -759,11 +855,13 @@
'xij xiF xin xiY',
'20120102090705',
'12 Dey 10 1390',
+ '12 Dey 10 1390',
'Iranian'
),
array(
'xjj xjF xjn xjY',
'20120102090705',
+ '7 Tevet 4 5772',
'7 Tevet 4 5772',
'Hebrew'
),
@@ -771,11 +869,13 @@
'xjt',
'20120102090705',
'29',
+ '29',
'Hebrew number of days in month'
),
array(
'xjx',
'20120102090705',
+ 'Tevet',
'Tevet',
'Hebrew genitive month name (No difference in
EN)'
),
@@ -783,11 +883,13 @@
'xkY',
'20120102090705',
'2555',
+ '2555',
'Thai year'
),
array(
'xoY',
'20120102090705',
+ '101',
'101',
'Minguo'
),
@@ -795,11 +897,13 @@
'xtY',
'20120102090705',
'平成24',
+ '平成24',
'nengo'
),
array(
'xrxkYY',
'20120102090705',
+ 'MMDLV2012',
'MMDLV2012',
'Roman numerals'
),
@@ -807,18 +911,21 @@
'xhxjYY',
'20120102090705',
'ה\'תשע"ב2012',
+ 'ה\'תשע"ב2012',
'Hebrew numberals'
),
array(
'xnY',
'20120102090705',
'2012',
+ '2012',
'Raw numerals (doesn\'t mean much in EN)'
),
array(
'[[Y "(yea"\\r)]] \\"xx\\"',
'20120102090705',
'[[2012 (year)]] "x"',
+ '[[2012 (year)]] "x"',
'Various escaping'
),
--
To view, visit https://gerrit.wikimedia.org/r/55274
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Iea1f78428bc0d32d6395818311dbe4b94d776c42
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Anomie <[email protected]>
Gerrit-Reviewer: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: Demon <[email protected]>
Gerrit-Reviewer: Nemo bis <[email protected]>
Gerrit-Reviewer: Parent5446 <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: Tim Starling <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
