• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 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
udtitvfmt_open(const char * locale,const UChar * skeleton,int32_t skeletonLength,const UChar * tzID,int32_t tzIDLength,UErrorCode * status)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 {
33     if (U_FAILURE(*status)) {
34         return NULL;
35     }
36     if ((skeleton == NULL ? skeletonLength != 0 : skeletonLength < -1) ||
37         (tzID == NULL ? tzIDLength != 0 : tzIDLength < -1)
38     ) {
39         *status = U_ILLEGAL_ARGUMENT_ERROR;
40         return NULL;
41     }
42     UnicodeString skel((UBool)(skeletonLength == -1), skeleton, skeletonLength);
43     LocalPointer<DateIntervalFormat> formatter(
44             DateIntervalFormat::createInstance(skel, Locale(locale), *status));
45     if (U_FAILURE(*status)) {
46         return NULL;
47     }
48     if(tzID != 0) {
49         TimeZone *zone = TimeZone::createTimeZone(UnicodeString((UBool)(tzIDLength == -1), tzID, tzIDLength));
50         if(zone == NULL) {
51             *status = U_MEMORY_ALLOCATION_ERROR;
52             return NULL;
53         }
54         formatter->adoptTimeZone(zone);
55     }
56     return (UDateIntervalFormat*)formatter.orphan();
57 }
58 
59 
60 U_CAPI void U_EXPORT2
udtitvfmt_close(UDateIntervalFormat * formatter)61 udtitvfmt_close(UDateIntervalFormat *formatter)
62 {
63     delete (DateIntervalFormat*)formatter;
64 }
65 
66 
67 U_CAPI int32_t U_EXPORT2
udtitvfmt_format(const UDateIntervalFormat * formatter,UDate fromDate,UDate toDate,UChar * result,int32_t resultCapacity,UFieldPosition * position,UErrorCode * status)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 {
76     if (U_FAILURE(*status)) {
77         return -1;
78     }
79     if (result == NULL ? resultCapacity != 0 : resultCapacity < 0) {
80         *status = U_ILLEGAL_ARGUMENT_ERROR;
81         return 0;
82     }
83     UnicodeString res;
84     if (result != NULL) {
85         // NULL destination for pure preflighting: empty dummy string
86         // otherwise, alias the destination buffer (copied from udat_format)
87         res.setTo(result, 0, resultCapacity);
88     }
89     FieldPosition fp;
90     if (position != 0) {
91         fp.setField(position->field);
92     }
93 
94     DateInterval interval = DateInterval(fromDate,toDate);
95     ((const DateIntervalFormat*)formatter)->format( &interval, res, fp, *status );
96     if (U_FAILURE(*status)) {
97         return -1;
98     }
99     if (position != 0) {
100         position->beginIndex = fp.getBeginIndex();
101         position->endIndex = fp.getEndIndex();
102     }
103 
104     return res.extract(result, resultCapacity, *status);
105 }
106 
107 
108 #endif /* #if !UCONFIG_NO_FORMATTING */
109