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"
25 U_CAPI UDateIntervalFormat
* U_EXPORT2
26 udtitvfmt_open(const char* locale
,
27 const UChar
* skeleton
,
28 int32_t skeletonLength
,
33 if (U_FAILURE(*status
)) {
36 if ((skeleton
== NULL
? skeletonLength
!= 0 : skeletonLength
< -1) ||
37 (tzID
== NULL
? tzIDLength
!= 0 : tzIDLength
< -1)
39 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
42 UnicodeString
skel((UBool
)(skeletonLength
== -1), skeleton
, skeletonLength
);
43 LocalPointer
<DateIntervalFormat
> formatter(
44 DateIntervalFormat::createInstance(skel
, Locale(locale
), *status
));
45 if (U_FAILURE(*status
)) {
49 TimeZone
*zone
= TimeZone::createTimeZone(UnicodeString((UBool
)(tzIDLength
== -1), tzID
, tzIDLength
));
51 *status
= U_MEMORY_ALLOCATION_ERROR
;
54 formatter
->adoptTimeZone(zone
);
56 return (UDateIntervalFormat
*)formatter
.orphan();
61 udtitvfmt_close(UDateIntervalFormat
*formatter
)
63 delete (DateIntervalFormat
*)formatter
;
67 U_CAPI
int32_t U_EXPORT2
68 udtitvfmt_format(const UDateIntervalFormat
* formatter
,
72 int32_t resultCapacity
,
73 UFieldPosition
* position
,
76 if (U_FAILURE(*status
)) {
79 if (result
== NULL
? resultCapacity
!= 0 : resultCapacity
< 0) {
80 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
85 // NULL destination for pure preflighting: empty dummy string
86 // otherwise, alias the destination buffer (copied from udat_format)
87 res
.setTo(result
, 0, resultCapacity
);
91 fp
.setField(position
->field
);
94 DateInterval interval
= DateInterval(fromDate
,toDate
);
95 ((const DateIntervalFormat
*)formatter
)->format( &interval
, res
, fp
, *status
);
96 if (U_FAILURE(*status
)) {
100 position
->beginIndex
= fp
.getBeginIndex();
101 position
->endIndex
= fp
.getEndIndex();
104 return res
.extract(result
, resultCapacity
, *status
);
107 U_CAPI
void U_EXPORT2
108 udtitvfmt_setAttribute(UDateIntervalFormat
* formatter
,
109 UDateIntervalFormatAttribute attr
,
110 UDateIntervalFormatAttributeValue value
,
113 if (U_FAILURE(*status
)) {
116 ((DateIntervalFormat
*)formatter
)->setAttribute( attr
, value
, *status
);
119 #endif /* #if !UCONFIG_NO_FORMATTING */