/src/mozilla-central/intl/icu/source/i18n/udateintervalformat.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
10 | | #include "unicode/utypes.h" |
11 | | |
12 | | #if !UCONFIG_NO_FORMATTING |
13 | | |
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 | | |
22 | | U_NAMESPACE_USE |
23 | | |
24 | | |
25 | | U_CAPI UDateIntervalFormat* U_EXPORT2 |
26 | | udtitvfmt_open(const char* locale, |
27 | | const UChar* skeleton, |
28 | | int32_t skeletonLength, |
29 | | const UChar* tzID, |
30 | | int32_t tzIDLength, |
31 | | UErrorCode* status) |
32 | 0 | { |
33 | 0 | if (U_FAILURE(*status)) { |
34 | 0 | return NULL; |
35 | 0 | } |
36 | 0 | if ((skeleton == NULL ? skeletonLength != 0 : skeletonLength < -1) || |
37 | 0 | (tzID == NULL ? tzIDLength != 0 : tzIDLength < -1) |
38 | 0 | ) { |
39 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
40 | 0 | return NULL; |
41 | 0 | } |
42 | 0 | UnicodeString skel((UBool)(skeletonLength == -1), skeleton, skeletonLength); |
43 | 0 | LocalPointer<DateIntervalFormat> formatter( |
44 | 0 | DateIntervalFormat::createInstance(skel, Locale(locale), *status)); |
45 | 0 | if (U_FAILURE(*status)) { |
46 | 0 | return NULL; |
47 | 0 | } |
48 | 0 | if(tzID != 0) { |
49 | 0 | TimeZone *zone = TimeZone::createTimeZone(UnicodeString((UBool)(tzIDLength == -1), tzID, tzIDLength)); |
50 | 0 | if(zone == NULL) { |
51 | 0 | *status = U_MEMORY_ALLOCATION_ERROR; |
52 | 0 | return NULL; |
53 | 0 | } |
54 | 0 | formatter->adoptTimeZone(zone); |
55 | 0 | } |
56 | 0 | return (UDateIntervalFormat*)formatter.orphan(); |
57 | 0 | } |
58 | | |
59 | | |
60 | | U_CAPI void U_EXPORT2 |
61 | | udtitvfmt_close(UDateIntervalFormat *formatter) |
62 | 0 | { |
63 | 0 | delete (DateIntervalFormat*)formatter; |
64 | 0 | } |
65 | | |
66 | | |
67 | | U_CAPI int32_t U_EXPORT2 |
68 | | udtitvfmt_format(const UDateIntervalFormat* formatter, |
69 | | UDate fromDate, |
70 | | UDate toDate, |
71 | | UChar* result, |
72 | | int32_t resultCapacity, |
73 | | UFieldPosition* position, |
74 | | UErrorCode* status) |
75 | 0 | { |
76 | 0 | if (U_FAILURE(*status)) { |
77 | 0 | return -1; |
78 | 0 | } |
79 | 0 | if (result == NULL ? resultCapacity != 0 : resultCapacity < 0) { |
80 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
81 | 0 | return 0; |
82 | 0 | } |
83 | 0 | UnicodeString res; |
84 | 0 | if (result != NULL) { |
85 | 0 | // NULL destination for pure preflighting: empty dummy string |
86 | 0 | // otherwise, alias the destination buffer (copied from udat_format) |
87 | 0 | res.setTo(result, 0, resultCapacity); |
88 | 0 | } |
89 | 0 | FieldPosition fp; |
90 | 0 | if (position != 0) { |
91 | 0 | fp.setField(position->field); |
92 | 0 | } |
93 | 0 |
|
94 | 0 | DateInterval interval = DateInterval(fromDate,toDate); |
95 | 0 | ((const DateIntervalFormat*)formatter)->format( &interval, res, fp, *status ); |
96 | 0 | if (U_FAILURE(*status)) { |
97 | 0 | return -1; |
98 | 0 | } |
99 | 0 | if (position != 0) { |
100 | 0 | position->beginIndex = fp.getBeginIndex(); |
101 | 0 | position->endIndex = fp.getEndIndex(); |
102 | 0 | } |
103 | 0 |
|
104 | 0 | return res.extract(result, resultCapacity, *status); |
105 | 0 | } |
106 | | |
107 | | |
108 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |