dateformat_helpers.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Gustavo Lopes <cataphract@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "../intl_cppshims.h"
  17. #include <unicode/calendar.h>
  18. #include <unicode/gregocal.h>
  19. #include "dateformat_helpers.h"
  20. extern "C" {
  21. #include "../php_intl.h"
  22. #include <Zend/zend_operators.h>
  23. #define USE_CALENDAR_POINTER 1
  24. #include "../calendar/calendar_class.h"
  25. }
  26. int datefmt_process_calendar_arg(zval* calendar_zv,
  27. Locale const& locale,
  28. const char *func_name,
  29. intl_error *err,
  30. Calendar*& cal,
  31. long& cal_int_type,
  32. bool& calendar_owned TSRMLS_DC)
  33. {
  34. char *msg;
  35. UErrorCode status = UErrorCode();
  36. if (calendar_zv == NULL || Z_TYPE_P(calendar_zv) == IS_NULL) {
  37. // default requested
  38. cal = new GregorianCalendar(locale, status);
  39. calendar_owned = true;
  40. cal_int_type = UCAL_GREGORIAN;
  41. } else if (Z_TYPE_P(calendar_zv) == IS_LONG) {
  42. long v = Z_LVAL_P(calendar_zv);
  43. if (v != (long)UCAL_TRADITIONAL && v != (long)UCAL_GREGORIAN) {
  44. spprintf(&msg, 0, "%s: invalid value for calendar type; it must be "
  45. "one of IntlDateFormatter::TRADITIONAL (locale's default "
  46. "calendar) or IntlDateFormatter::GREGORIAN. "
  47. "Alternatively, it can be an IntlCalendar object",
  48. func_name);
  49. intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR, msg, 1 TSRMLS_CC);
  50. efree(msg);
  51. return FAILURE;
  52. } else if (v == (long)UCAL_TRADITIONAL) {
  53. cal = Calendar::createInstance(locale, status);
  54. } else { //UCAL_GREGORIAN
  55. cal = new GregorianCalendar(locale, status);
  56. }
  57. calendar_owned = true;
  58. cal_int_type = Z_LVAL_P(calendar_zv);
  59. } else if (Z_TYPE_P(calendar_zv) == IS_OBJECT &&
  60. instanceof_function_ex(Z_OBJCE_P(calendar_zv),
  61. Calendar_ce_ptr, 0 TSRMLS_CC)) {
  62. cal = calendar_fetch_native_calendar(calendar_zv TSRMLS_CC);
  63. if (cal == NULL) {
  64. spprintf(&msg, 0, "%s: Found unconstructed IntlCalendar object",
  65. func_name);
  66. intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR, msg, 1 TSRMLS_CC);
  67. efree(msg);
  68. return FAILURE;
  69. }
  70. calendar_owned = false;
  71. cal_int_type = -1;
  72. } else {
  73. spprintf(&msg, 0, "%s: Invalid calendar argument; should be an integer "
  74. "or an IntlCalendar instance", func_name);
  75. intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR, msg, 1 TSRMLS_CC);
  76. efree(msg);
  77. return FAILURE;
  78. }
  79. if (cal == NULL && !U_FAILURE(status)) {
  80. status = U_MEMORY_ALLOCATION_ERROR;
  81. }
  82. if (U_FAILURE(status)) {
  83. spprintf(&msg, 0, "%s: Failure instantiating calendar", func_name);
  84. intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR, msg, 1 TSRMLS_CC);
  85. efree(msg);
  86. return FAILURE;
  87. }
  88. return SUCCESS;
  89. }