Format.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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. if (($options['precision'] == 0) && ($input[strlen($input) - 1] == '.')) {
  247. $input = substr($input, 0, -1);
  248. }
  249. }
  250. return $input;
  251. }
  252. /**
  253. * Returns a locale formatted number depending on the given options.
  254. * The seperation and fraction sign is used from the set locale.
  255. * ##0.# -> 12345.12345 -> 12345.12345
  256. * ##0.00 -> 12345.12345 -> 12345.12
  257. * ##,##0.00 -> 12345.12345 -> 12,345.12
  258. *
  259. * @param string $input Localized number string
  260. * @param array $options Options: number_format, locale, precision. See {@link setOptions()} for details.
  261. * @return string locale formatted number
  262. * @throws Zend_Locale_Exception
  263. */
  264. public static function toNumber($value, array $options = array())
  265. {
  266. // load class within method for speed
  267. require_once 'Zend/Locale/Math.php';
  268. $value = Zend_Locale_Math::normalize($value);
  269. $value = Zend_Locale_Math::floatalize($value);
  270. $options = self::_checkOptions($options) + self::$_options;
  271. $options['locale'] = (string) $options['locale'];
  272. // Get correct signs for this locale
  273. $symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
  274. $oenc = iconv_get_encoding('internal_encoding');
  275. iconv_set_encoding('internal_encoding', 'UTF-8');
  276. // Get format
  277. $format = $options['number_format'];
  278. if ($format === null) {
  279. $format = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
  280. $format = self::_seperateFormat($format, $value, $options['precision']);
  281. } else {
  282. // seperate negative format pattern when available
  283. $format = self::_seperateFormat($format, $value, $options['precision']);
  284. if (strpos($format, '.')) {
  285. if (is_numeric($options['precision'])) {
  286. $value = Zend_Locale_Math::round($value, $options['precision']);
  287. } else {
  288. if (substr($format, iconv_strpos($format, '.') + 1, 3) == '###') {
  289. $options['precision'] = null;
  290. } else {
  291. $options['precision'] = iconv_strlen(iconv_substr($format, iconv_strpos($format, '.') + 1,
  292. iconv_strrpos($format, '0') - iconv_strpos($format, '.')));
  293. $format = iconv_substr($format, 0, iconv_strpos($format, '.') + 1) . '###'
  294. . iconv_substr($format, iconv_strrpos($format, '0') + 1);
  295. }
  296. }
  297. } else {
  298. $value = Zend_Locale_Math::round($value, 0);
  299. $options['precision'] = 0;
  300. }
  301. $value = Zend_Locale_Math::normalize($value);
  302. }
  303. if (iconv_strpos($format, '0') === false) {
  304. iconv_set_encoding('internal_encoding', $oenc);
  305. require_once 'Zend/Locale/Exception.php';
  306. throw new Zend_Locale_Exception('Wrong format... missing 0');
  307. }
  308. // get number parts
  309. $pos = iconv_strpos($value, '.');
  310. if ($pos !== false) {
  311. if ($options['precision'] === null) {
  312. $precstr = iconv_substr($value, $pos + 1);
  313. } else {
  314. $precstr = iconv_substr($value, $pos + 1, $options['precision']);
  315. if (iconv_strlen($precstr) < $options['precision']) {
  316. $precstr = $precstr . str_pad("0", ($options['precision'] - iconv_strlen($precstr)), "0");
  317. }
  318. }
  319. } else {
  320. if ($options['precision'] > 0) {
  321. $precstr = str_pad("0", ($options['precision']), "0");
  322. }
  323. }
  324. if ($options['precision'] === null) {
  325. if (isset($precstr)) {
  326. $options['precision'] = iconv_strlen($precstr);
  327. } else {
  328. $options['precision'] = 0;
  329. }
  330. }
  331. // get fraction and format lengths
  332. if (strpos($value, '.') !== false) {
  333. $number = substr((string) $value, 0, strpos($value, '.'));
  334. } else {
  335. $number = $value;
  336. }
  337. $prec = call_user_func(Zend_Locale_Math::$sub, $value, $number, $options['precision']);
  338. $prec = Zend_Locale_Math::floatalize($prec);
  339. $prec = Zend_Locale_Math::normalize($prec);
  340. if (iconv_strpos($prec, '-') !== false) {
  341. $prec = iconv_substr($prec, 1);
  342. }
  343. if (($prec == 0) and ($options['precision'] > 0)) {
  344. $prec = "0.0";
  345. }
  346. if (($options['precision'] + 2) > iconv_strlen($prec)) {
  347. $prec = str_pad((string) $prec, $options['precision'] + 2, "0", STR_PAD_RIGHT);
  348. }
  349. if (iconv_strpos($number, '-') !== false) {
  350. $number = iconv_substr($number, 1);
  351. }
  352. $group = iconv_strrpos($format, ',');
  353. $group2 = iconv_strpos ($format, ',');
  354. $point = iconv_strpos ($format, '0');
  355. // Add fraction
  356. $rest = "";
  357. if (iconv_strpos($format, '.')) {
  358. $rest = iconv_substr($format, iconv_strpos($format, '.') + 1);
  359. $length = iconv_strlen($rest);
  360. for($x = 0; $x < $length; ++$x) {
  361. if (($rest[0] == '0') || ($rest[0] == '#')) {
  362. $rest = iconv_substr($rest, 1);
  363. }
  364. }
  365. $format = iconv_substr($format, 0, iconv_strlen($format) - iconv_strlen($rest));
  366. }
  367. if ($options['precision'] == '0') {
  368. if (iconv_strrpos($format, '-') != 0) {
  369. $format = iconv_substr($format, 0, $point)
  370. . iconv_substr($format, iconv_strrpos($format, '#') + 2);
  371. } else {
  372. $format = iconv_substr($format, 0, $point);
  373. }
  374. } else {
  375. $format = iconv_substr($format, 0, $point) . $symbols['decimal']
  376. . iconv_substr($prec, 2);
  377. }
  378. $format .= $rest;
  379. // Add seperation
  380. if ($group == 0) {
  381. // no seperation
  382. $format = $number . iconv_substr($format, $point);
  383. } else if ($group == $group2) {
  384. // only 1 seperation
  385. $seperation = ($point - $group);
  386. for ($x = iconv_strlen($number); $x > $seperation; $x -= $seperation) {
  387. if (iconv_substr($number, 0, $x - $seperation) !== "") {
  388. $number = iconv_substr($number, 0, $x - $seperation) . $symbols['group']
  389. . iconv_substr($number, $x - $seperation);
  390. }
  391. }
  392. $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
  393. } else {
  394. // 2 seperations
  395. if (iconv_strlen($number) > ($point - $group)) {
  396. $seperation = ($point - $group);
  397. $number = iconv_substr($number, 0, iconv_strlen($number) - $seperation) . $symbols['group']
  398. . iconv_substr($number, iconv_strlen($number) - $seperation);
  399. if ((iconv_strlen($number) - 1) > ($point - $group + 1)) {
  400. $seperation2 = ($group - $group2 - 1);
  401. for ($x = iconv_strlen($number) - $seperation2 - 2; $x > $seperation2; $x -= $seperation2) {
  402. $number = iconv_substr($number, 0, $x - $seperation2) . $symbols['group']
  403. . iconv_substr($number, $x - $seperation2);
  404. }
  405. }
  406. }
  407. $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
  408. }
  409. // set negative sign
  410. if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $options['precision']) < 0) {
  411. if (iconv_strpos($format, '-') === false) {
  412. $format = $symbols['minus'] . $format;
  413. } else {
  414. $format = str_replace('-', $symbols['minus'], $format);
  415. }
  416. }
  417. iconv_set_encoding('internal_encoding', $oenc);
  418. return (string) $format;
  419. }
  420. private static function _seperateFormat($format, $value, $precision)
  421. {
  422. if (iconv_strpos($format, ';') !== false) {
  423. if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $precision) < 0) {
  424. $tmpformat = iconv_substr($format, iconv_strpos($format, ';') + 1);
  425. if ($tmpformat[0] == '(') {
  426. $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
  427. } else {
  428. $format = $tmpformat;
  429. }
  430. } else {
  431. $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
  432. }
  433. }
  434. return $format;
  435. }
  436. /**
  437. * Checks if the input contains a normalized or localized number
  438. *
  439. * @param string $input Localized number string
  440. * @param array $options Options: locale. See {@link setOptions()} for details.
  441. * @return boolean Returns true if a number was found
  442. */
  443. public static function isNumber($input, array $options = array())
  444. {
  445. $options = self::_checkOptions($options) + self::$_options;
  446. // Get correct signs for this locale
  447. $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
  448. $regexs = Zend_Locale_Format::_getRegexForType('decimalnumber', $options);
  449. $regexs = array_merge($regexs, Zend_Locale_Format::_getRegexForType('scientificnumber', $options));
  450. if (!empty($input) && ($input[0] == $symbols['decimal'])) {
  451. $input = 0 . $input;
  452. }
  453. foreach ($regexs as $regex) {
  454. preg_match($regex, $input, $found);
  455. if (isset($found[0])) {
  456. return true;
  457. }
  458. }
  459. return false;
  460. }
  461. /**
  462. * Internal method to convert cldr number syntax into regex
  463. *
  464. * @param string $type
  465. * @return string
  466. */
  467. private static function _getRegexForType($type, $options)
  468. {
  469. $decimal = Zend_Locale_Data::getContent($options['locale'], $type);
  470. $decimal = preg_replace('/[^#0,;\.\-Ee]/', '',$decimal);
  471. $patterns = explode(';', $decimal);
  472. if (count($patterns) == 1) {
  473. $patterns[1] = '-' . $patterns[0];
  474. }
  475. $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
  476. foreach($patterns as $pkey => $pattern) {
  477. $regex[$pkey] = '/^';
  478. $rest = 0;
  479. $end = null;
  480. if (strpos($pattern, '.') !== false) {
  481. $end = substr($pattern, strpos($pattern, '.') + 1);
  482. $pattern = substr($pattern, 0, -strlen($end) - 1);
  483. }
  484. if (strpos($pattern, ',') !== false) {
  485. $parts = explode(',', $pattern);
  486. $count = count($parts);
  487. foreach($parts as $key => $part) {
  488. switch ($part) {
  489. case '#':
  490. case '-#':
  491. if ($part[0] == '-') {
  492. $regex[$pkey] .= '[' . $symbols['minus'] . '-]{0,1}';
  493. } else {
  494. $regex[$pkey] .= '[' . $symbols['plus'] . '+]{0,1}';
  495. }
  496. if (($parts[$key + 1]) == '##0') {
  497. $regex[$pkey] .= '[0-9]{1,3}';
  498. } else if (($parts[$key + 1]) == '##') {
  499. $regex[$pkey] .= '[0-9]{1,2}';
  500. } else {
  501. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 1):"' . $pattern . '"');
  502. }
  503. break;
  504. case '##':
  505. if ($parts[$key + 1] == '##0') {
  506. $regex[$pkey] .= '(\\' . $symbols['group'] . '{0,1}[0-9]{2})*';
  507. } else {
  508. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 2):"' . $pattern . '"');
  509. }
  510. break;
  511. case '##0':
  512. if ($parts[$key - 1] == '##') {
  513. $regex[$pkey] .= '[0-9]';
  514. } else if (($parts[$key - 1] == '#') || ($parts[$key - 1] == '-#')) {
  515. $regex[$pkey] .= '(\\' . $symbols['group'] . '{0,1}[0-9]{3})*';
  516. } else {
  517. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 3):"' . $pattern . '"');
  518. }
  519. break;
  520. case '#0':
  521. if ($key == 0) {
  522. $regex[$pkey] .= '[0-9]*';
  523. } else {
  524. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 4):"' . $pattern . '"');
  525. }
  526. break;
  527. }
  528. }
  529. }
  530. if (strpos($pattern, 'E') !== false) {
  531. if (($pattern == '#E0') || ($pattern == '#E00')) {
  532. $regex[$pkey] .= '[' . $symbols['plus']. '+]{0,1}[0-9]{1,}(\\' . $symbols['decimal'] . '[0-9]{1,})*[eE][' . $symbols['plus']. '+]{0,1}[0-9]{1,}';
  533. } else if (($pattern == '-#E0') || ($pattern == '-#E00')) {
  534. $regex[$pkey] .= '[' . $symbols['minus']. '-]{0,1}[0-9]{1,}(\\' . $symbols['decimal'] . '[0-9]{1,})*[eE][' . $symbols['minus']. '-]{0,1}[0-9]{1,}';
  535. } else {
  536. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 5):"' . $pattern . '"');
  537. }
  538. }
  539. if (!empty($end)) {
  540. if ($end == '###') {
  541. $regex[$pkey] .= '(\\' . $symbols['decimal'] . '{1}[0-9]{1,}){0,1}';
  542. } else if ($end == '###-') {
  543. $regex[$pkey] .= '(\\' . $symbols['decimal'] . '{1}[0-9]{1,}){0,1}[' . $symbols['minus']. '-]';
  544. } else {
  545. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 6):"' . $pattern . '"');
  546. }
  547. }
  548. $regex[$pkey] .= '$/';
  549. }
  550. return $regex;
  551. }
  552. /**
  553. * Alias for getNumber
  554. *
  555. * @param string $value Number to localize
  556. * @param array $options Options: locale, precision. See {@link setOptions()} for details.
  557. * @return float
  558. */
  559. public static function getFloat($input, array $options = array())
  560. {
  561. return floatval(self::getNumber($input, $options));
  562. }
  563. /**
  564. * Returns a locale formatted integer number
  565. * Alias for toNumber()
  566. *
  567. * @param string $value Number to normalize
  568. * @param array $options Options: locale, precision. See {@link setOptions()} for details.
  569. * @return string Locale formatted number
  570. */
  571. public static function toFloat($value, array $options = array())
  572. {
  573. $options['number_format'] = Zend_Locale_Format::STANDARD;
  574. return self::toNumber($value, $options);
  575. }
  576. /**
  577. * Returns if a float was found
  578. * Alias for isNumber()
  579. *
  580. * @param string $input Localized number string
  581. * @param array $options Options: locale. See {@link setOptions()} for details.
  582. * @return boolean Returns true if a number was found
  583. */
  584. public static function isFloat($value, array $options = array())
  585. {
  586. return self::isNumber($value, $options);
  587. }
  588. /**
  589. * Returns the first found integer from an string
  590. * Parsing depends on given locale (grouping and decimal)
  591. *
  592. * Examples for input:
  593. * ' 2345.4356,1234' = 23455456
  594. * '+23,3452.123' = 233452
  595. * ' 12343 ' = 12343
  596. * '-9456km' = -9456
  597. * '0' = 0
  598. * '(-){0,1}(\d+(\.){0,1})*(\,){0,1})\d+'
  599. *
  600. * @param string $input Input string to parse for numbers
  601. * @param array $options Options: locale. See {@link setOptions()} for details.
  602. * @return integer Returns the extracted number
  603. */
  604. public static function getInteger($input, array $options = array())
  605. {
  606. $options['precision'] = 0;
  607. return intval(self::getFloat($input, $options));
  608. }
  609. /**
  610. * Returns a localized number
  611. *
  612. * @param string $value Number to normalize
  613. * @param array $options Options: locale. See {@link setOptions()} for details.
  614. * @return string Locale formatted number
  615. */
  616. public static function toInteger($value, array $options = array())
  617. {
  618. $options['precision'] = 0;
  619. $options['number_format'] = Zend_Locale_Format::STANDARD;
  620. return self::toNumber($value, $options);
  621. }
  622. /**
  623. * Returns if a integer was found
  624. *
  625. * @param string $input Localized number string
  626. * @param array $options Options: locale. See {@link setOptions()} for details.
  627. * @return boolean Returns true if a integer was found
  628. */
  629. public static function isInteger($value, array $options = array())
  630. {
  631. if (!self::isNumber($value, $options)) {
  632. return false;
  633. }
  634. if (self::getInteger($value, $options) == self::getFloat($value, $options)) {
  635. return true;
  636. }
  637. return false;
  638. }
  639. /**
  640. * Converts a format string from PHP's date format to ISO format
  641. * Remember that Zend Date always returns localized string, so a month name which returns the english
  642. * month in php's date() will return the translated month name with this function... use 'en' as locale
  643. * if you are in need of the original english names
  644. *
  645. * The conversion has the following restrictions:
  646. * 'a', 'A' - Meridiem is not explicit upper/lowercase, you have to upper/lowercase the translated value yourself
  647. *
  648. * @param string $format Format string in PHP's date format
  649. * @return string Format string in ISO format
  650. */
  651. public static function convertPhpToIsoFormat($format)
  652. {
  653. if ($format === null) {
  654. return null;
  655. }
  656. $convert = array('d' => 'dd' , 'D' => 'EE' , 'j' => 'd' , 'l' => 'EEEE', 'N' => 'eee' , 'S' => 'SS' ,
  657. 'w' => 'e' , 'z' => 'D' , 'W' => 'ww' , 'F' => 'MMMM', 'm' => 'MM' , 'M' => 'MMM' ,
  658. 'n' => 'M' , 't' => 'ddd' , 'L' => 'l' , 'o' => 'YYYY', 'Y' => 'yyyy', 'y' => 'yy' ,
  659. 'a' => 'a' , 'A' => 'a' , 'B' => 'B' , 'g' => 'h' , 'G' => 'H' , 'h' => 'hh' ,
  660. 'H' => 'HH' , 'i' => 'mm' , 's' => 'ss' , 'e' => 'zzzz', 'I' => 'I' , 'O' => 'Z' ,
  661. 'P' => 'ZZZZ', 'T' => 'z' , 'Z' => 'X' , 'c' => 'yyyy-MM-ddTHH:mm:ssZZZZ',
  662. 'r' => 'r' , 'U' => 'U');
  663. $values = str_split($format);
  664. foreach ($values as $key => $value) {
  665. if (isset($convert[$value]) === true) {
  666. $values[$key] = $convert[$value];
  667. }
  668. }
  669. return join($values);
  670. }
  671. /**
  672. * Parse date and split in named array fields
  673. *
  674. * @param string $date Date string to parse
  675. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  676. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  677. */
  678. private static function _parseDate($date, $options)
  679. {
  680. $options = self::_checkOptions($options) + self::$_options;
  681. $test = array('h', 'H', 'm', 's', 'y', 'Y', 'M', 'd', 'D', 'E', 'S', 'l', 'B', 'I',
  682. 'X', 'r', 'U', 'G', 'w', 'e', 'a', 'A', 'Z', 'z', 'v');
  683. $format = $options['date_format'];
  684. $number = $date; // working copy
  685. $result['date_format'] = $format; // save the format used to normalize $number (convenience)
  686. $result['locale'] = $options['locale']; // save the locale used to normalize $number (convenience)
  687. $oenc = iconv_get_encoding('internal_encoding');
  688. iconv_set_encoding('internal_encoding', 'UTF-8');
  689. $day = iconv_strpos($format, 'd');
  690. $month = iconv_strpos($format, 'M');
  691. $year = iconv_strpos($format, 'y');
  692. $hour = iconv_strpos($format, 'H');
  693. $min = iconv_strpos($format, 'm');
  694. $sec = iconv_strpos($format, 's');
  695. $am = null;
  696. if ($hour === false) {
  697. $hour = iconv_strpos($format, 'h');
  698. }
  699. if ($year === false) {
  700. $year = iconv_strpos($format, 'Y');
  701. }
  702. if ($day === false) {
  703. $day = iconv_strpos($format, 'E');
  704. if ($day === false) {
  705. $day = iconv_strpos($format, 'D');
  706. }
  707. }
  708. if ($day !== false) {
  709. $parse[$day] = 'd';
  710. if (!empty($options['locale']) && ($options['locale'] !== 'root') &&
  711. (!is_object($options['locale']) || ((string) $options['locale'] !== 'root'))) {
  712. // erase day string
  713. $daylist = Zend_Locale_Data::getList($options['locale'], 'day');
  714. foreach($daylist as $key => $name) {
  715. if (iconv_strpos($number, $name) !== false) {
  716. $number = str_replace($name, "EEEE", $number);
  717. break;
  718. }
  719. }
  720. }
  721. }
  722. $position = false;
  723. if ($month !== false) {
  724. $parse[$month] = 'M';
  725. if (!empty($options['locale']) && ($options['locale'] !== 'root') &&
  726. (!is_object($options['locale']) || ((string) $options['locale'] !== 'root'))) {
  727. // prepare to convert month name to their numeric equivalents, if requested,
  728. // and we have a $options['locale']
  729. $position = self::_replaceMonth($number, Zend_Locale_Data::getList($options['locale'],
  730. 'month'));
  731. if ($position === false) {
  732. $position = self::_replaceMonth($number, Zend_Locale_Data::getList($options['locale'],
  733. 'month', array('gregorian', 'format', 'abbreviated')));
  734. }
  735. }
  736. }
  737. if ($year !== false) {
  738. $parse[$year] = 'y';
  739. }
  740. if ($hour !== false) {
  741. $parse[$hour] = 'H';
  742. }
  743. if ($min !== false) {
  744. $parse[$min] = 'm';
  745. }
  746. if ($sec !== false) {
  747. $parse[$sec] = 's';
  748. }
  749. if (empty($parse)) {
  750. iconv_set_encoding('internal_encoding', $oenc);
  751. require_once 'Zend/Locale/Exception.php';
  752. throw new Zend_Locale_Exception("Unknown date format, neither date nor time in '" . $format . "' found");
  753. }
  754. ksort($parse);
  755. // get daytime
  756. if (iconv_strpos($format, 'a') !== false) {
  757. if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'am'))) !== false) {
  758. $am = true;
  759. } else if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'pm'))) !== false) {
  760. $am = false;
  761. }
  762. }
  763. // split number parts
  764. $split = false;
  765. preg_match_all('/\d+/u', $number, $splitted);
  766. if (count($splitted[0]) == 0) {
  767. iconv_set_encoding('internal_encoding', $oenc);
  768. require_once 'Zend/Locale/Exception.php';
  769. throw new Zend_Locale_Exception("No date part in '$date' found.");
  770. }
  771. if (count($splitted[0]) == 1) {
  772. $split = 0;
  773. }
  774. $cnt = 0;
  775. foreach($parse as $key => $value) {
  776. switch($value) {
  777. case 'd':
  778. if ($split === false) {
  779. if (count($splitted[0]) > $cnt) {
  780. $result['day'] = $splitted[0][$cnt];
  781. }
  782. } else {
  783. $result['day'] = iconv_substr($splitted[0][0], $split, 2);
  784. $split += 2;
  785. }
  786. ++$cnt;
  787. break;
  788. case 'M':
  789. if ($split === false) {
  790. if (count($splitted[0]) > $cnt) {
  791. $result['month'] = $splitted[0][$cnt];
  792. }
  793. } else {
  794. $result['month'] = iconv_substr($splitted[0][0], $split, 2);
  795. $split += 2;
  796. }
  797. ++$cnt;
  798. break;
  799. case 'y':
  800. $length = 2;
  801. if ((iconv_substr($format, $year, 4) == 'yyyy')
  802. || (iconv_substr($format, $year, 4) == 'YYYY')) {
  803. $length = 4;
  804. }
  805. if ($split === false) {
  806. if (count($splitted[0]) > $cnt) {
  807. $result['year'] = $splitted[0][$cnt];
  808. }
  809. } else {
  810. $result['year'] = iconv_substr($splitted[0][0], $split, $length);
  811. $split += $length;
  812. }
  813. ++$cnt;
  814. break;
  815. case 'H':
  816. if ($split === false) {
  817. if (count($splitted[0]) > $cnt) {
  818. $result['hour'] = $splitted[0][$cnt];
  819. }
  820. } else {
  821. $result['hour'] = iconv_substr($splitted[0][0], $split, 2);
  822. $split += 2;
  823. }
  824. ++$cnt;
  825. break;
  826. case 'm':
  827. if ($split === false) {
  828. if (count($splitted[0]) > $cnt) {
  829. $result['minute'] = $splitted[0][$cnt];
  830. }
  831. } else {
  832. $result['minute'] = iconv_substr($splitted[0][0], $split, 2);
  833. $split += 2;
  834. }
  835. ++$cnt;
  836. break;
  837. case 's':
  838. if ($split === false) {
  839. if (count($splitted[0]) > $cnt) {
  840. $result['second'] = $splitted[0][$cnt];
  841. }
  842. } else {
  843. $result['second'] = iconv_substr($splitted[0][0], $split, 2);
  844. $split += 2;
  845. }
  846. ++$cnt;
  847. break;
  848. }
  849. }
  850. // AM/PM correction
  851. if ($hour !== false) {
  852. if (($am === true) and ($result['hour'] == 12)){
  853. $result['hour'] = 0;
  854. } else if (($am === false) and ($result['hour'] != 12)) {
  855. $result['hour'] += 12;
  856. }
  857. }
  858. if ($options['fix_date'] === true) {
  859. $result['fixed'] = 0; // nothing has been "fixed" by swapping date parts around (yet)
  860. }
  861. if ($day !== false) {
  862. // fix false month
  863. if (isset($result['day']) and isset($result['month'])) {
  864. if (($position !== false) and ((iconv_strpos($date, $result['day']) === false) or
  865. (isset($result['year']) and (iconv_strpos($date, $result['year']) === false)))) {
  866. if ($options['fix_date'] !== true) {
  867. iconv_set_encoding('internal_encoding', $oenc);
  868. require_once 'Zend/Locale/Exception.php';
  869. throw new Zend_Locale_Exception("Unable to parse date '$date' using '" . $format
  870. . "' (false month, $position, $month)");
  871. }
  872. $temp = $result['day'];
  873. $result['day'] = $result['month'];
  874. $result['month'] = $temp;
  875. $result['fixed'] = 1;
  876. }
  877. }
  878. // fix switched values d <> y
  879. if (isset($result['day']) and isset($result['year'])) {
  880. if ($result['day'] > 31) {
  881. if ($options['fix_date'] !== true) {
  882. iconv_set_encoding('internal_encoding', $oenc);
  883. require_once 'Zend/Locale/Exception.php';
  884. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  885. . $format . "' (d <> y)");
  886. }
  887. $temp = $result['year'];
  888. $result['year'] = $result['day'];
  889. $result['day'] = $temp;
  890. $result['fixed'] = 2;
  891. }
  892. }
  893. // fix switched values M <> y
  894. if (isset($result['month']) and isset($result['year'])) {
  895. if ($result['month'] > 31) {
  896. if ($options['fix_date'] !== true) {
  897. iconv_set_encoding('internal_encoding', $oenc);
  898. require_once 'Zend/Locale/Exception.php';
  899. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  900. . $format . "' (M <> y)");
  901. }
  902. $temp = $result['year'];
  903. $result['year'] = $result['month'];
  904. $result['month'] = $temp;
  905. $result['fixed'] = 3;
  906. }
  907. }
  908. // fix switched values M <> d
  909. if (isset($result['month']) and isset($result['day'])) {
  910. if ($result['month'] > 12) {
  911. if ($options['fix_date'] !== true || $result['month'] > 31) {
  912. iconv_set_encoding('internal_encoding', $oenc);
  913. require_once 'Zend/Locale/Exception.php';
  914. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  915. . $format . "' (M <> d)");
  916. }
  917. $temp = $result['day'];
  918. $result['day'] = $result['month'];
  919. $result['month'] = $temp;
  920. $result['fixed'] = 4;
  921. }
  922. }
  923. }
  924. if (isset($result['year'])) {
  925. if (((iconv_strlen($result['year']) == 2) && ($result['year'] < 10)) ||
  926. (((iconv_strpos($format, 'yy') !== false) && (iconv_strpos($format, 'yyyy') === false)) ||
  927. ((iconv_strpos($format, 'YY') !== false) && (iconv_strpos($format, 'YYYY') === false)))) {
  928. if (($result['year'] >= 0) && ($result['year'] < 100)) {
  929. if ($result['year'] < 70) {
  930. $result['year'] = (int) $result['year'] + 100;
  931. }
  932. $result['year'] = (int) $result['year'] + 1900;
  933. }
  934. }
  935. }
  936. iconv_set_encoding('internal_encoding', $oenc);
  937. return $result;
  938. }
  939. /**
  940. * Search $number for a month name found in $monthlist, and replace if found.
  941. *
  942. * @param string $number Date string (modified)
  943. * @param array $monthlist List of month names
  944. *
  945. * @return int|false Position of replaced string (false if nothing replaced)
  946. */
  947. protected static function _replaceMonth(&$number, $monthlist)
  948. {
  949. // If $locale was invalid, $monthlist will default to a "root" identity
  950. // mapping for each month number from 1 to 12.
  951. // If no $locale was given, or $locale was invalid, do not use this identity mapping to normalize.
  952. // Otherwise, translate locale aware month names in $number to their numeric equivalents.
  953. $position = false;
  954. if ($monthlist && $monthlist[1] != 1) {
  955. foreach($monthlist as $key => $name) {
  956. if (($position = iconv_strpos($number, $name, 0, 'UTF-8')) !== false) {
  957. $number = str_ireplace($name, $key, $number);
  958. return $position;
  959. }
  960. }
  961. }
  962. return false;
  963. }
  964. /**
  965. * Returns the default date format for $locale.
  966. *
  967. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  968. * @return string format
  969. * @throws Zend_Locale_Exception throws an exception when locale data is broken
  970. */
  971. public static function getDateFormat($locale = null)
  972. {
  973. $format = Zend_Locale_Data::getContent($locale, 'date');
  974. if (empty($format)) {
  975. require_once 'Zend/Locale/Exception.php';
  976. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  977. }
  978. return $format;
  979. }
  980. /**
  981. * Returns an array with the normalized date from an locale date
  982. * a input of 10.01.2006 without a $locale would return:
  983. * array ('day' => 10, 'month' => 1, 'year' => 2006)
  984. * The 'locale' option is only used to convert human readable day
  985. * and month names to their numeric equivalents.
  986. * The 'format' option allows specification of self-defined date formats,
  987. * when not using the default format for the 'locale'.
  988. *
  989. * @param string $date Date string
  990. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  991. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  992. */
  993. public static function getDate($date, array $options = array())
  994. {
  995. $options = self::_checkOptions($options) + self::$_options;
  996. if (empty($options['date_format'])) {
  997. $options['format_type'] = 'iso';
  998. $options['date_format'] = self::getDateFormat($options['locale']);
  999. }
  1000. return self::_parseDate($date, $options);
  1001. }
  1002. /**
  1003. * Returns if the given datestring contains all date parts from the given format.
  1004. * If no format is given, the default date format from the locale is used
  1005. * If you want to check if the date is a proper date you should use Zend_Date::isDate()
  1006. *
  1007. * @param string $date Date string
  1008. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1009. * @return boolean
  1010. */
  1011. public static function checkDateFormat($date, array $options = array())
  1012. {
  1013. try {
  1014. $date = self::getDate($date, $options);
  1015. } catch (Exception $e) {
  1016. return false;
  1017. }
  1018. if (empty($options['date_format'])) {
  1019. $options['format_type'] = 'iso';
  1020. $options['date_format'] = self::getDateFormat($options['locale']);
  1021. }
  1022. $options = self::_checkOptions($options) + self::$_options;
  1023. // day expected but not parsed
  1024. if ((iconv_strpos($options['date_format'], 'd', 0, 'UTF-8') !== false) and (!isset($date['day']) or ($date['day'] == ""))) {
  1025. return false;
  1026. }
  1027. // month expected but not parsed
  1028. if ((iconv_strpos($options['date_format'], 'M', 0, 'UTF-8') !== false) and (!isset($date['month']) or ($date['month'] == ""))) {
  1029. return false;
  1030. }
  1031. // year expected but not parsed
  1032. if (((iconv_strpos($options['date_format'], 'Y', 0, 'UTF-8') !== false) or
  1033. (iconv_strpos($options['date_format'], 'y', 0, 'UTF-8') !== false)) and (!isset($date['year']) or ($date['year'] == ""))) {
  1034. return false;
  1035. }
  1036. // second expected but not parsed
  1037. if ((iconv_strpos($options['date_format'], 's', 0, 'UTF-8') !== false) and (!isset($date['second']) or ($date['second'] == ""))) {
  1038. return false;
  1039. }
  1040. // minute expected but not parsed
  1041. if ((iconv_strpos($options['date_format'], 'm', 0, 'UTF-8') !== false) and (!isset($date['minute']) or ($date['minute'] == ""))) {
  1042. return false;
  1043. }
  1044. // hour expected but not parsed
  1045. if (((iconv_strpos($options['date_format'], 'H', 0, 'UTF-8') !== false) or
  1046. (iconv_strpos($options['date_format'], 'h', 0, 'UTF-8') !== false)) and (!isset($date['hour']) or ($date['hour'] == ""))) {
  1047. return false;
  1048. }
  1049. return true;
  1050. }
  1051. /**
  1052. * Returns the default time format for $locale.
  1053. *
  1054. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1055. * @return string format
  1056. */
  1057. public static function getTimeFormat($locale = null)
  1058. {
  1059. $format = Zend_Locale_Data::getContent($locale, 'time');
  1060. if (empty($format)) {
  1061. require_once 'Zend/Locale/Exception.php';
  1062. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  1063. }
  1064. return $format;
  1065. }
  1066. /**
  1067. * Returns an array with 'hour', 'minute', and 'second' elements extracted from $time
  1068. * according to the order described in $format. For a format of 'H:m:s', and
  1069. * an input of 11:20:55, getTime() would return:
  1070. * array ('hour' => 11, 'minute' => 20, 'second' => 55)
  1071. * The optional $locale parameter may be used to help extract times from strings
  1072. * containing both a time and a day or month name.
  1073. *
  1074. * @param string $time Time string
  1075. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1076. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  1077. */
  1078. public static function getTime($time, array $options = array())
  1079. {
  1080. $options = self::_checkOptions($options) + self::$_options;
  1081. if (empty($options['date_format'])) {
  1082. $options['format_type'] = 'iso';
  1083. $options['date_format'] = self::getTimeFormat($options['locale']);
  1084. }
  1085. return self::_parseDate($time, $options);
  1086. }
  1087. /**
  1088. * Returns the default datetime format for $locale.
  1089. *
  1090. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1091. * @return string format
  1092. */
  1093. public static function getDateTimeFormat($locale = null)
  1094. {
  1095. $format = Zend_Locale_Data::getContent($locale, 'datetime');
  1096. if (empty($format)) {
  1097. require_once 'Zend/Locale/Exception.php';
  1098. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  1099. }
  1100. return $format;
  1101. }
  1102. /**
  1103. * Returns an array with 'year', 'month', 'day', 'hour', 'minute', and 'second' elements
  1104. * extracted from $datetime according to the order described in $format. For a format of 'd.M.y H:m:s',
  1105. * and an input of 10.05.1985 11:20:55, getDateTime() would return:
  1106. * array ('year' => 1985, 'month' => 5, 'day' => 10, 'hour' => 11, 'minute' => 20, 'second' => 55)
  1107. * The optional $locale parameter may be used to help extract times from strings
  1108. * containing both a time and a day or month name.
  1109. *
  1110. * @param string $datetime DateTime string
  1111. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1112. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  1113. */
  1114. public static function getDateTime($datetime, array $options = array())
  1115. {
  1116. $options = self::_checkOptions($options) + self::$_options;
  1117. if (empty($options['date_format'])) {
  1118. $options['format_type'] = 'iso';
  1119. $options['date_format'] = self::getDateTimeFormat($options['locale']);
  1120. }
  1121. return self::_parseDate($datetime, $options);
  1122. }
  1123. }