PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/runtime/ext/icu/ext_icu_date_fmt.php

https://gitlab.com/Blueprint-Marketing/hhvm
PHP | 607 lines | 146 code | 44 blank | 417 comment | 1 complexity | 4d04f2465abaacac4b671bc853cc64cd MD5 | raw file
  1. <?hh
  2. /**
  3. * Date Formatter is a concrete class that enables locale-dependent
  4. * formatting/parsing of dates using pattern strings and/or canned patterns.
  5. * This class represents the ICU date formatting functionality. It allows
  6. * users to display dates in a localized format or to parse strings into PHP
  7. * date values using pattern strings and/or canned patterns.
  8. */
  9. <<__NativeData("IntlDateFormatter")>>
  10. class IntlDateFormatter {
  11. /**
  12. * Create a date formatter
  13. *
  14. * @param string $locale - Locale to use when formatting or parsing or
  15. * NULL to use the value specified in the ini setting
  16. * intl.default_locale.
  17. * @param int $datetype - Date type to use (none, short, medium, long,
  18. * full). This is one of the IntlDateFormatter constants. It can also
  19. * be NULL, in which case ICU's default date type will be used.
  20. * @param int $timetype - Time type to use (none, short, medium, long,
  21. * full). This is one of the IntlDateFormatter constants. It can also
  22. * be NULL, in which case ICU's default time type will be used.
  23. * @param mixed $timezone - Time zone ID. The default (and the one used
  24. * if NULL is given) is the one returned by date_default_timezone_get()
  25. * or, if applicable, that of the IntlCalendar object passed for the
  26. * calendar parameter. This ID must be a valid identifier on ICU's
  27. * database or an ID representing an explicit offset, such as
  28. * GMT-05:30. This can also be an IntlTimeZone or a DateTimeZone
  29. * object.
  30. * @param mixed $calendar - Calendar to use for formatting or parsing.
  31. * The default value is NULL, which corresponds to
  32. * IntlDateFormatter::GREGORIAN. This can either be one of the
  33. * IntlDateFormatter calendar constants or an IntlCalendar. Any
  34. * IntlCalendar object passed will be clone; it will not be changed by
  35. * the IntlDateFormatter. This will determine the calendar type used
  36. * (gregorian, islamic, persian, etc.) and, if NULL is given for the
  37. * timezone parameter, also the timezone used.
  38. * @param string $pattern - Optional pattern to use when formatting or
  39. * parsing. Possible patterns are documented at .
  40. *
  41. * @return IntlDateFormatter - The created IntlDateFormatter or FALSE
  42. * in case of failure.
  43. */
  44. <<__Native>>
  45. public function __construct(string $locale, int $datetype, int $timetype,
  46. mixed $timezone = NULL, mixed $calendar = NULL,
  47. string $pattern = ''): void;
  48. public static function create($locale, $datetype, $timetype,
  49. $timezone = NULL, $calendar = NULL,
  50. $pattern = ''): mixed {
  51. try {
  52. return new IntlDateFormatter($locale, $datetype, $timetype,
  53. $timezone, $calendar, $pattern);
  54. } catch (Exception $e) {
  55. return false;
  56. }
  57. }
  58. /**
  59. * Format the date/time value as a string
  60. *
  61. * @param mixed $value - Value to format. This may be a DateTime
  62. * object, an IntlCalendar object, a numeric type representing a
  63. * (possibly fractional) number of seconds since epoch or an array in
  64. * the format output by localtime(). If a DateTime or an IntlCalendar
  65. * object is passed, its timezone is not considered. The object will be
  66. * formatted using the formater's configured timezone. If one wants to
  67. * use the timezone of the object to be formatted,
  68. * IntlDateFormatter::setTimeZone() must be called before with the
  69. * object's timezone. Alternatively, the static function
  70. * IntlDateFormatter::formatObject() may be used instead.
  71. *
  72. * @return string - The formatted string or, if an error occurred,
  73. * FALSE.
  74. */
  75. <<__Native>>
  76. public function format(mixed $value): string;
  77. /**
  78. * Formats an object
  79. *
  80. * @param object $object -
  81. * @param mixed $format -
  82. * @param string $locale -
  83. *
  84. * @return string - A string with result.
  85. */
  86. <<__Native>>
  87. public static function FormatObject(object $obj,
  88. mixed $format = NULL,
  89. ?string $locale = NULL): string;
  90. /**
  91. * Get the calendar type used for the IntlDateFormatter
  92. *
  93. * @return int - The calendar type being used by the formatter. Either
  94. * IntlDateFormatter::TRADITIONAL or IntlDateFormatter::GREGORIAN.
  95. */
  96. <<__Native>>
  97. function getCalendar(): int;
  98. /**
  99. * Get the datetype used for the IntlDateFormatter
  100. *
  101. * @return int - The current date type value of the formatter.
  102. */
  103. <<__Native>>
  104. public function getDateType(): int;
  105. /**
  106. * Get the error code from last operation
  107. *
  108. * @return int - The error code, one of UErrorCode values. Initial
  109. * value is U_ZERO_ERROR.
  110. */
  111. <<__Native>>
  112. public function getErrorCode(): int;
  113. /**
  114. * Get the error text from the last operation.
  115. *
  116. * @return string - Description of the last error.
  117. */
  118. <<__Native>>
  119. public function getErrorMessage(): string;
  120. /**
  121. * Get the locale used by formatter
  122. *
  123. * @param int $which - Default ULOC_ACTUAL_LOCALE
  124. *
  125. * @return string - the locale of this formatter or 'false' if error
  126. */
  127. <<__Native>>
  128. public function getLocale(?int $which = null): string;
  129. /**
  130. * Get the pattern used for the IntlDateFormatter
  131. *
  132. * @return string - The pattern string being used to format/parse.
  133. */
  134. <<__Native>>
  135. public function getPattern(): string;
  136. /**
  137. * Get the timetype used for the IntlDateFormatter
  138. *
  139. * @return int - The current date type value of the formatter.
  140. */
  141. <<__Native>>
  142. public function getTimeType(): int;
  143. /**
  144. * Get the timezone-id used for the IntlDateFormatter
  145. *
  146. * @return string - ID string for the time zone used by this
  147. * formatter.
  148. */
  149. <<__Native>>
  150. public function getTimeZoneId(): string;
  151. /**
  152. * Get copy of formatter's calendar object
  153. *
  154. * @return IntlCalendar - A copy of the internal calendar object used
  155. * by this formatter.
  156. */
  157. <<__Native>>
  158. public function getCalendarObject(): object;
  159. /**
  160. * Get formatter's timezone
  161. *
  162. * @return IntlTimeZone - The associated IntlTimeZone object.
  163. */
  164. <<__Native>>
  165. public function getTimeZone(): object;
  166. /**
  167. * Get the lenient used for the IntlDateFormatter
  168. *
  169. * @return bool - TRUE if parser is lenient, FALSE if parser is strict.
  170. * By default the parser is lenient.
  171. */
  172. <<__Native>>
  173. public function isLenient(): bool;
  174. /**
  175. * Parse string to a field-based time value
  176. *
  177. * @param string $value - string to convert to a time
  178. * @param int $position - Position at which to start the parsing in
  179. * $value (zero-based). If no error occurs before $value is consumed,
  180. * $parse_pos will contain -1 otherwise it will contain the position at
  181. * which parsing ended . If $parse_pos > strlen($value), the parse
  182. * fails immediately.
  183. *
  184. * @return array - Localtime compatible array of integers : contains 24
  185. * hour clock value in tm_hour field
  186. */
  187. <<__Native>>
  188. public function localtime(string $value,
  189. mixed &$position): mixed;
  190. /**
  191. * Parse string to a timestamp value
  192. *
  193. * @param string $value - string to convert to a time
  194. * @param int $position - Position at which to start the parsing in
  195. * $value (zero-based). If no error occurs before $value is consumed,
  196. * $parse_pos will contain -1 otherwise it will contain the position at
  197. * which parsing ended (and the error occurred). This variable will
  198. * contain the end position if the parse fails. If $parse_pos >
  199. * strlen($value), the parse fails immediately.
  200. *
  201. * @return int - timestamp parsed value, or FALSE if value can't be
  202. * parsed.
  203. */
  204. <<__Native>>
  205. public function parse(string $value,
  206. mixed &$position = null): mixed;
  207. /**
  208. * Sets the calendar type used by the formatter
  209. *
  210. * @param mixed $which - This can either be: the calendar type to use
  211. * (default is IntlDateFormatter::GREGORIAN, which is also used if NULL
  212. * is specified) or an IntlCalendar object. Any IntlCalendar object
  213. * passed in will be cloned; no modifications will be made to the
  214. * argument object. The timezone of the formatter will only be kept
  215. * if an IntlCalendar object is not passed, otherwise the new timezone
  216. * will be that of the passed object.
  217. *
  218. * @return bool -
  219. */
  220. <<__Native>>
  221. function setCalendar(mixed $which): bool;
  222. /**
  223. * Set the leniency of the parser
  224. *
  225. * @param bool $lenient - Sets whether the parser is lenient or not,
  226. * default is TRUE (lenient).
  227. *
  228. * @return bool -
  229. */
  230. <<__Native>>
  231. public function setLenient(bool $lenient): bool;
  232. /**
  233. * Set the pattern used for the IntlDateFormatter
  234. *
  235. * @param string $pattern - New pattern string to use. Possible
  236. * patterns are documented at .
  237. *
  238. * @return bool - Bad formatstrings are usually the cause of the
  239. * failure.
  240. */
  241. <<__Native>>
  242. public function setPattern(string $pattern): bool;
  243. /**
  244. * Sets the time zone to use
  245. *
  246. * @param string $zone - The time zone ID string of the time zone to
  247. * use. If NULL or the empty string, the default time zone for the
  248. * runtime is used.
  249. *
  250. * @return bool -
  251. */
  252. public function setTimeZoneId(string $zone): bool {
  253. trigger_error("Use datefmt_set_timezone() instead, which also accepts ".
  254. "a plain time zone identifier and for which this function ".
  255. "is now an alias", E_DEPRECATED);
  256. return $this->setTimeZone($zone);
  257. }
  258. /**
  259. * Sets formatter's timezone
  260. *
  261. * @param mixed $zone -
  262. *
  263. * @return bool - Returns TRUE on success and FALSE on failure.
  264. */
  265. <<__Native>>
  266. public function setTimeZone(mixed $zone): bool;
  267. }
  268. /**
  269. * Create a date formatter
  270. *
  271. * @param string $locale - Locale to use when formatting or parsing or
  272. * NULL to use the value specified in the ini setting
  273. * intl.default_locale.
  274. * @param int $datetype - Date type to use (none, short, medium, long,
  275. * full). This is one of the IntlDateFormatter constants. It can also be
  276. * NULL, in which case ICU's default date type will be used.
  277. * @param int $timetype - Time type to use (none, short, medium, long,
  278. * full). This is one of the IntlDateFormatter constants. It can also be
  279. * NULL, in which case ICU's default time type will be used.
  280. * @param mixed $timezone - Time zone ID. The default (and the one used
  281. * if NULL is given) is the one returned by date_default_timezone_get()
  282. * or, if applicable, that of the IntlCalendar object passed for the
  283. * calendar parameter. This ID must be a valid identifier on ICU's
  284. * database or an ID representing an explicit offset, such as GMT-05:30.
  285. * This can also be an IntlTimeZone or a DateTimeZone object.
  286. * @param mixed $calendar - Calendar to use for formatting or parsing.
  287. * The default value is NULL, which corresponds to
  288. * IntlDateFormatter::GREGORIAN. This can either be one of the
  289. * IntlDateFormatter calendar constants or an IntlCalendar. Any
  290. * IntlCalendar object passed will be clone; it will not be changed by
  291. * the IntlDateFormatter. This will determine the calendar type used
  292. * (gregorian, islamic, persian, etc.) and, if NULL is given for the
  293. * timezone parameter, also the timezone used.
  294. * @param string $pattern - Optional pattern to use when formatting or
  295. * parsing. Possible patterns are documented at .
  296. *
  297. * @return IntlDateFormatter - The created IntlDateFormatter or FALSE in
  298. * case of failure.
  299. */
  300. function datefmt_create($locale,
  301. $datetype,
  302. $timetype,
  303. $timezone = NULL,
  304. $calendar = NULL,
  305. $pattern = ''): mixed {
  306. return IntlDateFormatter::create($locale, $datetype, $timetype,
  307. $timezone, $calendar, $pattern);
  308. }
  309. /**
  310. * Format the date/time value as a string
  311. *
  312. * @param intldateformatter $fmt - The date formatter resource.
  313. * @param mixed $value - Value to format. This may be a DateTime object,
  314. * an IntlCalendar object, a numeric type representing a (possibly
  315. * fractional) number of seconds since epoch or an array in the format
  316. * output by localtime(). If a DateTime or an IntlCalendar object is
  317. * passed, its timezone is not considered. The object will be formatted
  318. * using the formater's configured timezone. If one wants to use the
  319. * timezone of the object to be formatted,
  320. * IntlDateFormatter::setTimeZone() must be called before with the
  321. * object's timezone. Alternatively, the static function
  322. * IntlDateFormatter::formatObject() may be used instead.
  323. *
  324. * @return string - The formatted string or, if an error occurred,
  325. * FALSE.
  326. */
  327. function datefmt_format(IntlDateFormatter $fmt,
  328. $value): string {
  329. return $fmt->format($value);
  330. }
  331. /**
  332. * Formats an object
  333. *
  334. * @param object $object -
  335. * @param mixed $format -
  336. * @param string $locale -
  337. *
  338. * @return string - A string with result.
  339. */
  340. function datefmt_format_object($obj,
  341. $format = NULL,
  342. $locale = NULL): string {
  343. return IntlDateFormatter::FormatObject($obj, $format, $locale);
  344. }
  345. /**
  346. * Get the calendar type used for the IntlDateFormatter
  347. *
  348. * @param intldateformatter $fmt - The formatter resource
  349. *
  350. * @return int - The calendar type being used by the formatter. Either
  351. * IntlDateFormatter::TRADITIONAL or IntlDateFormatter::GREGORIAN.
  352. */
  353. function datefmt_get_calendar(IntlDateFormatter $fmt): int {
  354. return $fmt->getCalendar();
  355. }
  356. /**
  357. * Get the datetype used for the IntlDateFormatter
  358. *
  359. * @param intldateformatter $fmt - The formatter resource.
  360. *
  361. * @return int - The current date type value of the formatter.
  362. */
  363. function datefmt_get_datetype(IntlDateFormatter $fmt): int {
  364. return $fmt->getDateType();
  365. }
  366. /**
  367. * Get the error code from last operation
  368. *
  369. * @param intldateformatter $fmt - The formatter resource.
  370. *
  371. * @return int - The error code, one of UErrorCode values. Initial value
  372. * is U_ZERO_ERROR.
  373. */
  374. function datefmt_get_error_code(IntlDateFormatter $fmt): int {
  375. return $fmt->getErrorCode();
  376. }
  377. /**
  378. * Get the error text from the last operation.
  379. *
  380. * @param intldateformatter $fmt - The formatter resource.
  381. *
  382. * @return string - Description of the last error.
  383. */
  384. function datefmt_get_error_message(IntlDateFormatter $fmt): string {
  385. return $fmt->getErrorMessage();
  386. }
  387. /**
  388. * Get the locale used by formatter
  389. *
  390. * @param intldateformatter $fmt - The formatter resource
  391. * @param int $which -
  392. *
  393. * @return string - the locale of this formatter or 'false' if error
  394. */
  395. function datefmt_get_locale(IntlDateFormatter $fmt, ?int $which = null) {
  396. return $fmt->getLocale($which);
  397. }
  398. /**
  399. * Get the pattern used for the IntlDateFormatter
  400. *
  401. * @param intldateformatter $fmt - The formatter resource.
  402. *
  403. * @return string - The pattern string being used to format/parse.
  404. */
  405. function datefmt_get_pattern(IntlDateFormatter $fmt): string {
  406. return $fmt->getPattern();
  407. }
  408. /**
  409. * Get the timetype used for the IntlDateFormatter
  410. *
  411. * @param intldateformatter $fmt - The formatter resource.
  412. *
  413. * @return int - The current date type value of the formatter.
  414. */
  415. function datefmt_get_timetype(IntlDateFormatter $fmt): int {
  416. return $fmt->getTimeType();
  417. }
  418. /**
  419. * Get the timezone-id used for the IntlDateFormatter
  420. *
  421. * @param intldateformatter $fmt - The formatter resource.
  422. *
  423. * @return string - ID string for the time zone used by this formatter.
  424. */
  425. function datefmt_get_timezone_id(IntlDateFormatter $fmt): string {
  426. return $fmt->getTimeZoneId();
  427. }
  428. /**
  429. * Get copy of formatter's calendar object
  430. *
  431. * @return IntlCalendar - A copy of the internal calendar object used by
  432. * this formatter.
  433. */
  434. function datefmt_get_calendar_object(IntlDateFormatter $fmt): Object {
  435. return $fmt->getCalendarObject();
  436. }
  437. /**
  438. * Get formatter's timezone
  439. *
  440. * @return IntlTimeZone - The associated IntlTimeZone object.
  441. */
  442. function datefmt_get_timezone(IntlDateFormatter $fmt): Object {
  443. return $fmt->getTimeZone();
  444. }
  445. /**
  446. * Get the lenient used for the IntlDateFormatter
  447. *
  448. * @param intldateformatter $fmt - The formatter resource.
  449. *
  450. * @return bool - TRUE if parser is lenient, FALSE if parser is strict.
  451. * By default the parser is lenient.
  452. */
  453. function datefmt_is_lenient(IntlDateFormatter $fmt): bool {
  454. return $fmt->isLenient();
  455. }
  456. /**
  457. * Parse string to a field-based time value
  458. *
  459. * @param intldateformatter $fmt - The formatter resource
  460. * @param string $value - string to convert to a time
  461. * @param int $position - Position at which to start the parsing in
  462. * $value (zero-based). If no error occurs before $value is consumed,
  463. * $parse_pos will contain -1 otherwise it will contain the position at
  464. * which parsing ended . If $parse_pos > strlen($value), the parse fails
  465. * immediately.
  466. *
  467. * @return array - Localtime compatible array of integers : contains 24
  468. * hour clock value in tm_hour field
  469. */
  470. function datefmt_localtime(IntlDateFormatter $fmt,
  471. $value,
  472. &$position): mixed {
  473. return $fmt->localTime($value, $position);
  474. }
  475. /**
  476. * Parse string to a timestamp value
  477. *
  478. * @param intldateformatter $fmt - The formatter resource
  479. * @param string $value - string to convert to a time
  480. * @param int $position - Position at which to start the parsing in
  481. * $value (zero-based). If no error occurs before $value is consumed,
  482. * $parse_pos will contain -1 otherwise it will contain the position at
  483. * which parsing ended (and the error occurred). This variable will
  484. * contain the end position if the parse fails. If $parse_pos >
  485. * strlen($value), the parse fails immediately.
  486. *
  487. * @return int - timestamp parsed value, or FALSE if value can't be
  488. * parsed.
  489. */
  490. function datefmt_parse(IntlDateFormatter $fmt,
  491. $value,
  492. &$position = null): mixed {
  493. return $fmt->parse($value, $position);
  494. }
  495. /**
  496. * Sets the calendar type used by the formatter
  497. *
  498. * @param intldateformatter $fmt - The formatter resource.
  499. * @param mixed $which - This can either be: the calendar type to use
  500. * (default is IntlDateFormatter::GREGORIAN, which is also used if NULL
  501. * is specified) or an IntlCalendar object. Any IntlCalendar object
  502. * passed in will be cloned; no modifications will be made to the
  503. * argument object. The timezone of the formatter will only be kept if
  504. * an IntlCalendar object is not passed, otherwise the new timezone will
  505. * be that of the passed object.
  506. *
  507. * @return bool -
  508. */
  509. function datefmt_set_calendar(IntlDateFormatter $fmt,
  510. $which): bool {
  511. return $fmt->setCalendar($which);
  512. }
  513. /**
  514. * Set the leniency of the parser
  515. *
  516. * @param intldateformatter $fmt - The formatter resource
  517. * @param bool $lenient - Sets whether the parser is lenient or not,
  518. * default is TRUE (lenient).
  519. *
  520. * @return bool -
  521. */
  522. function datefmt_set_lenient(IntlDateFormatter $fmt,
  523. $lenient): bool {
  524. return $fmt->setLenient($lenient);
  525. }
  526. /**
  527. * Set the pattern used for the IntlDateFormatter
  528. *
  529. * @param intldateformatter $fmt - The formatter resource.
  530. * @param string $pattern - New pattern string to use. Possible patterns
  531. * are documented at .
  532. *
  533. * @return bool - Bad formatstrings are usually the cause of the
  534. * failure.
  535. */
  536. function datefmt_set_pattern(IntlDateFormatter $fmt,
  537. $pattern): bool {
  538. return $fmt->setPattern($pattern);
  539. }
  540. /**
  541. * Sets the time zone to use
  542. *
  543. * @param intldateformatter $fmt - The formatter resource.
  544. * @param string $zone - The time zone ID string of the time zone to use.
  545. * If NULL or the empty string, the default time zone for the runtime is
  546. * used.
  547. *
  548. * @return bool -
  549. */
  550. function datefmt_set_timezone_id(IntlDateFormatter $fmt,
  551. $zone): bool {
  552. return $fmt->setTimeZoneId($zone);
  553. }
  554. /**
  555. * Sets formatter's timezone
  556. *
  557. * @param mixed $zone -
  558. *
  559. * @return bool - Returns TRUE on success and FALSE on failure.
  560. */
  561. function datefmt_set_timezone(IntlDateFormatter $fmt, $zone): bool {
  562. return $fmt->setTimeZone($zone);
  563. }