Format.php 52 KB

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