1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/udateintervalformat.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,106 @@ 1.4 +/* 1.5 +***************************************************************************************** 1.6 +* Copyright (C) 2010-2011, International Business Machines 1.7 +* Corporation and others. All Rights Reserved. 1.8 +***************************************************************************************** 1.9 +*/ 1.10 + 1.11 +#include "unicode/utypes.h" 1.12 + 1.13 +#if !UCONFIG_NO_FORMATTING 1.14 + 1.15 +#include "unicode/udateintervalformat.h" 1.16 +#include "unicode/dtitvfmt.h" 1.17 +#include "unicode/dtintrv.h" 1.18 +#include "unicode/localpointer.h" 1.19 +#include "unicode/timezone.h" 1.20 +#include "unicode/locid.h" 1.21 +#include "unicode/unistr.h" 1.22 + 1.23 +U_NAMESPACE_USE 1.24 + 1.25 + 1.26 +U_CAPI UDateIntervalFormat* U_EXPORT2 1.27 +udtitvfmt_open(const char* locale, 1.28 + const UChar* skeleton, 1.29 + int32_t skeletonLength, 1.30 + const UChar* tzID, 1.31 + int32_t tzIDLength, 1.32 + UErrorCode* status) 1.33 +{ 1.34 + if (U_FAILURE(*status)) { 1.35 + return NULL; 1.36 + } 1.37 + if ((skeleton == NULL ? skeletonLength != 0 : skeletonLength < -1) || 1.38 + (tzID == NULL ? tzIDLength != 0 : tzIDLength < -1) 1.39 + ) { 1.40 + *status = U_ILLEGAL_ARGUMENT_ERROR; 1.41 + return NULL; 1.42 + } 1.43 + UnicodeString skel((UBool)(skeletonLength == -1), skeleton, skeletonLength); 1.44 + LocalPointer<DateIntervalFormat> formatter( 1.45 + DateIntervalFormat::createInstance(skel, Locale(locale), *status)); 1.46 + if (U_FAILURE(*status)) { 1.47 + return NULL; 1.48 + } 1.49 + if(tzID != 0) { 1.50 + TimeZone *zone = TimeZone::createTimeZone(UnicodeString((UBool)(tzIDLength == -1), tzID, tzIDLength)); 1.51 + if(zone == NULL) { 1.52 + *status = U_MEMORY_ALLOCATION_ERROR; 1.53 + return NULL; 1.54 + } 1.55 + formatter->adoptTimeZone(zone); 1.56 + } 1.57 + return (UDateIntervalFormat*)formatter.orphan(); 1.58 +} 1.59 + 1.60 + 1.61 +U_CAPI void U_EXPORT2 1.62 +udtitvfmt_close(UDateIntervalFormat *formatter) 1.63 +{ 1.64 + delete (DateIntervalFormat*)formatter; 1.65 +} 1.66 + 1.67 + 1.68 +U_CAPI int32_t U_EXPORT2 1.69 +udtitvfmt_format(const UDateIntervalFormat* formatter, 1.70 + UDate fromDate, 1.71 + UDate toDate, 1.72 + UChar* result, 1.73 + int32_t resultCapacity, 1.74 + UFieldPosition* position, 1.75 + UErrorCode* status) 1.76 +{ 1.77 + if (U_FAILURE(*status)) { 1.78 + return -1; 1.79 + } 1.80 + if (result == NULL ? resultCapacity != 0 : resultCapacity < 0) { 1.81 + *status = U_ILLEGAL_ARGUMENT_ERROR; 1.82 + return 0; 1.83 + } 1.84 + UnicodeString res; 1.85 + if (result != NULL) { 1.86 + // NULL destination for pure preflighting: empty dummy string 1.87 + // otherwise, alias the destination buffer (copied from udat_format) 1.88 + res.setTo(result, 0, resultCapacity); 1.89 + } 1.90 + FieldPosition fp; 1.91 + if (position != 0) { 1.92 + fp.setField(position->field); 1.93 + } 1.94 + 1.95 + DateInterval interval = DateInterval(fromDate,toDate); 1.96 + ((const DateIntervalFormat*)formatter)->format( &interval, res, fp, *status ); 1.97 + if (U_FAILURE(*status)) { 1.98 + return -1; 1.99 + } 1.100 + if (position != 0) { 1.101 + position->beginIndex = fp.getBeginIndex(); 1.102 + position->endIndex = fp.getEndIndex(); 1.103 + } 1.104 + 1.105 + return res.extract(result, resultCapacity, *status); 1.106 +} 1.107 + 1.108 + 1.109 +#endif /* #if !UCONFIG_NO_FORMATTING */