]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/udatintv.cpp
ICU-461.18.tar.gz
[apple/icu.git] / icuSources / i18n / udatintv.cpp
CommitLineData
729e4ab9
A
1/*
2*****************************************************************************************
3* Copyright (C) 2010 Apple Inc. All Rights Reserved.
4*****************************************************************************************
5*/
6
7#include "unicode/utypes.h"
8
9#if !UCONFIG_NO_FORMATTING
10
11#include "unicode/udatintv.h"
12#include "unicode/dtitvfmt.h"
13#include "unicode/dtintrv.h"
14#include "unicode/locid.h"
15#include "unicode/unistr.h"
16
17U_NAMESPACE_USE
18
19
20U_CAPI UDateIntervalFormat* U_EXPORT2
21udatintv_open(const char* locale,
22 const UChar* skeleton,
23 int32_t skeletonLength,
24 UErrorCode* status)
25{
26 if (U_FAILURE(*status)) {
27 return 0;
28 }
29 UnicodeString skel((UBool)(skeletonLength == -1), skeleton, skeletonLength);
30 return (UDateIntervalFormat*)DateIntervalFormat::createInstance(skel, Locale(locale), *status);
31}
32
33
34U_CAPI void U_EXPORT2
35udatintv_close(UDateIntervalFormat *datintv)
36{
37 delete (DateIntervalFormat*)datintv;
38}
39
40
41U_INTERNAL int32_t U_EXPORT2
42udatintv_format(const UDateIntervalFormat* datintv,
43 UDate fromDate,
44 UDate toDate,
45 UChar* result,
46 int32_t resultCapacity,
47 UFieldPosition* position,
48 UErrorCode* status)
49{
50 if (U_FAILURE(*status)) {
51 return -1;
52 }
53 UnicodeString res;
54 if (!(result==NULL && resultCapacity==0)) {
55 // NULL destination for pure preflighting: empty dummy string
56 // otherwise, alias the destination buffer (copied from udat_format)
57 res.setTo(result, 0, resultCapacity);
58 }
59 FieldPosition fp;
60 if (position != 0) {
61 fp.setField(position->field);
62 }
63
64 DateInterval interval = DateInterval(fromDate,toDate);
65 ((const DateIntervalFormat*)datintv)->format( &interval, res, fp, *status );
66 if (U_FAILURE(*status)) {
67 return -1;
68 }
69 if (position != 0) {
70 position->beginIndex = fp.getBeginIndex();
71 position->endIndex = fp.getEndIndex();
72 }
73
74 return res.extract(result, resultCapacity, *status);
75}
76
77
78#endif /* #if !UCONFIG_NO_FORMATTING */