Format.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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-2014 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-2014 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 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 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. $stringValue = (string)(is_array($value) ? implode(' ', $value) : $value);
  97. throw new Zend_Locale_Exception("Unknown number format type '" . gettype($value) . "'. "
  98. . "Format '$stringValue' must be a valid number format string.");
  99. }
  100. break;
  101. case 'date_format' :
  102. if ($value == Zend_Locale_Format::STANDARD) {
  103. $locale = self::$_options['locale'];
  104. if (isset($options['locale'])) {
  105. $locale = $options['locale'];
  106. }
  107. $options['date_format'] = Zend_Locale_Format::getDateFormat($locale);
  108. } else if ((gettype($value) !== 'string') and ($value !== NULL)) {
  109. require_once 'Zend/Locale/Exception.php';
  110. $stringValue = (string)(is_array($value) ? implode(' ', $value) : $value);
  111. throw new Zend_Locale_Exception("Unknown dateformat type '" . gettype($value) . "'. "
  112. . "Format '$stringValue' must be a valid ISO or PHP date format string.");
  113. } else {
  114. if (((isset($options['format_type']) === true) and ($options['format_type'] == 'php')) or
  115. ((isset($options['format_type']) === false) and (self::$_options['format_type'] == 'php'))) {
  116. $options['date_format'] = Zend_Locale_Format::convertPhpToIsoFormat($value);
  117. }
  118. }
  119. break;
  120. case 'format_type' :
  121. if (($value != 'php') && ($value != 'iso')) {
  122. require_once 'Zend/Locale/Exception.php';
  123. throw new Zend_Locale_Exception("Unknown date format type '$value'. Only 'iso' and 'php'"
  124. . " are supported.");
  125. }
  126. break;
  127. case 'fix_date' :
  128. if (($value !== true) && ($value !== false)) {
  129. require_once 'Zend/Locale/Exception.php';
  130. throw new Zend_Locale_Exception("Enabling correction of dates must be either true or false"
  131. . "(fix_date='$value').");
  132. }
  133. break;
  134. case 'locale' :
  135. $options['locale'] = Zend_Locale::findLocale($value);
  136. break;
  137. case 'cache' :
  138. if ($value instanceof Zend_Cache_Core) {
  139. Zend_Locale_Data::setCache($value);
  140. }
  141. break;
  142. case 'disablecache' :
  143. Zend_Locale_Data::disableCache($value);
  144. break;
  145. case 'precision' :
  146. if ($value === NULL) {
  147. $value = -1;
  148. }
  149. if (($value < -1) || ($value > 30)) {
  150. require_once 'Zend/Locale/Exception.php';
  151. throw new Zend_Locale_Exception("'$value' precision is not a whole number less than 30.");
  152. }
  153. break;
  154. default:
  155. require_once 'Zend/Locale/Exception.php';
  156. throw new Zend_Locale_Exception("Unknown option: '$name' = '$value'");
  157. break;
  158. }
  159. }
  160. return $options;
  161. }
  162. /**
  163. * Changes the numbers/digits within a given string from one script to another
  164. * 'Decimal' representated the stardard numbers 0-9, if a script does not exist
  165. * an exception will be thrown.
  166. *
  167. * Examples for conversion from Arabic to Latin numerals:
  168. * convertNumerals('١١٠ Tests', 'Arab'); -> returns '100 Tests'
  169. * Example for conversion from Latin to Arabic numerals:
  170. * convertNumerals('100 Tests', 'Latn', 'Arab'); -> returns '١١٠ Tests'
  171. *
  172. * @param string $input String to convert
  173. * @param string $from Script to parse, see {@link Zend_Locale::getScriptList()} for details.
  174. * @param string $to OPTIONAL Script to convert to
  175. * @return string Returns the converted input
  176. * @throws Zend_Locale_Exception
  177. */
  178. public static function convertNumerals($input, $from, $to = null)
  179. {
  180. if (!self::_getUniCodeSupport()) {
  181. trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
  182. }
  183. $from = strtolower($from);
  184. $source = Zend_Locale_Data::getContent('en', 'numberingsystem', $from);
  185. if (empty($source)) {
  186. require_once 'Zend/Locale/Exception.php';
  187. throw new Zend_Locale_Exception("Unknown script '$from'. Use 'Latn' for digits 0,1,2,3,4,5,6,7,8,9.");
  188. }
  189. if ($to !== null) {
  190. $to = strtolower($to);
  191. $target = Zend_Locale_Data::getContent('en', 'numberingsystem', $to);
  192. if (empty($target)) {
  193. require_once 'Zend/Locale/Exception.php';
  194. throw new Zend_Locale_Exception("Unknown script '$to'. Use 'Latn' for digits 0,1,2,3,4,5,6,7,8,9.");
  195. }
  196. } else {
  197. $target = '0123456789';
  198. }
  199. for ($x = 0; $x < 10; ++$x) {
  200. $asource[$x] = "/" . iconv_substr($source, $x, 1, 'UTF-8') . "/u";
  201. $atarget[$x] = iconv_substr($target, $x, 1, 'UTF-8');
  202. }
  203. return preg_replace($asource, $atarget, $input);
  204. }
  205. /**
  206. * Returns the normalized number from a localized one
  207. * Parsing depends on given locale (grouping and decimal)
  208. *
  209. * Examples for input:
  210. * '2345.4356,1234' = 23455456.1234
  211. * '+23,3452.123' = 233452.123
  212. * '12343 ' = 12343
  213. * '-9456' = -9456
  214. * '0' = 0
  215. *
  216. * @param string $input Input string to parse for numbers
  217. * @param array $options Options: locale, precision. See {@link setOptions()} for details.
  218. * @return string Returns the extracted number
  219. * @throws Zend_Locale_Exception
  220. */
  221. public static function getNumber($input, array $options = array())
  222. {
  223. $options = self::_checkOptions($options) + self::$_options;
  224. if (!is_string($input)) {
  225. return $input;
  226. }
  227. if (!self::isNumber($input, $options)) {
  228. require_once 'Zend/Locale/Exception.php';
  229. throw new Zend_Locale_Exception('No localized value in ' . $input . ' found, or the given number does not match the localized format');
  230. }
  231. // Get correct signs for this locale
  232. $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
  233. // Change locale input to be default number
  234. if (($input[0] == $symbols['minus']) && ('-' != $input[0])) {
  235. $input = '-' . substr($input, 1);
  236. }
  237. $input = str_replace($symbols['group'],'', $input);
  238. if (strpos($input, $symbols['decimal']) !== false) {
  239. if ($symbols['decimal'] != '.') {
  240. $input = str_replace($symbols['decimal'], ".", $input);
  241. }
  242. $pre = substr($input, strpos($input, '.') + 1);
  243. if ($options['precision'] === null) {
  244. $options['precision'] = strlen($pre);
  245. }
  246. if (strlen($pre) >= $options['precision']) {
  247. $input = substr($input, 0, strlen($input) - strlen($pre) + $options['precision']);
  248. }
  249. if (($options['precision'] == 0) && ($input[strlen($input) - 1] == '.')) {
  250. $input = substr($input, 0, -1);
  251. }
  252. }
  253. return $input;
  254. }
  255. /**
  256. * Returns a locale formatted number depending on the given options.
  257. * The seperation and fraction sign is used from the set locale.
  258. * ##0.# -> 12345.12345 -> 12345.12345
  259. * ##0.00 -> 12345.12345 -> 12345.12
  260. * ##,##0.00 -> 12345.12345 -> 12,345.12
  261. *
  262. * @param string $value Localized number string
  263. * @param array $options Options: number_format, locale, precision. See {@link setOptions()} for details.
  264. * @return string locale formatted number
  265. * @throws Zend_Locale_Exception
  266. */
  267. public static function toNumber($value, array $options = array())
  268. {
  269. // load class within method for speed
  270. require_once 'Zend/Locale/Math.php';
  271. $value = Zend_Locale_Math::floatalize($value);
  272. $value = Zend_Locale_Math::normalize($value);
  273. $options = self::_checkOptions($options) + self::$_options;
  274. $options['locale'] = (string) $options['locale'];
  275. // Get correct signs for this locale
  276. $symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
  277. $oenc = self::_getEncoding();
  278. self::_setEncoding('UTF-8');
  279. // Get format
  280. $format = $options['number_format'];
  281. if ($format === null) {
  282. $format = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
  283. $format = self::_seperateFormat($format, $value, $options['precision']);
  284. if ($options['precision'] !== null) {
  285. $value = Zend_Locale_Math::normalize(Zend_Locale_Math::round($value, $options['precision']));
  286. }
  287. } else {
  288. // seperate negative format pattern when available
  289. $format = self::_seperateFormat($format, $value, $options['precision']);
  290. if (strpos($format, '.')) {
  291. if (is_numeric($options['precision'])) {
  292. $value = Zend_Locale_Math::round($value, $options['precision']);
  293. } else {
  294. if (substr($format, iconv_strpos($format, '.') + 1, 3) == '###') {
  295. $options['precision'] = null;
  296. } else {
  297. $options['precision'] = iconv_strlen(iconv_substr($format, iconv_strpos($format, '.') + 1,
  298. iconv_strrpos($format, '0') - iconv_strpos($format, '.')));
  299. $format = iconv_substr($format, 0, iconv_strpos($format, '.') + 1) . '###'
  300. . iconv_substr($format, iconv_strrpos($format, '0') + 1);
  301. }
  302. }
  303. } else {
  304. $value = Zend_Locale_Math::round($value, 0);
  305. $options['precision'] = 0;
  306. }
  307. $value = Zend_Locale_Math::normalize($value);
  308. }
  309. if (iconv_strpos($format, '0') === false) {
  310. self::_setEncoding($oenc);
  311. require_once 'Zend/Locale/Exception.php';
  312. throw new Zend_Locale_Exception('Wrong format... missing 0');
  313. }
  314. // get number parts
  315. $pos = iconv_strpos($value, '.');
  316. if ($pos !== false) {
  317. if ($options['precision'] === null) {
  318. $precstr = iconv_substr($value, $pos + 1);
  319. } else {
  320. $precstr = iconv_substr($value, $pos + 1, $options['precision']);
  321. if (iconv_strlen($precstr) < $options['precision']) {
  322. $precstr = $precstr . str_pad("0", ($options['precision'] - iconv_strlen($precstr)), "0");
  323. }
  324. }
  325. } else {
  326. if ($options['precision'] > 0) {
  327. $precstr = str_pad("0", ($options['precision']), "0");
  328. }
  329. }
  330. if ($options['precision'] === null) {
  331. if (isset($precstr)) {
  332. $options['precision'] = iconv_strlen($precstr);
  333. } else {
  334. $options['precision'] = 0;
  335. }
  336. }
  337. // get fraction and format lengths
  338. if (strpos($value, '.') !== false) {
  339. $number = substr((string) $value, 0, strpos($value, '.'));
  340. } else {
  341. $number = $value;
  342. }
  343. $prec = call_user_func(Zend_Locale_Math::$sub, $value, $number, $options['precision']);
  344. $prec = Zend_Locale_Math::floatalize($prec);
  345. $prec = Zend_Locale_Math::normalize($prec);
  346. if (iconv_strpos($prec, '-') !== false) {
  347. $prec = iconv_substr($prec, 1);
  348. }
  349. if (($prec == 0) and ($options['precision'] > 0)) {
  350. $prec = "0.0";
  351. }
  352. if (($options['precision'] + 2) > iconv_strlen($prec)) {
  353. $prec = str_pad((string) $prec, $options['precision'] + 2, "0", STR_PAD_RIGHT);
  354. }
  355. if (iconv_strpos($number, '-') !== false) {
  356. $number = iconv_substr($number, 1);
  357. }
  358. $group = iconv_strrpos($format, ',');
  359. $group2 = iconv_strpos ($format, ',');
  360. $point = iconv_strpos ($format, '0');
  361. // Add fraction
  362. $rest = "";
  363. if (iconv_strpos($format, '.')) {
  364. $rest = iconv_substr($format, iconv_strpos($format, '.') + 1);
  365. $length = iconv_strlen($rest);
  366. for($x = 0; $x < $length; ++$x) {
  367. if (($rest[0] == '0') || ($rest[0] == '#')) {
  368. $rest = iconv_substr($rest, 1);
  369. }
  370. }
  371. $format = iconv_substr($format, 0, iconv_strlen($format) - iconv_strlen($rest));
  372. }
  373. if ($options['precision'] == '0') {
  374. if (iconv_strrpos($format, '-') != 0) {
  375. $format = iconv_substr($format, 0, $point)
  376. . iconv_substr($format, iconv_strrpos($format, '#') + 2);
  377. } else {
  378. $format = iconv_substr($format, 0, $point);
  379. }
  380. } else {
  381. $format = iconv_substr($format, 0, $point) . $symbols['decimal']
  382. . iconv_substr($prec, 2);
  383. }
  384. $format .= $rest;
  385. // Add seperation
  386. if ($group == 0) {
  387. // no seperation
  388. $format = $number . iconv_substr($format, $point);
  389. } else if ($group == $group2) {
  390. // only 1 seperation
  391. $seperation = ($point - $group);
  392. for ($x = iconv_strlen($number); $x > $seperation; $x -= $seperation) {
  393. if (iconv_substr($number, 0, $x - $seperation) !== "") {
  394. $number = iconv_substr($number, 0, $x - $seperation) . $symbols['group']
  395. . iconv_substr($number, $x - $seperation);
  396. }
  397. }
  398. $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
  399. } else {
  400. // 2 seperations
  401. if (iconv_strlen($number) > ($point - $group)) {
  402. $seperation = ($point - $group);
  403. $number = iconv_substr($number, 0, iconv_strlen($number) - $seperation) . $symbols['group']
  404. . iconv_substr($number, iconv_strlen($number) - $seperation);
  405. if ((iconv_strlen($number) - 1) > ($point - $group + 1)) {
  406. $seperation2 = ($group - $group2 - 1);
  407. for ($x = iconv_strlen($number) - $seperation2 - 2; $x > $seperation2; $x -= $seperation2) {
  408. $number = iconv_substr($number, 0, $x - $seperation2) . $symbols['group']
  409. . iconv_substr($number, $x - $seperation2);
  410. }
  411. }
  412. }
  413. $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
  414. }
  415. // set negative sign
  416. if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $options['precision']) < 0) {
  417. if (iconv_strpos($format, '-') === false) {
  418. $format = $symbols['minus'] . $format;
  419. } else {
  420. $format = str_replace('-', $symbols['minus'], $format);
  421. }
  422. }
  423. self::_setEncoding($oenc);
  424. return (string) $format;
  425. }
  426. /**
  427. * @param string $format
  428. * @param string $value
  429. * @param int $precision
  430. * @return string
  431. */
  432. private static function _seperateFormat($format, $value, $precision)
  433. {
  434. if (iconv_strpos($format, ';') !== false) {
  435. if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $precision) < 0) {
  436. $tmpformat = iconv_substr($format, iconv_strpos($format, ';') + 1);
  437. if ($tmpformat[0] == '(') {
  438. $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
  439. } else {
  440. $format = $tmpformat;
  441. }
  442. } else {
  443. $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
  444. }
  445. }
  446. return $format;
  447. }
  448. /**
  449. * Checks if the input contains a normalized or localized number
  450. *
  451. * @param string $input Localized number string
  452. * @param array $options Options: locale. See {@link setOptions()} for details.
  453. * @return boolean Returns true if a number was found
  454. */
  455. public static function isNumber($input, array $options = array())
  456. {
  457. if (!self::_getUniCodeSupport()) {
  458. trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
  459. }
  460. $options = self::_checkOptions($options) + self::$_options;
  461. // Get correct signs for this locale
  462. $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
  463. $regexs = Zend_Locale_Format::_getRegexForType('decimalnumber', $options);
  464. $regexs = array_merge($regexs, Zend_Locale_Format::_getRegexForType('scientificnumber', $options));
  465. if (!empty($input) && ($input[0] == $symbols['decimal'])) {
  466. $input = 0 . $input;
  467. }
  468. foreach ($regexs as $regex) {
  469. preg_match($regex, $input, $found);
  470. if (isset($found[0])) {
  471. return true;
  472. }
  473. }
  474. return false;
  475. }
  476. /**
  477. * Internal method to convert cldr number syntax into regex
  478. *
  479. * @param string $type
  480. * @param array $options Options: locale. See {@link setOptions()} for details.
  481. * @return string
  482. * @throws Zend_Locale_Exception
  483. */
  484. private static function _getRegexForType($type, $options)
  485. {
  486. $decimal = Zend_Locale_Data::getContent($options['locale'], $type);
  487. $decimal = preg_replace('/[^#0,;\.\-Ee]/u', '',$decimal);
  488. $patterns = explode(';', $decimal);
  489. if (count($patterns) == 1) {
  490. $patterns[1] = '-' . $patterns[0];
  491. }
  492. $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
  493. foreach($patterns as $pkey => $pattern) {
  494. $regex[$pkey] = '/^';
  495. $rest = 0;
  496. $end = null;
  497. if (strpos($pattern, '.') !== false) {
  498. $end = substr($pattern, strpos($pattern, '.') + 1);
  499. $pattern = substr($pattern, 0, -strlen($end) - 1);
  500. }
  501. if (strpos($pattern, ',') !== false) {
  502. $parts = explode(',', $pattern);
  503. $count = count($parts);
  504. foreach($parts as $key => $part) {
  505. switch ($part) {
  506. case '#':
  507. case '-#':
  508. if ($part[0] == '-') {
  509. $regex[$pkey] .= '[' . $symbols['minus'] . '-]{0,1}';
  510. } else {
  511. $regex[$pkey] .= '[' . $symbols['plus'] . '+]{0,1}';
  512. }
  513. if (($parts[$key + 1]) == '##0') {
  514. $regex[$pkey] .= '[0-9]{1,3}';
  515. } else if (($parts[$key + 1]) == '##') {
  516. $regex[$pkey] .= '[0-9]{1,2}';
  517. } else {
  518. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 1):"' . $pattern . '"');
  519. }
  520. break;
  521. case '##':
  522. if ($parts[$key + 1] == '##0') {
  523. $regex[$pkey] .= '(\\' . $symbols['group'] . '{0,1}[0-9]{2})*';
  524. } else {
  525. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 2):"' . $pattern . '"');
  526. }
  527. break;
  528. case '##0':
  529. if ($parts[$key - 1] == '##') {
  530. $regex[$pkey] .= '[0-9]';
  531. } else if (($parts[$key - 1] == '#') || ($parts[$key - 1] == '-#')) {
  532. $regex[$pkey] .= '(\\' . $symbols['group'] . '{0,1}[0-9]{3})*';
  533. } else {
  534. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 3):"' . $pattern . '"');
  535. }
  536. break;
  537. case '#0':
  538. if ($key == 0) {
  539. $regex[$pkey] .= '[0-9]*';
  540. } else {
  541. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 4):"' . $pattern . '"');
  542. }
  543. break;
  544. }
  545. }
  546. }
  547. if (strpos($pattern, 'E') !== false) {
  548. if (($pattern == '#E0') || ($pattern == '#E00')) {
  549. $regex[$pkey] .= '[' . $symbols['plus']. '+]{0,1}[0-9]{1,}(\\' . $symbols['decimal'] . '[0-9]{1,})*[eE][' . $symbols['plus']. '+]{0,1}[0-9]{1,}';
  550. } else if (($pattern == '-#E0') || ($pattern == '-#E00')) {
  551. $regex[$pkey] .= '[' . $symbols['minus']. '-]{0,1}[0-9]{1,}(\\' . $symbols['decimal'] . '[0-9]{1,})*[eE][' . $symbols['minus']. '-]{0,1}[0-9]{1,}';
  552. } else {
  553. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 5):"' . $pattern . '"');
  554. }
  555. }
  556. if (!empty($end)) {
  557. if ($end == '###') {
  558. $regex[$pkey] .= '(\\' . $symbols['decimal'] . '{1}[0-9]{1,}){0,1}';
  559. } else if ($end == '###-') {
  560. $regex[$pkey] .= '(\\' . $symbols['decimal'] . '{1}[0-9]{1,}){0,1}[' . $symbols['minus']. '-]';
  561. } else {
  562. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 6):"' . $pattern . '"');
  563. }
  564. }
  565. $regex[$pkey] .= '$/u';
  566. }
  567. return $regex;
  568. }
  569. /**
  570. * Alias for getNumber
  571. *
  572. * @param string $input Number to localize
  573. * @param array $options Options: locale, precision. See {@link setOptions()} for details.
  574. * @return float
  575. */
  576. public static function getFloat($input, array $options = array())
  577. {
  578. return floatval(self::getNumber($input, $options));
  579. }
  580. /**
  581. * Returns a locale formatted integer number
  582. * Alias for toNumber()
  583. *
  584. * @param string $value Number to normalize
  585. * @param array $options Options: locale, precision. See {@link setOptions()} for details.
  586. * @return string Locale formatted number
  587. */
  588. public static function toFloat($value, array $options = array())
  589. {
  590. $options['number_format'] = Zend_Locale_Format::STANDARD;
  591. return self::toNumber($value, $options);
  592. }
  593. /**
  594. * Returns if a float was found
  595. * Alias for isNumber()
  596. *
  597. * @param string $value Localized number string
  598. * @param array $options Options: locale. See {@link setOptions()} for details.
  599. * @return boolean Returns true if a number was found
  600. */
  601. public static function isFloat($value, array $options = array())
  602. {
  603. return self::isNumber($value, $options);
  604. }
  605. /**
  606. * Returns the first found integer from an string
  607. * Parsing depends on given locale (grouping and decimal)
  608. *
  609. * Examples for input:
  610. * ' 2345.4356,1234' = 23455456
  611. * '+23,3452.123' = 233452
  612. * ' 12343 ' = 12343
  613. * '-9456km' = -9456
  614. * '0' = 0
  615. * '(-){0,1}(\d+(\.){0,1})*(\,){0,1})\d+'
  616. *
  617. * @param string $input Input string to parse for numbers
  618. * @param array $options Options: locale. See {@link setOptions()} for details.
  619. * @return integer Returns the extracted number
  620. */
  621. public static function getInteger($input, array $options = array())
  622. {
  623. $options['precision'] = 0;
  624. return intval(self::getFloat($input, $options));
  625. }
  626. /**
  627. * Returns a localized number
  628. *
  629. * @param string $value Number to normalize
  630. * @param array $options Options: locale. See {@link setOptions()} for details.
  631. * @return string Locale formatted number
  632. */
  633. public static function toInteger($value, array $options = array())
  634. {
  635. $options['precision'] = 0;
  636. $options['number_format'] = Zend_Locale_Format::STANDARD;
  637. return self::toNumber($value, $options);
  638. }
  639. /**
  640. * Returns if a integer was found
  641. *
  642. * @param string $value Localized number string
  643. * @param array $options Options: locale. See {@link setOptions()} for details.
  644. * @return boolean Returns true if a integer was found
  645. */
  646. public static function isInteger($value, array $options = array())
  647. {
  648. if (!self::isNumber($value, $options)) {
  649. return false;
  650. }
  651. if (self::getInteger($value, $options) == self::getFloat($value, $options)) {
  652. return true;
  653. }
  654. return false;
  655. }
  656. /**
  657. * Converts a format string from PHP's date format to ISO format
  658. * Remember that Zend Date always returns localized string, so a month name which returns the english
  659. * month in php's date() will return the translated month name with this function... use 'en' as locale
  660. * if you are in need of the original english names
  661. *
  662. * The conversion has the following restrictions:
  663. * 'a', 'A' - Meridiem is not explicit upper/lowercase, you have to upper/lowercase the translated value yourself
  664. *
  665. * @param string $format Format string in PHP's date format
  666. * @return string Format string in ISO format
  667. */
  668. public static function convertPhpToIsoFormat($format)
  669. {
  670. if ($format === null) {
  671. return null;
  672. }
  673. $convert = array(
  674. 'd' => 'dd' , 'D' => 'EE' , 'j' => 'd' , 'l' => 'EEEE',
  675. 'N' => 'eee' , 'S' => 'SS' , 'w' => 'e' , 'z' => 'D' ,
  676. 'W' => 'ww' , 'F' => 'MMMM', 'm' => 'MM' , 'M' => 'MMM' ,
  677. 'n' => 'M' , 't' => 'ddd' , 'L' => 'l' , 'o' => 'YYYY',
  678. 'Y' => 'yyyy', 'y' => 'yy' , 'a' => 'a' , 'A' => 'a' ,
  679. 'B' => 'B' , 'g' => 'h' , 'G' => 'H' , 'h' => 'hh' ,
  680. 'H' => 'HH' , 'i' => 'mm' , 's' => 'ss' , 'e' => 'zzzz',
  681. 'I' => 'I' , 'O' => 'Z' , 'P' => 'ZZZZ', 'T' => 'z' ,
  682. 'Z' => 'X' , 'c' => 'yyyy-MM-ddTHH:mm:ssZZZZ', 'r' => 'r',
  683. 'U' => 'U',
  684. );
  685. $escaped = false;
  686. $inEscapedString = false;
  687. $converted = array();
  688. foreach (str_split($format) as $char) {
  689. if (!$escaped && $char == '\\') {
  690. // Next char will be escaped: let's remember it
  691. $escaped = true;
  692. } elseif ($escaped) {
  693. if (!$inEscapedString) {
  694. // First escaped string: start the quoted chunk
  695. $converted[] = "'";
  696. $inEscapedString = true;
  697. }
  698. // Since the previous char was a \ and we are in the quoted
  699. // chunk, let's simply add $char as it is
  700. $converted[] = $char;
  701. $escaped = false;
  702. } elseif ($char == "'") {
  703. // Single quotes need to be escaped like this
  704. $converted[] = "''";
  705. } else {
  706. if ($inEscapedString) {
  707. // Close the single-quoted chunk
  708. $converted[] = "'";
  709. $inEscapedString = false;
  710. }
  711. // Convert the unescaped char if needed
  712. if (isset($convert[$char])) {
  713. $converted[] = $convert[$char];
  714. } else {
  715. $converted[] = $char;
  716. }
  717. }
  718. }
  719. return implode($converted);
  720. }
  721. /**
  722. * Parse date and split in named array fields
  723. *
  724. * @param string $date Date string to parse
  725. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  726. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  727. * @throws Zend_Locale_Exception
  728. */
  729. private static function _parseDate($date, $options)
  730. {
  731. if (!self::_getUniCodeSupport()) {
  732. trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
  733. }
  734. $options = self::_checkOptions($options) + self::$_options;
  735. $test = array('h', 'H', 'm', 's', 'y', 'Y', 'M', 'd', 'D', 'E', 'S', 'l', 'B', 'I',
  736. 'X', 'r', 'U', 'G', 'w', 'e', 'a', 'A', 'Z', 'z', 'v');
  737. $format = $options['date_format'];
  738. $number = $date; // working copy
  739. $result['date_format'] = $format; // save the format used to normalize $number (convenience)
  740. $result['locale'] = $options['locale']; // save the locale used to normalize $number (convenience)
  741. $oenc = self::_getEncoding();
  742. self::_setEncoding('UTF-8');
  743. $day = iconv_strpos($format, 'd');
  744. $month = iconv_strpos($format, 'M');
  745. $year = iconv_strpos($format, 'y');
  746. $hour = iconv_strpos($format, 'H');
  747. $min = iconv_strpos($format, 'm');
  748. $sec = iconv_strpos($format, 's');
  749. $am = null;
  750. if ($hour === false) {
  751. $hour = iconv_strpos($format, 'h');
  752. }
  753. if ($year === false) {
  754. $year = iconv_strpos($format, 'Y');
  755. }
  756. if ($day === false) {
  757. $day = iconv_strpos($format, 'E');
  758. if ($day === false) {
  759. $day = iconv_strpos($format, 'D');
  760. }
  761. }
  762. if ($day !== false) {
  763. $parse[$day] = 'd';
  764. if (!empty($options['locale']) && ($options['locale'] !== 'root') &&
  765. (!is_object($options['locale']) || ((string) $options['locale'] !== 'root'))) {
  766. // erase day string
  767. $daylist = Zend_Locale_Data::getList($options['locale'], 'day');
  768. foreach($daylist as $key => $name) {
  769. if (iconv_strpos($number, $name) !== false) {
  770. $number = str_replace($name, "EEEE", $number);
  771. break;
  772. }
  773. }
  774. }
  775. }
  776. $position = false;
  777. if ($month !== false) {
  778. $parse[$month] = 'M';
  779. if (!empty($options['locale']) && ($options['locale'] !== 'root') &&
  780. (!is_object($options['locale']) || ((string) $options['locale'] !== 'root'))) {
  781. // prepare to convert month name to their numeric equivalents, if requested,
  782. // and we have a $options['locale']
  783. $position = self::_replaceMonth($number, Zend_Locale_Data::getList($options['locale'],
  784. 'month'));
  785. if ($position === false) {
  786. $position = self::_replaceMonth($number, Zend_Locale_Data::getList($options['locale'],
  787. 'month', array('gregorian', 'format', 'abbreviated')));
  788. }
  789. }
  790. }
  791. if ($year !== false) {
  792. $parse[$year] = 'y';
  793. }
  794. if ($hour !== false) {
  795. $parse[$hour] = 'H';
  796. }
  797. if ($min !== false) {
  798. $parse[$min] = 'm';
  799. }
  800. if ($sec !== false) {
  801. $parse[$sec] = 's';
  802. }
  803. if (empty($parse)) {
  804. self::_setEncoding($oenc);
  805. require_once 'Zend/Locale/Exception.php';
  806. throw new Zend_Locale_Exception("Unknown date format, neither date nor time in '" . $format . "' found");
  807. }
  808. ksort($parse);
  809. // get daytime
  810. if (iconv_strpos($format, 'a') !== false) {
  811. if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'am'))) !== false) {
  812. $am = true;
  813. } else if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'pm'))) !== false) {
  814. $am = false;
  815. }
  816. }
  817. // split number parts
  818. $split = false;
  819. preg_match_all('/\d+/u', $number, $splitted);
  820. if (count($splitted[0]) == 0) {
  821. self::_setEncoding($oenc);
  822. require_once 'Zend/Locale/Exception.php';
  823. throw new Zend_Locale_Exception("No date part in '$date' found.");
  824. }
  825. if (count($splitted[0]) == 1) {
  826. $split = 0;
  827. }
  828. $cnt = 0;
  829. foreach($parse as $key => $value) {
  830. switch($value) {
  831. case 'd':
  832. if ($split === false) {
  833. if (count($splitted[0]) > $cnt) {
  834. $result['day'] = $splitted[0][$cnt];
  835. }
  836. } else {
  837. $result['day'] = iconv_substr($splitted[0][0], $split, 2);
  838. $split += 2;
  839. }
  840. ++$cnt;
  841. break;
  842. case 'M':
  843. if ($split === false) {
  844. if (count($splitted[0]) > $cnt) {
  845. $result['month'] = $splitted[0][$cnt];
  846. }
  847. } else {
  848. $result['month'] = iconv_substr($splitted[0][0], $split, 2);
  849. $split += 2;
  850. }
  851. ++$cnt;
  852. break;
  853. case 'y':
  854. $length = 2;
  855. if ((iconv_substr($format, $year, 4) == 'yyyy')
  856. || (iconv_substr($format, $year, 4) == 'YYYY')) {
  857. $length = 4;
  858. }
  859. if ($split === false) {
  860. if (count($splitted[0]) > $cnt) {
  861. $result['year'] = $splitted[0][$cnt];
  862. }
  863. } else {
  864. $result['year'] = iconv_substr($splitted[0][0], $split, $length);
  865. $split += $length;
  866. }
  867. ++$cnt;
  868. break;
  869. case 'H':
  870. if ($split === false) {
  871. if (count($splitted[0]) > $cnt) {
  872. $result['hour'] = $splitted[0][$cnt];
  873. }
  874. } else {
  875. $result['hour'] = iconv_substr($splitted[0][0], $split, 2);
  876. $split += 2;
  877. }
  878. ++$cnt;
  879. break;
  880. case 'm':
  881. if ($split === false) {
  882. if (count($splitted[0]) > $cnt) {
  883. $result['minute'] = $splitted[0][$cnt];
  884. }
  885. } else {
  886. $result['minute'] = iconv_substr($splitted[0][0], $split, 2);
  887. $split += 2;
  888. }
  889. ++$cnt;
  890. break;
  891. case 's':
  892. if ($split === false) {
  893. if (count($splitted[0]) > $cnt) {
  894. $result['second'] = $splitted[0][$cnt];
  895. }
  896. } else {
  897. $result['second'] = iconv_substr($splitted[0][0], $split, 2);
  898. $split += 2;
  899. }
  900. ++$cnt;
  901. break;
  902. }
  903. }
  904. // AM/PM correction
  905. if ($hour !== false) {
  906. if (($am === true) and ($result['hour'] == 12)){
  907. $result['hour'] = 0;
  908. } else if (($am === false) and ($result['hour'] != 12)) {
  909. $result['hour'] += 12;
  910. }
  911. }
  912. if ($options['fix_date'] === true) {
  913. $result['fixed'] = 0; // nothing has been "fixed" by swapping date parts around (yet)
  914. }
  915. if ($day !== false) {
  916. // fix false month
  917. if (isset($result['day']) and isset($result['month'])) {
  918. if (($position !== false) and ((iconv_strpos($date, $result['day']) === false) or
  919. (isset($result['year']) and (iconv_strpos($date, $result['year']) === false)))) {
  920. if ($options['fix_date'] !== true) {
  921. self::_setEncoding($oenc);
  922. require_once 'Zend/Locale/Exception.php';
  923. throw new Zend_Locale_Exception("Unable to parse date '$date' using '" . $format
  924. . "' (false month, $position, $month)");
  925. }
  926. $temp = $result['day'];
  927. $result['day'] = $result['month'];
  928. $result['month'] = $temp;
  929. $result['fixed'] = 1;
  930. }
  931. }
  932. // fix switched values d <> y
  933. if (isset($result['day']) and isset($result['year'])) {
  934. if ($result['day'] > 31) {
  935. if ($options['fix_date'] !== true) {
  936. self::_setEncoding($oenc);
  937. require_once 'Zend/Locale/Exception.php';
  938. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  939. . $format . "' (d <> y)");
  940. }
  941. $temp = $result['year'];
  942. $result['year'] = $result['day'];
  943. $result['day'] = $temp;
  944. $result['fixed'] = 2;
  945. }
  946. }
  947. // fix switched values M <> y
  948. if (isset($result['month']) and isset($result['year'])) {
  949. if ($result['month'] > 31) {
  950. if ($options['fix_date'] !== true) {
  951. self::_setEncoding($oenc);
  952. require_once 'Zend/Locale/Exception.php';
  953. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  954. . $format . "' (M <> y)");
  955. }
  956. $temp = $result['year'];
  957. $result['year'] = $result['month'];
  958. $result['month'] = $temp;
  959. $result['fixed'] = 3;
  960. }
  961. }
  962. // fix switched values M <> d
  963. if (isset($result['month']) and isset($result['day'])) {
  964. if ($result['month'] > 12) {
  965. if ($options['fix_date'] !== true || $result['month'] > 31) {
  966. self::_setEncoding($oenc);
  967. require_once 'Zend/Locale/Exception.php';
  968. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  969. . $format . "' (M <> d)");
  970. }
  971. $temp = $result['day'];
  972. $result['day'] = $result['month'];
  973. $result['month'] = $temp;
  974. $result['fixed'] = 4;
  975. }
  976. }
  977. }
  978. if (isset($result['year'])) {
  979. if (((iconv_strlen($result['year']) == 2) && ($result['year'] < 10)) ||
  980. (((iconv_strpos($format, 'yy') !== false) && (iconv_strpos($format, 'yyyy') === false)) ||
  981. ((iconv_strpos($format, 'YY') !== false) && (iconv_strpos($format, 'YYYY') === false)))) {
  982. if (($result['year'] >= 0) && ($result['year'] < 100)) {
  983. if ($result['year'] < 70) {
  984. $result['year'] = (int) $result['year'] + 100;
  985. }
  986. $result['year'] = (int) $result['year'] + 1900;
  987. }
  988. }
  989. }
  990. self::_setEncoding($oenc);
  991. return $result;
  992. }
  993. /**
  994. * Search $number for a month name found in $monthlist, and replace if found.
  995. *
  996. * @param string $number Date string (modified)
  997. * @param array $monthlist List of month names
  998. *
  999. * @return int|false Position of replaced string (false if nothing replaced)
  1000. */
  1001. protected static function _replaceMonth(&$number, $monthlist)
  1002. {
  1003. // If $locale was invalid, $monthlist will default to a "root" identity
  1004. // mapping for each month number from 1 to 12.
  1005. // If no $locale was given, or $locale was invalid, do not use this identity mapping to normalize.
  1006. // Otherwise, translate locale aware month names in $number to their numeric equivalents.
  1007. $position = false;
  1008. if ($monthlist && $monthlist[1] != 1) {
  1009. foreach($monthlist as $key => $name) {
  1010. if (($position = iconv_strpos($number, $name, 0, 'UTF-8')) !== false) {
  1011. $number = str_ireplace($name, $key, $number);
  1012. return $position;
  1013. }
  1014. }
  1015. }
  1016. return false;
  1017. }
  1018. /**
  1019. * Returns the default date format for $locale.
  1020. *
  1021. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1022. * @return string format
  1023. * @throws Zend_Locale_Exception throws an exception when locale data is broken
  1024. */
  1025. public static function getDateFormat($locale = null)
  1026. {
  1027. $format = Zend_Locale_Data::getContent($locale, 'date');
  1028. if (empty($format)) {
  1029. require_once 'Zend/Locale/Exception.php';
  1030. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  1031. }
  1032. return $format;
  1033. }
  1034. /**
  1035. * Returns an array with the normalized date from an locale date
  1036. * a input of 10.01.2006 without a $locale would return:
  1037. * array ('day' => 10, 'month' => 1, 'year' => 2006)
  1038. * The 'locale' option is only used to convert human readable day
  1039. * and month names to their numeric equivalents.
  1040. * The 'format' option allows specification of self-defined date formats,
  1041. * when not using the default format for the 'locale'.
  1042. *
  1043. * @param string $date Date string
  1044. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1045. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  1046. */
  1047. public static function getDate($date, array $options = array())
  1048. {
  1049. $options = self::_checkOptions($options) + self::$_options;
  1050. if (empty($options['date_format'])) {
  1051. $options['format_type'] = 'iso';
  1052. $options['date_format'] = self::getDateFormat($options['locale']);
  1053. }
  1054. return self::_parseDate($date, $options);
  1055. }
  1056. /**
  1057. * Returns if the given datestring contains all date parts from the given format.
  1058. * If no format is given, the default date format from the locale is used
  1059. * If you want to check if the date is a proper date you should use Zend_Date::isDate()
  1060. *
  1061. * @param string $date Date string
  1062. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1063. * @return boolean
  1064. */
  1065. public static function checkDateFormat($date, array $options = array())
  1066. {
  1067. try {
  1068. $date = self::getDate($date, $options);
  1069. } catch (Exception $e) {
  1070. return false;
  1071. }
  1072. if (empty($options['date_format'])) {
  1073. $options['format_type'] = 'iso';
  1074. $options['date_format'] = self::getDateFormat(isset($options['locale']) ? $options['locale'] : null);
  1075. }
  1076. $options = self::_checkOptions($options) + self::$_options;
  1077. // day expected but not parsed
  1078. if ((iconv_strpos($options['date_format'], 'd', 0, 'UTF-8') !== false) and (!isset($date['day']) or ($date['day'] === ""))) {
  1079. return false;
  1080. }
  1081. // month expected but not parsed
  1082. if ((iconv_strpos($options['date_format'], 'M', 0, 'UTF-8') !== false) and (!isset($date['month']) or ($date['month'] === ""))) {
  1083. return false;
  1084. }
  1085. // year expected but not parsed
  1086. if (((iconv_strpos($options['date_format'], 'Y', 0, 'UTF-8') !== false) or
  1087. (iconv_strpos($options['date_format'], 'y', 0, 'UTF-8') !== false)) and (!isset($date['year']) or ($date['year'] === ""))) {
  1088. return false;
  1089. }
  1090. // second expected but not parsed
  1091. if ((iconv_strpos($options['date_format'], 's', 0, 'UTF-8') !== false) and (!isset($date['second']) or ($date['second'] === ""))) {
  1092. return false;
  1093. }
  1094. // minute expected but not parsed
  1095. if ((iconv_strpos($options['date_format'], 'm', 0, 'UTF-8') !== false) and (!isset($date['minute']) or ($date['minute'] === ""))) {
  1096. return false;
  1097. }
  1098. // hour expected but not parsed
  1099. if (((iconv_strpos($options['date_format'], 'H', 0, 'UTF-8') !== false) or
  1100. (iconv_strpos($options['date_format'], 'h', 0, 'UTF-8') !== false)) and (!isset($date['hour']) or ($date['hour'] === ""))) {
  1101. return false;
  1102. }
  1103. return true;
  1104. }
  1105. /**
  1106. * Returns the default time format for $locale.
  1107. *
  1108. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1109. * @return string format
  1110. * @throws Zend_Locale_Exception
  1111. */
  1112. public static function getTimeFormat($locale = null)
  1113. {
  1114. $format = Zend_Locale_Data::getContent($locale, 'time');
  1115. if (empty($format)) {
  1116. require_once 'Zend/Locale/Exception.php';
  1117. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  1118. }
  1119. return $format;
  1120. }
  1121. /**
  1122. * Returns an array with 'hour', 'minute', and 'second' elements extracted from $time
  1123. * according to the order described in $format. For a format of 'H:i:s', and
  1124. * an input of 11:20:55, getTime() would return:
  1125. * array ('hour' => 11, 'minute' => 20, 'second' => 55)
  1126. * The optional $locale parameter may be used to help extract times from strings
  1127. * containing both a time and a day or month name.
  1128. *
  1129. * @param string $time Time string
  1130. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1131. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  1132. */
  1133. public static function getTime($time, array $options = array())
  1134. {
  1135. $options = self::_checkOptions($options) + self::$_options;
  1136. if (empty($options['date_format'])) {
  1137. $options['format_type'] = 'iso';
  1138. $options['date_format'] = self::getTimeFormat($options['locale']);
  1139. }
  1140. return self::_parseDate($time, $options);
  1141. }
  1142. /**
  1143. * Returns the default datetime format for $locale.
  1144. *
  1145. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1146. * @return string format
  1147. * @throws Zend_Locale_Exception
  1148. */
  1149. public static function getDateTimeFormat($locale = null)
  1150. {
  1151. $format = Zend_Locale_Data::getContent($locale, 'datetime');
  1152. if (empty($format)) {
  1153. require_once 'Zend/Locale/Exception.php';
  1154. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  1155. }
  1156. return $format;
  1157. }
  1158. /**
  1159. * Returns an array with 'year', 'month', 'day', 'hour', 'minute', and 'second' elements
  1160. * extracted from $datetime according to the order described in $format. For a format of 'd.M.y H:i:s',
  1161. * and an input of 10.05.1985 11:20:55, getDateTime() would return:
  1162. * array ('year' => 1985, 'month' => 5, 'day' => 10, 'hour' => 11, 'minute' => 20, 'second' => 55)
  1163. * The optional $locale parameter may be used to help extract times from strings
  1164. * containing both a time and a day or month name.
  1165. *
  1166. * @param string $datetime DateTime string
  1167. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1168. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  1169. */
  1170. public static function getDateTime($datetime, array $options = array())
  1171. {
  1172. $options = self::_checkOptions($options) + self::$_options;
  1173. if (empty($options['date_format'])) {
  1174. $options['format_type'] = 'iso';
  1175. $options['date_format'] = self::getDateTimeFormat($options['locale']);
  1176. }
  1177. return self::_parseDate($datetime, $options);
  1178. }
  1179. /**
  1180. * Internal method to detect of Unicode supports UTF8
  1181. * which should be enabled within vanilla php installations
  1182. *
  1183. * @return boolean
  1184. */
  1185. protected static function _getUniCodeSupport()
  1186. {
  1187. return (@preg_match('/\pL/u', 'a')) ? true : false;
  1188. }
  1189. /**
  1190. * Internal method to retrieve the current encoding via the ini setting
  1191. * default_charset for PHP >= 5.6 or iconv_get_encoding otherwise.
  1192. *
  1193. * @return string
  1194. */
  1195. protected static function _getEncoding()
  1196. {
  1197. $oenc = PHP_VERSION_ID < 50600
  1198. ? iconv_get_encoding('internal_encoding')
  1199. : ini_get('default_charset');
  1200. return $oenc;
  1201. }
  1202. /**
  1203. * Internal method to set the encoding via the ini setting
  1204. * default_charset for PHP >= 5.6 or iconv_set_encoding otherwise.
  1205. *
  1206. * @param string $encoding
  1207. * @return void
  1208. */
  1209. protected static function _setEncoding($encoding)
  1210. {
  1211. if (PHP_VERSION_ID < 50600) {
  1212. iconv_set_encoding('internal_encoding', $encoding);
  1213. } else {
  1214. ini_set('default_charset', $encoding);
  1215. }
  1216. }
  1217. }