[PATCH 2/2] Mama Java: Add java MamaDateTime::getAsFormattedString() method.


Adrienne Ambrose <a.ambrose@...>
 

Bugzilla Ticket :- Bug 182

 

Testing:-

Not middleware or O/S specific. 

Attached modified MamaListen.java application to the Bugzilla ticket, which instead of using the normal print function will now call the new 
datetime.getAsFormattedString() function. 

Example of code added :
MamaTimeZone tz = new MamaTimeZone ("EST"); 
System.out.println (datetime.formatString ("%F %T", null)); 
System.out.println (datetime.formatString ("%F %T", tz)); 

Prints out the following :- 
wQuoteTime | 442 | TIME | 2015-03-10 15:18:51 <-------- normal time 
                                                 2015-03-10 10:18:51 <-------- EST timezone. 

 

=========================================================================================================

 

 

From 1b1b07a6062c2b7d7e200a95a25458d80bfe889d Mon Sep 17 00:00:00 2001

From: A Ambrose <a.ambrose@...>

Date: Wed, 11 Mar 2015 11:36:36 +0000

Subject: [PATCH 2/2] Mama Java: Add java MamaDateTime::getAsFormattedString()

method.

 

Method currently missing from Java implementation.

Add new MamaDateTime::getAsFormattedString() method.

 

Signed-off-by: A Ambrose <a.ambrose@...>

---

mama/jni/src/c/mamadatetimejni.c               | 53 ++++++++++++++++++++++++++

mama/jni/src/com/wombat/mama/MamaDateTime.java | 11 +++++-

2 files changed, 63 insertions(+), 1 deletion(-)

 

diff --git a/mama/jni/src/c/mamadatetimejni.c b/mama/jni/src/c/mamadatetimejni.c

index 8664df9..768aefc 100644

--- a/mama/jni/src/c/mamadatetimejni.c

+++ b/mama/jni/src/c/mamadatetimejni.c

@@ -714,6 +714,59 @@ JNIEXPORT jstring JNICALL Java_com_wombat_mama_MamaDateTime_getAsString

 /*

  * Class:     com_wombat_mama_MamaDateTime

+ * Method:    getAsFormattedString

+ * Signature: ()Ljava/lang/String

+ */

+JNIEXPORT jstring JNICALL Java_com_wombat_mama_MamaDateTime_getAsFormattedString

+  (JNIEnv* env, jobject this, jstring str, jstring timeZone)

+{

+    jlong       pDateTime   = 0;

+    jlong       pTimeZone   = 0;

+    const char* c_Str       = NULL;

+    mama_status status      = MAMA_STATUS_OK;

+    char        ret_c       [MAX_DATE_TIME_STR_LEN+1];

+    char        errorString [UTILS_MAX_ERROR_STRING_LENGTH];

+   

+    pDateTime = (*env)->GetLongField (env,this,dateTimePointerFieldId_g);

+    MAMA_THROW_NULL_PARAMETER_RETURN_VALUE(pDateTime, 

+                             "Null parameter, MamaDateTime may have already been destroyed.", NULL) ;

+

+    if (NULL != timeZone)

+    {

+        pTimeZone = (*env)->GetLongField (env,this,tzFieldObjectFieldId_g);

+        if (0 == pTimeZone)

+        {

+            pTimeZone = createTimeZone (env, this);

+            assert (0!=pTimeZone);

+        }

+        timeZone_set (env, pTimeZone, timeZone);

+    }

+

+    c_Str = (*env)->GetStringUTFChars(env,str,0);

+

+    if (!c_Str ||

+        MAMA_STATUS_OK!=(status=mamaDateTime_getAsFormattedStringWithTz(

+                            CAST_JLONG_TO_POINTER(mamaDateTime,pDateTime),

+                            ret_c,

+                            MAX_DATE_TIME_STR_LEN,

+                            c_Str,

+                            timeZone

+                            ? CAST_JLONG_TO_POINTER (mamaTimeZone, pTimeZone)

+                            : NULL)))

+    {

+         utils_buildErrorStringForStatus(

+                errorString,

+                UTILS_MAX_ERROR_STRING_LENGTH,

+                "Error calling MamaDateTime.getAsStringWithTz().",

+                status);

+        utils_throwExceptionForMamaStatus (env,status,errorString);

+        return 0;

+    }

+    return (*env)->NewStringUTF (env, ret_c);

+} 

+

+/*

+ * Class:     com_wombat_mama_MamaDateTime

  * Method:    getTimeAsString

  * Signature: ()Ljava/lang/String

  */

diff --git a/mama/jni/src/com/wombat/mama/MamaDateTime.java b/mama/jni/src/com/wombat/mama/MamaDateTime.java

index c46ae60..010b77b 100644

--- a/mama/jni/src/com/wombat/mama/MamaDateTime.java

+++ b/mama/jni/src/com/wombat/mama/MamaDateTime.java

@@ -100,8 +100,14 @@ public class MamaDateTime implements Comparable

         return getAsString ();

     }

+    public String formatString (String       str,

+                                MamaTimeZone tz)

+    {

+        return getAsFormattedString (str, (tz!=null ? tz.tz():null));

+    }

+

     public int compareTo (Object obj)

-    { 

+    {

         // If comparison is not possible this will throw a ClassCastException

         // which is what the Comparable spec requires.

         MamaDateTime time = (MamaDateTime) obj;

@@ -283,6 +289,9 @@ public class MamaDateTime implements Comparable

     public native String getAsString ();

+    private native String getAsFormattedString (String format,

+                                                String tz);

+

     public native String getTimeAsString ();

     public native String getDateAsString ();

--

2.1.0

 

Join Openmama-dev@lists.openmama.org to automatically receive all group messages.