[ Index ] |
|
Code source de PRADO 3.0.6 |
1 <?php 2 /** 3 * TDateFromat formatting component. 4 * 5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com> 6 * @link http://www.pradosoft.com/ 7 * @copyright Copyright © 2005 PradoSoft 8 * @license http://www.pradosoft.com/license/ 9 * @version $Id: TDateFormat.php 1397 2006-09-07 07:55:53Z wei $ 10 * @package System.I18N 11 */ 12 13 /** 14 * Get the DateFormat class. 15 */ 16 Prado::using('System.I18N.core.DateFormat'); 17 18 /** 19 * Get the parent control class. 20 */ 21 Prado::using('System.I18N.TI18NControl'); 22 23 /** 24 * To format dates and/or time according to the current locale use 25 * <code> 26 * <com:TDateFormat Pattern="dd:MMM:yyyy" Value="01/01/2001" /> 27 *</code> 28 * The date will be formatted according to the current locale (or culture) 29 * using the format specified by 'Pattern' attribute. 30 * 31 * To format date and/or time for a locale (e.g. de_DE) include a Culture 32 * attribute, for example: 33 * <code> 34 * <com:TDateFormat Culture="de_DE" Value="01/01/2001 12:00" /> 35 * </code> 36 * The date will be formatted according to this format. 37 * 38 * If no Pattern was specified then the date will be formatted with the 39 * default format (both date and time). If no value for the date is specified 40 * then the current date will be used. E.g.: <code><com:TDateFormat /></code> 41 * will result in the current date, formatted with default localized pattern. 42 * 43 * Namespace: System.I18N 44 * 45 * Properties 46 * - <b>Value</b>, date, 47 * <br>Gets or sets the date to format. The tag content is used as Value 48 * if the Value property is not specified. 49 * - <b>Pattern</b>, string, 50 * <br>Gets or sets the formatting pattern. The predefined patterns are 51 * 'fulldate', 'longdate', 'mediumdate', 'shortdate', 'fulltime', 52 * 'longtime', 'mediumtime', and 'shorttime'. Custom patterns can specified 53 * when the Pattern property does not match the predefined patterns. 54 * 55 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com> 56 * @version v1.0, last update on Sat Dec 11 15:25:11 EST 2004 57 * @package System.I18N 58 */ 59 class TDateFormat extends TI18NControl 60 { 61 /** 62 * Default DateFormat, set to the application culture. 63 * @var DateFormat 64 */ 65 protected static $formatter; 66 67 /** 68 * A set of pattern presets and their respective formatting shorthand. 69 * @var array 70 */ 71 static private $_patternPresets = array( 72 'fulldate'=>'P','full'=>'P', 73 'longdate'=>'D','long'=>'d', 74 'mediumdate'=>'p','medium'=>'p', 75 'shortdate'=>'d','short'=>'d', 76 'fulltime'=>'Q', 'longtime'=>'T', 77 'mediumtime'=>'q', 'shorttime'=>'t'); 78 79 /** 80 * Sets the date time formatting pattern. 81 * @param string format pattern. 82 */ 83 public function setPattern($value) 84 { 85 $this->setViewState('Pattern',$value,''); 86 } 87 88 /** 89 * Gets the date time format pattern. 90 * @return string format pattern. 91 */ 92 public function getPattern() 93 { 94 $string = $this->getViewState('Pattern',''); 95 96 $pattern = null; 97 98 //try the subpattern of "date time" presets 99 $subpatterns = explode(' ',$string,2); 100 $datetime = array(); 101 if(count($subpatterns)==2) 102 { 103 $datetime[] = $this->getPreset($subpatterns[0]); 104 $datetime[] = $this->getPreset($subpatterns[1]); 105 } 106 107 //we have a good subpattern 108 if(count($datetime) == 2 109 && strlen($datetime[0]) == 1 110 && strlen($datetime[1]) == 1) 111 { 112 $pattern = $datetime; 113 } 114 else //no subpattern, try the presets 115 $pattern = $this->getPreset($string); 116 117 //no presets found, use the string as the pattern 118 //and let the DateFormat handle it. 119 if(is_null($pattern)) 120 $pattern = $string; 121 if (!is_array($pattern) && strlen($pattern) == 0) 122 $pattern = null; 123 return $pattern; 124 } 125 126 /** 127 * For a given string, try and find a preset pattern. 128 * @param string the preset pattern name 129 * @return string a preset pattern if found, null otherwise. 130 */ 131 protected function getPreset($string) 132 { 133 $string = strtolower($string); 134 foreach(self::$_patternPresets as $pattern => $preset) 135 { 136 if($string == $pattern) 137 return $preset; 138 } 139 } 140 141 /** 142 * Get the date-time value for this control. 143 * @return string date time value. 144 */ 145 public function getValue() 146 { 147 $value = $this->getViewState('Value',''); 148 if(empty($value)) 149 return time(); 150 return $value; 151 } 152 153 /** 154 * Set the date-time value for this control. 155 * @param string the date-time value. 156 */ 157 public function setValue($value) 158 { 159 $this->setViewState('Value',$value,''); 160 } 161 162 /** 163 * Renders the localized version of the date-time value. 164 * If the culture is not specified, the default application 165 * culture will be used. 166 * This method overrides parent's implementation. 167 */ 168 protected function getFormattedDate() 169 { 170 $app = $this->getApplication()->getGlobalization(); 171 172 //initialized the default class wide formatter 173 if(is_null(self::$formatter)) 174 self::$formatter = new DateFormat($app->getCulture()); 175 176 $culture = $this->getCulture(); 177 178 //return the specific cultural formatted date time 179 if(strlen($culture) && $app->getCulture() !== $culture) 180 { 181 $formatter = new DateFormat($culture); 182 return $formatter->format($this->getValue(), 183 $this->getPattern(), 184 $this->getCharset()); 185 } 186 //return the application wide culture formatted date time. 187 $result = self::$formatter->format($this->getValue(), 188 $this->getPattern(), 189 $this->getCharset()); 190 return $result; 191 } 192 193 public function render($writer) 194 { 195 $writer->write($this->getFormattedDate()); 196 } 197 198 } 199 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |