]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/udateintervalformat.cpp
ICU-511.35.tar.gz
[apple/icu.git] / icuSources / i18n / udateintervalformat.cpp
CommitLineData
4388f060
A
1/*
2*****************************************************************************************
3* Copyright (C) 2010-2011, International Business Machines
4* Corporation and others. All Rights Reserved.
5*****************************************************************************************
6*/
7
8#include "unicode/utypes.h"
9
10#if !UCONFIG_NO_FORMATTING
11
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"
19
20U_NAMESPACE_USE
21
22
23U_CAPI UDateIntervalFormat* U_EXPORT2
24udtitvfmt_open(const char* locale,
25 const UChar* skeleton,
26 int32_t skeletonLength,
27 const UChar* tzID,
28 int32_t tzIDLength,
29 UErrorCode* status)
30{
31 if (U_FAILURE(*status)) {
32 return NULL;
33 }
34 if ((skeleton == NULL ? skeletonLength != 0 : skeletonLength < -1) ||
35 (tzID == NULL ? tzIDLength != 0 : tzIDLength < -1)
36 ) {
37 *status = U_ILLEGAL_ARGUMENT_ERROR;
38 return NULL;
39 }
40 UnicodeString skel((UBool)(skeletonLength == -1), skeleton, skeletonLength);
41 LocalPointer<DateIntervalFormat> formatter(
42 DateIntervalFormat::createInstance(skel, Locale(locale), *status));
43 if (U_FAILURE(*status)) {
44 return NULL;
45 }
46 if(tzID != 0) {
47 TimeZone *zone = TimeZone::createTimeZone(UnicodeString((UBool)(tzIDLength == -1), tzID, tzIDLength));
48 if(zone == NULL) {
49 *status = U_MEMORY_ALLOCATION_ERROR;
50 return NULL;
51 }
52 formatter->adoptTimeZone(zone);
53 }
54 return (UDateIntervalFormat*)formatter.orphan();
55}
56
57
58U_CAPI void U_EXPORT2
59udtitvfmt_close(UDateIntervalFormat *formatter)
60{
61 delete (DateIntervalFormat*)formatter;
62}
63
64
65U_CAPI int32_t U_EXPORT2
66udtitvfmt_format(const UDateIntervalFormat* formatter,
67 UDate fromDate,
68 UDate toDate,
69 UChar* result,
70 int32_t resultCapacity,
71 UFieldPosition* position,
72 UErrorCode* status)
73{
74 if (U_FAILURE(*status)) {
75 return -1;
76 }
77 if (result == NULL ? resultCapacity != 0 : resultCapacity < 0) {
78 *status = U_ILLEGAL_ARGUMENT_ERROR;
79 return 0;
80 }
81 UnicodeString res;
82 if (result != NULL) {
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);
86 }
87 FieldPosition fp;
88 if (position != 0) {
89 fp.setField(position->field);
90 }
91
92 DateInterval interval = DateInterval(fromDate,toDate);
93 ((const DateIntervalFormat*)formatter)->format( &interval, res, fp, *status );
94 if (U_FAILURE(*status)) {
95 return -1;
96 }
97 if (position != 0) {
98 position->beginIndex = fp.getBeginIndex();
99 position->endIndex = fp.getEndIndex();
100 }
101
102 return res.extract(result, resultCapacity, *status);
103}
104
105U_CAPI void U_EXPORT2
106udtitvfmt_setAttribute(UDateIntervalFormat* formatter,
107 UDateIntervalFormatAttribute attr,
108 UDateIntervalFormatAttributeValue value,
109 UErrorCode* status)
110{
111 if (U_FAILURE(*status)) {
112 return;
113 }
114 ((DateIntervalFormat*)formatter)->setAttribute( attr, value, *status );
115}
116
117#endif /* #if !UCONFIG_NO_FORMATTING */