Format.php 52 KB

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