11cb0ef41Sopenharmony_ci// © 2016 and later: Unicode, Inc. and others.
21cb0ef41Sopenharmony_ci// License & terms of use: http://www.unicode.org/copyright.html
31cb0ef41Sopenharmony_ci/*
41cb0ef41Sopenharmony_ci *******************************************************************************
51cb0ef41Sopenharmony_ci * Copyright (C) 1997-2015, International Business Machines Corporation and    *
61cb0ef41Sopenharmony_ci * others. All Rights Reserved.                                                *
71cb0ef41Sopenharmony_ci *******************************************************************************
81cb0ef41Sopenharmony_ci *
91cb0ef41Sopenharmony_ci * File DATEFMT.CPP
101cb0ef41Sopenharmony_ci *
111cb0ef41Sopenharmony_ci * Modification History:
121cb0ef41Sopenharmony_ci *
131cb0ef41Sopenharmony_ci *   Date        Name        Description
141cb0ef41Sopenharmony_ci *   02/19/97    aliu        Converted from java.
151cb0ef41Sopenharmony_ci *   03/31/97    aliu        Modified extensively to work with 50 locales.
161cb0ef41Sopenharmony_ci *   04/01/97    aliu        Added support for centuries.
171cb0ef41Sopenharmony_ci *   08/12/97    aliu        Fixed operator== to use Calendar::equivalentTo.
181cb0ef41Sopenharmony_ci *   07/20/98    stephen     Changed ParsePosition initialization
191cb0ef41Sopenharmony_ci ********************************************************************************
201cb0ef41Sopenharmony_ci */
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#include "unicode/utypes.h"
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci#if !UCONFIG_NO_FORMATTING
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci#include "unicode/ures.h"
271cb0ef41Sopenharmony_ci#include "unicode/datefmt.h"
281cb0ef41Sopenharmony_ci#include "unicode/smpdtfmt.h"
291cb0ef41Sopenharmony_ci#include "unicode/dtptngen.h"
301cb0ef41Sopenharmony_ci#include "unicode/udisplaycontext.h"
311cb0ef41Sopenharmony_ci#include "reldtfmt.h"
321cb0ef41Sopenharmony_ci#include "sharedobject.h"
331cb0ef41Sopenharmony_ci#include "unifiedcache.h"
341cb0ef41Sopenharmony_ci#include "uarrsort.h"
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci#include "cstring.h"
371cb0ef41Sopenharmony_ci#include "windtfmt.h"
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci#if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL)
401cb0ef41Sopenharmony_ci#include <stdio.h>
411cb0ef41Sopenharmony_ci#endif
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci// *****************************************************************************
441cb0ef41Sopenharmony_ci// class DateFormat
451cb0ef41Sopenharmony_ci// *****************************************************************************
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciU_NAMESPACE_BEGIN
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciclass DateFmtBestPattern : public SharedObject {
501cb0ef41Sopenharmony_cipublic:
511cb0ef41Sopenharmony_ci    UnicodeString fPattern;
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci    DateFmtBestPattern(const UnicodeString &pattern)
541cb0ef41Sopenharmony_ci            : fPattern(pattern) { }
551cb0ef41Sopenharmony_ci    ~DateFmtBestPattern();
561cb0ef41Sopenharmony_ci};
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciDateFmtBestPattern::~DateFmtBestPattern() {
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_citemplate<>
621cb0ef41Sopenharmony_ciconst DateFmtBestPattern *LocaleCacheKey<DateFmtBestPattern>::createObject(
631cb0ef41Sopenharmony_ci        const void * /*creationContext*/, UErrorCode &status) const {
641cb0ef41Sopenharmony_ci    status = U_UNSUPPORTED_ERROR;
651cb0ef41Sopenharmony_ci    return nullptr;
661cb0ef41Sopenharmony_ci}
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciclass DateFmtBestPatternKey : public LocaleCacheKey<DateFmtBestPattern> {
691cb0ef41Sopenharmony_ciprivate:
701cb0ef41Sopenharmony_ci    UnicodeString fSkeleton;
711cb0ef41Sopenharmony_ciprotected:
721cb0ef41Sopenharmony_ci    virtual bool equals(const CacheKeyBase &other) const override {
731cb0ef41Sopenharmony_ci       if (!LocaleCacheKey<DateFmtBestPattern>::equals(other)) {
741cb0ef41Sopenharmony_ci           return false;
751cb0ef41Sopenharmony_ci       }
761cb0ef41Sopenharmony_ci       // We know that this and other are of same class if we get this far.
771cb0ef41Sopenharmony_ci       return operator==(static_cast<const DateFmtBestPatternKey &>(other));
781cb0ef41Sopenharmony_ci    }
791cb0ef41Sopenharmony_cipublic:
801cb0ef41Sopenharmony_ci    DateFmtBestPatternKey(
811cb0ef41Sopenharmony_ci        const Locale &loc,
821cb0ef41Sopenharmony_ci        const UnicodeString &skeleton,
831cb0ef41Sopenharmony_ci        UErrorCode &status)
841cb0ef41Sopenharmony_ci            : LocaleCacheKey<DateFmtBestPattern>(loc),
851cb0ef41Sopenharmony_ci              fSkeleton(DateTimePatternGenerator::staticGetSkeleton(skeleton, status)) { }
861cb0ef41Sopenharmony_ci    DateFmtBestPatternKey(const DateFmtBestPatternKey &other) :
871cb0ef41Sopenharmony_ci            LocaleCacheKey<DateFmtBestPattern>(other),
881cb0ef41Sopenharmony_ci            fSkeleton(other.fSkeleton) { }
891cb0ef41Sopenharmony_ci    virtual ~DateFmtBestPatternKey();
901cb0ef41Sopenharmony_ci    virtual int32_t hashCode() const override {
911cb0ef41Sopenharmony_ci        return (int32_t)(37u * (uint32_t)LocaleCacheKey<DateFmtBestPattern>::hashCode() + (uint32_t)fSkeleton.hashCode());
921cb0ef41Sopenharmony_ci    }
931cb0ef41Sopenharmony_ci    inline bool operator==(const DateFmtBestPatternKey &other) const {
941cb0ef41Sopenharmony_ci        return fSkeleton == other.fSkeleton;
951cb0ef41Sopenharmony_ci    }
961cb0ef41Sopenharmony_ci    virtual CacheKeyBase *clone() const override {
971cb0ef41Sopenharmony_ci        return new DateFmtBestPatternKey(*this);
981cb0ef41Sopenharmony_ci    }
991cb0ef41Sopenharmony_ci    virtual const DateFmtBestPattern *createObject(
1001cb0ef41Sopenharmony_ci            const void * /*unused*/, UErrorCode &status) const override {
1011cb0ef41Sopenharmony_ci        LocalPointer<DateTimePatternGenerator> dtpg(
1021cb0ef41Sopenharmony_ci                    DateTimePatternGenerator::createInstance(fLoc, status));
1031cb0ef41Sopenharmony_ci        if (U_FAILURE(status)) {
1041cb0ef41Sopenharmony_ci            return nullptr;
1051cb0ef41Sopenharmony_ci        }
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci        LocalPointer<DateFmtBestPattern> pattern(
1081cb0ef41Sopenharmony_ci                new DateFmtBestPattern(
1091cb0ef41Sopenharmony_ci                        dtpg->getBestPattern(fSkeleton, status)),
1101cb0ef41Sopenharmony_ci                status);
1111cb0ef41Sopenharmony_ci        if (U_FAILURE(status)) {
1121cb0ef41Sopenharmony_ci            return nullptr;
1131cb0ef41Sopenharmony_ci        }
1141cb0ef41Sopenharmony_ci        DateFmtBestPattern *result = pattern.orphan();
1151cb0ef41Sopenharmony_ci        result->addRef();
1161cb0ef41Sopenharmony_ci        return result;
1171cb0ef41Sopenharmony_ci    }
1181cb0ef41Sopenharmony_ci};
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ciDateFmtBestPatternKey::~DateFmtBestPatternKey() { }
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ciDateFormat::DateFormat()
1241cb0ef41Sopenharmony_ci:   fCalendar(0),
1251cb0ef41Sopenharmony_ci    fNumberFormat(0),
1261cb0ef41Sopenharmony_ci    fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
1271cb0ef41Sopenharmony_ci{
1281cb0ef41Sopenharmony_ci}
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ciDateFormat::DateFormat(const DateFormat& other)
1331cb0ef41Sopenharmony_ci:   Format(other),
1341cb0ef41Sopenharmony_ci    fCalendar(0),
1351cb0ef41Sopenharmony_ci    fNumberFormat(0),
1361cb0ef41Sopenharmony_ci    fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
1371cb0ef41Sopenharmony_ci{
1381cb0ef41Sopenharmony_ci    *this = other;
1391cb0ef41Sopenharmony_ci}
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ciDateFormat& DateFormat::operator=(const DateFormat& other)
1441cb0ef41Sopenharmony_ci{
1451cb0ef41Sopenharmony_ci    if (this != &other)
1461cb0ef41Sopenharmony_ci    {
1471cb0ef41Sopenharmony_ci        delete fCalendar;
1481cb0ef41Sopenharmony_ci        delete fNumberFormat;
1491cb0ef41Sopenharmony_ci        if(other.fCalendar) {
1501cb0ef41Sopenharmony_ci          fCalendar = other.fCalendar->clone();
1511cb0ef41Sopenharmony_ci        } else {
1521cb0ef41Sopenharmony_ci          fCalendar = nullptr;
1531cb0ef41Sopenharmony_ci        }
1541cb0ef41Sopenharmony_ci        if(other.fNumberFormat) {
1551cb0ef41Sopenharmony_ci          fNumberFormat = other.fNumberFormat->clone();
1561cb0ef41Sopenharmony_ci        } else {
1571cb0ef41Sopenharmony_ci          fNumberFormat = nullptr;
1581cb0ef41Sopenharmony_ci        }
1591cb0ef41Sopenharmony_ci        fBoolFlags = other.fBoolFlags;
1601cb0ef41Sopenharmony_ci        fCapitalizationContext = other.fCapitalizationContext;
1611cb0ef41Sopenharmony_ci    }
1621cb0ef41Sopenharmony_ci    return *this;
1631cb0ef41Sopenharmony_ci}
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ciDateFormat::~DateFormat()
1681cb0ef41Sopenharmony_ci{
1691cb0ef41Sopenharmony_ci    delete fCalendar;
1701cb0ef41Sopenharmony_ci    delete fNumberFormat;
1711cb0ef41Sopenharmony_ci}
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_cibool
1761cb0ef41Sopenharmony_ciDateFormat::operator==(const Format& other) const
1771cb0ef41Sopenharmony_ci{
1781cb0ef41Sopenharmony_ci    if (this == &other) {
1791cb0ef41Sopenharmony_ci        return true;
1801cb0ef41Sopenharmony_ci    }
1811cb0ef41Sopenharmony_ci    if (!(Format::operator==(other))) {
1821cb0ef41Sopenharmony_ci        return false;
1831cb0ef41Sopenharmony_ci    }
1841cb0ef41Sopenharmony_ci    // Format::operator== guarantees that this cast is safe
1851cb0ef41Sopenharmony_ci    DateFormat* fmt = (DateFormat*)&other;
1861cb0ef41Sopenharmony_ci    return fCalendar&&(fCalendar->isEquivalentTo(*fmt->fCalendar)) &&
1871cb0ef41Sopenharmony_ci         (fNumberFormat && *fNumberFormat == *fmt->fNumberFormat) &&
1881cb0ef41Sopenharmony_ci         (fCapitalizationContext == fmt->fCapitalizationContext);
1891cb0ef41Sopenharmony_ci}
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ciUnicodeString&
1941cb0ef41Sopenharmony_ciDateFormat::format(const Formattable& obj,
1951cb0ef41Sopenharmony_ci                   UnicodeString& appendTo,
1961cb0ef41Sopenharmony_ci                   FieldPosition& fieldPosition,
1971cb0ef41Sopenharmony_ci                   UErrorCode& status) const
1981cb0ef41Sopenharmony_ci{
1991cb0ef41Sopenharmony_ci    if (U_FAILURE(status)) return appendTo;
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci    // if the type of the Formattable is double or long, treat it as if it were a Date
2021cb0ef41Sopenharmony_ci    UDate date = 0;
2031cb0ef41Sopenharmony_ci    switch (obj.getType())
2041cb0ef41Sopenharmony_ci    {
2051cb0ef41Sopenharmony_ci    case Formattable::kDate:
2061cb0ef41Sopenharmony_ci        date = obj.getDate();
2071cb0ef41Sopenharmony_ci        break;
2081cb0ef41Sopenharmony_ci    case Formattable::kDouble:
2091cb0ef41Sopenharmony_ci        date = (UDate)obj.getDouble();
2101cb0ef41Sopenharmony_ci        break;
2111cb0ef41Sopenharmony_ci    case Formattable::kLong:
2121cb0ef41Sopenharmony_ci        date = (UDate)obj.getLong();
2131cb0ef41Sopenharmony_ci        break;
2141cb0ef41Sopenharmony_ci    default:
2151cb0ef41Sopenharmony_ci        status = U_ILLEGAL_ARGUMENT_ERROR;
2161cb0ef41Sopenharmony_ci        return appendTo;
2171cb0ef41Sopenharmony_ci    }
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ci    // Is this right?
2201cb0ef41Sopenharmony_ci    //if (fieldPosition.getBeginIndex() == fieldPosition.getEndIndex())
2211cb0ef41Sopenharmony_ci    //  status = U_ILLEGAL_ARGUMENT_ERROR;
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci    return format(date, appendTo, fieldPosition);
2241cb0ef41Sopenharmony_ci}
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
2271cb0ef41Sopenharmony_ci
2281cb0ef41Sopenharmony_ciUnicodeString&
2291cb0ef41Sopenharmony_ciDateFormat::format(const Formattable& obj,
2301cb0ef41Sopenharmony_ci                   UnicodeString& appendTo,
2311cb0ef41Sopenharmony_ci                   FieldPositionIterator* posIter,
2321cb0ef41Sopenharmony_ci                   UErrorCode& status) const
2331cb0ef41Sopenharmony_ci{
2341cb0ef41Sopenharmony_ci    if (U_FAILURE(status)) return appendTo;
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ci    // if the type of the Formattable is double or long, treat it as if it were a Date
2371cb0ef41Sopenharmony_ci    UDate date = 0;
2381cb0ef41Sopenharmony_ci    switch (obj.getType())
2391cb0ef41Sopenharmony_ci    {
2401cb0ef41Sopenharmony_ci    case Formattable::kDate:
2411cb0ef41Sopenharmony_ci        date = obj.getDate();
2421cb0ef41Sopenharmony_ci        break;
2431cb0ef41Sopenharmony_ci    case Formattable::kDouble:
2441cb0ef41Sopenharmony_ci        date = (UDate)obj.getDouble();
2451cb0ef41Sopenharmony_ci        break;
2461cb0ef41Sopenharmony_ci    case Formattable::kLong:
2471cb0ef41Sopenharmony_ci        date = (UDate)obj.getLong();
2481cb0ef41Sopenharmony_ci        break;
2491cb0ef41Sopenharmony_ci    default:
2501cb0ef41Sopenharmony_ci        status = U_ILLEGAL_ARGUMENT_ERROR;
2511cb0ef41Sopenharmony_ci        return appendTo;
2521cb0ef41Sopenharmony_ci    }
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ci    // Is this right?
2551cb0ef41Sopenharmony_ci    //if (fieldPosition.getBeginIndex() == fieldPosition.getEndIndex())
2561cb0ef41Sopenharmony_ci    //  status = U_ILLEGAL_ARGUMENT_ERROR;
2571cb0ef41Sopenharmony_ci
2581cb0ef41Sopenharmony_ci    return format(date, appendTo, posIter, status);
2591cb0ef41Sopenharmony_ci}
2601cb0ef41Sopenharmony_ci
2611cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_ci// Default implementation for backwards compatibility, subclasses should implement.
2641cb0ef41Sopenharmony_ciUnicodeString&
2651cb0ef41Sopenharmony_ciDateFormat::format(Calendar& /* unused cal */,
2661cb0ef41Sopenharmony_ci                   UnicodeString& appendTo,
2671cb0ef41Sopenharmony_ci                   FieldPositionIterator* /* unused posIter */,
2681cb0ef41Sopenharmony_ci                   UErrorCode& status) const {
2691cb0ef41Sopenharmony_ci    if (U_SUCCESS(status)) {
2701cb0ef41Sopenharmony_ci        status = U_UNSUPPORTED_ERROR;
2711cb0ef41Sopenharmony_ci    }
2721cb0ef41Sopenharmony_ci    return appendTo;
2731cb0ef41Sopenharmony_ci}
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_ciUnicodeString&
2781cb0ef41Sopenharmony_ciDateFormat::format(UDate date, UnicodeString& appendTo, FieldPosition& fieldPosition) const {
2791cb0ef41Sopenharmony_ci    if (fCalendar != nullptr) {
2801cb0ef41Sopenharmony_ci        // Use a clone of our calendar instance
2811cb0ef41Sopenharmony_ci        Calendar* calClone = fCalendar->clone();
2821cb0ef41Sopenharmony_ci        if (calClone != nullptr) {
2831cb0ef41Sopenharmony_ci            UErrorCode ec = U_ZERO_ERROR;
2841cb0ef41Sopenharmony_ci            calClone->setTime(date, ec);
2851cb0ef41Sopenharmony_ci            if (U_SUCCESS(ec)) {
2861cb0ef41Sopenharmony_ci                format(*calClone, appendTo, fieldPosition);
2871cb0ef41Sopenharmony_ci            }
2881cb0ef41Sopenharmony_ci            delete calClone;
2891cb0ef41Sopenharmony_ci        }
2901cb0ef41Sopenharmony_ci    }
2911cb0ef41Sopenharmony_ci    return appendTo;
2921cb0ef41Sopenharmony_ci}
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
2951cb0ef41Sopenharmony_ci
2961cb0ef41Sopenharmony_ciUnicodeString&
2971cb0ef41Sopenharmony_ciDateFormat::format(UDate date, UnicodeString& appendTo, FieldPositionIterator* posIter,
2981cb0ef41Sopenharmony_ci                   UErrorCode& status) const {
2991cb0ef41Sopenharmony_ci    if (fCalendar != nullptr) {
3001cb0ef41Sopenharmony_ci        Calendar* calClone = fCalendar->clone();
3011cb0ef41Sopenharmony_ci        if (calClone != nullptr) {
3021cb0ef41Sopenharmony_ci            calClone->setTime(date, status);
3031cb0ef41Sopenharmony_ci            if (U_SUCCESS(status)) {
3041cb0ef41Sopenharmony_ci               format(*calClone, appendTo, posIter, status);
3051cb0ef41Sopenharmony_ci            }
3061cb0ef41Sopenharmony_ci            delete calClone;
3071cb0ef41Sopenharmony_ci        }
3081cb0ef41Sopenharmony_ci    }
3091cb0ef41Sopenharmony_ci    return appendTo;
3101cb0ef41Sopenharmony_ci}
3111cb0ef41Sopenharmony_ci
3121cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
3131cb0ef41Sopenharmony_ci
3141cb0ef41Sopenharmony_ciUnicodeString&
3151cb0ef41Sopenharmony_ciDateFormat::format(UDate date, UnicodeString& appendTo) const
3161cb0ef41Sopenharmony_ci{
3171cb0ef41Sopenharmony_ci    // Note that any error information is just lost.  That's okay
3181cb0ef41Sopenharmony_ci    // for this convenience method.
3191cb0ef41Sopenharmony_ci    FieldPosition fpos(FieldPosition::DONT_CARE);
3201cb0ef41Sopenharmony_ci    return format(date, appendTo, fpos);
3211cb0ef41Sopenharmony_ci}
3221cb0ef41Sopenharmony_ci
3231cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ciUDate
3261cb0ef41Sopenharmony_ciDateFormat::parse(const UnicodeString& text,
3271cb0ef41Sopenharmony_ci                  ParsePosition& pos) const
3281cb0ef41Sopenharmony_ci{
3291cb0ef41Sopenharmony_ci    UDate d = 0; // Error return UDate is 0 (the epoch)
3301cb0ef41Sopenharmony_ci    if (fCalendar != nullptr) {
3311cb0ef41Sopenharmony_ci        Calendar* calClone = fCalendar->clone();
3321cb0ef41Sopenharmony_ci        if (calClone != nullptr) {
3331cb0ef41Sopenharmony_ci            int32_t start = pos.getIndex();
3341cb0ef41Sopenharmony_ci            calClone->clear();
3351cb0ef41Sopenharmony_ci            parse(text, *calClone, pos);
3361cb0ef41Sopenharmony_ci            if (pos.getIndex() != start) {
3371cb0ef41Sopenharmony_ci                UErrorCode ec = U_ZERO_ERROR;
3381cb0ef41Sopenharmony_ci                d = calClone->getTime(ec);
3391cb0ef41Sopenharmony_ci                if (U_FAILURE(ec)) {
3401cb0ef41Sopenharmony_ci                    // We arrive here if fCalendar => calClone is non-lenient and
3411cb0ef41Sopenharmony_ci                    // there is an out-of-range field.  We don't know which field
3421cb0ef41Sopenharmony_ci                    // was illegal so we set the error index to the start.
3431cb0ef41Sopenharmony_ci                    pos.setIndex(start);
3441cb0ef41Sopenharmony_ci                    pos.setErrorIndex(start);
3451cb0ef41Sopenharmony_ci                    d = 0;
3461cb0ef41Sopenharmony_ci                }
3471cb0ef41Sopenharmony_ci            }
3481cb0ef41Sopenharmony_ci            delete calClone;
3491cb0ef41Sopenharmony_ci        }
3501cb0ef41Sopenharmony_ci    }
3511cb0ef41Sopenharmony_ci    return d;
3521cb0ef41Sopenharmony_ci}
3531cb0ef41Sopenharmony_ci
3541cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
3551cb0ef41Sopenharmony_ci
3561cb0ef41Sopenharmony_ciUDate
3571cb0ef41Sopenharmony_ciDateFormat::parse(const UnicodeString& text,
3581cb0ef41Sopenharmony_ci                  UErrorCode& status) const
3591cb0ef41Sopenharmony_ci{
3601cb0ef41Sopenharmony_ci    if (U_FAILURE(status)) return 0;
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ci    ParsePosition pos(0);
3631cb0ef41Sopenharmony_ci    UDate result = parse(text, pos);
3641cb0ef41Sopenharmony_ci    if (pos.getIndex() == 0) {
3651cb0ef41Sopenharmony_ci#if defined (U_DEBUG_CAL)
3661cb0ef41Sopenharmony_ci      fprintf(stderr, "%s:%d - - failed to parse  - err index %d\n"
3671cb0ef41Sopenharmony_ci              , __FILE__, __LINE__, pos.getErrorIndex() );
3681cb0ef41Sopenharmony_ci#endif
3691cb0ef41Sopenharmony_ci      status = U_ILLEGAL_ARGUMENT_ERROR;
3701cb0ef41Sopenharmony_ci    }
3711cb0ef41Sopenharmony_ci    return result;
3721cb0ef41Sopenharmony_ci}
3731cb0ef41Sopenharmony_ci
3741cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
3751cb0ef41Sopenharmony_ci
3761cb0ef41Sopenharmony_civoid
3771cb0ef41Sopenharmony_ciDateFormat::parseObject(const UnicodeString& source,
3781cb0ef41Sopenharmony_ci                        Formattable& result,
3791cb0ef41Sopenharmony_ci                        ParsePosition& pos) const
3801cb0ef41Sopenharmony_ci{
3811cb0ef41Sopenharmony_ci    result.setDate(parse(source, pos));
3821cb0ef41Sopenharmony_ci}
3831cb0ef41Sopenharmony_ci
3841cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
3851cb0ef41Sopenharmony_ci
3861cb0ef41Sopenharmony_ciDateFormat* U_EXPORT2
3871cb0ef41Sopenharmony_ciDateFormat::createTimeInstance(DateFormat::EStyle style,
3881cb0ef41Sopenharmony_ci                               const Locale& aLocale)
3891cb0ef41Sopenharmony_ci{
3901cb0ef41Sopenharmony_ci    return createDateTimeInstance(kNone, style, aLocale);
3911cb0ef41Sopenharmony_ci}
3921cb0ef41Sopenharmony_ci
3931cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
3941cb0ef41Sopenharmony_ci
3951cb0ef41Sopenharmony_ciDateFormat* U_EXPORT2
3961cb0ef41Sopenharmony_ciDateFormat::createDateInstance(DateFormat::EStyle style,
3971cb0ef41Sopenharmony_ci                               const Locale& aLocale)
3981cb0ef41Sopenharmony_ci{
3991cb0ef41Sopenharmony_ci    return createDateTimeInstance(style, kNone, aLocale);
4001cb0ef41Sopenharmony_ci}
4011cb0ef41Sopenharmony_ci
4021cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
4031cb0ef41Sopenharmony_ci
4041cb0ef41Sopenharmony_ciDateFormat* U_EXPORT2
4051cb0ef41Sopenharmony_ciDateFormat::createDateTimeInstance(EStyle dateStyle,
4061cb0ef41Sopenharmony_ci                                   EStyle timeStyle,
4071cb0ef41Sopenharmony_ci                                   const Locale& aLocale)
4081cb0ef41Sopenharmony_ci{
4091cb0ef41Sopenharmony_ci   if(dateStyle != kNone)
4101cb0ef41Sopenharmony_ci   {
4111cb0ef41Sopenharmony_ci       dateStyle = (EStyle) (dateStyle + kDateOffset);
4121cb0ef41Sopenharmony_ci   }
4131cb0ef41Sopenharmony_ci   return create(timeStyle, dateStyle, aLocale);
4141cb0ef41Sopenharmony_ci}
4151cb0ef41Sopenharmony_ci
4161cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
4171cb0ef41Sopenharmony_ci
4181cb0ef41Sopenharmony_ciDateFormat* U_EXPORT2
4191cb0ef41Sopenharmony_ciDateFormat::createInstance()
4201cb0ef41Sopenharmony_ci{
4211cb0ef41Sopenharmony_ci    return createDateTimeInstance(kShort, kShort, Locale::getDefault());
4221cb0ef41Sopenharmony_ci}
4231cb0ef41Sopenharmony_ci
4241cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
4251cb0ef41Sopenharmony_ci
4261cb0ef41Sopenharmony_ciUnicodeString U_EXPORT2
4271cb0ef41Sopenharmony_ciDateFormat::getBestPattern(
4281cb0ef41Sopenharmony_ci        const Locale &locale,
4291cb0ef41Sopenharmony_ci        const UnicodeString &skeleton,
4301cb0ef41Sopenharmony_ci        UErrorCode &status) {
4311cb0ef41Sopenharmony_ci    UnifiedCache *cache = UnifiedCache::getInstance(status);
4321cb0ef41Sopenharmony_ci    if (U_FAILURE(status)) {
4331cb0ef41Sopenharmony_ci        return UnicodeString();
4341cb0ef41Sopenharmony_ci    }
4351cb0ef41Sopenharmony_ci    DateFmtBestPatternKey key(locale, skeleton, status);
4361cb0ef41Sopenharmony_ci    const DateFmtBestPattern *patternPtr = nullptr;
4371cb0ef41Sopenharmony_ci    cache->get(key, patternPtr, status);
4381cb0ef41Sopenharmony_ci    if (U_FAILURE(status)) {
4391cb0ef41Sopenharmony_ci        return UnicodeString();
4401cb0ef41Sopenharmony_ci    }
4411cb0ef41Sopenharmony_ci    UnicodeString result(patternPtr->fPattern);
4421cb0ef41Sopenharmony_ci    patternPtr->removeRef();
4431cb0ef41Sopenharmony_ci    return result;
4441cb0ef41Sopenharmony_ci}
4451cb0ef41Sopenharmony_ci
4461cb0ef41Sopenharmony_ciDateFormat* U_EXPORT2
4471cb0ef41Sopenharmony_ciDateFormat::createInstanceForSkeleton(
4481cb0ef41Sopenharmony_ci        Calendar *calendarToAdopt,
4491cb0ef41Sopenharmony_ci        const UnicodeString& skeleton,
4501cb0ef41Sopenharmony_ci        const Locale &locale,
4511cb0ef41Sopenharmony_ci        UErrorCode &status) {
4521cb0ef41Sopenharmony_ci    LocalPointer<Calendar> calendar(calendarToAdopt);
4531cb0ef41Sopenharmony_ci    if (U_FAILURE(status)) {
4541cb0ef41Sopenharmony_ci        return nullptr;
4551cb0ef41Sopenharmony_ci    }
4561cb0ef41Sopenharmony_ci    if (calendar.isNull()) {
4571cb0ef41Sopenharmony_ci        status = U_ILLEGAL_ARGUMENT_ERROR;
4581cb0ef41Sopenharmony_ci        return nullptr;
4591cb0ef41Sopenharmony_ci    }
4601cb0ef41Sopenharmony_ci    Locale localeWithCalendar = locale;
4611cb0ef41Sopenharmony_ci    localeWithCalendar.setKeywordValue("calendar", calendar->getType(), status);
4621cb0ef41Sopenharmony_ci    if (U_FAILURE(status)) {
4631cb0ef41Sopenharmony_ci        return nullptr;
4641cb0ef41Sopenharmony_ci    }
4651cb0ef41Sopenharmony_ci    DateFormat *result = createInstanceForSkeleton(skeleton, localeWithCalendar, status);
4661cb0ef41Sopenharmony_ci    if (U_FAILURE(status)) {
4671cb0ef41Sopenharmony_ci        return nullptr;
4681cb0ef41Sopenharmony_ci    }
4691cb0ef41Sopenharmony_ci    result->adoptCalendar(calendar.orphan());
4701cb0ef41Sopenharmony_ci    return result;
4711cb0ef41Sopenharmony_ci}
4721cb0ef41Sopenharmony_ci
4731cb0ef41Sopenharmony_ciDateFormat* U_EXPORT2
4741cb0ef41Sopenharmony_ciDateFormat::createInstanceForSkeleton(
4751cb0ef41Sopenharmony_ci        const UnicodeString& skeleton,
4761cb0ef41Sopenharmony_ci        const Locale &locale,
4771cb0ef41Sopenharmony_ci        UErrorCode &status) {
4781cb0ef41Sopenharmony_ci    if (U_FAILURE(status)) {
4791cb0ef41Sopenharmony_ci        return nullptr;
4801cb0ef41Sopenharmony_ci    }
4811cb0ef41Sopenharmony_ci    LocalPointer<DateFormat> df(
4821cb0ef41Sopenharmony_ci        new SimpleDateFormat(
4831cb0ef41Sopenharmony_ci            getBestPattern(locale, skeleton, status),
4841cb0ef41Sopenharmony_ci            locale, status),
4851cb0ef41Sopenharmony_ci        status);
4861cb0ef41Sopenharmony_ci    return U_SUCCESS(status) ? df.orphan() : nullptr;
4871cb0ef41Sopenharmony_ci}
4881cb0ef41Sopenharmony_ci
4891cb0ef41Sopenharmony_ciDateFormat* U_EXPORT2
4901cb0ef41Sopenharmony_ciDateFormat::createInstanceForSkeleton(
4911cb0ef41Sopenharmony_ci        const UnicodeString& skeleton,
4921cb0ef41Sopenharmony_ci        UErrorCode &status) {
4931cb0ef41Sopenharmony_ci    return createInstanceForSkeleton(
4941cb0ef41Sopenharmony_ci            skeleton, Locale::getDefault(), status);
4951cb0ef41Sopenharmony_ci}
4961cb0ef41Sopenharmony_ci
4971cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
4981cb0ef41Sopenharmony_ci
4991cb0ef41Sopenharmony_ciDateFormat* U_EXPORT2
5001cb0ef41Sopenharmony_ciDateFormat::create(EStyle timeStyle, EStyle dateStyle, const Locale& locale)
5011cb0ef41Sopenharmony_ci{
5021cb0ef41Sopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
5031cb0ef41Sopenharmony_ci#if U_PLATFORM_USES_ONLY_WIN32_API
5041cb0ef41Sopenharmony_ci    char buffer[8];
5051cb0ef41Sopenharmony_ci    int32_t count = locale.getKeywordValue("compat", buffer, sizeof(buffer), status);
5061cb0ef41Sopenharmony_ci
5071cb0ef41Sopenharmony_ci    // if the locale has "@compat=host", create a host-specific DateFormat...
5081cb0ef41Sopenharmony_ci    if (count > 0 && uprv_strcmp(buffer, "host") == 0) {
5091cb0ef41Sopenharmony_ci        Win32DateFormat *f = new Win32DateFormat(timeStyle, dateStyle, locale, status);
5101cb0ef41Sopenharmony_ci
5111cb0ef41Sopenharmony_ci        if (U_SUCCESS(status)) {
5121cb0ef41Sopenharmony_ci            return f;
5131cb0ef41Sopenharmony_ci        }
5141cb0ef41Sopenharmony_ci
5151cb0ef41Sopenharmony_ci        delete f;
5161cb0ef41Sopenharmony_ci    }
5171cb0ef41Sopenharmony_ci#endif
5181cb0ef41Sopenharmony_ci
5191cb0ef41Sopenharmony_ci    // is it relative?
5201cb0ef41Sopenharmony_ci    if(/*((timeStyle!=UDAT_NONE)&&(timeStyle & UDAT_RELATIVE)) || */((dateStyle!=kNone)&&((dateStyle-kDateOffset) & UDAT_RELATIVE))) {
5211cb0ef41Sopenharmony_ci        RelativeDateFormat *r = new RelativeDateFormat((UDateFormatStyle)timeStyle, (UDateFormatStyle)(dateStyle-kDateOffset), locale, status);
5221cb0ef41Sopenharmony_ci        if(U_SUCCESS(status)) return r;
5231cb0ef41Sopenharmony_ci        delete r;
5241cb0ef41Sopenharmony_ci        status = U_ZERO_ERROR;
5251cb0ef41Sopenharmony_ci    }
5261cb0ef41Sopenharmony_ci
5271cb0ef41Sopenharmony_ci    // Try to create a SimpleDateFormat of the desired style.
5281cb0ef41Sopenharmony_ci    SimpleDateFormat *f = new SimpleDateFormat(timeStyle, dateStyle, locale, status);
5291cb0ef41Sopenharmony_ci    if (U_SUCCESS(status)) return f;
5301cb0ef41Sopenharmony_ci    delete f;
5311cb0ef41Sopenharmony_ci
5321cb0ef41Sopenharmony_ci    // If that fails, try to create a format using the default pattern and
5331cb0ef41Sopenharmony_ci    // the DateFormatSymbols for this locale.
5341cb0ef41Sopenharmony_ci    status = U_ZERO_ERROR;
5351cb0ef41Sopenharmony_ci    f = new SimpleDateFormat(locale, status);
5361cb0ef41Sopenharmony_ci    if (U_SUCCESS(status)) return f;
5371cb0ef41Sopenharmony_ci    delete f;
5381cb0ef41Sopenharmony_ci
5391cb0ef41Sopenharmony_ci    // This should never really happen, because the preceding constructor
5401cb0ef41Sopenharmony_ci    // should always succeed.  If the resource data is unavailable, a last
5411cb0ef41Sopenharmony_ci    // resort object should be returned.
5421cb0ef41Sopenharmony_ci    return 0;
5431cb0ef41Sopenharmony_ci}
5441cb0ef41Sopenharmony_ci
5451cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
5461cb0ef41Sopenharmony_ci
5471cb0ef41Sopenharmony_ciconst Locale* U_EXPORT2
5481cb0ef41Sopenharmony_ciDateFormat::getAvailableLocales(int32_t& count)
5491cb0ef41Sopenharmony_ci{
5501cb0ef41Sopenharmony_ci    // Get the list of installed locales.
5511cb0ef41Sopenharmony_ci    // Even if root has the correct date format for this locale,
5521cb0ef41Sopenharmony_ci    // it's still a valid locale (we don't worry about data fallbacks).
5531cb0ef41Sopenharmony_ci    return Locale::getAvailableLocales(count);
5541cb0ef41Sopenharmony_ci}
5551cb0ef41Sopenharmony_ci
5561cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
5571cb0ef41Sopenharmony_ci
5581cb0ef41Sopenharmony_civoid
5591cb0ef41Sopenharmony_ciDateFormat::adoptCalendar(Calendar* newCalendar)
5601cb0ef41Sopenharmony_ci{
5611cb0ef41Sopenharmony_ci    delete fCalendar;
5621cb0ef41Sopenharmony_ci    fCalendar = newCalendar;
5631cb0ef41Sopenharmony_ci}
5641cb0ef41Sopenharmony_ci
5651cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
5661cb0ef41Sopenharmony_civoid
5671cb0ef41Sopenharmony_ciDateFormat::setCalendar(const Calendar& newCalendar)
5681cb0ef41Sopenharmony_ci{
5691cb0ef41Sopenharmony_ci    Calendar* newCalClone = newCalendar.clone();
5701cb0ef41Sopenharmony_ci    if (newCalClone != nullptr) {
5711cb0ef41Sopenharmony_ci        adoptCalendar(newCalClone);
5721cb0ef41Sopenharmony_ci    }
5731cb0ef41Sopenharmony_ci}
5741cb0ef41Sopenharmony_ci
5751cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
5761cb0ef41Sopenharmony_ci
5771cb0ef41Sopenharmony_ciconst Calendar*
5781cb0ef41Sopenharmony_ciDateFormat::getCalendar() const
5791cb0ef41Sopenharmony_ci{
5801cb0ef41Sopenharmony_ci    return fCalendar;
5811cb0ef41Sopenharmony_ci}
5821cb0ef41Sopenharmony_ci
5831cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
5841cb0ef41Sopenharmony_ci
5851cb0ef41Sopenharmony_civoid
5861cb0ef41Sopenharmony_ciDateFormat::adoptNumberFormat(NumberFormat* newNumberFormat)
5871cb0ef41Sopenharmony_ci{
5881cb0ef41Sopenharmony_ci    delete fNumberFormat;
5891cb0ef41Sopenharmony_ci    fNumberFormat = newNumberFormat;
5901cb0ef41Sopenharmony_ci    newNumberFormat->setParseIntegerOnly(true);
5911cb0ef41Sopenharmony_ci    newNumberFormat->setGroupingUsed(false);
5921cb0ef41Sopenharmony_ci}
5931cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
5941cb0ef41Sopenharmony_ci
5951cb0ef41Sopenharmony_civoid
5961cb0ef41Sopenharmony_ciDateFormat::setNumberFormat(const NumberFormat& newNumberFormat)
5971cb0ef41Sopenharmony_ci{
5981cb0ef41Sopenharmony_ci    NumberFormat* newNumFmtClone = newNumberFormat.clone();
5991cb0ef41Sopenharmony_ci    if (newNumFmtClone != nullptr) {
6001cb0ef41Sopenharmony_ci        adoptNumberFormat(newNumFmtClone);
6011cb0ef41Sopenharmony_ci    }
6021cb0ef41Sopenharmony_ci}
6031cb0ef41Sopenharmony_ci
6041cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
6051cb0ef41Sopenharmony_ci
6061cb0ef41Sopenharmony_ciconst NumberFormat*
6071cb0ef41Sopenharmony_ciDateFormat::getNumberFormat() const
6081cb0ef41Sopenharmony_ci{
6091cb0ef41Sopenharmony_ci    return fNumberFormat;
6101cb0ef41Sopenharmony_ci}
6111cb0ef41Sopenharmony_ci
6121cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
6131cb0ef41Sopenharmony_ci
6141cb0ef41Sopenharmony_civoid
6151cb0ef41Sopenharmony_ciDateFormat::adoptTimeZone(TimeZone* zone)
6161cb0ef41Sopenharmony_ci{
6171cb0ef41Sopenharmony_ci    if (fCalendar != nullptr) {
6181cb0ef41Sopenharmony_ci        fCalendar->adoptTimeZone(zone);
6191cb0ef41Sopenharmony_ci    }
6201cb0ef41Sopenharmony_ci}
6211cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
6221cb0ef41Sopenharmony_ci
6231cb0ef41Sopenharmony_civoid
6241cb0ef41Sopenharmony_ciDateFormat::setTimeZone(const TimeZone& zone)
6251cb0ef41Sopenharmony_ci{
6261cb0ef41Sopenharmony_ci    if (fCalendar != nullptr) {
6271cb0ef41Sopenharmony_ci        fCalendar->setTimeZone(zone);
6281cb0ef41Sopenharmony_ci    }
6291cb0ef41Sopenharmony_ci}
6301cb0ef41Sopenharmony_ci
6311cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
6321cb0ef41Sopenharmony_ci
6331cb0ef41Sopenharmony_ciconst TimeZone&
6341cb0ef41Sopenharmony_ciDateFormat::getTimeZone() const
6351cb0ef41Sopenharmony_ci{
6361cb0ef41Sopenharmony_ci    if (fCalendar != nullptr) {
6371cb0ef41Sopenharmony_ci        return fCalendar->getTimeZone();
6381cb0ef41Sopenharmony_ci    }
6391cb0ef41Sopenharmony_ci    // If calendar doesn't exists, create default timezone.
6401cb0ef41Sopenharmony_ci    // fCalendar is rarely null
6411cb0ef41Sopenharmony_ci    return *(TimeZone::createDefault());
6421cb0ef41Sopenharmony_ci}
6431cb0ef41Sopenharmony_ci
6441cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
6451cb0ef41Sopenharmony_ci
6461cb0ef41Sopenharmony_civoid
6471cb0ef41Sopenharmony_ciDateFormat::setLenient(UBool lenient)
6481cb0ef41Sopenharmony_ci{
6491cb0ef41Sopenharmony_ci    if (fCalendar != nullptr) {
6501cb0ef41Sopenharmony_ci        fCalendar->setLenient(lenient);
6511cb0ef41Sopenharmony_ci    }
6521cb0ef41Sopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
6531cb0ef41Sopenharmony_ci    setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, lenient, status);
6541cb0ef41Sopenharmony_ci    setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, lenient, status);
6551cb0ef41Sopenharmony_ci}
6561cb0ef41Sopenharmony_ci
6571cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
6581cb0ef41Sopenharmony_ci
6591cb0ef41Sopenharmony_ciUBool
6601cb0ef41Sopenharmony_ciDateFormat::isLenient() const
6611cb0ef41Sopenharmony_ci{
6621cb0ef41Sopenharmony_ci    UBool lenient = true;
6631cb0ef41Sopenharmony_ci    if (fCalendar != nullptr) {
6641cb0ef41Sopenharmony_ci        lenient = fCalendar->isLenient();
6651cb0ef41Sopenharmony_ci    }
6661cb0ef41Sopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
6671cb0ef41Sopenharmony_ci    return lenient
6681cb0ef41Sopenharmony_ci        && getBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, status)
6691cb0ef41Sopenharmony_ci        && getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, status);
6701cb0ef41Sopenharmony_ci}
6711cb0ef41Sopenharmony_ci
6721cb0ef41Sopenharmony_civoid
6731cb0ef41Sopenharmony_ciDateFormat::setCalendarLenient(UBool lenient)
6741cb0ef41Sopenharmony_ci{
6751cb0ef41Sopenharmony_ci    if (fCalendar != nullptr) {
6761cb0ef41Sopenharmony_ci        fCalendar->setLenient(lenient);
6771cb0ef41Sopenharmony_ci    }
6781cb0ef41Sopenharmony_ci}
6791cb0ef41Sopenharmony_ci
6801cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
6811cb0ef41Sopenharmony_ci
6821cb0ef41Sopenharmony_ciUBool
6831cb0ef41Sopenharmony_ciDateFormat::isCalendarLenient() const
6841cb0ef41Sopenharmony_ci{
6851cb0ef41Sopenharmony_ci    if (fCalendar != nullptr) {
6861cb0ef41Sopenharmony_ci        return fCalendar->isLenient();
6871cb0ef41Sopenharmony_ci    }
6881cb0ef41Sopenharmony_ci    // fCalendar is rarely null
6891cb0ef41Sopenharmony_ci    return false;
6901cb0ef41Sopenharmony_ci}
6911cb0ef41Sopenharmony_ci
6921cb0ef41Sopenharmony_ci
6931cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
6941cb0ef41Sopenharmony_ci
6951cb0ef41Sopenharmony_ci
6961cb0ef41Sopenharmony_civoid DateFormat::setContext(UDisplayContext value, UErrorCode& status)
6971cb0ef41Sopenharmony_ci{
6981cb0ef41Sopenharmony_ci    if (U_FAILURE(status))
6991cb0ef41Sopenharmony_ci        return;
7001cb0ef41Sopenharmony_ci    if ( (UDisplayContextType)((uint32_t)value >> 8) == UDISPCTX_TYPE_CAPITALIZATION ) {
7011cb0ef41Sopenharmony_ci        fCapitalizationContext = value;
7021cb0ef41Sopenharmony_ci    } else {
7031cb0ef41Sopenharmony_ci        status = U_ILLEGAL_ARGUMENT_ERROR;
7041cb0ef41Sopenharmony_ci   }
7051cb0ef41Sopenharmony_ci}
7061cb0ef41Sopenharmony_ci
7071cb0ef41Sopenharmony_ci
7081cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
7091cb0ef41Sopenharmony_ci
7101cb0ef41Sopenharmony_ci
7111cb0ef41Sopenharmony_ciUDisplayContext DateFormat::getContext(UDisplayContextType type, UErrorCode& status) const
7121cb0ef41Sopenharmony_ci{
7131cb0ef41Sopenharmony_ci    if (U_FAILURE(status))
7141cb0ef41Sopenharmony_ci        return (UDisplayContext)0;
7151cb0ef41Sopenharmony_ci    if (type != UDISPCTX_TYPE_CAPITALIZATION) {
7161cb0ef41Sopenharmony_ci        status = U_ILLEGAL_ARGUMENT_ERROR;
7171cb0ef41Sopenharmony_ci        return (UDisplayContext)0;
7181cb0ef41Sopenharmony_ci    }
7191cb0ef41Sopenharmony_ci    return fCapitalizationContext;
7201cb0ef41Sopenharmony_ci}
7211cb0ef41Sopenharmony_ci
7221cb0ef41Sopenharmony_ci
7231cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
7241cb0ef41Sopenharmony_ci
7251cb0ef41Sopenharmony_ci
7261cb0ef41Sopenharmony_ciDateFormat&
7271cb0ef41Sopenharmony_ciDateFormat::setBooleanAttribute(UDateFormatBooleanAttribute attr,
7281cb0ef41Sopenharmony_ci    									UBool newValue,
7291cb0ef41Sopenharmony_ci    									UErrorCode &status) {
7301cb0ef41Sopenharmony_ci    if(!fBoolFlags.isValidValue(newValue)) {
7311cb0ef41Sopenharmony_ci        status = U_ILLEGAL_ARGUMENT_ERROR;
7321cb0ef41Sopenharmony_ci    } else {
7331cb0ef41Sopenharmony_ci        fBoolFlags.set(attr, newValue);
7341cb0ef41Sopenharmony_ci    }
7351cb0ef41Sopenharmony_ci
7361cb0ef41Sopenharmony_ci    return *this;
7371cb0ef41Sopenharmony_ci}
7381cb0ef41Sopenharmony_ci
7391cb0ef41Sopenharmony_ci//----------------------------------------------------------------------
7401cb0ef41Sopenharmony_ci
7411cb0ef41Sopenharmony_ciUBool
7421cb0ef41Sopenharmony_ciDateFormat::getBooleanAttribute(UDateFormatBooleanAttribute attr, UErrorCode &/*status*/) const {
7431cb0ef41Sopenharmony_ci
7441cb0ef41Sopenharmony_ci    return static_cast<UBool>(fBoolFlags.get(attr));
7451cb0ef41Sopenharmony_ci}
7461cb0ef41Sopenharmony_ci
7471cb0ef41Sopenharmony_ciU_NAMESPACE_END
7481cb0ef41Sopenharmony_ci
7491cb0ef41Sopenharmony_ci#endif /* #if !UCONFIG_NO_FORMATTING */
7501cb0ef41Sopenharmony_ci
7511cb0ef41Sopenharmony_ci//eof
752