r28515 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r28514‎ | r28515 | r28516 >
Date:14:35, 15 December 2007
Author:rotem
Status:old
Tags:
Comment:
Adding xjt (to check the number of days in the Hebrew month), fixing methods order, cleaning up Hebrew calendar code and comments.
Modified paths:
  • /trunk/phase3/languages/Language.php (modified) (history)

Diff [purge]

Index: trunk/phase3/languages/Language.php
@@ -509,6 +509,7 @@
510510 *
511511 * xjj j (day number) in Hebrew calendar
512512 * xjF F (month name) in Hebrew calendar
 513+ * xjt t (days in month) in Hebrew calendar
513514 * xjx xg (genitive month name) in Hebrew calendar
514515 * xjn n (month number) in Hebrew calendar
515516 * xjY Y (full year) in Hebrew calendar
@@ -648,6 +649,10 @@
649650 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
650651 $num = gmdate( 't', $unix );
651652 break;
 653+ case 'xjt':
 654+ if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
 655+ $num = $hebrew[3];
 656+ break;
652657 case 'L':
653658 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
654659 $num = gmdate( 'L', $unix );
@@ -829,27 +834,27 @@
830835 # Month number when March = 1, February = 12
831836 $month -= 2;
832837 if( $month <= 0 ) {
833 - # January of February
 838+ # January or February
834839 $month += 12;
835840 $year--;
836841 }
837842
838843 # Days since 1 March - calculating 30 days a month,
839844 # and then adding the missing number of days
840 - $day += intval( 7 * $month / 12 + 30 * ( $month - 1 ) );
 845+ $dayOfYear = $day + intval( 7 * $month / 12 + 30 * ( $month - 1 ) );
841846 # Calculate Hebrew year for days after 1 Nisan
842847 $hebrewYear = $year + 3760;
843848 # Passover date for this year (as days since 1 March)
844849 $passover = self::passoverDate( $hebrewYear );
845 - if( $day <= $passover - 15 ) {
 850+ if( $dayOfYear <= $passover - 15 ) {
846851 # Day is before 1 Nisan (passover is 15 Nisan) - it is the previous year
847852 # Next year's passover (as days since 1 March)
848853 $anchor = $passover;
849854 # Add days since previous year's 1 March
850 - $day += 365;
 855+ $dayOfYear += 365;
851856 if( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
852857 # Leap year
853 - $day++;
 858+ $dayOfYear++;
854859 }
855860 # Previous year
856861 $year--;
@@ -862,7 +867,7 @@
863868 }
864869
865870 # Days since 1 Nisan
866 - $day -= $passover - 15;
 871+ $dayOfYear -= $passover - 15;
867872 # Difference between this year's passover date by gregorian calendar,
868873 # and the next year's one + 12 days. This should be 1 days for a regular year,
869874 # but 0 for incomplete one, 2 for complete, and those + 30 days of Adar I
@@ -874,15 +879,22 @@
875880 $anchor++;
876881 }
877882
 883+ # Check the year pattern
 884+ # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
 885+ # This is mod 30, to work on both leap years (which add 30 days of Adar I)
 886+ # and non-leap years
 887+ $yearPattern = $anchor % 30;
 888+
878889 # Calculate day in the month from number of days sine 1 Nisan
879 - # Don't check Adar - if the day is not in adar, we will stop before;
880 - # if it is in adar, we will use it to check if it is Adar I or Adar II
 890+ # Don't check Adar - if the day is not in Adar, we will stop before;
 891+ # if it is in Adar, we will use it to check if it is Adar I or Adar II
 892+ $day = $dayOfYear;
881893 for( $month = 0; $month < 11; $month++ ) {
882894 # Calculate days in this month
883 - if( $month == 7 && $anchor % 30 == 2 ) {
 895+ if( $month == 7 && $yearPattern == 2 ) {
884896 # Cheshvan in a complete year (otherwise as the rule below)
885897 $days = 30;
886 - } else if( $month == 8 && $anchor % 30 == 0 ) {
 898+ } else if( $month == 8 && $yearPattern == 0 ) {
887899 # Kislev in an incomplete year (otherwise as the rule below)
888900 $days = 29;
889901 } else {
@@ -905,44 +917,31 @@
906918 # Recalculate month number so that we start from Tishrei
907919 $month = ( $month + 6 ) % 12 + 1;
908920
909 - # Fix Adar
910 - if( $month == 6 && $anchor >= 30 ) {
911 - # This *is* adar, and this year is leap
912 - if( $day > 30 ) {
913 - # Adar II
914 - $month = 14;
915 - $day -= 30;
 921+ # Get Adar type and number of days
 922+ if( $month == 6 ) {
 923+ # Adar
 924+ if( $anchor >= 30 ) {
 925+ # Leap year - check type and set number of days
 926+ if( $day > 30 ) {
 927+ # Adar II - 29 days
 928+ $month = 14;
 929+ $day -= 30;
 930+ $days = 29;
 931+ } else {
 932+ # Adar I - 30 days
 933+ $month = 13;
 934+ $days = 30;
 935+ }
916936 } else {
917 - # Adar I
918 - $month = 13;
 937+ # Non-leap year - just set number of days (29)
 938+ $days = 29;
919939 }
920940 }
921941
922 - return array( $hebrewYear, $month, $day );
 942+ return array( $hebrewYear, $month, $day, $days );
923943 }
924944
925945 /**
926 - * Algorithm to convert Gregorian dates to Thai solar dates.
927 - *
928 - * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
929 - *
930 - * @param string $ts 14-character timestamp
931 - * @return array converted year, month, day
932 - */
933 - private static function tsToThai( $ts ) {
934 - $gy = substr( $ts, 0, 4 );
935 - $gm = substr( $ts, 4, 2 );
936 - $gd = substr( $ts, 6, 2 );
937 -
938 - # Add 543 years to the Gregorian calendar
939 - # Months and days are identical
940 - $gy_thai = $gy + 543;
941 -
942 - return array( $gy_thai, $gm, $gd );
943 - }
944 -
945 -
946 - /**
947946 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
948947 * Used for Hebrew date.
949948 */
@@ -973,6 +972,27 @@
974973 }
975974
976975 /**
 976+ * Algorithm to convert Gregorian dates to Thai solar dates.
 977+ *
 978+ * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
 979+ *
 980+ * @param string $ts 14-character timestamp
 981+ * @return array converted year, month, day
 982+ */
 983+ private static function tsToThai( $ts ) {
 984+ $gy = substr( $ts, 0, 4 );
 985+ $gm = substr( $ts, 4, 2 );
 986+ $gd = substr( $ts, 6, 2 );
 987+
 988+ # Add 543 years to the Gregorian calendar
 989+ # Months and days are identical
 990+ $gy_thai = $gy + 543;
 991+
 992+ return array( $gy_thai, $gm, $gd );
 993+ }
 994+
 995+
 996+ /**
977997 * Roman number formatting up to 3000
978998 */
979999 static function romanNumeral( $num ) {

Status & tagging log