From: Tim Starling Date: Tue, 13 Nov 2007 04:05:13 +0000 (+0000) Subject: revert revert (r27446): I gave the OK for this feature. sprintfDate is a big tent... X-Git-Tag: 1.31.0-rc.0~50892 X-Git-Url: http://git.cyclocoop.org/%22%2C%20generer_url_ecrire%28?a=commitdiff_plain;h=537d5b9b90dfe3fe92c18e4f299e17b61c328320;p=lhc%2Fweb%2Fwiklou.git revert revert (r27446): I gave the OK for this feature. sprintfDate is a big tent, supporting all languages with a general format syntax. It already has specific support for European languages in the form of genitive month names and roman numerals. There's no reason we can't extend support to other continents as well. --- diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 92d742c63e..c82e5718ac 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -51,6 +51,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 1405) Add wgUseNPPatrol option to control patroling for new articles on Special:Newpages * LogLine hook added to allow formatting custom entries in Special:Log. +* Support for Iranian calendar * (bug 1401) Allow hiding logged-in users, bots and patrolled pages on Special:Newpages * ChangesListInsertArticleLink hook added for adding extra article info to RC. * MediaWikiPerformAction hook added for diverting control after the main diff --git a/languages/Language.php b/languages/Language.php index 9d54e1519d..b5303ed893 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -90,6 +90,13 @@ class Language { 'sep', 'oct', 'nov', 'dec' ); + static public $mIranianCalendarMonthMsgs = array( + 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3', + 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6', + 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9', + 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12' + ); + /** * Create a language object for a given language code */ @@ -393,6 +400,11 @@ class Language { return $this->getMessageFromDB( self::$mWeekdayAbbrevMsgs[$key-1] ); } + function getIranianCalendarMonthName( $key ) { + return $this->getMessageFromDB( self::$mIranianCalendarMonthMsgs[$key-1] ); + } + + /** * Used by date() and time() to adjust the time output. * @public @@ -464,6 +476,11 @@ class Language { * xx Literal x * xg Genitive month name * + * xij j (day number) in Iranian calendar + * xiF F (month name) in Iranian calendar + * xin n (month number) in Iranian calendar + * xiY Y (full year) in Iranian calendar + * * Characters enclosed in double quotes will be considered literal (with * the quotes themselves removed). Unmatched quotes will be considered * literal quotes. Example: @@ -487,13 +504,18 @@ class Language { $roman = false; $unix = false; $rawToggle = false; + $iranian = false; for ( $p = 0; $p < strlen( $format ); $p++ ) { $num = false; $code = $format[$p]; if ( $code == 'x' && $p < strlen( $format ) - 1 ) { $code .= $format[++$p]; } - + + if ( $code === 'xi' && $p < strlen( $format ) - 1 ) { + $code .= $format[++$p]; + } + switch ( $code ) { case 'xx': $s .= 'x'; @@ -520,6 +542,10 @@ class Language { case 'j': $num = intval( substr( $ts, 6, 2 ) ); break; + case 'xij': + if ( !$iranian ) $iranian = self::tsToIranian( $ts ); + $num = $iranian[2]; + break; case 'l': if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts ); $s .= $this->getWeekdayName( gmdate( 'w', $unix ) + 1 ); @@ -540,10 +566,14 @@ class Language { case 'W': if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts ); $num = gmdate( 'W', $unix ); - break; + break; case 'F': $s .= $this->getMonthName( substr( $ts, 4, 2 ) ); break; + case 'xiF': + if ( !$iranian ) $iranian = self::tsToIranian( $ts ); + $s .= $this->getIranianCalendarMonthName( $iranian[1] ); + break; case 'm': $num = substr( $ts, 4, 2 ); break; @@ -553,6 +583,10 @@ class Language { case 'n': $num = intval( substr( $ts, 4, 2 ) ); break; + case 'xin': + if ( !$iranian ) $iranian = self::tsToIranian( $ts ); + $num = $iranian[1]; + break; case 't': if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts ); $num = gmdate( 't', $unix ); @@ -560,10 +594,14 @@ class Language { case 'L': if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts ); $num = gmdate( 'L', $unix ); - break; + break; case 'Y': $num = substr( $ts, 0, 4 ); break; + case 'xiY': + if ( !$iranian ) $iranian = self::tsToIranian( $ts ); + $num = $iranian[0]; + break; case 'y': $num = substr( $ts, 2, 2 ); break; @@ -648,6 +686,64 @@ class Language { return $s; } + private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); + private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 ); + /** + * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert + * Gregorian dates to Iranian dates. Originally written in C, it + * is released under the terms of GNU Lesser General Public + * License. Conversion to PHP was performed by Niklas Laxström. + * + * Link: http://www.farsiweb.info/jalali/jalali.c + */ + private static function tsToIranian( $ts ) { + $gy = substr( $ts, 0, 4 ) -1600; + $gm = substr( $ts, 4, 2 ) -1; + $gd = substr( $ts, 6, 2 ) -1; + + # Days passed from the beginning (including leap years) + $gDayNo = 365*$gy + + floor(($gy+3) / 4) + - floor(($gy+99) / 100) + + floor(($gy+399) / 400); + + + // Add days of the past months of this year + for( $i = 0; $i < $gm; $i++ ) { + $gDayNo += self::$GREG_DAYS[$i]; + } + + // Leap years + if ( $gm > 1 && (($gy%4===0 && $gy%100!==0 || ($gy%400==0)))) { + $gDayNo++; + } + + // Days passed in current month + $gDayNo += $gd; + + $jDayNo = $gDayNo - 79; + + $jNp = floor($jDayNo / 12053); + $jDayNo %= 12053; + + $jy = 979 + 33*$jNp + 4*floor($jDayNo/1461); + $jDayNo %= 1461; + + if ( $jDayNo >= 366 ) { + $jy += floor(($jDayNo-1)/365); + $jDayNo = floor(($jDayNo-1)%365); + } + + for ( $i = 0; $i < 11 && $jDayNo >= self::$IRANIAN_DAYS[$i]; $i++ ) { + $jDayNo -= self::$IRANIAN_DAYS[$i]; + } + + $jm= $i+1; + $jd= $jDayNo+1; + + return array($jy, $jm, $jd); + } + /** * Roman number formatting up to 3000 */ @@ -1917,4 +2013,4 @@ class Language { $text = $this->getMessageFromDB( $msg ); return str_replace( '$1', $this->formatNum( $size ), $text ); } -} \ No newline at end of file +} diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 531944b25e..dd108e7943 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -3000,4 +3000,18 @@ $1', 'watchlisttools-edit' => 'View and edit watchlist', 'watchlisttools-raw' => 'Edit raw watchlist', +# Iranian month names +'iranian-calendar-m1' => 'Farvardin', # only translate this message to other languages if you have to change it +'iranian-calendar-m2' => 'Ordibehesht', # only translate this message to other languages if you have to change it +'iranian-calendar-m3' => 'Khordad', # only translate this message to other languages if you have to change it +'iranian-calendar-m4' => 'Tir', # only translate this message to other languages if you have to change it +'iranian-calendar-m5' => 'Mordad', # only translate this message to other languages if you have to change it +'iranian-calendar-m6' => 'Shahrivar', # only translate this message to other languages if you have to change it +'iranian-calendar-m7' => 'Mehr', # only translate this message to other languages if you have to change it +'iranian-calendar-m8' => 'Aban', # only translate this message to other languages if you have to change it +'iranian-calendar-m9' => 'Azar', # only translate this message to other languages if you have to change it +'iranian-calendar-m10' => 'Dey', # only translate this message to other languages if you have to change it +'iranian-calendar-m11' => 'Bahman', # only translate this message to other languages if you have to change it +'iranian-calendar-m12' => 'Esfand', # only translate this message to other languages if you have to change it + ); diff --git a/maintenance/language/messageTypes.inc b/maintenance/language/messageTypes.inc index 8cf3a3eae6..3cb8e90079 100644 --- a/maintenance/language/messageTypes.inc +++ b/maintenance/language/messageTypes.inc @@ -214,6 +214,18 @@ $wgOptionalMessages = array( 'size-kilobytes', 'size-megabytes', 'size-gigabytes', + 'iranian-calendar-m1', + 'iranian-calendar-m2', + 'iranian-calendar-m3', + 'iranian-calendar-m4', + 'iranian-calendar-m5', + 'iranian-calendar-m6', + 'iranian-calendar-m7', + 'iranian-calendar-m8', + 'iranian-calendar-m9', + 'iranian-calendar-m10', + 'iranian-calendar-m11', + 'iranian-calendar-m12', ); /** EXIF messages, which may be set as optional in several checks, but are generally mandatory */ diff --git a/maintenance/language/messages.inc b/maintenance/language/messages.inc index 5e7733f8e3..88984d13a2 100644 --- a/maintenance/language/messages.inc +++ b/maintenance/language/messages.inc @@ -2226,6 +2226,20 @@ $wgMessageStructure = array( 'watchlisttools-edit', 'watchlisttools-raw', ), + 'iranian-dates' => array( + 'iranian-calendar-m1', + 'iranian-calendar-m2', + 'iranian-calendar-m3', + 'iranian-calendar-m4', + 'iranian-calendar-m5', + 'iranian-calendar-m6', + 'iranian-calendar-m7', + 'iranian-calendar-m8', + 'iranian-calendar-m9', + 'iranian-calendar-m10', + 'iranian-calendar-m11', + 'iranian-calendar-m12', + ), ); /** Comments for each block */ $wgBlockComments = array(