1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*****************************************************************************************
* Copyright (C) 2010-2011, International Business Machines
* Corporation and others. All Rights Reserved.
*****************************************************************************************
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/udateintervalformat.h"
#include "unicode/dtitvfmt.h"
#include "unicode/dtintrv.h"
#include "unicode/localpointer.h"
#include "unicode/timezone.h"
#include "unicode/locid.h"
#include "unicode/unistr.h"
#include "formattedval_impl.h"
U_NAMESPACE_USE
// Magic number: FDIV in ASCII
UPRV_FORMATTED_VALUE_CAPI_AUTO_IMPL(
FormattedDateInterval,
UFormattedDateInterval,
UFormattedDateIntervalImpl,
UFormattedDateIntervalApiHelper,
udtitvfmt,
0x46444956)
U_CAPI UDateIntervalFormat* U_EXPORT2
udtitvfmt_open(const char* locale,
const UChar* skeleton,
int32_t skeletonLength,
const UChar* tzID,
int32_t tzIDLength,
UErrorCode* status)
{
if (U_FAILURE(*status)) {
return NULL;
}
if ((skeleton == NULL ? skeletonLength != 0 : skeletonLength < -1) ||
(tzID == NULL ? tzIDLength != 0 : tzIDLength < -1)
) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return NULL;
}
UnicodeString skel((UBool)(skeletonLength == -1), skeleton, skeletonLength);
LocalPointer<DateIntervalFormat> formatter(
DateIntervalFormat::createInstance(skel, Locale(locale), *status));
if (U_FAILURE(*status)) {
return NULL;
}
if(tzID != 0) {
TimeZone *zone = TimeZone::createTimeZone(UnicodeString((UBool)(tzIDLength == -1), tzID, tzIDLength));
if(zone == NULL) {
*status = U_MEMORY_ALLOCATION_ERROR;
return NULL;
}
formatter->adoptTimeZone(zone);
}
return (UDateIntervalFormat*)formatter.orphan();
}
U_CAPI void U_EXPORT2
udtitvfmt_close(UDateIntervalFormat *formatter)
{
delete (DateIntervalFormat*)formatter;
}
U_CAPI int32_t U_EXPORT2
udtitvfmt_format(const UDateIntervalFormat* formatter,
UDate fromDate,
UDate toDate,
UChar* result,
int32_t resultCapacity,
UFieldPosition* position,
UErrorCode* status)
{
if (U_FAILURE(*status)) {
return -1;
}
if (result == NULL ? resultCapacity != 0 : resultCapacity < 0) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
UnicodeString res;
if (result != NULL) {
// NULL destination for pure preflighting: empty dummy string
// otherwise, alias the destination buffer (copied from udat_format)
res.setTo(result, 0, resultCapacity);
}
FieldPosition fp;
if (position != 0) {
fp.setField(position->field);
}
DateInterval interval = DateInterval(fromDate,toDate);
((const DateIntervalFormat*)formatter)->format( &interval, res, fp, *status );
if (U_FAILURE(*status)) {
return -1;
}
if (position != 0) {
position->beginIndex = fp.getBeginIndex();
position->endIndex = fp.getEndIndex();
}
return res.extract(result, resultCapacity, *status);
}
U_DRAFT void U_EXPORT2
udtitvfmt_formatToResult(
const UDateIntervalFormat* formatter,
UDate fromDate,
UDate toDate,
UFormattedDateInterval* result,
UErrorCode* status) {
if (U_FAILURE(*status)) {
return;
}
auto* resultImpl = UFormattedDateIntervalApiHelper::validate(result, *status);
DateInterval interval = DateInterval(fromDate,toDate);
if (resultImpl != nullptr) {
resultImpl->fImpl = reinterpret_cast<const DateIntervalFormat*>(formatter)
->formatToValue(interval, *status);
}
}
U_DRAFT void U_EXPORT2
udtitvfmt_formatCalendarToResult(
const UDateIntervalFormat* formatter,
UCalendar* fromCalendar,
UCalendar* toCalendar,
UFormattedDateInterval* result,
UErrorCode* status) {
if (U_FAILURE(*status)) {
return;
}
auto* resultImpl = UFormattedDateIntervalApiHelper::validate(result, *status);
if (resultImpl != nullptr) {
resultImpl->fImpl = reinterpret_cast<const DateIntervalFormat*>(formatter)
->formatToValue(*(Calendar *)fromCalendar, *(Calendar *)toCalendar, *status);
}
}
#endif /* #if !UCONFIG_NO_FORMATTING */
|