]>
Commit | Line | Data |
---|---|---|
2ca993e8 A |
1 | /* |
2 | ***************************************************************************************** | |
3 | * Copyright (C) 2016, International Business Machines | |
4 | * Corporation and others. All Rights Reserved. | |
5 | ***************************************************************************************** | |
6 | */ | |
7 | ||
8 | #ifndef URELDATEFMT_H | |
9 | #define URELDATEFMT_H | |
10 | ||
11 | #include "unicode/utypes.h" | |
12 | ||
13 | #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION | |
14 | ||
15 | #include "unicode/unum.h" | |
16 | #include "unicode/udisplaycontext.h" | |
17 | #include "unicode/localpointer.h" | |
18 | ||
19 | /** | |
20 | * \file | |
21 | * \brief C API: URelativeDateTimeFormatter, relative date formatting of unit + numeric offset. | |
22 | * | |
23 | * Provides simple formatting of relative dates, in two ways | |
24 | * <ul> | |
25 | * <li>relative dates with a quantity e.g "in 5 days"</li> | |
26 | * <li>relative dates without a quantity e.g "next Tuesday"</li> | |
27 | * </ul> | |
28 | * <p> | |
29 | * This does not provide compound formatting for multiple units, | |
30 | * other than the ability to combine a time string with a relative date, | |
31 | * as in "next Tuesday at 3:45 PM". It also does not provide support | |
32 | * for determining which unit to use, such as deciding between "in 7 days" | |
33 | * and "in 1 week". | |
34 | * | |
35 | * @draft ICU 57 | |
36 | */ | |
37 | ||
38 | /** | |
39 | * The formatting style | |
40 | * @stable ICU 54 | |
41 | */ | |
42 | typedef enum UDateRelativeDateTimeFormatterStyle { | |
43 | /** | |
44 | * Everything spelled out. | |
45 | * @stable ICU 54 | |
46 | */ | |
47 | UDAT_STYLE_LONG, | |
48 | ||
49 | /** | |
50 | * Abbreviations used when possible. | |
51 | * @stable ICU 54 | |
52 | */ | |
53 | UDAT_STYLE_SHORT, | |
54 | ||
55 | /** | |
56 | * Use the shortest possible form. | |
57 | * @stable ICU 54 | |
58 | */ | |
59 | UDAT_STYLE_NARROW, | |
60 | ||
61 | /** | |
62 | * The number of styles. | |
63 | * @stable ICU 54 | |
64 | */ | |
65 | UDAT_STYLE_COUNT | |
66 | } UDateRelativeDateTimeFormatterStyle; | |
67 | ||
68 | #ifndef U_HIDE_DRAFT_API | |
69 | /** | |
70 | * Represents the unit for formatting a relative date. e.g "in 5 days" | |
71 | * or "next year" | |
72 | * @draft ICU 57 | |
73 | */ | |
74 | typedef enum URelativeDateTimeUnit { | |
75 | /** | |
76 | * Specifies that relative unit is year, e.g. "last year", | |
77 | * "in 5 years". | |
78 | * @draft ICU 57 | |
79 | */ | |
80 | UDAT_REL_UNIT_YEAR, | |
81 | /** | |
82 | * Specifies that relative unit is quarter, e.g. "last quarter", | |
83 | * "in 5 quarters". | |
84 | * @draft ICU 57 | |
85 | */ | |
86 | UDAT_REL_UNIT_QUARTER, | |
87 | /** | |
88 | * Specifies that relative unit is month, e.g. "last month", | |
89 | * "in 5 months". | |
90 | * @draft ICU 57 | |
91 | */ | |
92 | UDAT_REL_UNIT_MONTH, | |
93 | /** | |
94 | * Specifies that relative unit is week, e.g. "last week", | |
95 | * "in 5 weeks". | |
96 | * @draft ICU 57 | |
97 | */ | |
98 | UDAT_REL_UNIT_WEEK, | |
99 | /** | |
100 | * Specifies that relative unit is day, e.g. "yesterday", | |
101 | * "in 5 days". | |
102 | * @draft ICU 57 | |
103 | */ | |
104 | UDAT_REL_UNIT_DAY, | |
105 | /** | |
106 | * Specifies that relative unit is hour, e.g. "1 hour ago", | |
107 | * "in 5 hours". | |
108 | * @draft ICU 57 | |
109 | */ | |
110 | UDAT_REL_UNIT_HOUR, | |
111 | /** | |
112 | * Specifies that relative unit is minute, e.g. "1 minute ago", | |
113 | * "in 5 minutes". | |
114 | * @draft ICU 57 | |
115 | */ | |
116 | UDAT_REL_UNIT_MINUTE, | |
117 | /** | |
118 | * Specifies that relative unit is second, e.g. "1 second ago", | |
119 | * "in 5 seconds". | |
120 | * @draft ICU 57 | |
121 | */ | |
122 | UDAT_REL_UNIT_SECOND, | |
123 | /** | |
124 | * Specifies that relative unit is Sunday, e.g. "last Sunday", | |
125 | * "this Sunday", "next Sunday", "in 5 Sundays". | |
126 | * @draft ICU 57 | |
127 | */ | |
128 | UDAT_REL_UNIT_SUNDAY, | |
129 | /** | |
130 | * Specifies that relative unit is Monday, e.g. "last Monday", | |
131 | * "this Monday", "next Monday", "in 5 Mondays". | |
132 | * @draft ICU 57 | |
133 | */ | |
134 | UDAT_REL_UNIT_MONDAY, | |
135 | /** | |
136 | * Specifies that relative unit is Tuesday, e.g. "last Tuesday", | |
137 | * "this Tuesday", "next Tuesday", "in 5 Tuesdays". | |
138 | * @draft ICU 57 | |
139 | */ | |
140 | UDAT_REL_UNIT_TUESDAY, | |
141 | /** | |
142 | * Specifies that relative unit is Wednesday, e.g. "last Wednesday", | |
143 | * "this Wednesday", "next Wednesday", "in 5 Wednesdays". | |
144 | * @draft ICU 57 | |
145 | */ | |
146 | UDAT_REL_UNIT_WEDNESDAY, | |
147 | /** | |
148 | * Specifies that relative unit is Thursday, e.g. "last Thursday", | |
149 | * "this Thursday", "next Thursday", "in 5 Thursdays". | |
150 | * @draft ICU 57 | |
151 | */ | |
152 | UDAT_REL_UNIT_THURSDAY, | |
153 | /** | |
154 | * Specifies that relative unit is Friday, e.g. "last Friday", | |
155 | * "this Friday", "next Friday", "in 5 Fridays". | |
156 | * @draft ICU 57 | |
157 | */ | |
158 | UDAT_REL_UNIT_FRIDAY, | |
159 | /** | |
160 | * Specifies that relative unit is Saturday, e.g. "last Saturday", | |
161 | * "this Saturday", "next Saturday", "in 5 Saturdays". | |
162 | * @draft ICU 57 | |
163 | */ | |
164 | UDAT_REL_UNIT_SATURDAY, | |
165 | /** | |
166 | * Count of URelativeDateTimeUnit values | |
167 | * @draft ICU 57 | |
168 | */ | |
169 | UDAT_REL_UNIT_COUNT | |
170 | } URelativeDateTimeUnit; | |
171 | #endif /* U_HIDE_DRAFT_API */ | |
172 | ||
173 | #ifndef U_HIDE_DRAFT_API | |
174 | ||
175 | /** | |
176 | * Opaque URelativeDateTimeFormatter object for use in C programs. | |
177 | * @draft ICU 57 | |
178 | */ | |
179 | struct URelativeDateTimeFormatter; | |
180 | typedef struct URelativeDateTimeFormatter URelativeDateTimeFormatter; /**< C typedef for struct URelativeDateTimeFormatter. @draft ICU 57 */ | |
181 | ||
182 | ||
183 | /** | |
184 | * Open a new URelativeDateTimeFormatter object for a given locale using the | |
185 | * specified width and capitalizationContext, along with a number formatter | |
186 | * (if desired) to override the default formatter that would be used for | |
187 | * display of numeric field offsets. The default formatter typically rounds | |
188 | * toward 0 and has a minimum of 0 fraction digits and a maximum of 3 | |
189 | * fraction digits (i.e. it will show as many decimal places as necessary | |
190 | * up to 3, without showing trailing 0s). | |
191 | * | |
192 | * @param locale | |
193 | * The locale | |
194 | * @param nfToAdopt | |
195 | * A number formatter to set for this URelativeDateTimeFormatter | |
196 | * object (instead of the default decimal formatter). Ownership of | |
197 | * this UNumberFormat object will pass to the URelativeDateTimeFormatter | |
198 | * object (the URelativeDateTimeFormatter adopts the UNumberFormat), | |
199 | * which becomes responsible for closing it. If the caller wishes to | |
200 | * retain ownership of the UNumberFormat object, the caller must clone | |
201 | * it (with unum_clone) and pass the clone to ureldatefmt_open. May be | |
202 | * NULL to use the default decimal formatter. | |
203 | * @param width | |
204 | * The width - wide, short, narrow, etc. | |
205 | * @param capitalizationContext | |
206 | * A value from UDisplayContext that pertains to capitalization, e.g. | |
207 | * UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE. | |
208 | * @param status | |
209 | * A pointer to a UErrorCode to receive any errors. | |
210 | * @return | |
211 | * A pointer to a URelativeDateTimeFormatter object for the specified locale, | |
212 | * or NULL if an error occurred. | |
213 | * @draft ICU 57 | |
214 | */ | |
215 | U_DRAFT URelativeDateTimeFormatter* U_EXPORT2 | |
216 | ureldatefmt_open( const char* locale, | |
217 | UNumberFormat* nfToAdopt, | |
218 | UDateRelativeDateTimeFormatterStyle width, | |
219 | UDisplayContext capitalizationContext, | |
220 | UErrorCode* status ); | |
221 | ||
222 | /** | |
223 | * Close a URelativeDateTimeFormatter object. Once closed it may no longer be used. | |
224 | * @param reldatefmt | |
225 | * The URelativeDateTimeFormatter object to close. | |
226 | * @draft ICU 57 | |
227 | */ | |
228 | U_DRAFT void U_EXPORT2 | |
229 | ureldatefmt_close(URelativeDateTimeFormatter *reldatefmt); | |
230 | ||
231 | #if U_SHOW_CPLUSPLUS_API | |
232 | ||
233 | U_NAMESPACE_BEGIN | |
234 | ||
235 | /** | |
236 | * \class LocalURelativeDateTimeFormatterPointer | |
237 | * "Smart pointer" class, closes a URelativeDateTimeFormatter via ureldatefmt_close(). | |
238 | * For most methods see the LocalPointerBase base class. | |
239 | * | |
240 | * @see LocalPointerBase | |
241 | * @see LocalPointer | |
242 | * @draft ICU 57 | |
243 | */ | |
244 | U_DEFINE_LOCAL_OPEN_POINTER(LocalURelativeDateTimeFormatterPointer, URelativeDateTimeFormatter, ureldatefmt_close); | |
245 | ||
246 | U_NAMESPACE_END | |
247 | ||
248 | #endif | |
249 | ||
250 | /** | |
251 | * Format a combination of URelativeDateTimeUnit and numeric | |
252 | * offset using a numeric style, e.g. "1 week ago", "in 1 week", | |
253 | * "5 weeks ago", "in 5 weeks". | |
254 | * | |
255 | * @param reldatefmt | |
256 | * The URelativeDateTimeFormatter object specifying the | |
257 | * format conventions. | |
258 | * @param offset | |
259 | * The signed offset for the specified unit. This will | |
260 | * be formatted according to this object's UNumberFormat | |
261 | * object. | |
262 | * @param unit | |
263 | * The unit to use when formatting the relative | |
264 | * date, e.g. UDAT_REL_UNIT_WEEK, UDAT_REL_UNIT_FRIDAY. | |
265 | * @param result | |
266 | * A pointer to a buffer to receive the formatted result. | |
267 | * @param resultCapacity | |
268 | * The maximum size of result. | |
269 | * @param status | |
270 | * A pointer to a UErrorCode to receive any errors. In | |
271 | * case of error status, the contents of result are | |
272 | * undefined. | |
273 | * @return | |
274 | * The length of the formatted result; may be greater | |
275 | * than resultCapacity, in which case an error is returned. | |
276 | * @draft ICU 57 | |
277 | */ | |
278 | U_DRAFT int32_t U_EXPORT2 | |
279 | ureldatefmt_formatNumeric( const URelativeDateTimeFormatter* reldatefmt, | |
280 | double offset, | |
281 | URelativeDateTimeUnit unit, | |
282 | UChar* result, | |
283 | int32_t resultCapacity, | |
284 | UErrorCode* status); | |
285 | ||
286 | /** | |
287 | * Format a combination of URelativeDateTimeUnit and numeric offset | |
288 | * using a text style if possible, e.g. "last week", "this week", | |
289 | * "next week", "yesterday", "tomorrow". Falls back to numeric | |
290 | * style if no appropriate text term is available for the specified | |
291 | * offset in the object's locale. | |
292 | * | |
293 | * @param reldatefmt | |
294 | * The URelativeDateTimeFormatter object specifying the | |
295 | * format conventions. | |
296 | * @param offset | |
297 | * The signed offset for the specified unit. | |
298 | * @param unit | |
299 | * The unit to use when formatting the relative | |
300 | * date, e.g. UDAT_REL_UNIT_WEEK, UDAT_REL_UNIT_FRIDAY. | |
301 | * @param result | |
302 | * A pointer to a buffer to receive the formatted result. | |
303 | * @param resultCapacity | |
304 | * The maximum size of result. | |
305 | * @param status | |
306 | * A pointer to a UErrorCode to receive any errors. In | |
307 | * case of error status, the contents of result are | |
308 | * undefined. | |
309 | * @return | |
310 | * The length of the formatted result; may be greater | |
311 | * than resultCapacity, in which case an error is returned. | |
312 | * @draft ICU 57 | |
313 | */ | |
314 | U_DRAFT int32_t U_EXPORT2 | |
315 | ureldatefmt_format( const URelativeDateTimeFormatter* reldatefmt, | |
316 | double offset, | |
317 | URelativeDateTimeUnit unit, | |
318 | UChar* result, | |
319 | int32_t resultCapacity, | |
320 | UErrorCode* status); | |
321 | ||
322 | /** | |
323 | * Combines a relative date string and a time string in this object's | |
324 | * locale. This is done with the same date-time separator used for the | |
325 | * default calendar in this locale to produce a result such as | |
326 | * "yesterday at 3:45 PM". | |
327 | * | |
328 | * @param reldatefmt | |
329 | * The URelativeDateTimeFormatter object specifying the format conventions. | |
330 | * @param relativeDateString | |
331 | * The relative date string. | |
332 | * @param relativeDateStringLen | |
333 | * The length of relativeDateString; may be -1 if relativeDateString | |
334 | * is zero-terminated. | |
335 | * @param timeString | |
336 | * The time string. | |
337 | * @param timeStringLen | |
338 | * The length of timeString; may be -1 if timeString is zero-terminated. | |
339 | * @param result | |
340 | * A pointer to a buffer to receive the formatted result. | |
341 | * @param resultCapacity | |
342 | * The maximum size of result. | |
343 | * @param status | |
344 | * A pointer to a UErrorCode to receive any errors. In case of error status, | |
345 | * the contents of result are undefined. | |
346 | * @return | |
347 | * The length of the formatted result; may be greater than resultCapacity, | |
348 | * in which case an error is returned. | |
349 | * @draft ICU 57 | |
350 | */ | |
351 | U_DRAFT int32_t U_EXPORT2 | |
352 | ureldatefmt_combineDateAndTime( const URelativeDateTimeFormatter* reldatefmt, | |
353 | const UChar * relativeDateString, | |
354 | int32_t relativeDateStringLen, | |
355 | const UChar * timeString, | |
356 | int32_t timeStringLen, | |
357 | UChar* result, | |
358 | int32_t resultCapacity, | |
359 | UErrorCode* status ); | |
360 | ||
361 | #endif /* U_HIDE_DRAFT_API */ | |
362 | ||
363 | #endif /* !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION */ | |
364 | ||
365 | #endif |