sfDateTimeFormatInfo.class.php 15 KB

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