Format.php 52 KB

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