1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *****************************************************************************************
5 * Copyright (C) 2010-2011, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *****************************************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/udateintervalformat.h"
15 #include "unicode/dtitvfmt.h"
16 #include "unicode/dtintrv.h"
17 #include "unicode/localpointer.h"
18 #include "unicode/timezone.h"
19 #include "unicode/locid.h"
20 #include "unicode/unistr.h"
21 #include "formattedval_impl.h"
26 // Magic number: FDIV in ASCII
27 UPRV_FORMATTED_VALUE_CAPI_AUTO_IMPL(
28 FormattedDateInterval
,
29 UFormattedDateInterval
,
30 UFormattedDateIntervalImpl
,
31 UFormattedDateIntervalApiHelper
,
36 U_CAPI UDateIntervalFormat
* U_EXPORT2
37 udtitvfmt_open(const char* locale
,
38 const UChar
* skeleton
,
39 int32_t skeletonLength
,
44 if (U_FAILURE(*status
)) {
47 if ((skeleton
== NULL
? skeletonLength
!= 0 : skeletonLength
< -1) ||
48 (tzID
== NULL
? tzIDLength
!= 0 : tzIDLength
< -1)
50 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
53 UnicodeString
skel((UBool
)(skeletonLength
== -1), skeleton
, skeletonLength
);
54 LocalPointer
<DateIntervalFormat
> formatter(
55 DateIntervalFormat::createInstance(skel
, Locale(locale
), *status
));
56 if (U_FAILURE(*status
)) {
60 TimeZone
*zone
= TimeZone::createTimeZone(UnicodeString((UBool
)(tzIDLength
== -1), tzID
, tzIDLength
));
62 *status
= U_MEMORY_ALLOCATION_ERROR
;
65 formatter
->adoptTimeZone(zone
);
67 return (UDateIntervalFormat
*)formatter
.orphan();
72 udtitvfmt_close(UDateIntervalFormat
*formatter
)
74 delete (DateIntervalFormat
*)formatter
;
78 U_CAPI
int32_t U_EXPORT2
79 udtitvfmt_format(const UDateIntervalFormat
* formatter
,
83 int32_t resultCapacity
,
84 UFieldPosition
* position
,
87 if (U_FAILURE(*status
)) {
90 if (result
== NULL
? resultCapacity
!= 0 : resultCapacity
< 0) {
91 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
96 // NULL destination for pure preflighting: empty dummy string
97 // otherwise, alias the destination buffer (copied from udat_format)
98 res
.setTo(result
, 0, resultCapacity
);
102 fp
.setField(position
->field
);
105 DateInterval interval
= DateInterval(fromDate
,toDate
);
106 ((const DateIntervalFormat
*)formatter
)->format( &interval
, res
, fp
, *status
);
107 if (U_FAILURE(*status
)) {
111 position
->beginIndex
= fp
.getBeginIndex();
112 position
->endIndex
= fp
.getEndIndex();
115 return res
.extract(result
, resultCapacity
, *status
);
118 U_CAPI
void U_EXPORT2
119 udtitvfmt_setAttribute(UDateIntervalFormat
* formatter
,
120 UDateIntervalFormatAttribute attr
,
121 UDateIntervalFormatAttributeValue value
,
124 if (U_FAILURE(*status
)) {
127 ((DateIntervalFormat
*)formatter
)->setAttribute( attr
, value
, *status
);
130 U_DRAFT
void U_EXPORT2
131 udtitvfmt_formatToResult(
132 const UDateIntervalFormat
* formatter
,
133 UFormattedDateInterval
* result
,
136 UErrorCode
* status
) {
137 if (U_FAILURE(*status
)) {
140 auto* resultImpl
= UFormattedDateIntervalApiHelper::validate(result
, *status
);
141 DateInterval interval
= DateInterval(fromDate
,toDate
);
142 resultImpl
->fImpl
= reinterpret_cast<const DateIntervalFormat
*>(formatter
)
143 ->formatToValue(interval
, *status
);
146 U_CAPI
void U_EXPORT2
147 udtitvfmt_setContext(UDateIntervalFormat
* formatter
, UDisplayContext value
, UErrorCode
* status
)
149 ((DateIntervalFormat
*)formatter
)->setContext( value
, *status
);
152 U_CAPI UDisplayContext U_EXPORT2
153 udtitvfmt_getContext(const UDateIntervalFormat
* formatter
, UDisplayContextType type
, UErrorCode
* status
)
155 return ((DateIntervalFormat
*)formatter
)->getContext( type
, *status
);
159 #endif /* #if !UCONFIG_NO_FORMATTING */