udateintervalformat.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *****************************************************************************************
  5. * Copyright (C) 2010-2011, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *****************************************************************************************
  8. */
  9. #include "unicode/utypes.h"
  10. #if !UCONFIG_NO_FORMATTING
  11. #include "unicode/udateintervalformat.h"
  12. #include "unicode/dtitvfmt.h"
  13. #include "unicode/dtintrv.h"
  14. #include "unicode/localpointer.h"
  15. #include "unicode/timezone.h"
  16. #include "unicode/locid.h"
  17. #include "unicode/unistr.h"
  18. #include "unicode/udisplaycontext.h"
  19. #include "formattedval_impl.h"
  20. U_NAMESPACE_USE
  21. // Magic number: FDIV in ASCII
  22. UPRV_FORMATTED_VALUE_CAPI_AUTO_IMPL(
  23. FormattedDateInterval,
  24. UFormattedDateInterval,
  25. UFormattedDateIntervalImpl,
  26. UFormattedDateIntervalApiHelper,
  27. udtitvfmt,
  28. 0x46444956)
  29. U_CAPI UDateIntervalFormat* U_EXPORT2
  30. udtitvfmt_open(const char* locale,
  31. const char16_t* skeleton,
  32. int32_t skeletonLength,
  33. const char16_t* tzID,
  34. int32_t tzIDLength,
  35. UErrorCode* status)
  36. {
  37. if (U_FAILURE(*status)) {
  38. return nullptr;
  39. }
  40. if ((skeleton == nullptr ? skeletonLength != 0 : skeletonLength < -1) ||
  41. (tzID == nullptr ? tzIDLength != 0 : tzIDLength < -1)
  42. ) {
  43. *status = U_ILLEGAL_ARGUMENT_ERROR;
  44. return nullptr;
  45. }
  46. UnicodeString skel((UBool)(skeletonLength == -1), skeleton, skeletonLength);
  47. LocalPointer<DateIntervalFormat> formatter(
  48. DateIntervalFormat::createInstance(skel, Locale(locale), *status));
  49. if (U_FAILURE(*status)) {
  50. return nullptr;
  51. }
  52. if(tzID != 0) {
  53. TimeZone *zone = TimeZone::createTimeZone(UnicodeString((UBool)(tzIDLength == -1), tzID, tzIDLength));
  54. if(zone == nullptr) {
  55. *status = U_MEMORY_ALLOCATION_ERROR;
  56. return nullptr;
  57. }
  58. formatter->adoptTimeZone(zone);
  59. }
  60. return (UDateIntervalFormat*)formatter.orphan();
  61. }
  62. U_CAPI void U_EXPORT2
  63. udtitvfmt_close(UDateIntervalFormat *formatter)
  64. {
  65. delete (DateIntervalFormat*)formatter;
  66. }
  67. U_CAPI int32_t U_EXPORT2
  68. udtitvfmt_format(const UDateIntervalFormat* formatter,
  69. UDate fromDate,
  70. UDate toDate,
  71. char16_t* result,
  72. int32_t resultCapacity,
  73. UFieldPosition* position,
  74. UErrorCode* status)
  75. {
  76. if (U_FAILURE(*status)) {
  77. return -1;
  78. }
  79. if (result == nullptr ? resultCapacity != 0 : resultCapacity < 0) {
  80. *status = U_ILLEGAL_ARGUMENT_ERROR;
  81. return 0;
  82. }
  83. UnicodeString res;
  84. if (result != nullptr) {
  85. // nullptr destination for pure preflighting: empty dummy string
  86. // otherwise, alias the destination buffer (copied from udat_format)
  87. res.setTo(result, 0, resultCapacity);
  88. }
  89. FieldPosition fp;
  90. if (position != 0) {
  91. fp.setField(position->field);
  92. }
  93. DateInterval interval = DateInterval(fromDate,toDate);
  94. ((const DateIntervalFormat*)formatter)->format( &interval, res, fp, *status );
  95. if (U_FAILURE(*status)) {
  96. return -1;
  97. }
  98. if (position != 0) {
  99. position->beginIndex = fp.getBeginIndex();
  100. position->endIndex = fp.getEndIndex();
  101. }
  102. return res.extract(result, resultCapacity, *status);
  103. }
  104. U_CAPI void U_EXPORT2
  105. udtitvfmt_formatToResult(
  106. const UDateIntervalFormat* formatter,
  107. UDate fromDate,
  108. UDate toDate,
  109. UFormattedDateInterval* result,
  110. UErrorCode* status) {
  111. if (U_FAILURE(*status)) {
  112. return;
  113. }
  114. auto* resultImpl = UFormattedDateIntervalApiHelper::validate(result, *status);
  115. DateInterval interval = DateInterval(fromDate,toDate);
  116. if (resultImpl != nullptr) {
  117. resultImpl->fImpl = reinterpret_cast<const DateIntervalFormat*>(formatter)
  118. ->formatToValue(interval, *status);
  119. }
  120. }
  121. U_CAPI void U_EXPORT2
  122. udtitvfmt_formatCalendarToResult(
  123. const UDateIntervalFormat* formatter,
  124. UCalendar* fromCalendar,
  125. UCalendar* toCalendar,
  126. UFormattedDateInterval* result,
  127. UErrorCode* status) {
  128. if (U_FAILURE(*status)) {
  129. return;
  130. }
  131. auto* resultImpl = UFormattedDateIntervalApiHelper::validate(result, *status);
  132. if (resultImpl != nullptr) {
  133. resultImpl->fImpl = reinterpret_cast<const DateIntervalFormat*>(formatter)
  134. ->formatToValue(*(Calendar *)fromCalendar, *(Calendar *)toCalendar, *status);
  135. }
  136. }
  137. U_CAPI void U_EXPORT2
  138. udtitvfmt_setContext(UDateIntervalFormat* formatter,
  139. UDisplayContext value,
  140. UErrorCode* status) {
  141. if (U_FAILURE(*status)) {
  142. return;
  143. }
  144. reinterpret_cast<DateIntervalFormat*>(formatter)->setContext( value, *status );
  145. }
  146. U_CAPI UDisplayContext U_EXPORT2
  147. udtitvfmt_getContext(const UDateIntervalFormat* formatter,
  148. UDisplayContextType type,
  149. UErrorCode* status) {
  150. if (U_FAILURE(*status)) {
  151. return (UDisplayContext)0;
  152. }
  153. return reinterpret_cast<const DateIntervalFormat*>(formatter)->getContext( type, *status );
  154. }
  155. #endif /* #if !UCONFIG_NO_FORMATTING */