r24528 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r24527‎ | r24528 | r24529 >
Date:18:00, 1 August 2007
Author:rotem
Status:old
Tags:
Comment:
Introducing a new parser function, "#timel", which is the same as "#time" but uses the local time specified by the computer or by wgLocaltimezone. It passes a local timestamp to Language::sprintfDate or the compatibility function, per its comment ("Input timestamp is assumed to be pre-normalized to the desired local time zone, if any"), but this doesn't cause problems (except for the "+0000" written when using c and r flags).
Modified paths:
  • /trunk/extensions/ParserFunctions/ParserFunctions.i18n.php (modified) (history)
  • /trunk/extensions/ParserFunctions/ParserFunctions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserFunctions/ParserFunctions.i18n.php
@@ -21,6 +21,7 @@
2222 'default' => array( 0, '#default' ),
2323 'ifexist' => array( 0, 'ifexist' ),
2424 'time' => array( 0, 'time' ),
 25+ 'timel' => array( 0, 'timel' ),
2526 'rel2abs' => array( 0, 'rel2abs' ),
2627 'titleparts' => array( 0, 'titleparts' ),
2728 );
@@ -44,16 +45,17 @@
4546 * Hebrew
4647 */
4748 $words['he'] = array(
48 - 'expr' => array( 0, 'חשב', 'expr' ),
49 - 'if' => array( 0, 'תנאי', 'if' ),
50 - 'ifeq' => array( 0, 'שווה', 'ifeq' ),
51 - 'ifexpr' => array( 0, 'חשב תנאי', 'ifexpr' ),
52 - 'switch' => array( 0, 'בחר', 'switch' ),
53 - 'default' => array( 0, '#ברירת מחדל', '#default' ),
54 - 'ifexist' => array( 0, 'קיים', 'ifexist' ),
55 - 'time' => array( 0, 'זמן', 'time' ),
56 - 'rel2abs' => array( 0, 'יחסי למוחלט', 'rel2abs' ),
57 - 'titleparts' => array( 0, 'חלק בכותרת', 'titleparts' ),
 49+ 'expr' => array( 0, 'חשב', 'expr' ),
 50+ 'if' => array( 0, 'תנאי', 'if' ),
 51+ 'ifeq' => array( 0, 'שווה', 'ifeq' ),
 52+ 'ifexpr' => array( 0, 'חשב תנאי', 'ifexpr' ),
 53+ 'switch' => array( 0, 'בחר', 'switch' ),
 54+ 'default' => array( 0, '#ברירת מחדל', '#default' ),
 55+ 'ifexist' => array( 0, 'קיים', 'ifexist' ),
 56+ 'time' => array( 0, 'זמן', 'time' ),
 57+ 'timel' => array( 0, 'זמןמ', 'timel' ),
 58+ 'rel2abs' => array( 0, 'יחסי למוחלט', 'rel2abs' ),
 59+ 'titleparts' => array( 0, 'חלק בכותרת', 'titleparts' ),
5860 );
5961
6062 /**
Index: trunk/extensions/ParserFunctions/ParserFunctions.php
@@ -182,10 +182,10 @@
183183 return $else;
184184 }
185185
186 - function time( &$parser, $format = '', $date = '' ) {
187 - global $wgContLang;
188 - if ( isset( $this->mTimeCache[$format][$date] ) ) {
189 - return $this->mTimeCache[$format][$date];
 186+ function time( &$parser, $format = '', $date = '', $local = false ) {
 187+ global $wgContLang, $wgLocaltimezone;
 188+ if ( isset( $this->mTimeCache[$format][$date][$local] ) ) {
 189+ return $this->mTimeCache[$format][$date][$local];
190190 }
191191
192192 if ( $date !== '' ) {
@@ -201,7 +201,21 @@
202202 if ( $this->mTimeChars > $this->mMaxTimeChars ) {
203203 return wfMsgForContent( 'pfunc_time_too_long' );
204204 } else {
205 - $ts = wfTimestamp( TS_MW, $unix );
 205+ if ( $local ) {
 206+ # Use the time zone
 207+ if ( isset( $wgLocaltimezone ) ) {
 208+ $oldtz = getenv( 'TZ' );
 209+ putenv( 'TZ='.$wgLocaltimezone );
 210+ }
 211+ wfSuppressWarnings(); // E_STRICT system time bitching
 212+ $ts = date( 'YmdHis', $unix );
 213+ wfRestoreWarnings();
 214+ if ( isset( $wgLocaltimezone ) ) {
 215+ putenv( 'TZ='.$oldtz );
 216+ }
 217+ } else {
 218+ $ts = wfTimestamp( TS_MW, $unix );
 219+ }
206220 if ( method_exists( $wgContLang, 'sprintfDate' ) ) {
207221 $result = $wgContLang->sprintfDate( $format, $ts );
208222 } else {
@@ -213,10 +227,14 @@
214228 }
215229 }
216230 }
217 - $this->mTimeCache[$format][$date] = $result;
 231+ $this->mTimeCache[$format][$date][$local] = $result;
218232 return $result;
219233 }
220234
 235+ function localTime( &$parser, $format = '', $date = '' ) {
 236+ return $this->time( $parser, $format, $date, true );
 237+ }
 238+
221239 /**
222240 * Obtain a specified number of slash-separated parts of a title,
223241 * e.g. {{#titleparts:Hello/World|1}} => "Hello"
@@ -264,6 +282,7 @@
265283 $wgParser->setFunctionHook( 'switch', array( &$wgExtParserFunctions, 'switchHook' ) );
266284 $wgParser->setFunctionHook( 'ifexist', array( &$wgExtParserFunctions, 'ifexist' ) );
267285 $wgParser->setFunctionHook( 'time', array( &$wgExtParserFunctions, 'time' ) );
 286+ $wgParser->setFunctionHook( 'timel', array( &$wgExtParserFunctions, 'localTime' ) );
268287 $wgParser->setFunctionHook( 'rel2abs', array( &$wgExtParserFunctions, 'rel2abs' ) );
269288 $wgParser->setFunctionHook( 'titleparts', array( &$wgExtParserFunctions, 'titleparts' ) );
270289

Status & tagging log