[pLog-svn] r4794 - in plog/branches/lifetype-1.2/class: locale test/tests/locale

oscar at devel.lifetype.net oscar at devel.lifetype.net
Tue Feb 20 17:28:23 EST 2007


Author: oscar
Date: 2007-02-20 17:28:23 -0500 (Tue, 20 Feb 2007)
New Revision: 4794

Modified:
   plog/branches/lifetype-1.2/class/locale/locale.class.php
   plog/branches/lifetype-1.2/class/test/tests/locale/locale_test.class.php
Log:
same changes to Locale::formatDateGMT() and a unit test for this method, just to make sure that I didn't break anything.


Modified: plog/branches/lifetype-1.2/class/locale/locale.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/locale/locale.class.php	2007-02-20 22:08:24 UTC (rev 4793)
+++ plog/branches/lifetype-1.2/class/locale/locale.class.php	2007-02-20 22:28:23 UTC (rev 4794)
@@ -1,4 +1,4 @@
-<?php
+""<?php
 
     /**
 	 * \defgroup Locale
@@ -658,10 +658,19 @@
 		 */
         function formatDateGMT( $timeStamp, $format = null, $blog = null )
         {
-            // load the file if it hadn't been loaded yet       
-            if( !is_array($this->_messages))
-                $this->_loadLocaleFile();       
-        
+	
+
+			// load the file if it hadn't been loaded yet		
+			if( !is_array($this->_messages))
+				$this->_loadLocaleFile();	
+		
+			// 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);
+            $timeZoneSec = date("Z", $time);
             if ( $blog ) {
                 //
                 // The blog was specified.  Use it to get the time offset
@@ -675,81 +684,116 @@
                     $timeStamp->addSeconds( $timeDiff * 3600 );
                 else
                     $timeStamp->subtractSeconds( $timeDiff * (-3600));
-            
-            }
-            lt_include( PLOG_CLASS_PATH."class/data/Date.class.php" );
-            // Get the unix time stamp 
-            $time = $timeStamp->getTimestamp(DATE_FORMAT_UNIXTIME);
+            }				
+				
+			$text = $format;				
+				
+			if( strpos( "%a", $text )  !== FALSE ) {
+            	$weekdayId = gmdate( "w", $time );
+	            $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 = gmdate( "w", $time );
+				$text = str_replace( "%A", $this->_messages["days"][$weekdayId], $text );	
+			}
+			if( strpos( "%b", $text ) !== FALSE ) {
+            	$monthId = gmdate( "n", $time );
+	            $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);
 
-            // This does not take into account the time offset that the user
-            // specified, since we are converting the time to GMT.  Since the
-            // offset is just used to handle time zone differences, it is not 
-            // necessary, as the time GMT is still the same.
-            
-            // timestamp only returns the values in english, so we should translate
-            // them before using
-            $monthId = gmdate( "n", $time );
-            $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 = gmdate( "w", $time );
-            $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);
-            
-            // 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"] = gmdate( "d", $time );
-            $values["%e"] = intval(gmdate( "d", $time ));
-            $values["%j"] = gmdate( "j", $time );
-            $values["%H"] = gmdate( "H", $time );
-            $values["%I"] = gmdate( "g", $time );
-            $values["%p"] = gmdate( "a", $time );
-            $values["%P"] = gmdate( "A", $time );
-            $values["%M"] = gmdate( "i", $time );
-            $values["%m"] = gmdate( "m", $time );
-            $values["%S"] = gmdate( "s", $time );
-            $values["%y"] = gmdate( "y", $time );
-            $values["%Y"] = gmdate( "Y", $time );
-            $values["%O"] = "+0000";
-            $values["%%"] = "%";
-            $values["%T"] = $this->getDateOrdinal( gmdate( "d", $time ) )." ".$this->tr("of")." ".$monthStr;
-            $values["%D"] = $this->getDateOrdinal( gmdate( "d", $time )  );
-            /* Start Hack By FiFtHeLeMeNt For Persian Language */
+				$text = str_replace( "%b", $shortMonthStr, $text );				
+			}
+			if( strpos( "%B", $text ) !== FALSE ) {
+            	$monthId = gmdate( "n", $time );
+				$text = str_replace( "%B", $this->_messages["months"][$monthId-1], $text );				
+			}	
+			if( strpos( "%d", $text ) !== FALSE ) {
+				$text = str_replace( "%d", gmdate( "d", $time ), $text );
+			}
+			if( strpos( "%e", $text ) !== FALSE ) {
+				$text = str_replace( "%e", intval(gmdate( "d", $time )), $text );
+			}
+			if( strpos( "%j", $text ) !== FALSE ) {
+				$text = str_replace( "%j", gmdate( "j", $time ), $text );
+			}
+			if( strpos( "%H", $text ) !== FALSE ) {
+				$text = str_replace( "%H", gmdate( "H", $time ), $text );				
+			}			
+			if( strpos( "%I", $text ) !== FALSE ) {
+				$text = str_replace( "%I", gmdate( "g", $time ), $text );	
+			}
+			if( strpos( "%p", $text ) !== FALSE ) {
+				$text = str_replace( "%p", gmdate( "a", $time ), $text );					
+			}
+			if( strpos( "%P", $text ) !== FALSE ) {
+				$text = str_replace( "%P", gmdate( "A", $time ), $text );				
+			}
+			if( strpos( "%M", $text ) !== FALSE ) {
+				$text = str_replace( "%M",  gmdate( "i", $time ), $text );					
+			}
+			if( strpos( "%m", $text ) !== FALSE ) {
+				$text = str_replace( "%m", gmdate( "m", $time ), $text );				
+			}
+			if( strpos( "%S", $text ) !== FALSE ) {
+				$text = str_replace( "%S", gmdate( "s", $time ), $text );					
+			}
+			if( strpos( "%y", $text ) !== FALSE ) {
+				$text = str_replace( "%y", gmdate( "y", $time ), $text );									
+			}
+			if( strpos( "%Y", $text ) !== FALSE ) {
+				$text = str_replace( "%Y", gmdate( "Y", $time ), $text );				
+			}
+			if( strpos( "%O", $text ) !== FALSE ) {
+				$text = str_replace( "%O", "+0000", $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->getDateOrdinal( gmdate( "d", $time ))." ".$this->tr("of")." ".$monthStr, $text );				
+			}			
+			if( strpos( "%D", $text ) !== FALSE ) {
+				$text = str_replace( "%D", $this->getDateOrdinal( gmdate( "d", $time )), $text );
+			}
+			
     		if ( $this->_code == 'fa_IR' )
