Format.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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. if ($format === null) {
  651. return null;
  652. }
  653. $convert = array('d' => 'dd' , 'D' => 'EE' , 'j' => 'd' , 'l' => 'EEEE', 'N' => 'eee' , 'S' => 'SS' ,
  654. 'w' => 'e' , 'z' => 'D' , 'W' => 'ww' , 'F' => 'MMMM', 'm' => 'MM' , 'M' => 'MMM' ,
  655. 'n' => 'M' , 't' => 'ddd' , 'L' => 'l' , 'o' => 'YYYY', 'Y' => 'yyyy', 'y' => 'yy' ,
  656. 'a' => 'a' , 'A' => 'a' , 'B' => 'B' , 'g' => 'h' , 'G' => 'H' , 'h' => 'hh' ,
  657. 'H' => 'HH' , 'i' => 'mm' , 's' => 'ss' , 'e' => 'zzzz', 'I' => 'I' , 'O' => 'Z' ,
  658. 'P' => 'ZZZZ', 'T' => 'z' , 'Z' => 'X' , 'c' => 'yyyy-MM-ddTHH:mm:ssZZZZ',
  659. 'r' => 'r' , 'U' => 'U');
  660. $values = str_split($format);
  661. foreach ($values as $key => $value) {
  662. if (isset($convert[$value]) === true) {
  663. $values[$key] = $convert[$value];
  664. }
  665. }
  666. return join($values);
  667. }
  668. /**
  669. * Parse date and split in named array fields
  670. *
  671. * @param string $date Date string to parse
  672. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  673. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  674. */
  675. private static function _parseDate($date, $options)
  676. {
  677. $options = self::_checkOptions($options) + self::$_options;
  678. $test = array('h', 'H', 'm', 's', 'y', 'Y', 'M', 'd', 'D', 'E', 'S', 'l', 'B', 'I',
  679. 'X', 'r', 'U', 'G', 'w', 'e', 'a', 'A', 'Z', 'z', 'v');
  680. $format = $options['date_format'];
  681. $number = $date; // working copy
  682. $result['date_format'] = $format; // save the format used to normalize $number (convenience)
  683. $result['locale'] = $options['locale']; // save the locale used to normalize $number (convenience)
  684. $oenc = iconv_get_encoding('internal_encoding');
  685. iconv_set_encoding('internal_encoding', 'UTF-8');
  686. $day = iconv_strpos($format, 'd');
  687. $month = iconv_strpos($format, 'M');
  688. $year = iconv_strpos($format, 'y');
  689. $hour = iconv_strpos($format, 'H');
  690. $min = iconv_strpos($format, 'm');
  691. $sec = iconv_strpos($format, 's');
  692. $am = null;
  693. if ($hour === false) {
  694. $hour = iconv_strpos($format, 'h');
  695. }
  696. if ($year === false) {
  697. $year = iconv_strpos($format, 'Y');
  698. }
  699. if ($day === false) {
  700. $day = iconv_strpos($format, 'E');
  701. if ($day === false) {
  702. $day = iconv_strpos($format, 'D');
  703. }
  704. }
  705. if ($day !== false) {
  706. $parse[$day] = 'd';
  707. if (!empty($options['locale']) && ($options['locale'] !== 'root') &&
  708. (!is_object($options['locale']) || ((string) $options['locale'] !== 'root'))) {
  709. // erase day string
  710. $daylist = Zend_Locale_Data::getList($options['locale'], 'day');
  711. foreach($daylist as $key => $name) {
  712. if (iconv_strpos($number, $name) !== false) {
  713. $number = str_replace($name, "EEEE", $number);
  714. break;
  715. }
  716. }
  717. }
  718. }
  719. $position = false;
  720. if ($month !== false) {
  721. $parse[$month] = 'M';
  722. if (!empty($options['locale']) && ($options['locale'] !== 'root') &&
  723. (!is_object($options['locale']) || ((string) $options['locale'] !== 'root'))) {
  724. // prepare to convert month name to their numeric equivalents, if requested,
  725. // and we have a $options['locale']
  726. $position = self::_replaceMonth($number, Zend_Locale_Data::getList($options['locale'],
  727. 'month'));
  728. if ($position === false) {
  729. $position = self::_replaceMonth($number, Zend_Locale_Data::getList($options['locale'],
  730. 'month', array('gregorian', 'format', 'abbreviated')));
  731. }
  732. }
  733. }
  734. if ($year !== false) {
  735. $parse[$year] = 'y';
  736. }
  737. if ($hour !== false) {
  738. $parse[$hour] = 'H';
  739. }
  740. if ($min !== false) {
  741. $parse[$min] = 'm';
  742. }
  743. if ($sec !== false) {
  744. $parse[$sec] = 's';
  745. }
  746. if (empty($parse)) {
  747. iconv_set_encoding('internal_encoding', $oenc);
  748. require_once 'Zend/Locale/Exception.php';
  749. throw new Zend_Locale_Exception("Unknown date format, neither date nor time in '" . $format . "' found");
  750. }
  751. ksort($parse);
  752. // get daytime
  753. if (iconv_strpos($format, 'a') !== false) {
  754. if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'am'))) !== false) {
  755. $am = true;
  756. } else if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'pm'))) !== false) {
  757. $am = false;
  758. }
  759. }
  760. // split number parts
  761. $split = false;
  762. preg_match_all('/\d+/u', $number, $splitted);
  763. if (count($splitted[0]) == 0) {
  764. iconv_set_encoding('internal_encoding', $oenc);
  765. require_once 'Zend/Locale/Exception.php';
  766. throw new Zend_Locale_Exception("No date part in '$date' found.");
  767. }
  768. if (count($splitted[0]) == 1) {
  769. $split = 0;
  770. }
  771. $cnt = 0;
  772. foreach($parse as $key => $value) {
  773. switch($value) {
  774. case 'd':
  775. if ($split === false) {
  776. if (count($splitted[0]) > $cnt) {
  777. $result['day'] = $splitted[0][$cnt];
  778. }
  779. } else {
  780. $result['day'] = iconv_substr($splitted[0][0], $split, 2);
  781. $split += 2;
  782. }
  783. ++$cnt;
  784. break;
  785. case 'M':
  786. if ($split === false) {
  787. if (count($splitted[0]) > $cnt) {
  788. $result['month'] = $splitted[0][$cnt];
  789. }
  790. } else {
  791. $result['month'] = iconv_substr($splitted[0][0], $split, 2);
  792. $split += 2;
  793. }
  794. ++$cnt;
  795. break;
  796. case 'y':
  797. $length = 2;
  798. if ((iconv_substr($format, $year, 4) == 'yyyy')
  799. || (iconv_substr($format, $year, 4) == 'YYYY')) {
  800. $length = 4;
  801. }
  802. if ($split === false) {
  803. if (count($splitted[0]) > $cnt) {
  804. $result['year'] = $splitted[0][$cnt];
  805. }
  806. } else {
  807. $result['year'] = iconv_substr($splitted[0][0], $split, $length);
  808. $split += $length;
  809. }
  810. ++$cnt;
  811. break;
  812. case 'H':
  813. if ($split === false) {
  814. if (count($splitted[0]) > $cnt) {
  815. $result['hour'] = $splitted[0][$cnt];
  816. }
  817. } else {
  818. $result['hour'] = iconv_substr($splitted[0][0], $split, 2);
  819. $split += 2;
  820. }
  821. ++$cnt;
  822. break;
  823. case 'm':
  824. if ($split === false) {
  825. if (count($splitted[0]) > $cnt) {
  826. $result['minute'] = $splitted[0][$cnt];
  827. }
  828. } else {
  829. $result['minute'] = iconv_substr($splitted[0][0], $split, 2);
  830. $split += 2;
  831. }
  832. ++$cnt;
  833. break;
  834. case 's':
  835. if ($split === false) {
  836. if (count($splitted[0]) > $cnt) {
  837. $result['second'] = $splitted[0][$cnt];
  838. }
  839. } else {
  840. $result['second'] = iconv_substr($splitted[0][0], $split, 2);
  841. $split += 2;
  842. }
  843. ++$cnt;
  844. break;
  845. }
  846. }
  847. // AM/PM correction
  848. if ($hour !== false) {
  849. if (($am === true) and ($result['hour'] == 12)){
  850. $result['hour'] = 0;
  851. } else if (($am === false) and ($result['hour'] != 12)) {
  852. $result['hour'] += 12;
  853. }
  854. }
  855. if ($options['fix_date'] === true) {
  856. $result['fixed'] = 0; // nothing has been "fixed" by swapping date parts around (yet)
  857. }
  858. if ($day !== false) {
  859. // fix false month
  860. if (isset($result['day']) and isset($result['month'])) {
  861. if (($position !== false) and ((iconv_strpos($date, $result['day']) === false) or
  862. (isset($result['year']) and (iconv_strpos($date, $result['year']) === false)))) {
  863. if ($options['fix_date'] !== true) {
  864. iconv_set_encoding('internal_encoding', $oenc);
  865. require_once 'Zend/Locale/Exception.php';
  866. throw new Zend_Locale_Exception("Unable to parse date '$date' using '" . $format
  867. . "' (false month, $position, $month)");
  868. }
  869. $temp = $result['day'];
  870. $result['day'] = $result['month'];
  871. $result['month'] = $temp;
  872. $result['fixed'] = 1;
  873. }
  874. }
  875. // fix switched values d <> y
  876. if (isset($result['day']) and isset($result['year'])) {
  877. if ($result['day'] > 31) {
  878. if ($options['fix_date'] !== true) {
  879. iconv_set_encoding('internal_encoding', $oenc);
  880. require_once 'Zend/Locale/Exception.php';
  881. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  882. . $format . "' (d <> y)");
  883. }
  884. $temp = $result['year'];
  885. $result['year'] = $result['day'];
  886. $result['day'] = $temp;
  887. $result['fixed'] = 2;
  888. }
  889. }
  890. // fix switched values M <> y
  891. if (isset($result['month']) and isset($result['year'])) {
  892. if ($result['month'] > 31) {
  893. if ($options['fix_date'] !== true) {
  894. iconv_set_encoding('internal_encoding', $oenc);
  895. require_once 'Zend/Locale/Exception.php';
  896. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  897. . $format . "' (M <> y)");
  898. }
  899. $temp = $result['year'];
  900. $result['year'] = $result['month'];
  901. $result['month'] = $temp;
  902. $result['fixed'] = 3;
  903. }
  904. }
  905. // fix switched values M <> d
  906. if (isset($result['month']) and isset($result['day'])) {
  907. if ($result['month'] > 12) {
  908. if ($options['fix_date'] !== true || $result['month'] > 31) {
  909. iconv_set_encoding('internal_encoding', $oenc);
  910. require_once 'Zend/Locale/Exception.php';
  911. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  912. . $format . "' (M <> d)");
  913. }
  914. $temp = $result['day'];
  915. $result['day'] = $result['month'];
  916. $result['month'] = $temp;
  917. $result['fixed'] = 4;
  918. }
  919. }
  920. }
  921. if (isset($result['year'])) {
  922. if (((iconv_strlen($result['year']) == 2) && ($result['year'] < 10)) ||
  923. (((iconv_strpos($format, 'yy') !== false) && (iconv_strpos($format, 'yyyy') === false)) ||
  924. ((iconv_strpos($format, 'YY') !== false) && (iconv_strpos($format, 'YYYY') === false)))) {
  925. if (($result['year'] >= 0) && ($result['year'] < 100)) {
  926. if ($result['year'] < 70) {
  927. $result['year'] = (int) $result['year'] + 100;
  928. }
  929. $result['year'] = (int) $result['year'] + 1900;
  930. }
  931. }
  932. }
  933. iconv_set_encoding('internal_encoding', $oenc);
  934. return $result;
  935. }
  936. /**
  937. * Search $number for a month name found in $monthlist, and replace if found.
  938. *
  939. * @param string $number Date string (modified)
  940. * @param array $monthlist List of month names
  941. *
  942. * @return int|false Position of replaced string (false if nothing replaced)
  943. */
  944. protected static function _replaceMonth(&$number, $monthlist)
  945. {
  946. // If $locale was invalid, $monthlist will default to a "root" identity
  947. // mapping for each month number from 1 to 12.
  948. // If no $locale was given, or $locale was invalid, do not use this identity mapping to normalize.
  949. // Otherwise, translate locale aware month names in $number to their numeric equivalents.
  950. $position = false;
  951. if ($monthlist && $monthlist[1] != 1) {
  952. foreach($monthlist as $key => $name) {
  953. if (($position = iconv_strpos($number, $name, 0, 'UTF-8')) !== false) {
  954. $number = str_ireplace($name, $key, $number);
  955. return $position;
  956. }
  957. }
  958. }
  959. return false;
  960. }
  961. /**
  962. * Returns the default date format for $locale.
  963. *
  964. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  965. * @return string format
  966. * @throws Zend_Locale_Exception throws an exception when locale data is broken
  967. */
  968. public static function getDateFormat($locale = null)
  969. {
  970. $format = Zend_Locale_Data::getContent($locale, 'date');
  971. if (empty($format)) {
  972. require_once 'Zend/Locale/Exception.php';
  973. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  974. }
  975. return $format;
  976. }
  977. /**
  978. * Returns an array with the normalized date from an locale date
  979. * a input of 10.01.2006 without a $locale would return:
  980. * array ('day' => 10, 'month' => 1, 'year' => 2006)
  981. * The 'locale' option is only used to convert human readable day
  982. * and month names to their numeric equivalents.
  983. * The 'format' option allows specification of self-defined date formats,
  984. * when not using the default format for the 'locale'.
  985. *
  986. * @param string $date Date string
  987. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  988. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  989. */
  990. public static function getDate($date, array $options = array())
  991. {
  992. $options = self::_checkOptions($options) + self::$_options;
  993. if (empty($options['date_format'])) {
  994. $options['format_type'] = 'iso';
  995. $options['date_format'] = self::getDateFormat($options['locale']);
  996. }
  997. return self::_parseDate($date, $options);
  998. }
  999. /**
  1000. * Returns if the given datestring contains all date parts from the given format.
  1001. * If no format is given, the default date format from the locale is used
  1002. * If you want to check if the date is a proper date you should use Zend_Date::isDate()
  1003. *
  1004. * @param string $date Date string
  1005. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1006. * @return boolean
  1007. */
  1008. public static function checkDateFormat($date, array $options = array())
  1009. {
  1010. try {
  1011. $date = self::getDate($date, $options);
  1012. } catch (Exception $e) {
  1013. return false;
  1014. }
  1015. if (empty($options['date_format'])) {
  1016. $options['format_type'] = 'iso';
  1017. $options['date_format'] = self::getDateFormat($options['locale']);
  1018. }
  1019. $options = self::_checkOptions($options) + self::$_options;
  1020. // day expected but not parsed
  1021. if ((iconv_strpos($options['date_format'], 'd', 0, 'UTF-8') !== false) and (!isset($date['day']) or ($date['day'] == ""))) {
  1022. return false;
  1023. }
  1024. // month expected but not parsed
  1025. if ((iconv_strpos($options['date_format'], 'M', 0, 'UTF-8') !== false) and (!isset($date['month']) or ($date['month'] == ""))) {
  1026. return false;
  1027. }
  1028. // year expected but not parsed
  1029. if (((iconv_strpos($options['date_format'], 'Y', 0, 'UTF-8') !== false) or
  1030. (iconv_strpos($options['date_format'], 'y', 0, 'UTF-8') !== false)) and (!isset($date['year']) or ($date['year'] == ""))) {
  1031. return false;
  1032. }
  1033. // second expected but not parsed
  1034. if ((iconv_strpos($options['date_format'], 's', 0, 'UTF-8') !== false) and (!isset($date['second']) or ($date['second'] == ""))) {
  1035. return false;
  1036. }
  1037. // minute expected but not parsed
  1038. if ((iconv_strpos($options['date_format'], 'm', 0, 'UTF-8') !== false) and (!isset($date['minute']) or ($date['minute'] == ""))) {
  1039. return false;
  1040. }
  1041. // hour expected but not parsed
  1042. if (((iconv_strpos($options['date_format'], 'H', 0, 'UTF-8') !== false) or
  1043. (iconv_strpos($options['date_format'], 'h', 0, 'UTF-8') !== false)) and (!isset($date['hour']) or ($date['hour'] == ""))) {
  1044. return false;
  1045. }
  1046. return true;
  1047. }
  1048. /**
  1049. * Returns the default time format for $locale.
  1050. *
  1051. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1052. * @return string format
  1053. */
  1054. public static function getTimeFormat($locale = null)
  1055. {
  1056. $format = Zend_Locale_Data::getContent($locale, 'time');
  1057. if (empty($format)) {
  1058. require_once 'Zend/Locale/Exception.php';
  1059. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  1060. }
  1061. return $format;
  1062. }
  1063. /**
  1064. * Returns an array with 'hour', 'minute', and 'second' elements extracted from $time
  1065. * according to the order described in $format. For a format of 'H:m:s', and
  1066. * an input of 11:20:55, getTime() would return:
  1067. * array ('hour' => 11, 'minute' => 20, 'second' => 55)
  1068. * The optional $locale parameter may be used to help extract times from strings
  1069. * containing both a time and a day or month name.
  1070. *
  1071. * @param string $time Time string
  1072. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1073. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  1074. */
  1075. public static function getTime($time, array $options = array())
  1076. {
  1077. $options = self::_checkOptions($options) + self::$_options;
  1078. if (empty($options['date_format'])) {
  1079. $options['format_type'] = 'iso';
  1080. $options['date_format'] = self::getTimeFormat($options['locale']);
  1081. }
  1082. return self::_parseDate($time, $options);
  1083. }
  1084. /**
  1085. * Returns the default datetime format for $locale.
  1086. *
  1087. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1088. * @return string format
  1089. */
  1090. public static function getDateTimeFormat($locale = null)
  1091. {
  1092. $format = Zend_Locale_Data::getContent($locale, 'datetime');
  1093. if (empty($format)) {
  1094. require_once 'Zend/Locale/Exception.php';
  1095. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  1096. }
  1097. return $format;
  1098. }
  1099. /**
  1100. * Returns an array with 'year', 'month', 'day', 'hour', 'minute', and 'second' elements
  1101. * extracted from $datetime according to the order described in $format. For a format of 'd.M.y H:m:s',
  1102. * and an input of 10.05.1985 11:20:55, getDateTime() would return:
  1103. * array ('year' => 1985, 'month' => 5, 'day' => 10, 'hour' => 11, 'minute' => 20, 'second' => 55)
  1104. * The optional $locale parameter may be used to help extract times from strings
  1105. * containing both a time and a day or month name.
  1106. *
  1107. * @param string $datetime DateTime string
  1108. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1109. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  1110. */
  1111. public static function getDateTime($datetime, array $options = array())
  1112. {
  1113. $options = self::_checkOptions($options) + self::$_options;
  1114. if (empty($options['date_format'])) {
  1115. $options['format_type'] = 'iso';
  1116. $options['date_format'] = self::getDateTimeFormat($options['locale']);
  1117. }
  1118. return self::_parseDate($datetime, $options);
  1119. }
  1120. }