michael@0: /* michael@0: ***************************************************************************************** michael@0: * Copyright (C) 2010-2011, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ***************************************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/udateintervalformat.h" michael@0: #include "unicode/dtitvfmt.h" michael@0: #include "unicode/dtintrv.h" michael@0: #include "unicode/localpointer.h" michael@0: #include "unicode/timezone.h" michael@0: #include "unicode/locid.h" michael@0: #include "unicode/unistr.h" michael@0: michael@0: U_NAMESPACE_USE michael@0: michael@0: michael@0: U_CAPI UDateIntervalFormat* U_EXPORT2 michael@0: udtitvfmt_open(const char* locale, michael@0: const UChar* skeleton, michael@0: int32_t skeletonLength, michael@0: const UChar* tzID, michael@0: int32_t tzIDLength, michael@0: UErrorCode* status) michael@0: { michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if ((skeleton == NULL ? skeletonLength != 0 : skeletonLength < -1) || michael@0: (tzID == NULL ? tzIDLength != 0 : tzIDLength < -1) michael@0: ) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: UnicodeString skel((UBool)(skeletonLength == -1), skeleton, skeletonLength); michael@0: LocalPointer formatter( michael@0: DateIntervalFormat::createInstance(skel, Locale(locale), *status)); michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if(tzID != 0) { michael@0: TimeZone *zone = TimeZone::createTimeZone(UnicodeString((UBool)(tzIDLength == -1), tzID, tzIDLength)); michael@0: if(zone == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: formatter->adoptTimeZone(zone); michael@0: } michael@0: return (UDateIntervalFormat*)formatter.orphan(); michael@0: } michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: udtitvfmt_close(UDateIntervalFormat *formatter) michael@0: { michael@0: delete (DateIntervalFormat*)formatter; michael@0: } michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: udtitvfmt_format(const UDateIntervalFormat* formatter, michael@0: UDate fromDate, michael@0: UDate toDate, michael@0: UChar* result, michael@0: int32_t resultCapacity, michael@0: UFieldPosition* position, michael@0: UErrorCode* status) michael@0: { michael@0: if (U_FAILURE(*status)) { michael@0: return -1; michael@0: } michael@0: if (result == NULL ? resultCapacity != 0 : resultCapacity < 0) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: UnicodeString res; michael@0: if (result != NULL) { michael@0: // NULL destination for pure preflighting: empty dummy string michael@0: // otherwise, alias the destination buffer (copied from udat_format) michael@0: res.setTo(result, 0, resultCapacity); michael@0: } michael@0: FieldPosition fp; michael@0: if (position != 0) { michael@0: fp.setField(position->field); michael@0: } michael@0: michael@0: DateInterval interval = DateInterval(fromDate,toDate); michael@0: ((const DateIntervalFormat*)formatter)->format( &interval, res, fp, *status ); michael@0: if (U_FAILURE(*status)) { michael@0: return -1; michael@0: } michael@0: if (position != 0) { michael@0: position->beginIndex = fp.getBeginIndex(); michael@0: position->endIndex = fp.getEndIndex(); michael@0: } michael@0: michael@0: return res.extract(result, resultCapacity, *status); michael@0: } michael@0: michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */