[pLog-svn] r4792 - plog/branches/lifetype-1.2/class/locale

oscar at devel.lifetype.net oscar at devel.lifetype.net
Tue Feb 20 17:04:33 EST 2007


Author: oscar
Date: 2007-02-20 17:04:33 -0500 (Tue, 20 Feb 2007)
New Revision: 4792

Modified:
   plog/branches/lifetype-1.2/class/locale/locale.class.php
Log:
These changes don't exactly make the code pretty, but they save a few hundred function calls every time we call Locale::formatDate(). The way this method is currently implemented is so that even if we're only asked to replace one formatter in the $format string, we'll pre-calculate all of them and then do one single str_replace for the one we need. The new implementation first checks whether a formatter is part of the string and if so calculate its value and to the str_replace. According to the xdebug trace we save 2000-3000 function and method calls when using the new approach.

I will also modify Locale::formatDateGMT to follow this new approach.

Modified: plog/branches/lifetype-1.2/class/locale/locale.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/locale/locale.class.php	2007-02-20 21:29:22 UTC (rev 4791)
+++ plog/branches/lifetype-1.2/class/locale/locale.class.php	2007-02-20 22:04:33 UTC (rev 4792)
@@ -490,31 +490,14 @@
 		{
 			// load the file if it hadn't been loaded yet		
 			if( !is_array($this->_messages))
-				$this->_loadLocaleFile();		
+				$this->_loadLocaleFile();	
 		
-        	// timestamp only returns the values in english, so we should translate
-            // them before using
-            $monthId    = (int)$timeStamp->getMonth();
-            $monthStr   = $this->_messages["months"][$monthId-1];
-            if( !empty( $this->_messages["monthsshort"] ) )
-            	$shortMonthStr = $this->_messages["monthsshort"][$monthId-1];
-            else
-            	$shortMonthStr = function_exists('html_entity_decode') ? htmlentities(substr(html_entity_decode($monthStr), 0, 3 )) : substr($monthStr, 0, 3);
-
-            //print("monthstr: $monthStr");
-            // and the same for the weekdays
-            $weekdayId = $timeStamp->getWeekdayId();
-            $weekday = $this->_messages["days"][$weekdayId];
-            if( !empty( $this->_messages["weekdaysshort"] ) )
-            	$shortWeekday = $this->_messages["weekdaysshort"][$weekdayId];
-            else
-            	$shortWeekday = function_exists('html_entity_decode') ? htmlentities(substr(html_entity_decode($weekday), 0, 3 )) : substr($weekday, 0, 3);
-
-            lt_include( PLOG_CLASS_PATH."class/data/Date.class.php" );
+			// if the user did not specify a format, let's use the default one
+			if( $format == null )
+				$format = $this->_dateFormat;
+				
             // Get the unix time stamp 
             $time = $timeStamp->getTimestamp(DATE_FORMAT_UNIXTIME);
-
-            // Get the time zone offset for the server, on the specified date
             $timeZoneSec = date("Z", $time);
             if ( $blog ) {
                 //
@@ -523,63 +506,124 @@
                 $timeDiff = 0;
                 $blogSettings = $blog->getSettings();
                 $timeDiff = $blogSettings->getValue( 'time_offset' );
-                
+
                 // The following line relies on the fact that the result will
                 // be an int.
                 $timeZoneSec += ( $timeDiff * 3600 );
-            }
-            // Now convert the time zone seconds to hours and minutes
-            $timeZoneHours = intval( abs($timeZoneSec) / 3600 );
-            $timeZoneMins = intval(( abs($timeZoneSec) % 3600 ) / 60 );
-            $timeZoneDirection = ($timeZoneSec < 0 ) ? "-" : "+";
-            
+            }				
+				
+			$text = $format;				
+				
+			if( strpos( "%a", $text )  !== FALSE ) {
+	            $weekdayId = $timeStamp->getWeekdayId();
+	            $weekday = $this->_messages["days"][$weekdayId];
+	            if( !empty( $this->_messages["weekdaysshort"] ) )
+	            	$shortWeekday = $this->_messages["weekdaysshort"][$weekdayId];
+	            else
+	            	$shortWeekday = function_exists('html_entity_decode') ? htmlentities(substr(html_entity_decode($weekday), 0, 3 )) : substr($weekday, 0, 3);
+	
+				$text = str_replace( "%a", $shortWeekday, $text );
+			}
+			if( strpos( "%A", $text ) !== FALSE ) {
+	            $weekdayId = $timeStamp->getWeekdayId();	
+				$text = str_replace( "%A", $this->_messages["days"][$weekdayId], $text );	
+			}
+			if( strpos( "%b", $text ) !== FALSE ) {
+	            $monthId    = (int)$timeStamp->getMonth();
+	            $monthStr   = $this->_messages["months"][$monthId-1];
+	            if( !empty( $this->_messages["monthsshort"] ) )
+	            	$shortMonthStr = $this->_messages["monthsshort"][$monthId-1];
+	            else
+	            	$shortMonthStr = function_exists('html_entity_decode') ? htmlentities(substr(html_entity_decode($monthStr), 0, 3 )) : substr($monthStr, 0, 3);
+
+				$text = str_replace( "%b", $shortMonthStr, $text );				
+			}
+			if( strpos( "%B", $text ) !== FALSE ) {
+	            $monthId    = (int)$timeStamp->getMonth();
+				$text = str_replace( "%B", $this->_messages["months"][$monthId-1], $text );				
+			}	
+			if( strpos( "%d", $text ) !== FALSE ) {
+				$text = str_replace( "%d", ($timeStamp->getDay() < 10) ? "0".$timeStamp->getDay() : $timeStamp->getDay(), $text );
+			}
+			if( strpos( "%e", $text ) !== FALSE ) {
+				$text = str_replace( "%e", intval($timeStamp->getDay()), $text );
+			}
+			if( strpos( "%j", $text ) !== FALSE ) {
+				$text = str_replace( "%j", $timeStamp->getDay(), $text );
+			}
+			if( strpos( "%H", $text ) !== FALSE ) {
+				$text = str_replace( "%H", $timeStamp->getHour(), $text );				
+			}			
+			if( strpos( "%I", $text ) !== FALSE ) {
+				$text = str_replace( "%I", ($timeStamp->getHour() != 0) ? ($timeStamp->getHour() > 12) ? $timeStamp->getHour()-12 : $timeStamp->getHour()+0 : 12, $text );	
+			}
+			if( strpos( "%p", $text ) !== FALSE ) {
+				$text = str_replace( "%p", $timeStamp->getHour() >= 12 ? "pm" : "am", $text );					
+			}
+			if( strpos( "%P", $text ) !== FALSE ) {
+				$text = str_replace( "%P", $timeStamp->getHour() >= 12 ? "PM" : "AM", $text );				
+			}
+			if( strpos( "%M", $text ) !== FALSE ) {
+				$text = str_replace( "%M",  $timeStamp->getMinutes(), $text );					
+			}
+			if( strpos( "%m", $text ) !== FALSE ) {
+				$text = str_replace( "%m", $timeStamp->getMonth(), $text );				
+			}
+			if( strpos( "%S", $text ) !== FALSE ) {
+				$text = str_replace( "%S", $timeStamp->getSeconds(), $text );					
+			}
+			if( strpos( "%y", $text ) !== FALSE ) {
+				$text = str_replace( "%y", substr($timeStamp->getYear(), 2, 4 ), $text );									
+			}
+			if( strpos( "%Y", $text ) !== FALSE ) {
+				$text = str_replace( "%Y", $timeStamp->getYear(), $text );				
+			}
+			if( strpos( "%O", $text ) !== FALSE ) {
+	            // Now convert the time zone seconds to hours and minutes
+	            $timeZoneHours = intval( abs($timeZoneSec) / 3600 );
+	            $timeZoneMins = intval(( abs($timeZoneSec) % 3600 ) / 60 );
+	            $timeZoneDirection = ($timeZoneSec < 0 ) ? "-" : "+";				
+				
+				$text = str_replace( "%O", sprintf( "%s%02d%02d", $timeZoneDirection, $timeZoneHours, $timeZoneMins ), $text );				
+			}					
+			if( strpos( "%%", $text ) !== FALSE ) {
+				$text = str_replace( "%%", "%", $text );				
+			}
+			if( strpos( "%T", $text ) !== FALSE ) {
+	            $monthId    = (int)$timeStamp->getMonth();
+	            $monthStr   = $this->_messages["months"][$monthId-1];				
+	
+				$text = str_replace( "%T", $this->getDayOrdinal( $timeStamp )." ".$this->tr("of")." ".$monthStr, $text );				
+			}			
+			if( strpos( "%D", $text ) !== FALSE ) {
+				$text = str_replace( "%D", $this->getDayOrdinal( $timeStamp ), $text );				
+			}
 			
-			// if the user did not specify a format, let's use the default one
-			if( $format == null )
-				$format = $this->_dateFormat;
-
-            // now we can continue normally
-            $values["%a"] = $shortWeekday;
-			$values["%A"] = $weekday;
-			$values["%b"] = $shortMonthStr;
-			$values["%B"] = $monthStr;
-			$values["%d"] = ($timeStamp->getDay() < 10) ? "0".$timeStamp->getDay() : $timeStamp->getDay();
-			$values["%e"] = intval($timeStamp->getDay());
-			$values["%j"] = $timeStamp->getDay();
-			$values["%H"] = $timeStamp->getHour();
-			$values["%I"] = ($timeStamp->getHour() != 0) ? ($timeStamp->getHour() > 12) ? $timeStamp->getHour()-12 : $timeStamp->getHour()+0 : 12;
-			$values["%p"] = $timeStamp->getHour() >= 12 ? "pm" : "am";
-			$values["%P"] = $timeStamp->getHour() >= 12 ? "PM" : "AM";
-			$values["%M"] = $timeStamp->getMinutes();
-			$values["%m"] = $timeStamp->getMonth();
-			$values["%S"] = $timeStamp->getSeconds();
-			$values["%y"] = substr($timeStamp->getYear(), 2, 4 );
-			$values["%Y"] = $timeStamp->getYear();
-			$values["%O"] = sprintf( "%s%02d%02d", $timeZoneDirection, $timeZoneHours, $timeZoneMins );
-			$values["%%"] = "%";
-            $values["%T"] = $this->getDayOrdinal( $timeStamp )." ".$this->tr("of")." ".$monthStr;
-            $values["%D"] = $this->getDayOrdinal( $timeStamp );
-
-            /* Start Hack By FiFtHeLeMeNt For Persian Language */
     		if ( $this->_code == 'fa_IR' )
