2 *****************************************************************************************
3 * Copyright (C) 2010-2011, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *****************************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
12 #include "unicode/udateintervalformat.h"
13 #include "unicode/dtitvfmt.h"
14 #include "unicode/dtintrv.h"
15 #include "unicode/localpointer.h"
16 #include "unicode/timezone.h"
17 #include "unicode/locid.h"
18 #include "unicode/unistr.h"
23 U_CAPI UDateIntervalFormat
* U_EXPORT2
24 udtitvfmt_open(const char* locale
,
25 const UChar
* skeleton
,
26 int32_t skeletonLength
,
31 if (U_FAILURE(*status
)) {
34 if ((skeleton
== NULL
? skeletonLength
!= 0 : skeletonLength
< -1) ||
35 (tzID
== NULL
? tzIDLength
!= 0 : tzIDLength
< -1)
37 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
40 UnicodeString
skel((UBool
)(skeletonLength
== -1), skeleton
, skeletonLength
);
41 LocalPointer
<DateIntervalFormat
> formatter(
42 DateIntervalFormat::createInstance(skel
, Locale(locale
), *status
));
43 if (U_FAILURE(*status
)) {
47 TimeZone
*zone
= TimeZone::createTimeZone(UnicodeString((UBool
)(tzIDLength
== -1), tzID
, tzIDLength
));
49 *status
= U_MEMORY_ALLOCATION_ERROR
;
52 formatter
->adoptTimeZone(zone
);
54 return (UDateIntervalFormat
*)formatter
.orphan();
59 udtitvfmt_close(UDateIntervalFormat
*formatter
)
61 delete (DateIntervalFormat
*)formatter
;
65 U_CAPI
int32_t U_EXPORT2
66 udtitvfmt_format(const UDateIntervalFormat
* formatter
,
70 int32_t resultCapacity
,
71 UFieldPosition
* position
,
74 if (U_FAILURE(*status
)) {
77 if (result
== NULL
? resultCapacity
!= 0 : resultCapacity
< 0) {
78 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
83 // NULL destination for pure preflighting: empty dummy string
84 // otherwise, alias the destination buffer (copied from udat_format)
85 res
.setTo(result
, 0, resultCapacity
);
89 fp
.setField(position
->field
);
92 DateInterval interval
= DateInterval(fromDate
,toDate
);
93 ((const DateIntervalFormat
*)formatter
)->format( &interval
, res
, fp
, *status
);
94 if (U_FAILURE(*status
)) {
98 position
->beginIndex
= fp
.getBeginIndex();
99 position
->endIndex
= fp
.getEndIndex();
102 return res
.extract(result
, resultCapacity
, *status
);
105 U_CAPI
void U_EXPORT2
106 udtitvfmt_setAttribute(UDateIntervalFormat
* formatter
,
107 UDateIntervalFormatAttribute attr
,
108 UDateIntervalFormatAttributeValue value
,
111 if (U_FAILURE(*status
)) {
114 ((DateIntervalFormat
*)formatter
)->setAttribute( attr
, value
, *status
);
117 #endif /* #if !UCONFIG_NO_FORMATTING */