Format.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Locale
  17. * @subpackage Format
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /**
  23. * include needed classes
  24. */
  25. require_once 'Zend/Locale/Data.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Locale
  29. * @subpackage Format
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Locale_Format
  34. {
  35. const STANDARD = 'auto';
  36. private static $_options = array('date_format' => null,
  37. 'number_format' => null,
  38. 'format_type' => 'iso',
  39. 'fix_date' => false,
  40. 'locale' => null,
  41. 'cache' => null,
  42. 'disableCache' => false,
  43. 'precision' => null);
  44. /**
  45. * Sets class wide options, if no option was given, the actual set options will be returned
  46. * The 'precision' option of a value is used to truncate or stretch extra digits. -1 means not to touch the extra digits.
  47. * The 'locale' option helps when parsing numbers and dates using separators and month names.
  48. * The date format 'format_type' option selects between CLDR/ISO date format specifier tokens and PHP's date() tokens.
  49. * The 'fix_date' option enables or disables heuristics that attempt to correct invalid dates.
  50. * The 'number_format' option can be used to specify a default number format string
  51. * The 'date_format' option can be used to specify a default date format string, but beware of using getDate(),
  52. * checkDateFormat() and getTime() after using setOptions() with a 'format'. To use these four methods
  53. * with the default date format for a locale, use array('date_format' => null, 'locale' => $locale) for their options.
  54. *
  55. * @param array $options Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false,
  56. * locale = Zend_Locale | locale string, precision = whole number between -1 and 30
  57. * @throws Zend_Locale_Exception
  58. * @return Options array if no option was given
  59. */
  60. public static function setOptions(array $options = array())
  61. {
  62. self::$_options = self::_checkOptions($options) + self::$_options;
  63. return self::$_options;
  64. }
  65. /**
  66. * Internal function for checking the options array of proper input values
  67. * See {@link setOptions()} for details.
  68. *
  69. * @param array $options Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false,
  70. * locale = Zend_Locale | locale string, precision = whole number between -1 and 30
  71. * @throws Zend_Locale_Exception
  72. * @return Options array if no option was given
  73. */
  74. private static function _checkOptions(array $options = array())
  75. {
  76. if (count($options) == 0) {
  77. return self::$_options;
  78. }
  79. foreach ($options as $name => $value) {
  80. $name = strtolower($name);
  81. if ($name !== 'locale') {
  82. if (gettype($value) === 'string') {
  83. $value = strtolower($value);
  84. }
  85. }
  86. switch($name) {
  87. case 'number_format' :
  88. if ($value == Zend_Locale_Format::STANDARD) {
  89. $locale = self::$_options['locale'];
  90. if (isset($options['locale'])) {
  91. $locale = $options['locale'];
  92. }
  93. $options['number_format'] = Zend_Locale_Data::getContent($locale, 'decimalnumber');
  94. } else if ((gettype($value) !== 'string') and ($value !== NULL)) {
  95. require_once 'Zend/Locale/Exception.php';
  96. throw new Zend_Locale_Exception("Unknown number format type '" . gettype($value) . "'. "
  97. . "Format '$value' must be a valid number format string.");
  98. }
  99. break;
  100. case 'date_format' :
  101. if ($value == Zend_Locale_Format::STANDARD) {
  102. $locale = self::$_options['locale'];
  103. if (isset($options['locale'])) {
  104. $locale = $options['locale'];
  105. }
  106. $options['date_format'] = Zend_Locale_Format::getDateFormat($locale);
  107. } else if ((gettype($value) !== 'string') and ($value !== NULL)) {
  108. require_once 'Zend/Locale/Exception.php';
  109. throw new Zend_Locale_Exception("Unknown dateformat type '" . gettype($value) . "'. "
  110. . "Format '$value' must be a valid ISO or PHP date format string.");
  111. } else {
  112. if (((isset($options['format_type']) === true) and ($options['format_type'] == 'php')) or
  113. ((isset($options['format_type']) === false) and (self::$_options['format_type'] == 'php'))) {
  114. $options['date_format'] = Zend_Locale_Format::convertPhpToIsoFormat($value);
  115. }
  116. }
  117. break;
  118. case 'format_type' :
  119. if (($value != 'php') && ($value != 'iso')) {
  120. require_once 'Zend/Locale/Exception.php';
  121. throw new Zend_Locale_Exception("Unknown date format type '$value'. Only 'iso' and 'php'"
  122. . " are supported.");
  123. }
  124. break;
  125. case 'fix_date' :
  126. if (($value !== true) && ($value !== false)) {
  127. require_once 'Zend/Locale/Exception.php';
  128. throw new Zend_Locale_Exception("Enabling correction of dates must be either true or false"
  129. . "(fix_date='$value').");
  130. }
  131. break;
  132. case 'locale' :
  133. $options['locale'] = Zend_Locale::findLocale($value);
  134. break;
  135. case 'cache' :
  136. if ($value instanceof Zend_Cache_Core) {
  137. Zend_Locale_Data::setCache($value);
  138. }
  139. break;
  140. case 'disablecache' :
  141. Zend_Locale_Data::disableCache($value);
  142. break;
  143. case 'precision' :
  144. if ($value === NULL) {
  145. $value = -1;
  146. }
  147. if (($value < -1) || ($value > 30)) {
  148. require_once 'Zend/Locale/Exception.php';
  149. throw new Zend_Locale_Exception("'$value' precision is not a whole number less than 30.");
  150. }
  151. break;
  152. default:
  153. require_once 'Zend/Locale/Exception.php';
  154. throw new Zend_Locale_Exception("Unknown option: '$name' = '$value'");
  155. break;
  156. }
  157. }
  158. return $options;
  159. }
  160. /**
  161. * Changes the numbers/digits within a given string from one script to another
  162. * 'Decimal' representated the stardard numbers 0-9, if a script does not exist
  163. * an exception will be thrown.
  164. *
  165. * Examples for conversion from Arabic to Latin numerals:
  166. * convertNumerals('١١٠ Tests', 'Arab'); -> returns '100 Tests'
  167. * Example for conversion from Latin to Arabic numerals:
  168. * convertNumerals('100 Tests', 'Latn', 'Arab'); -> returns '١١٠ Tests'
  169. *
  170. * @param string $input String to convert
  171. * @param string $from Script to parse, see {@link Zend_Locale::getScriptList()} for details.
  172. * @param string $to OPTIONAL Script to convert to
  173. * @return string Returns the converted input
  174. * @throws Zend_Locale_Exception
  175. */
  176. public static function convertNumerals($input, $from, $to = null)
  177. {
  178. $from = strtolower($from);
  179. $source = Zend_Locale_Data::getContent('en', 'numberingsystem', $from);
  180. if (empty($source)) {
  181. require_once 'Zend/Locale/Exception.php';
  182. throw new Zend_Locale_Exception("Unknown script '$from'. Use 'Latn' for digits 0,1,2,3,4,5,6,7,8,9.");
  183. }
  184. if ($to !== null) {
  185. $to = strtolower($to);
  186. $target = Zend_Locale_Data::getContent('en', 'numberingsystem', $to);
  187. if (empty($target)) {
  188. require_once 'Zend/Locale/Exception.php';
  189. throw new Zend_Locale_Exception("Unknown script '$to'. Use 'Latn' for digits 0,1,2,3,4,5,6,7,8,9.");
  190. }
  191. } else {
  192. $target = '0123456789';
  193. }
  194. for ($x = 0; $x < 10; ++$x) {
  195. $asource[$x] = "/" . iconv_substr($source, $x, 1, 'UTF-8') . "/u";
  196. $atarget[$x] = iconv_substr($target, $x, 1, 'UTF-8');
  197. }
  198. return preg_replace($asource, $atarget, $input);
  199. }
  200. /**
  201. * Returns the normalized number from a localized one
  202. * Parsing depends on given locale (grouping and decimal)
  203. *
  204. * Examples for input:
  205. * '2345.4356,1234' = 23455456.1234
  206. * '+23,3452.123' = 233452.123
  207. * '12343 ' = 12343
  208. * '-9456' = -9456
  209. * '0' = 0
  210. *
  211. * @param string $input Input string to parse for numbers
  212. * @param array $options Options: locale, precision. See {@link setOptions()} for details.
  213. * @return string Returns the extracted number
  214. * @throws Zend_Locale_Exception
  215. */
  216. public static function getNumber($input, array $options = array())
  217. {
  218. $options = self::_checkOptions($options) + self::$_options;
  219. if (!is_string($input)) {
  220. return $input;
  221. }
  222. if (!self::isNumber($input, $options)) {
  223. require_once 'Zend/Locale/Exception.php';
  224. throw new Zend_Locale_Exception('No localized value in ' . $input . ' found, or the given number does not match the localized format');
  225. }
  226. // Get correct signs for this locale
  227. $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
  228. // Change locale input to be default number
  229. if ((strpos($input, $symbols['minus']) !== false) ||
  230. (strpos($input, '-') !== false)) {
  231. $input = strtr($input, array($symbols['minus'] => '', '-' => ''));
  232. $input = '-' . $input;
  233. }
  234. $input = str_replace($symbols['group'],'', $input);
  235. if (strpos($input, $symbols['decimal']) !== false) {
  236. if ($symbols['decimal'] != '.') {
  237. $input = str_replace($symbols['decimal'], ".", $input);
  238. }
  239. $pre = substr($input, strpos($input, '.') + 1);
  240. if ($options['precision'] === null) {
  241. $options['precision'] = strlen($pre);
  242. }
  243. if (strlen($pre) >= $options['precision']) {
  244. $input = substr($input, 0, strlen($input) - strlen($pre) + $options['precision']);
  245. }
  246. }
  247. return $input;
  248. }
  249. /**
  250. * Returns a locale formatted number depending on the given options.
  251. * The seperation and fraction sign is used from the set locale.
  252. * ##0.# -> 12345.12345 -> 12345.12345
  253. * ##0.00 -> 12345.12345 -> 12345.12
  254. * ##,##0.00 -> 12345.12345 -> 12,345.12
  255. *
  256. * @param string $input Localized number string
  257. * @param array $options Options: number_format, locale, precision. See {@link setOptions()} for details.
  258. * @return string locale formatted number
  259. * @throws Zend_Locale_Exception
  260. */
  261. public static function toNumber($value, array $options = array())
  262. {
  263. // load class within method for speed
  264. require_once 'Zend/Locale/Math.php';
  265. $value = Zend_Locale_Math::normalize($value);
  266. $value = Zend_Locale_Math::floatalize($value);
  267. $options = self::_checkOptions($options) + self::$_options;
  268. $options['locale'] = (string) $options['locale'];
  269. // Get correct signs for this locale
  270. $symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
  271. $oenc = iconv_get_encoding('internal_encoding');
  272. iconv_set_encoding('internal_encoding', 'UTF-8');
  273. // Get format
  274. $format = $options['number_format'];
  275. if ($format === null) {
  276. $format = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
  277. $format = self::_seperateFormat($format, $value, $options['precision']);
  278. } else {
  279. // seperate negative format pattern when available
  280. $format = self::_seperateFormat($format, $value, $options['precision']);
  281. if (strpos($format, '.')) {
  282. if (is_numeric($options['precision'])) {
  283. $value = Zend_Locale_Math::round($value, $options['precision']);
  284. } else {
  285. if (substr($format, iconv_strpos($format, '.') + 1, 3) == '###') {
  286. $options['precision'] = null;
  287. } else {
  288. $options['precision'] = iconv_strlen(iconv_substr($format, iconv_strpos($format, '.') + 1,
  289. iconv_strrpos($format, '0') - iconv_strpos($format, '.')));
  290. $format = iconv_substr($format, 0, iconv_strpos($format, '.') + 1) . '###'
  291. . iconv_substr($format, iconv_strrpos($format, '0') + 1);
  292. }
  293. }
  294. } else {
  295. $value = Zend_Locale_Math::round($value, 0);
  296. $options['precision'] = 0;
  297. }
  298. $value = Zend_Locale_Math::normalize($value);
  299. }
  300. if (iconv_strpos($format, '0') === false) {
  301. iconv_set_encoding('internal_encoding', $oenc);
  302. require_once 'Zend/Locale/Exception.php';
  303. throw new Zend_Locale_Exception('Wrong format... missing 0');
  304. }
  305. // get number parts
  306. $pos = iconv_strpos($value, '.');
  307. if ($pos !== false) {
  308. if ($options['precision'] === null) {
  309. $precstr = iconv_substr($value, $pos + 1);
  310. } else {
  311. $precstr = iconv_substr($value, $pos + 1, $options['precision']);
  312. if (iconv_strlen($precstr) < $options['precision']) {
  313. $precstr = $precstr . str_pad("0", ($options['precision'] - iconv_strlen($precstr)), "0");
  314. }
  315. }
  316. } else {
  317. if ($options['precision'] > 0) {
  318. $precstr = str_pad("0", ($options['precision']), "0");
  319. }
  320. }
  321. if ($options['precision'] === null) {
  322. if (isset($precstr)) {
  323. $options['precision'] = iconv_strlen($precstr);
  324. } else {
  325. $options['precision'] = 0;
  326. }
  327. }
  328. // get fraction and format lengths
  329. if (strpos($value, '.') !== false) {
  330. $number = substr((string) $value, 0, strpos($value, '.'));
  331. } else {
  332. $number = $value;
  333. }
  334. $prec = call_user_func(Zend_Locale_Math::$sub, $value, $number, $options['precision']);
  335. $prec = Zend_Locale_Math::floatalize($prec);
  336. $prec = Zend_Locale_Math::normalize($prec);
  337. if (iconv_strpos($prec, '-') !== false) {
  338. $prec = iconv_substr($prec, 1);
  339. }
  340. if (($prec == 0) and ($options['precision'] > 0)) {
  341. $prec = "0.0";
  342. }
  343. if (($options['precision'] + 2) > iconv_strlen($prec)) {
  344. $prec = str_pad((string) $prec, $options['precision'] + 2, "0", STR_PAD_RIGHT);
  345. }
  346. if (iconv_strpos($number, '-') !== false) {
  347. $number = iconv_substr($number, 1);
  348. }
  349. $group = iconv_strrpos($format, ',');
  350. $group2 = iconv_strpos ($format, ',');
  351. $point = iconv_strpos ($format, '0');
  352. // Add fraction
  353. $rest = "";
  354. if (iconv_strpos($format, '.')) {
  355. $rest = iconv_substr($format, iconv_strpos($format, '.') + 1);
  356. $length = iconv_strlen($rest);
  357. for($x = 0; $x < $length; ++$x) {
  358. if (($rest[0] == '0') || ($rest[0] == '#')) {
  359. $rest = iconv_substr($rest, 1);
  360. }
  361. }
  362. $format = iconv_substr($format, 0, iconv_strlen($format) - iconv_strlen($rest));
  363. }
  364. if ($options['precision'] == '0') {
  365. if (iconv_strrpos($format, '-') != 0) {
  366. $format = iconv_substr($format, 0, $point)
  367. . iconv_substr($format, iconv_strrpos($format, '#') + 2);
  368. } else {
  369. $format = iconv_substr($format, 0, $point);
  370. }
  371. } else {
  372. $format = iconv_substr($format, 0, $point) . $symbols['decimal']
  373. . iconv_substr($prec, 2);
  374. }
  375. $format .= $rest;
  376. // Add seperation
  377. if ($group == 0) {
  378. // no seperation
  379. $format = $number . iconv_substr($format, $point);
  380. } else if ($group == $group2) {
  381. // only 1 seperation
  382. $seperation = ($point - $group);
  383. for ($x = iconv_strlen($number); $x > $seperation; $x -= $seperation) {
  384. if (iconv_substr($number, 0, $x - $seperation) !== "") {
  385. $number = iconv_substr($number, 0, $x - $seperation) . $symbols['group']
  386. . iconv_substr($number, $x - $seperation);
  387. }
  388. }
  389. $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
  390. } else {
  391. // 2 seperations
  392. if (iconv_strlen($number) > ($point - $group)) {
  393. $seperation = ($point - $group);
  394. $number = iconv_substr($number, 0, iconv_strlen($number) - $seperation) . $symbols['group']
  395. . iconv_substr($number, iconv_strlen($number) - $seperation);
  396. if ((iconv_strlen($number) - 1) > ($point - $group + 1)) {
  397. $seperation2 = ($group - $group2 - 1);
  398. for ($x = iconv_strlen($number) - $seperation2 - 2; $x > $seperation2; $x -= $seperation2) {
  399. $number = iconv_substr($number, 0, $x - $seperation2) . $symbols['group']
  400. . iconv_substr($number, $x - $seperation2);
  401. }
  402. }
  403. }
  404. $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
  405. }
  406. // set negative sign
  407. if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $options['precision']) < 0) {
  408. if (iconv_strpos($format, '-') === false) {
  409. $format = $symbols['minus'] . $format;
  410. } else {
  411. $format = str_replace('-', $symbols['minus'], $format);
  412. }
  413. }
  414. iconv_set_encoding('internal_encoding', $oenc);
  415. return (string) $format;
  416. }
  417. private static function _seperateFormat($format, $value, $precision)
  418. {
  419. if (iconv_strpos($format, ';') !== false) {
  420. if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $precision) < 0) {
  421. $tmpformat = iconv_substr($format, iconv_strpos($format, ';') + 1);
  422. if ($tmpformat[0] == '(') {
  423. $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
  424. } else {
  425. $format = $tmpformat;
  426. }
  427. } else {
  428. $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
  429. }
  430. }
  431. return $format;
  432. }
  433. /**
  434. * Checks if the input contains a normalized or localized number
  435. *
  436. * @param string $input Localized number string
  437. * @param array $options Options: locale. See {@link setOptions()} for details.
  438. * @return boolean Returns true if a number was found
  439. */
  440. public static function isNumber($input, array $options = array())
  441. {
  442. $options = self::_checkOptions($options) + self::$_options;
  443. // Get correct signs for this locale
  444. $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
  445. $regexs = Zend_Locale_Format::_getRegexForType('decimalnumber', $options);
  446. $regexs = array_merge($regexs, Zend_Locale_Format::_getRegexForType('scientificnumber', $options));
  447. if (!empty($input) && ($input[0] == $symbols['decimal'])) {
  448. $input = 0 . $input;
  449. }
  450. foreach ($regexs as $regex) {
  451. preg_match($regex, $input, $found);
  452. if (isset($found[0])) {
  453. return true;
  454. }
  455. }
  456. return false;
  457. }
  458. /**
  459. * Internal method to convert cldr number syntax into regex
  460. *
  461. * @param string $type
  462. * @return string
  463. */
  464. private static function _getRegexForType($type, $options)
  465. {
  466. $decimal = Zend_Locale_Data::getContent($options['locale'], $type);
  467. $decimal = preg_replace('/[^#0,;\.\-Ee]/', '',$decimal);
  468. $patterns = explode(';', $decimal);
  469. if (count($patterns) == 1) {
  470. $patterns[1] = '-' . $patterns[0];
  471. }
  472. $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
  473. foreach($patterns as $pkey => $pattern) {
  474. $regex[$pkey] = '/^';
  475. $rest = 0;
  476. $end = null;
  477. if (strpos($pattern, '.') !== false) {
  478. $end = substr($pattern, strpos($pattern, '.') + 1);
  479. $pattern = substr($pattern, 0, -strlen($end) - 1);
  480. }
  481. if (strpos($pattern, ',') !== false) {
  482. $parts = explode(',', $pattern);
  483. $count = count($parts);
  484. foreach($parts as $key => $part) {
  485. switch ($part) {
  486. case '#':
  487. case '-#':
  488. if ($part[0] == '-') {
  489. $regex[$pkey] .= '[' . $symbols['minus'] . '-]{0,1}';
  490. } else {
  491. $regex[$pkey] .= '[' . $symbols['plus'] . '+]{0,1}';
  492. }
  493. if (($parts[$key + 1]) == '##0') {
  494. $regex[$pkey] .= '[0-9]{1,3}';
  495. } else if (($parts[$key + 1]) == '##') {
  496. $regex[$pkey] .= '[0-9]{1,2}';
  497. } else {
  498. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 1):"' . $pattern . '"');
  499. }
  500. break;
  501. case '##':
  502. if ($parts[$key + 1] == '##0') {
  503. $regex[$pkey] .= '(\\' . $symbols['group'] . '{0,1}[0-9]{2})*';
  504. } else {
  505. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 2):"' . $pattern . '"');
  506. }
  507. break;
  508. case '##0':
  509. if ($parts[$key - 1] == '##') {
  510. $regex[$pkey] .= '[0-9]';
  511. } else if (($parts[$key - 1] == '#') || ($parts[$key - 1] == '-#')) {
  512. $regex[$pkey] .= '(\\' . $symbols['group'] . '{0,1}[0-9]{3})*';
  513. } else {
  514. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 3):"' . $pattern . '"');
  515. }
  516. break;
  517. case '#0':
  518. if ($key == 0) {
  519. $regex[$pkey] .= '[0-9]*';
  520. } else {
  521. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 4):"' . $pattern . '"');
  522. }
  523. break;
  524. }
  525. }
  526. }
  527. if (strpos($pattern, 'E') !== false) {
  528. if (($pattern == '#E0') || ($pattern == '#E00')) {
  529. $regex[$pkey] .= '[' . $symbols['plus']. '+]{0,1}[0-9]{1,}(\\' . $symbols['decimal'] . '[0-9]{1,})*[eE][' . $symbols['plus']. '+]{0,1}[0-9]{1,}';
  530. } else if (($pattern == '-#E0') || ($pattern == '-#E00')) {
  531. $regex[$pkey] .= '[' . $symbols['minus']. '-]{0,1}[0-9]{1,}(\\' . $symbols['decimal'] . '[0-9]{1,})*[eE][' . $symbols['minus']. '-]{0,1}[0-9]{1,}';
  532. } else {
  533. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 5):"' . $pattern . '"');
  534. }
  535. }
  536. if (!empty($end)) {
  537. if ($end == '###') {
  538. $regex[$pkey] .= '(\\' . $symbols['decimal'] . '{1}[0-9]{1,}){0,1}';
  539. } else if ($end == '###-') {
  540. $regex[$pkey] .= '(\\' . $symbols['decimal'] . '{1}[0-9]{1,}){0,1}[' . $symbols['minus']. '-]';
  541. } else {
  542. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 6):"' . $pattern . '"');
  543. }
  544. }
  545. $regex[$pkey] .= '$/';
  546. }
  547. return $regex;
  548. }
  549. /**
  550. * Alias for getNumber
  551. *
  552. * @param string $value Number to localize
  553. * @param array $options Options: locale, precision. See {@link setOptions()} for details.
  554. * @return float
  555. */
  556. public static function getFloat($input, array $options = array())
  557. {
  558. return floatval(self::getNumber($input, $options));
  559. }
  560. /**
  561. * Returns a locale formatted integer number
  562. * Alias for toNumber()
  563. *
  564. * @param string $value Number to normalize
  565. * @param array $options Options: locale, precision. See {@link setOptions()} for details.
  566. * @return string Locale formatted number
  567. */
  568. public static function toFloat($value, array $options = array())
  569. {
  570. $options['number_format'] = Zend_Locale_Format::STANDARD;
  571. return self::toNumber($value, $options);
  572. }
  573. /**
  574. * Returns if a float was found
  575. * Alias for isNumber()
  576. *
  577. * @param string $input Localized number string
  578. * @param array $options Options: locale. See {@link setOptions()} for details.
  579. * @return boolean Returns true if a number was found
  580. */
  581. public static function isFloat($value, array $options = array())
  582. {
  583. return self::isNumber($value, $options);
  584. }
  585. /**
  586. * Returns the first found integer from an string
  587. * Parsing depends on given locale (grouping and decimal)
  588. *
  589. * Examples for input:
  590. * ' 2345.4356,1234' = 23455456
  591. * '+23,3452.123' = 233452
  592. * ' 12343 ' = 12343
  593. * '-9456km' = -9456
  594. * '0' = 0
  595. * '(-){0,1}(\d+(\.){0,1})*(\,){0,1})\d+'
  596. *
  597. * @param string $input Input string to parse for numbers
  598. * @param array $options Options: locale. See {@link setOptions()} for details.
  599. * @return integer Returns the extracted number
  600. */
  601. public static function getInteger($input, array $options = array())
  602. {
  603. $options['precision'] = 0;
  604. return intval(self::getFloat($input, $options));
  605. }
  606. /**
  607. * Returns a localized number
  608. *
  609. * @param string $value Number to normalize
  610. * @param array $options Options: locale. See {@link setOptions()} for details.
  611. * @return string Locale formatted number
  612. */
  613. public static function toInteger($value, array $options = array())
  614. {
  615. $options['precision'] = 0;
  616. $options['number_format'] = Zend_Locale_Format::STANDARD;
  617. return self::toNumber($value, $options);
  618. }
  619. /**
  620. * Returns if a integer was found
  621. *
  622. * @param string $input Localized number string
  623. * @param array $options Options: locale. See {@link setOptions()} for details.
  624. * @return boolean Returns true if a integer was found
  625. */
  626. public static function isInteger($value, array $options = array())
  627. {
  628. if (!self::isNumber($value, $options)) {
  629. return false;
  630. }
  631. if (self::getInteger($value, $options) == self::getFloat($value, $options)) {
  632. return true;
  633. }
  634. return false;
  635. }
  636. /**
  637. * Converts a format string from PHP's date format to ISO format
  638. * Remember that Zend Date always returns localized string, so a month name which returns the english
  639. * month in php's date() will return the translated month name with this function... use 'en' as locale
  640. * if you are in need of the original english names
  641. *
  642. * The conversion has the following restrictions:
  643. * 'a', 'A' - Meridiem is not explicit upper/lowercase, you have to upper/lowercase the translated value yourself
  644. *
  645. * @param string $format Format string in PHP's date format
  646. * @return string Format string in ISO format
  647. */
  648. public static function convertPhpToIsoFormat($format)
  649. {
  650. $convert = array('d' => 'dd' , 'D' => 'EE' , 'j' => 'd' , 'l' => 'EEEE', 'N' => 'eee' , 'S' => 'SS' ,
  651. 'w' => 'e' , 'z' => 'D' , 'W' => 'ww' , 'F' => 'MMMM', 'm' => 'MM' , 'M' => 'MMM' ,
  652. 'n' => 'M' , 't' => 'ddd' , 'L' => 'l' , 'o' => 'YYYY', 'Y' => 'yyyy', 'y' => 'yy' ,
  653. 'a' => 'a' , 'A' => 'a' , 'B' => 'B' , 'g' => 'h' , 'G' => 'H' , 'h' => 'hh' ,
  654. 'H' => 'HH' , 'i' => 'mm' , 's' => 'ss' , 'e' => 'zzzz', 'I' => 'I' , 'O' => 'Z' ,
  655. 'P' => 'ZZZZ', 'T' => 'z' , 'Z' => 'X' , 'c' => 'yyyy-MM-ddTHH:mm:ssZZZZ',
  656. 'r' => 'r' , 'U' => 'U');
  657. $values = str_split($format);
  658. foreach ($values as $key => $value) {
  659. if (isset($convert[$value]) === true) {
  660. $values[$key] = $convert[$value];
  661. }
  662. }
  663. return join($values);
  664. }
  665. /**
  666. * Parse date and split in named array fields
  667. *
  668. * @param string $date Date string to parse
  669. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  670. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  671. */
  672. private static function _parseDate($date, $options)
  673. {
  674. $options = self::_checkOptions($options) + self::$_options;
  675. $test = array('h', 'H', 'm', 's', 'y', 'Y', 'M', 'd', 'D', 'E', 'S', 'l', 'B', 'I',
  676. 'X', 'r', 'U', 'G', 'w', 'e', 'a', 'A', 'Z', 'z', 'v');
  677. $format = $options['date_format'];
  678. $number = $date; // working copy
  679. $result['date_format'] = $format; // save the format used to normalize $number (convenience)
  680. $result['locale'] = $options['locale']; // save the locale used to normalize $number (convenience)
  681. $oenc = iconv_get_encoding('internal_encoding');
  682. iconv_set_encoding('internal_encoding', 'UTF-8');
  683. $day = iconv_strpos($format, 'd');
  684. $month = iconv_strpos($format, 'M');
  685. $year = iconv_strpos($format, 'y');
  686. $hour = iconv_strpos($format, 'H');
  687. $min = iconv_strpos($format, 'm');
  688. $sec = iconv_strpos($format, 's');
  689. $am = null;
  690. if ($hour === false) {
  691. $hour = iconv_strpos($format, 'h');
  692. }
  693. if ($year === false) {
  694. $year = iconv_strpos($format, 'Y');
  695. }
  696. if ($day === false) {
  697. $day = iconv_strpos($format, 'E');
  698. if ($day === false) {
  699. $day = iconv_strpos($format, 'D');
  700. }
  701. }
  702. if ($day !== false) {
  703. $parse[$day] = 'd';
  704. if (!empty($options['locale']) && ($options['locale'] !== 'root') &&
  705. (!is_object($options['locale']) || ((string) $options['locale'] !== 'root'))) {
  706. // erase day string
  707. $daylist = Zend_Locale_Data::getList($options['locale'], 'day');
  708. foreach($daylist as $key => $name) {
  709. if (iconv_strpos($number, $name) !== false) {
  710. $number = str_replace($name, "EEEE", $number);
  711. break;
  712. }
  713. }
  714. }
  715. }
  716. $position = false;
  717. if ($month !== false) {
  718. $parse[$month] = 'M';
  719. if (!empty($options['locale']) && ($options['locale'] !== 'root') &&
  720. (!is_object($options['locale']) || ((string) $options['locale'] !== 'root'))) {
  721. // prepare to convert month name to their numeric equivalents, if requested,
  722. // and we have a $options['locale']
  723. $position = self::_replaceMonth($number, Zend_Locale_Data::getList($options['locale'],
  724. 'month'));
  725. if ($position === false) {
  726. $position = self::_replaceMonth($number, Zend_Locale_Data::getList($options['locale'],
  727. 'month', array('gregorian', 'format', 'abbreviated')));
  728. }
  729. }
  730. }
  731. if ($year !== false) {
  732. $parse[$year] = 'y';
  733. }
  734. if ($hour !== false) {
  735. $parse[$hour] = 'H';
  736. }
  737. if ($min !== false) {
  738. $parse[$min] = 'm';
  739. }
  740. if ($sec !== false) {
  741. $parse[$sec] = 's';
  742. }
  743. if (empty($parse)) {
  744. iconv_set_encoding('internal_encoding', $oenc);
  745. require_once 'Zend/Locale/Exception.php';
  746. throw new Zend_Locale_Exception("Unknown date format, neither date nor time in '" . $format . "' found");
  747. }
  748. ksort($parse);
  749. // get daytime
  750. if (iconv_strpos($format, 'a') !== false) {
  751. if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'am'))) !== false) {
  752. $am = true;
  753. } else if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'pm'))) !== false) {
  754. $am = false;
  755. }
  756. }
  757. // split number parts
  758. $split = false;
  759. preg_match_all('/\d+/u', $number, $splitted);
  760. if (count($splitted[0]) == 0) {
  761. iconv_set_encoding('internal_encoding', $oenc);
  762. require_once 'Zend/Locale/Exception.php';
  763. throw new Zend_Locale_Exception("No date part in '$date' found.");
  764. }
  765. if (count($splitted[0]) == 1) {
  766. $split = 0;
  767. }
  768. $cnt = 0;
  769. foreach($parse as $key => $value) {
  770. switch($value) {
  771. case 'd':
  772. if ($split === false) {
  773. if (count($splitted[0]) > $cnt) {
  774. $result['day'] = $splitted[0][$cnt];
  775. }
  776. } else {
  777. $result['day'] = iconv_substr($splitted[0][0], $split, 2);
  778. $split += 2;
  779. }
  780. ++$cnt;
  781. break;
  782. case 'M':
  783. if ($split === false) {
  784. if (count($splitted[0]) > $cnt) {
  785. $result['month'] = $splitted[0][$cnt];
  786. }
  787. } else {
  788. $result['month'] = iconv_substr($splitted[0][0], $split, 2);
  789. $split += 2;
  790. }
  791. ++$cnt;
  792. break;
  793. case 'y':
  794. $length = 2;
  795. if ((iconv_substr($format, $year, 4) == 'yyyy')
  796. || (iconv_substr($format, $year, 4) == 'YYYY')) {
  797. $length = 4;
  798. }
  799. if ($split === false) {
  800. if (count($splitted[0]) > $cnt) {
  801. $result['year'] = $splitted[0][$cnt];
  802. }
  803. } else {
  804. $result['year'] = iconv_substr($splitted[0][0], $split, $length);
  805. $split += $length;
  806. }
  807. ++$cnt;
  808. break;
  809. case 'H':
  810. if ($split === false) {
  811. if (count($splitted[0]) > $cnt) {
  812. $result['hour'] = $splitted[0][$cnt];
  813. }
  814. } else {
  815. $result['hour'] = iconv_substr($splitted[0][0], $split, 2);
  816. $split += 2;
  817. }
  818. ++$cnt;
  819. break;
  820. case 'm':
  821. if ($split === false) {
  822. if (count($splitted[0]) > $cnt) {
  823. $result['minute'] = $splitted[0][$cnt];
  824. }
  825. } else {
  826. $result['minute'] = iconv_substr($splitted[0][0], $split, 2);
  827. $split += 2;
  828. }
  829. ++$cnt;
  830. break;
  831. case 's':
  832. if ($split === false) {
  833. if (count($splitted[0]) > $cnt) {
  834. $result['second'] = $splitted[0][$cnt];
  835. }
  836. } else {
  837. $result['second'] = iconv_substr($splitted[0][0], $split, 2);
  838. $split += 2;
  839. }
  840. ++$cnt;
  841. break;
  842. }
  843. }
  844. // AM/PM correction
  845. if ($hour !== false) {
  846. if (($am === true) and ($result['hour'] == 12)){
  847. $result['hour'] = 0;
  848. } else if (($am === false) and ($result['hour'] != 12)) {
  849. $result['hour'] += 12;
  850. }
  851. }
  852. if ($options['fix_date'] === true) {
  853. $result['fixed'] = 0; // nothing has been "fixed" by swapping date parts around (yet)
  854. }
  855. if ($day !== false) {
  856. // fix false month
  857. if (isset($result['day']) and isset($result['month'])) {
  858. if (($position !== false) and ((iconv_strpos($date, $result['day']) === false) or
  859. (isset($result['year']) and (iconv_strpos($date, $result['year']) === false)))) {
  860. if ($options['fix_date'] !== true) {
  861. iconv_set_encoding('internal_encoding', $oenc);
  862. require_once 'Zend/Locale/Exception.php';
  863. throw new Zend_Locale_Exception("Unable to parse date '$date' using '" . $format
  864. . "' (false month, $position, $month)");
  865. }
  866. $temp = $result['day'];
  867. $result['day'] = $result['month'];
  868. $result['month'] = $temp;
  869. $result['fixed'] = 1;
  870. }
  871. }
  872. // fix switched values d <> y
  873. if (isset($result['day']) and isset($result['year'])) {
  874. if ($result['day'] > 31) {
  875. if ($options['fix_date'] !== true) {
  876. iconv_set_encoding('internal_encoding', $oenc);
  877. require_once 'Zend/Locale/Exception.php';
  878. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  879. . $format . "' (d <> y)");
  880. }
  881. $temp = $result['year'];
  882. $result['year'] = $result['day'];
  883. $result['day'] = $temp;
  884. $result['fixed'] = 2;
  885. }
  886. }
  887. // fix switched values M <> y
  888. if (isset($result['month']) and isset($result['year'])) {
  889. if ($result['month'] > 31) {
  890. if ($options['fix_date'] !== true) {
  891. iconv_set_encoding('internal_encoding', $oenc);
  892. require_once 'Zend/Locale/Exception.php';
  893. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  894. . $format . "' (M <> y)");
  895. }
  896. $temp = $result['year'];
  897. $result['year'] = $result['month'];
  898. $result['month'] = $temp;
  899. $result['fixed'] = 3;
  900. }
  901. }
  902. // fix switched values M <> d
  903. if (isset($result['month']) and isset($result['day'])) {
  904. if ($result['month'] > 12) {
  905. if ($options['fix_date'] !== true || $result['month'] > 31) {
  906. iconv_set_encoding('internal_encoding', $oenc);
  907. require_once 'Zend/Locale/Exception.php';
  908. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  909. . $format . "' (M <> d)");
  910. }
  911. $temp = $result['day'];
  912. $result['day'] = $result['month'];
  913. $result['month'] = $temp;
  914. $result['fixed'] = 4;
  915. }
  916. }
  917. }
  918. if (isset($result['year'])) {
  919. if (((iconv_strlen($result['year']) == 2) && ($result['year'] < 10)) ||
  920. (((iconv_strpos($format, 'yy') !== false) && (iconv_strpos($format, 'yyyy') === false)) ||
  921. ((iconv_strpos($format, 'YY') !== false) && (iconv_strpos($format, 'YYYY') === false)))) {
  922. if (($result['year'] >= 0) && ($result['year'] < 100)) {
  923. if ($result['year'] < 70) {
  924. $result['year'] = (int) $result['year'] + 100;
  925. }
  926. $result['year'] = (int) $result['year'] + 1900;
  927. }
  928. }
  929. }
  930. iconv_set_encoding('internal_encoding', $oenc);
  931. return $result;
  932. }
  933. /**
  934. * Search $number for a month name found in $monthlist, and replace if found.
  935. *
  936. * @param string $number Date string (modified)
  937. * @param array $monthlist List of month names
  938. *
  939. * @return int|false Position of replaced string (false if nothing replaced)
  940. */
  941. protected static function _replaceMonth(&$number, $monthlist)
  942. {
  943. // If $locale was invalid, $monthlist will default to a "root" identity
  944. // mapping for each month number from 1 to 12.
  945. // If no $locale was given, or $locale was invalid, do not use this identity mapping to normalize.
  946. // Otherwise, translate locale aware month names in $number to their numeric equivalents.
  947. $position = false;
  948. if ($monthlist && $monthlist[1] != 1) {
  949. foreach($monthlist as $key => $name) {
  950. if (($position = iconv_strpos($number, $name, 0, 'UTF-8')) !== false) {
  951. $number = str_ireplace($name, $key, $number);
  952. return $position;
  953. }
  954. }
  955. }
  956. return false;
  957. }
  958. /**
  959. * Returns the default date format for $locale.
  960. *
  961. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  962. * @return string format
  963. * @throws Zend_Locale_Exception throws an exception when locale data is broken
  964. */
  965. public static function getDateFormat($locale = null)
  966. {
  967. $format = Zend_Locale_Data::getContent($locale, 'date');
  968. if (empty($format)) {
  969. require_once 'Zend/Locale/Exception.php';
  970. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  971. }
  972. return $format;
  973. }
  974. /**
  975. * Returns an array with the normalized date from an locale date
  976. * a input of 10.01.2006 without a $locale would return:
  977. * array ('day' => 10, 'month' => 1, 'year' => 2006)
  978. * The 'locale' option is only used to convert human readable day
  979. * and month names to their numeric equivalents.
  980. * The 'format' option allows specification of self-defined date formats,
  981. * when not using the default format for the 'locale'.
  982. *
  983. * @param string $date Date string
  984. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  985. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  986. */
  987. public static function getDate($date, array $options = array())
  988. {
  989. $options = self::_checkOptions($options) + self::$_options;
  990. if (empty($options['date_format'])) {
  991. $options['format_type'] = 'iso';
  992. $options['date_format'] = self::getDateFormat($options['locale']);
  993. }
  994. return self::_parseDate($date, $options);
  995. }
  996. /**
  997. * Returns if the given datestring contains all date parts from the given format.
  998. * If no format is given, the default date format from the locale is used
  999. * If you want to check if the date is a proper date you should use Zend_Date::isDate()
  1000. *
  1001. * @param string $date Date string
  1002. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1003. * @return boolean
  1004. */
  1005. public static function checkDateFormat($date, array $options = array())
  1006. {
  1007. try {
  1008. $date = self::getDate($date, $options);
  1009. } catch (Exception $e) {
  1010. return false;
  1011. }
  1012. if (empty($options['date_format'])) {
  1013. $options['format_type'] = 'iso';
  1014. $options['date_format'] = self::getDateFormat($options['locale']);
  1015. }
  1016. $options = self::_checkOptions($options) + self::$_options;
  1017. // day expected but not parsed
  1018. if ((iconv_strpos($options['date_format'], 'd', 0, 'UTF-8') !== false) and (!isset($date['day']) or ($date['day'] == ""))) {
  1019. return false;
  1020. }
  1021. // month expected but not parsed
  1022. if ((iconv_strpos($options['date_format'], 'M', 0, 'UTF-8') !== false) and (!isset($date['month']) or ($date['month'] == ""))) {
  1023. return false;
  1024. }
  1025. // year expected but not parsed
  1026. if (((iconv_strpos($options['date_format'], 'Y', 0, 'UTF-8') !== false) or
  1027. (iconv_strpos($options['date_format'], 'y', 0, 'UTF-8') !== false)) and (!isset($date['year']) or ($date['year'] == ""))) {
  1028. return false;
  1029. }
  1030. // second expected but not parsed
  1031. if ((iconv_strpos($options['date_format'], 's', 0, 'UTF-8') !== false) and (!isset($date['second']) or ($date['second'] == ""))) {
  1032. return false;
  1033. }
  1034. // minute expected but not parsed
  1035. if ((iconv_strpos($options['date_format'], 'm', 0, 'UTF-8') !== false) and (!isset($date['minute']) or ($date['minute'] == ""))) {
  1036. return false;
  1037. }
  1038. // hour expected but not parsed
  1039. if (((iconv_strpos($options['date_format'], 'H', 0, 'UTF-8') !== false) or
  1040. (iconv_strpos($options['date_format'], 'h', 0, 'UTF-8') !== false)) and (!isset($date['hour']) or ($date['hour'] == ""))) {
  1041. return false;
  1042. }
  1043. return true;
  1044. }
  1045. /**
  1046. * Returns the default time format for $locale.
  1047. *
  1048. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1049. * @return string format
  1050. */
  1051. public static function getTimeFormat($locale = null)
  1052. {
  1053. $format = Zend_Locale_Data::getContent($locale, 'time');
  1054. if (empty($format)) {
  1055. require_once 'Zend/Locale/Exception.php';
  1056. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  1057. }
  1058. return $format;
  1059. }
  1060. /**
  1061. * Returns an array with 'hour', 'minute', and 'second' elements extracted from $time
  1062. * according to the order described in $format. For a format of 'H:m:s', and
  1063. * an input of 11:20:55, getTime() would return:
  1064. * array ('hour' => 11, 'minute' => 20, 'second' => 55)
  1065. * The optional $locale parameter may be used to help extract times from strings
  1066. * containing both a time and a day or month name.
  1067. *
  1068. * @param string $time Time string
  1069. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1070. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  1071. */
  1072. public static function getTime($time, array $options = array())
  1073. {
  1074. $options = self::_checkOptions($options) + self::$_options;
  1075. if (empty($options['date_format'])) {
  1076. $options['format_type'] = 'iso';
  1077. $options['date_format'] = self::getTimeFormat($options['locale']);
  1078. }
  1079. return self::_parseDate($time, $options);
  1080. }
  1081. /**
  1082. * Returns the default datetime format for $locale.
  1083. *
  1084. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1085. * @return string format
  1086. */
  1087. public static function getDateTimeFormat($locale = null)
  1088. {
  1089. $format = Zend_Locale_Data::getContent($locale, 'datetime');
  1090. if (empty($format)) {
  1091. require_once 'Zend/Locale/Exception.php';
  1092. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  1093. }
  1094. return $format;
  1095. }
  1096. /**
  1097. * Returns an array with 'year', 'month', 'day', 'hour', 'minute', and 'second' elements
  1098. * extracted from $datetime according to the order described in $format. For a format of 'd.M.y H:m:s',
  1099. * and an input of 10.05.1985 11:20:55, getDateTime() would return:
  1100. * array ('year' => 1985, 'month' => 5, 'day' => 10, 'hour' => 11, 'minute' => 20, 'second' => 55)
  1101. * The optional $locale parameter may be used to help extract times from strings
  1102. * containing both a time and a day or month name.
  1103. *
  1104. * @param string $datetime DateTime string
  1105. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1106. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  1107. */
  1108. public static function getDateTime($datetime, array $options = array())
  1109. {
  1110. $options = self::_checkOptions($options) + self::$_options;
  1111. if (empty($options['date_format'])) {
  1112. $options['format_type'] = 'iso';
  1113. $options['date_format'] = self::getDateTimeFormat($options['locale']);
  1114. }
  1115. return self::_parseDate($datetime, $options);
  1116. }
  1117. }