-    		{
+    		{			
 	    		lt_include( PLOG_CLASS_PATH."class/data/jalalicalendar.class.php" );
-	      		list( $jyear, $jmonth, $jday ) = JalaliCalendar::gregorian_to_jalali($timeStamp->getYear(), $timeStamp->getMonth(), $timeStamp->getDay());
-	      		$values["%q"] = JalaliCalendar::Convertnumber2farsi($jyear);
-	      		$values["%w"]= JalaliCalendar::Convertnumber2farsi($jmonth);
-	      		$values["%o"] = JalaliCalendar::Convertnumber2farsi($jday);
-	      		$values["%R"] = JalaliCalendar::monthname($jmonth);
-	      		$values["%T"] = JalaliCalendar::Convertnumber2farsi($timeStamp->getHour());
-	      		$values["%U"] = JalaliCalendar::Convertnumber2farsi($timeStamp->getMinutes());
-    		}
-			/* End Hack By FiFtHeLeMeNt For Persian Language */		
+	            list( $jyear, $jmonth, $jday ) = JalaliCalendar::gregorian_to_jalali(gmdate( "Y", $time ), gmdate( "m", $time ), gmdate( "d", $time ));
+	
+				if( strpos( "%q", $text ) !== FALSE ) {
+		      		$text = str_replace( "%q", JalaliCalendar::Convertnumber2farsi($jyear), $text );				
+				}			
+				if( strpos( "%w", $text ) !== FALSE ) {
+		      		$text = str_replace( "%w", JalaliCalendar::Convertnumber2farsi($jmonth), $text );
+				}			
+				if( strpos( "%o", $text ) !== FALSE ) {
+		      		$text = str_replace( "%o", JalaliCalendar::Convertnumber2farsi($jday), $text );
+				}			
+				if( strpos( "%R", $text ) !== FALSE ) {
+		      		$text = str_replace( "%R", JalaliCalendar::monthname($jmonth), $text );
+				}			
+				if( strpos( "%T", $text ) !== FALSE ) {
+		      		$text = str_replace( "%T", JalaliCalendar::Convertnumber2farsi($timeStamp->getHour()), $text );					
+				}			
+				if( strpos( "%U", $text ) !== FALSE ) {
+		      		$text = str_replace( "%U", JalaliCalendar::Convertnumber2farsi($timeStamp->getMinutes()), $text );
+				}
+			}			
 
-			$text = $format;
-			foreach( array_keys($values) as $key ) {
-				if( strpos( $text, $key ) !== FALSE )
-                    $text = str_replace( $key, $values[$key], $text );
-			}
-
 			return $text;
 		}
 		



More information about the pLog-svn mailing list