]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/datefmt.cpp
ICU-461.18.tar.gz
[apple/icu.git] / icuSources / i18n / datefmt.cpp
index 842b910ba35f258cc30127cd0992239ea34b54c4..9280d2038e662d2b5ce623dff824f51e5a27a9c2 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *******************************************************************************
- * Copyright (C) 1997-2008, International Business Machines Corporation and    *
+ * Copyright (C) 1997-2010, International Business Machines Corporation and    *
  * others. All Rights Reserved.                                                *
  *******************************************************************************
  *
@@ -141,6 +141,55 @@ DateFormat::format(const Formattable& obj,
 
 //----------------------------------------------------------------------
 
+UnicodeString&
+DateFormat::format(const Formattable& obj,
+                   UnicodeString& appendTo,
+                   FieldPositionIterator* posIter,
+                   UErrorCode& status) const
+{
+    if (U_FAILURE(status)) return appendTo;
+
+    // if the type of the Formattable is double or long, treat it as if it were a Date
+    UDate date = 0;
+    switch (obj.getType())
+    {
+    case Formattable::kDate:
+        date = obj.getDate();
+        break;
+    case Formattable::kDouble:
+        date = (UDate)obj.getDouble();
+        break;
+    case Formattable::kLong:
+        date = (UDate)obj.getLong();
+        break;
+    default:
+        status = U_ILLEGAL_ARGUMENT_ERROR;
+        return appendTo;
+    }
+
+    // Is this right?
+    //if (fieldPosition.getBeginIndex() == fieldPosition.getEndIndex())
+    //  status = U_ILLEGAL_ARGUMENT_ERROR;
+
+    return format(date, appendTo, posIter, status);
+}
+
+//----------------------------------------------------------------------
+
+// Default implementation for backwards compatibility, subclasses should implement.
+UnicodeString&
+DateFormat::format(Calendar& /* unused cal */,
+                   UnicodeString& appendTo,
+                   FieldPositionIterator* /* unused posIter */,
+                   UErrorCode& status) const {
+    if (U_SUCCESS(status)) {
+        status = U_UNSUPPORTED_ERROR;
+    }
+    return appendTo;
+}
+
+//----------------------------------------------------------------------
+
 UnicodeString&
 DateFormat::format(UDate date, UnicodeString& appendTo, FieldPosition& fieldPosition) const {
     if (fCalendar != NULL) {
@@ -156,6 +205,20 @@ DateFormat::format(UDate date, UnicodeString& appendTo, FieldPosition& fieldPosi
 
 //----------------------------------------------------------------------
 
+UnicodeString&
+DateFormat::format(UDate date, UnicodeString& appendTo, FieldPositionIterator* posIter,
+                   UErrorCode& status) const {
+    if (fCalendar != NULL) {
+        fCalendar->setTime(date, status);
+        if (U_SUCCESS(status)) {
+            return format(*fCalendar, appendTo, posIter, status);
+        }
+    }
+    return appendTo;
+}
+
+//----------------------------------------------------------------------
+
 UnicodeString&
 DateFormat::format(UDate date, UnicodeString& appendTo) const
 {
@@ -275,41 +338,6 @@ DateFormat::createInstance()
     return create(kShort, (EStyle) (kShort + kDateOffset), Locale::getDefault());
 }
 
-//----------------------------------------------------------------------
-DateFormat* U_EXPORT2
-DateFormat::createPatternInstance(const UnicodeString& skeleton,
-                                  const Locale& locale,
-                                  UErrorCode& status) 
-{
-    if ( U_FAILURE(status) ) {
-        return NULL;
-    }
-
-    DateTimePatternGenerator* dtptg = 
-               DateTimePatternGenerator::createInstance(locale, status);
-    if ( dtptg == NULL ) {
-        status = U_MEMORY_ALLOCATION_ERROR;
-        delete dtptg;
-        return NULL;
-    }
-    if ( U_FAILURE(status) ) {
-        delete dtptg;
-        return NULL;
-    }
-
-    const UnicodeString pattern = dtptg->getBestPattern(skeleton, status);
-    delete dtptg;
-    if ( U_FAILURE(status) ) {
-        return NULL;
-    }
-    SimpleDateFormat* dtfmt = new SimpleDateFormat(pattern, locale, status);
-    if ( U_FAILURE(status) ) {
-        delete dtfmt;
-        return NULL;
-    }
-    return dtfmt;
-}
-
 //----------------------------------------------------------------------
 
 DateFormat* U_EXPORT2