Development

/branches/1.3/lib/i18n/sfDateTimeFormatInfo.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/i18n/sfDateTimeFormatInfo.class.php

Revision 23810, 14.6 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /**
4  * sfDateTimeFormatInfo class file.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the BSD License.
8  *
9  * Copyright(c) 2004 by Qiang Xue. All rights reserved.
10  *
11  * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
12  * The latest version of PRADO can be obtained from:
13  * {@link http://prado.sourceforge.net/}
14  *
15  * @author     Wei Zhuo <weizhuo[at]gmail[dot]com>
16  * @version    $Id$
17  * @package    symfony
18  * @subpackage i18n
19  */
20
21 /**
22  * Defines how DateTime values are formatted and displayed, depending
23  * on the culture.
24  *
25  * This class contains information, such as date patterns, time patterns,
26  * and AM/PM designators.
27  *
28  * To create a sfDateTimeFormatInfo for a specific culture, create a
29  * sfCultureInfo for that culture and retrieve the sfCultureInfo.sfDateTimeFormat
30  * property. For example:
31  * <code>
32  * $culture = new sfCultureInfo('en_AU');
33  * $dtfi = $culture->DateTimeFormat;
34  * </code>
35  *
36  * To create a sfDateTimeFormatInfo for the invariant culture, use
37  * <code>
38  * sfDateTimeFormatInfo::getInstance($culture=null);
39  * </code>
40  * you may pass a sfCultureInfo parameter $culture to get the sfDateTimeFormatInfo
41  * for a specific culture.
42  *
43  * sfDateTime values are formatted using standard or custom patterns stored in
44  * the properties of a sfDateTimeFormatInfo.
45  *
46  * The standard patterns can be replaced with custom patterns by setting the
47  * associated properties of sfDateTimeFormatInfo.
48  *
49  * The following table lists the standard format characters for each standard
50  * pattern and the associated sfDateTimeFormatInfo property that can be set to
51  * modify the standard pattern. The format characters are case-sensitive;
52  * for example, 'g' and 'G' represent slightly different patterns.
53  *
54  * <code>
55  *  Format Character    Associated Property     Example Format Pattern (en-US)
56  *  --------------------------------------------------------------------------
57  *  d                   ShortDatePattern        MM/dd/yyyy
58  *  D                   LongDatePattern         dddd, dd MMMM yyyy
59  *  F                   FullDateTimePattern     dddd, dd MMMM yyyy HH:mm:ss
60  *  m, M                MonthDayPattern         MMMM dd
61  *  r, R                RFC1123Pattern          ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
62  *  s                   SortableDateTimePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss
63  *  t                   ShortTimePattern        HH:mm
64  *  T                   LongTimePattern         HH:mm:ss
65  *  Y                   YearMonthPattern        yyyy MMMM
66  *  --------------------------------------------------------------------------
67  * </code>
68  *
69  * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
70  * @version v1.0, last update on Fri Dec 03 22:30:31 EST 2004
71  * @package    symfony
72  * @subpackage i18n
73  */
74 class sfDateTimeFormatInfo
75 {
76   /**
77    * ICU date time formatting data.
78    */
79   protected $data = array();
80
81   /**
82    * A list of properties that are accessable/writable.
83    */
84   protected $properties = array();
85
86   /**
87    * Allows functions that begins with 'set' to be called directly
88    * as an attribute/property to retrieve the value.
89    *
90    * @return mixed
91    */
92   function __get($name)
93   {
94     $getProperty = 'get'.$name;
95     if (in_array($getProperty, $this->properties))
96     {
97       return $this->$getProperty();
98     }
99     else
100     {
101       throw new sfException(sprintf('Property %s does not exists.', $name));
102     }
103   }
104
105   /**
106    * Allows functions that begins with 'set' to be called directly
107    * as an attribute/property to set the value.
108    */
109   function __set($name, $value)
110   {
111     $setProperty = 'set'.$name;
112     if (in_array($setProperty, $this->properties))
113     {
114       $this->$setProperty($value);
115     }
116     else
117     {
118       throw new sfException(sprintf('Property %s can not be set.', $name));
119     }
120   }
121
122   /**
123    * Initializes a new writable instance of the sfDateTimeFormatInfo class
124    * that is dependent on the ICU data for date time formatting
125    * information. <b>N.B.</b>You should not initialize this class directly
126    * unless you know what you are doing. Please use use
127    * sfDateTimeFormatInfo::getInstance() to create an instance.
128    *
129    * @param array   $data ICU data for date time formatting.
130    * @see getInstance()
131    */
132   function __construct($data = array())
133   {
134     $this->properties = get_class_methods($this);
135
136     if (empty($data))
137     {
138       throw new sfException('Please provide the ICU data to initialize.');
139     }
140
141     $this->data = $data;
142   }
143
144   /**
145    * Gets the internal ICU data for date time formatting.
146    *
147    * @return array ICU date time formatting data.
148    */
149   protected function getData()
150   {
151     return $this->data;
152   }
153
154   /**
155    * Gets the default sfDateTimeFormatInfo that is culture-independent (invariant).
156    *
157    * @return sfDateTimeFormatInfo default sfDateTimeFormatInfo.
158    */
159   static function getInvariantInfo()
160   {
161     static $invariant;
162
163     if (null === $invariant)
164     {
165       $invariant = sfCultureInfo::getInvariantCulture()->DateTimeFormat;
166     }
167
168     return $invariant;
169   }
170
171   /**
172    * Returns the sfDateTimeFormatInfo associated with the specified culture.
173    *
174    * @param sfCultureInfo   $culture  the culture that gets the sfDateTimeFormat property.
175    * @return sfDateTimeFormatInfo sfDateTimeFormatInfo for the specified
176    * culture.
177    */
178   static function getInstance($culture = null)
179   {
180     if ($culture instanceof sfCultureInfo)
181     {
182       return $culture->DateTimeFormat;
183     }
184     else if (is_string($culture))
185     {
186       return sfCultureInfo::getInstance($culture)->DateTimeFormat;
187     }
188     else
189     {
190       return sfCultureInfo::getInvariantCulture()->DateTimeFormat;
191     }
192   }
193
194   /**
195    * A one-dimensional array of type String containing
196    * the culture-specific abbreviated names of the days
197    * of the week. The array for InvariantInfo contains
198    * "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", and "Sat".
199    *
200    * @return array abbreviated day names
201    */
202   function getAbbreviatedDayNames()
203   {
204     return $this->data['dayNames']['format']['abbreviated'];
205   }
206
207   /**
208    * Sets the abbreviated day names. The value should be
209    * an array of string starting with Sunday and ends in Saturady.
210    * For example,
211    * <code>array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");</code>
212    *
213    * @param array $value abbreviated day names.
214    */
215   function setAbbreviatedDayNames($value)
216   {
217     $this->data['dayNames']['format']['abbreviated'] = $value;
218   }
219
220   /**
221    * A one-dimensional array of type String containing
222    * the culture-specific narrow names of the days
223    * of the week. The array for InvariantInfo contains
224    * "S", "M", "T", "W", "T", "F", and "S".
225    *
226    * @return array narrow day names
227    */
228   function getNarrowDayNames()
229   {
230     return $this->data['dayNames']['stand-alone']['narrow'];
231   }
232
233   /**
234    * Sets the narrow day names. The value should be
235    * an array of string starting with Sunday and ends in Saturady.
236    * For example,
237    * <code>array("S", "M", "T", "W", "T", "F", "S");</code>
238    *
239    * @param array $value narrow day names.
240    */
241   function setNarrowDayNames($value)
242   {
243     $this->data['dayNames']['stand-alone']['narrow'] = $value;
244   }
245
246   /**
247    * A one-dimensional array of type String containing the
248    * culture-specific full names of the days of the week.
249    * The array for InvariantInfo contains "Sunday", "Monday",
250    * "Tuesday", "Wednesday", "Thursday", "Friday", and "Saturday".
251    *
252    * @return array day names
253    */
254   function getDayNames()
255   {
256     return $this->data['dayNames']['format']['wide'];
257   }
258
259   /**
260    * Sets the day names. The value should be
261    * an array of string starting with Sunday and ends in Saturady.
262    * For example,
263    * <code>array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
264    * "Friday", "Saturday".);</code>
265    *
266    * @param array $value day names.
267    */
268   function setDayNames($value)
269   {
270     $this->data['dayNames']['format']['wide'] = $value;
271   }
272
273   /**
274    * A one-dimensional array of type String containing the
275    * culture-specific narrow names of the months. The array
276    * for InvariantInfo contains "J", "F", "M", "A", "M", "J",
277    * "J", "A", "S", "O", "N", and "D".
278    *
279    * @return array narrow month names.
280    */
281   function getNarrowMonthNames()
282   {
283     return $this->data['monthNames']['stand-alone']['narrow'];
284   }
285
286   /**
287    * Sets the narrow month names. The value should be
288    * an array of string starting with J and ends in D.
289    * For example,
290    * <code>array("J","F","M","A","M","J","J","A","S","O","N","D");</code>
291    *
292    * @param array $value month names.
293    */
294   function setNarrowMonthNames($value)
295   {
296     $this->data['monthNames']['stand-alone']['narrow'] = $value;
297   }
298
299   /**
300    * A one-dimensional array of type String containing the
301    * culture-specific abbreviated names of the months. The array
302    * for InvariantInfo contains "Jan", "Feb", "Mar", "Apr", "May",
303    * "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", and "Dec".
304    *
305    * Returns wide names if abbreviated names doesn't exist.
306    *
307    * @return array abbreviated month names.
308    */
309   function getAbbreviatedMonthNames()
310   {
311     if (isset($this->data['monthNames']['format']['abbreviated']))
312     {
313       return $this->data['monthNames']['format']['abbreviated'];
314     }
315     else
316     {
317       return $this->data['monthNames']['format']['wide'];
318     }
319   }
320
321   /**
322    * Sets the abbreviated month names. The value should be
323    * an array of string starting with Jan and ends in Dec.
324    * For example,
325    * <code>array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
326    * "Jul", "Aug", "Sep","Oct","Nov","Dec");</code>
327    *
328    * @param array $value month names.
329    */
330   function setAbbreviatedMonthNames($value)
331   {
332     $this->data['monthNames']['format']['abbreviated'] = $value;
333   }
334
335   /**
336    * A one-dimensional array of type String containing the
337    * culture-specific full names of the months. The array for
338    * InvariantInfo contains "January", "February", "March", "April",
339    * "May", "June", "July", "August", "September", "October", "November",
340    * and "December"
341    *
342    * @return array month names.
343    */
344   function getMonthNames()
345   {
346     return $this->data['monthNames']['format']['wide'];
347   }
348
349   /**
350    * Sets the month names. The value should be
351    * an array of string starting with Janurary and ends in December.
352    * For example,
353    * <code>array("January", "February", "March", "April", "May", "June",
354    * "July", "August", "September","October","November","December");</code>
355    *
356    * @param array $value month names.
357    */
358   function setMonthNames($value)
359   {
360     $this->data['monthNames']['format']['wide'] = $value;
361   }
362
363   /**
364    * A string containing the name of the era.
365    *
366    * @param int $era era The integer representing the era.
367    * @return string the era name.
368    */
369   function getEra($era)
370   {
371     return $this->data['eras']['abbreviated'][$era];
372   }
373
374   /**
375    * The string designator for hours that are "ante meridiem" (before noon).
376    * The default for InvariantInfo is "AM".
377    *
378    * @return string AM designator.
379    */
380   function getAMDesignator()
381   {
382     $result = $this->getAMPMMarkers();
383
384     return $result[0];
385   }
386
387   /**
388    * Sets the AM Designator. For example, 'AM'.
389    *
390    * @param string $value AM designator.
391    */
392   function setAMDesignator($value)
393   {
394     $markers = $this->getAMPMMarkers();
395     $markers[0] = $value;
396     $this->setAMPMMarkers($markers);
397   }
398
399   /**
400    * The string designator for hours that are "post meridiem" (after noon).
401    * The default for InvariantInfo is "PM".
402    *
403    * @return string PM designator.
404    */
405   function getPMDesignator()
406   {
407     $result = $this->getAMPMMarkers();
408
409     return $result[1];
410   }
411
412   /**
413    * Sets the PM Designator. For example, 'PM'.
414    *
415    * @param string $value PM designator.
416    */
417   function setPMDesignator($value)
418   {
419     $markers = $this->getAMPMMarkers();
420     $markers[1] = $value;
421     $this->setAMPMMarkers($markers);
422   }
423
424   /**
425    * Gets the AM and PM markers array.
426    * Default InvariantInfo for AM and PM is <code>array('AM','PM');</code>
427    *
428    * @return array AM and PM markers
429    */
430   function getAMPMMarkers()
431   {
432     return $this->data['AmPmMarkers'];
433   }
434
435   /**
436    * Sets the AM and PM markers array.
437    * For example <code>array('AM','PM');</code>
438    *
439    * @param array $value AM and PM markers
440    */
441   function setAMPMMarkers($value)
442   {
443     $this->data['AmPmMarkers'] = $value;
444   }
445
446   /**
447    * Returns the full time pattern "HH:mm:ss z" (default).
448    * This is culture sensitive.
449    *
450    * @return string pattern "HH:mm:ss z".
451    */
452   function getFullTimePattern()
453   {
454     return $this->data['DateTimePatterns'][0];
455   }
456
457   /**
458    * Returns the long time pattern "HH:mm:ss z" (default).
459    * This is culture sensitive.
460    *
461    * @return string pattern "HH:mm:ss z".
462    */
463   function getLongTimePattern()
464   {
465     return $this->data['DateTimePatterns'][1];
466   }
467
468   /**
469    * Returns the medium time pattern "HH:mm:ss" (default).
470    * This is culture sensitive.
471    *
472    * @return string pattern "HH:mm:ss".
473    */
474   function getMediumTimePattern()
475   {
476     return $this->data['DateTimePatterns'][2];
477   }
478
479   /**
480    * Returns the short time pattern "HH:mm" (default).
481    * This is culture sensitive.
482    *
483    * @return string pattern "HH:mm".
484    */
485   function getShortTimePattern()
486   {
487     return $this->data['DateTimePatterns'][3];
488   }
489
490   /**
491    * Returns the full date pattern "EEEE, yyyy MMMM dd" (default).
492    * This is culture sensitive.
493    * @return string pattern "EEEE, yyyy MMMM dd".
494    */
495   function getFullDatePattern()
496   {
497     return $this->data['DateTimePatterns'][4];
498   }
499
500   /**
501    * Returns the long date pattern "yyyy MMMM d" (default).
502    * This is culture sensitive.
503    * @return string pattern "yyyy MMMM d".
504    */
505   function getLongDatePattern()
506   {
507     return $this->data['DateTimePatterns'][5];
508   }
509
510   /**
511    * Returns the medium date pattern "yyyy MMMM d" (default).
512    * This is culture sensitive.
513    * @return string pattern "yyyy MMM d".
514    */
515   function getMediumDatePattern()
516   {
517     return $this->data['DateTimePatterns'][6];
518   }
519
520   /**
521    * Returns the short date pattern "yy/MM/dd" (default).
522    * This is culture sensitive.
523    *
524    * @return string pattern "yy/MM/dd".
525    */
526   function getShortDatePattern()
527   {
528     return $this->data['DateTimePatterns'][7];
529   }
530
531   /**
532    * Returns the date time order pattern, "{1} {0}" (default).
533    * This is culture sensitive.
534    *
535    * @return string pattern "{1} {0}".
536    */
537   function getDateTimeOrderPattern()
538   {
539     return $this->data['DateTimePatterns'][8];
540   }
541
542   /**
543    * Formats the date and time in a culture sensitive paterrn.
544    * The default is "Date Time".
545    *
546    * @return string date and time formated
547    */
548   function formatDateTime($date, $time)
549   {
550     return str_replace(array('{0}','{1}'), array($time, $date), $this->getDateTimeOrderPattern());
551   }
552 }
553
Note: See TracBrowser for help on using the browser.