Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | ***************************************************************************************** |
michael@0 | 3 | * Copyright (C) 2010-2011, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ***************************************************************************************** |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "unicode/utypes.h" |
michael@0 | 9 | |
michael@0 | 10 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 11 | |
michael@0 | 12 | #include "unicode/udateintervalformat.h" |
michael@0 | 13 | #include "unicode/dtitvfmt.h" |
michael@0 | 14 | #include "unicode/dtintrv.h" |
michael@0 | 15 | #include "unicode/localpointer.h" |
michael@0 | 16 | #include "unicode/timezone.h" |
michael@0 | 17 | #include "unicode/locid.h" |
michael@0 | 18 | #include "unicode/unistr.h" |
michael@0 | 19 | |
michael@0 | 20 | U_NAMESPACE_USE |
michael@0 | 21 | |
michael@0 | 22 | |
michael@0 | 23 | U_CAPI UDateIntervalFormat* U_EXPORT2 |
michael@0 | 24 | udtitvfmt_open(const char* locale, |
michael@0 | 25 | const UChar* skeleton, |
michael@0 | 26 | int32_t skeletonLength, |
michael@0 | 27 | const UChar* tzID, |
michael@0 | 28 | int32_t tzIDLength, |
michael@0 | 29 | UErrorCode* status) |
michael@0 | 30 | { |
michael@0 | 31 | if (U_FAILURE(*status)) { |
michael@0 | 32 | return NULL; |
michael@0 | 33 | } |
michael@0 | 34 | if ((skeleton == NULL ? skeletonLength != 0 : skeletonLength < -1) || |
michael@0 | 35 | (tzID == NULL ? tzIDLength != 0 : tzIDLength < -1) |
michael@0 | 36 | ) { |
michael@0 | 37 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 38 | return NULL; |
michael@0 | 39 | } |
michael@0 | 40 | UnicodeString skel((UBool)(skeletonLength == -1), skeleton, skeletonLength); |
michael@0 | 41 | LocalPointer<DateIntervalFormat> formatter( |
michael@0 | 42 | DateIntervalFormat::createInstance(skel, Locale(locale), *status)); |
michael@0 | 43 | if (U_FAILURE(*status)) { |
michael@0 | 44 | return NULL; |
michael@0 | 45 | } |
michael@0 | 46 | if(tzID != 0) { |
michael@0 | 47 | TimeZone *zone = TimeZone::createTimeZone(UnicodeString((UBool)(tzIDLength == -1), tzID, tzIDLength)); |
michael@0 | 48 | if(zone == NULL) { |
michael@0 | 49 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 50 | return NULL; |
michael@0 | 51 | } |
michael@0 | 52 | formatter->adoptTimeZone(zone); |
michael@0 | 53 | } |
michael@0 | 54 | return (UDateIntervalFormat*)formatter.orphan(); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | |
michael@0 | 58 | U_CAPI void U_EXPORT2 |
michael@0 | 59 | udtitvfmt_close(UDateIntervalFormat *formatter) |
michael@0 | 60 | { |
michael@0 | 61 | delete (DateIntervalFormat*)formatter; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | |
michael@0 | 65 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 66 | udtitvfmt_format(const UDateIntervalFormat* formatter, |
michael@0 | 67 | UDate fromDate, |
michael@0 | 68 | UDate toDate, |
michael@0 | 69 | UChar* result, |
michael@0 | 70 | int32_t resultCapacity, |
michael@0 | 71 | UFieldPosition* position, |
michael@0 | 72 | UErrorCode* status) |
michael@0 | 73 | { |
michael@0 | 74 | if (U_FAILURE(*status)) { |
michael@0 | 75 | return -1; |
michael@0 | 76 | } |
michael@0 | 77 | if (result == NULL ? resultCapacity != 0 : resultCapacity < 0) { |
michael@0 | 78 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 79 | return 0; |
michael@0 | 80 | } |
michael@0 | 81 | UnicodeString res; |
michael@0 | 82 | if (result != NULL) { |
michael@0 | 83 | // NULL destination for pure preflighting: empty dummy string |
michael@0 | 84 | // otherwise, alias the destination buffer (copied from udat_format) |
michael@0 | 85 | res.setTo(result, 0, resultCapacity); |
michael@0 | 86 | } |
michael@0 | 87 | FieldPosition fp; |
michael@0 | 88 | if (position != 0) { |
michael@0 | 89 | fp.setField(position->field); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | DateInterval interval = DateInterval(fromDate,toDate); |
michael@0 | 93 | ((const DateIntervalFormat*)formatter)->format( &interval, res, fp, *status ); |
michael@0 | 94 | if (U_FAILURE(*status)) { |
michael@0 | 95 | return -1; |
michael@0 | 96 | } |
michael@0 | 97 | if (position != 0) { |
michael@0 | 98 | position->beginIndex = fp.getBeginIndex(); |
michael@0 | 99 | position->endIndex = fp.getEndIndex(); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | return res.extract(result, resultCapacity, *status); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | |
michael@0 | 106 | #endif /* #if !UCONFIG_NO_FORMATTING */ |