3 * TDateFromat formatting component.
5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
13 * Get the DateFormat class.
15 Prado::using('System.I18N.core.DateFormat');
18 * Get the parent control class.
20 Prado::using('System.I18N.TI18NControl');
23 * To format dates and/or time according to the current locale use
25 * <com:TDateFormat Pattern="dd:MMM:yyyy" Value="01/01/2001" />
27 * The date will be formatted according to the current locale (or culture)
28 * using the format specified by 'Pattern' attribute.
30 * To format date and/or time for a locale (e.g. de_DE) include a Culture
31 * attribute, for example:
33 * <com:TDateFormat Culture="de_DE" Value="01/01/2001 12:00" />
35 * The date will be formatted according to this format.
37 * If no Pattern was specified then the date will be formatted with the
38 * default format (both date and time). If no value for the date is specified
39 * then the current date will be used. E.g.: <code><com:TDateFormat /></code>
40 * will result in the current date, formatted with default localized pattern.
42 * Namespace: System.I18N
45 * - <b>Value</b>, date,
46 * <br>Gets or sets the date to format. The tag content is used as Value
47 * if the Value property is not specified.
48 * - <b>Pattern</b>, string,
49 * <br>Gets or sets the formatting pattern. The predefined patterns are
50 * 'fulldate', 'longdate', 'mediumdate', 'shortdate', 'fulltime',
51 * 'longtime', 'mediumtime', and 'shorttime'. Custom patterns can specified
52 * when the Pattern property does not match the predefined patterns.
53 * - <b>DefaultText</b>, string,
54 * <br>Gets or sets the default text. If Value is not set, DefaultText will be
55 * shown instead of todays date and time.
57 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
58 * @version v1.0, last update on Sat Dec 11 15:25:11 EST 2004
59 * @package System.I18N
61 class TDateFormat extends TI18NControl implements IDataRenderer
64 * Default DateFormat, set to the application culture.
67 protected static $formatter;
70 * A set of pattern presets and their respective formatting shorthand.
73 static private $_patternPresets = array(
74 'fulldate'=>'P','full'=>'P',
75 'longdate'=>'D','long'=>'d',
76 'mediumdate'=>'p','medium'=>'p',
77 'shortdate'=>'d','short'=>'d',
78 'fulltime'=>'Q', 'longtime'=>'T',
79 'mediumtime'=>'q', 'shorttime'=>'t');
82 * Sets the date time formatting pattern.
83 * @param string format pattern.
85 public function setPattern($value)
87 $this->setViewState('Pattern',$value,'');
91 * Gets the date time format pattern.
92 * @return string format pattern.
94 public function getPattern()
96 $string = $this->getViewState('Pattern','');
100 //try the subpattern of "date time" presets
101 $subpatterns = explode(' ',$string,2);
103 if(count($subpatterns)==2)
105 $datetime[] = $this->getPreset($subpatterns[0]);
106 $datetime[] = $this->getPreset($subpatterns[1]);
109 //we have a good subpattern
110 if(count($datetime) == 2
111 && strlen($datetime[0]) == 1
112 && strlen($datetime[1]) == 1)
114 $pattern = $datetime;
116 else //no subpattern, try the presets
117 $pattern = $this->getPreset($string);
119 //no presets found, use the string as the pattern
120 //and let the DateFormat handle it.
123 if (!is_array($pattern) && strlen($pattern) == 0)
129 * For a given string, try and find a preset pattern.
130 * @param string the preset pattern name
131 * @return string a preset pattern if found, null otherwise.
133 protected function getPreset($string)
135 $string = strtolower($string);
136 foreach(self::$_patternPresets as $pattern => $preset)
138 if($string == $pattern)
144 * Get the date-time value for this control.
145 * @return string date time value.
147 public function getValue()
149 $value = $this->getViewState('Value','');
152 $defaultText = $this->getDefaultText();
153 if(empty($defaultText))
160 * Set the date-time value for this control.
161 * @param string the date-time value.
163 public function setValue($value)
165 $this->setViewState('Value',$value,'');
169 * Get the default text value for this control.
170 * @return string default text value
172 public function getDefaultText()
174 return $this->getViewState('DefaultText','');
178 * Set the default text value for this control.
179 * @param string default text value
181 public function setDefaultText($value)
183 $this->setViewState('DefaultText',$value,'');
187 * Get the date-time value for this control.
188 * This method is required by {@link IDataRenderer}.
189 * It is the same as {@link getValue()}.
190 * @return string date time value.
194 public function getData()
196 return $this->getValue();
200 * Set the date-time value for this control.
201 * This method is required by {@link IDataRenderer}.
202 * It is the same as {@link setValue()}.
203 * @param string the date-time value.
207 public function setData($value)
209 $this->setValue($value);
213 * Renders the localized version of the date-time value.
214 * If the culture is not specified, the default application
215 * culture will be used.
216 * This method overrides parent's implementation.
218 protected function getFormattedDate()
220 $value = $this->getValue();
221 $defaultText = $this->getDefaultText();
222 if(empty($value) && !empty($defaultText))
223 return $this->getDefaultText();
225 $app = $this->getApplication()->getGlobalization();
227 //initialized the default class wide formatter
228 if(self::$formatter===null)
229 self::$formatter = new DateFormat($app->getCulture());
231 $culture = $this->getCulture();
233 //return the specific cultural formatted date time
234 if(strlen($culture) && $app->getCulture() !== $culture)
236 $formatter = new DateFormat($culture);
237 return $formatter->format($value,
239 $this->getCharset());
241 //return the application wide culture formatted date time.
242 $result = self::$formatter->format($value,
244 $this->getCharset());
248 public function render($writer)
250 $writer->write($this->getFormattedDate());