Format.php 53 KB

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