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

Debug toolbar