-    		{
+    		{			
 	    		lt_include( PLOG_CLASS_PATH."class/data/jalalicalendar.class.php" );
 	            list( $jyear, $jmonth, $jday ) = JalaliCalendar::gregorian_to_jalali(gmdate( "Y", $time ), gmdate( "m", $time ), gmdate( "d", $time ));
-	            $values["%q"] = JalaliCalendar::Convertnumber2farsi($jyear);
-	            $values["%w"]= JalaliCalendar::Convertnumber2farsi($jmonth);
-	            $values["%o"] = JalaliCalendar::Convertnumber2farsi($jday);
-	            $values["%R"] = JalaliCalendar::monthname($jmonth);
-	            $values["%T"] = JalaliCalendar::Convertnumber2farsi(gmdate( "H", $time ));
-	            $values["%U"] = JalaliCalendar::Convertnumber2farsi(gmdate( "i", $time ));
-    		}
-            /* End Hack By FiFtHeLeMeNt For Persian Language */     
+	
+				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(gmdate( "H", $time )), $text );
+				}			
+				if( strpos( "%U", $text ) !== FALSE ) {
+		      		$text = str_replace( "%U", JalaliCalendar::Convertnumber2farsi(gmdate( "i", $time )), $text );
+				}
+			}			
 
-            $text = $format;
-            foreach( array_keys($values) as $key ) {
-                if( ereg($key, $text) )
-                    $text = str_replace( $key, $values[$key], $text );
-            }
-
-            return $text;
+			return $text;
         }
         
         /**

Modified: plog/branches/lifetype-1.2/class/test/tests/locale/locale_test.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/test/tests/locale/locale_test.class.php	2007-02-20 22:08:24 UTC (rev 4793)
+++ plog/branches/lifetype-1.2/class/test/tests/locale/locale_test.class.php	2007-02-20 22:28:23 UTC (rev 4794)
@@ -68,5 +68,33 @@
 			$this->assertEquals( "5th of February", $this->l->formatDate( $d, "%T" ));			
 			$this->assertEquals( "5th", $this->l->formatDate( $d, "%D" ));						
 		}
+		
+		/**
+		 * Tests that the Locale::testFormatDateGMT() method also behaves
+		 * as expected
+		 */
+		function testFormatDateGMT()
+		{
+			$d = new Timestamp( "20070205230000" );			
+			
+			$this->assertEquals( "Mon", $this->l->formatDateGMT( $d, "%a" ));
+			$this->assertEquals( "Monday", $this->l->formatDateGMT( $d, "%A" ));
+			$this->assertEquals( "Feb", $this->l->formatDateGMT( $d, "%b" ));
+			$this->assertEquals( "February", $this->l->formatDateGMT( $d, "%B" ));
+			$this->assertEquals( "05", $this->l->formatDateGMT( $d, "%d" ));
+			$this->assertEquals( "5", $this->l->formatDateGMT( $d, "%j" ));
+			$this->assertEquals( "21", $this->l->formatDateGMT( $d, "%H" ));
+			$this->assertEquals( "9", $this->l->formatDateGMT( $d, "%I" ));
+			$this->assertEquals( "pm", $this->l->formatDateGMT( $d, "%p" ));			
+			$this->assertEquals( "PM", $this->l->formatDateGMT( $d, "%P" ));						
+			$this->assertEquals( "00", $this->l->formatDateGMT( $d, "%M" ));			
+			$this->assertEquals( "02", $this->l->formatDateGMT( $d, "%m" ));
+			$this->assertEquals( "00", $this->l->formatDateGMT( $d, "%S" ));
+			$this->assertEquals( "07", $this->l->formatDateGMT( $d, "%y" ));			
+			$this->assertEquals( "2007", $this->l->formatDateGMT( $d, "%Y" ));
+			$this->assertEquals( "%", $this->l->formatDateGMT( $d, "%%" ));
+			$this->assertEquals( "5th of February", $this->l->formatDateGMT( $d, "%T" ));			
+			$this->assertEquals( "5th", $this->l->formatDateGMT( $d, "%D" ));						
+		}		
 	}
 ?>
\ No newline at end of file



More information about the pLog-svn mailing list