Browse Source

Fixed JNI Date conversion wit timegm and gmtime

JNI-44
Edouard Tisserant 10 years ago
parent
commit
e95aa5effa
  1. 1
      android/jni/Android.mk
  2. 7
      androidTests/app/src/main/java/com/pep/k9/MainActivity.java
  3. 40
      src/jniutils.cc

1
android/jni/Android.mk

@ -71,6 +71,7 @@ LOCAL_SRC_FILES := \
../../src/basic_api.cc \
../../src/jniutils.cc
LOCAL_C_INCLUDES := ../../src
#LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)

7
androidTests/app/src/main/java/com/pep/k9/MainActivity.java

@ -148,14 +148,17 @@ public class MainActivity extends AppCompatActivity {
msg.setSent(now);
Date res = msg.getSent();
if(!(res.equals(now))) throw new AssertionError();
// Conversion rounds to the second, java's Date is in millisecond.
if(!(java.lang.Math.abs(res.getTime() - now.getTime()) < 1000)) throw new AssertionError();
}
{
Date now = new Date();
msg.setRecv(now);
if(!(msg.getRecv().equals(now))) throw new AssertionError();
Date res = msg.getRecv();
// Conversion rounds to the second, java's Date is in millisecond.
if(!(java.lang.Math.abs(res.getTime() - now.getTime()) < 1000)) throw new AssertionError();
}
{

40
src/jniutils.cc

@ -19,6 +19,14 @@ time_t timegm(struct tm* const t) {
}
#endif
#if 0 // Enable if log needed
#include <android/log.h>
#define LOG_TAG "PEPJNIUTILS"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#else
#define LOGD(...)
#endif
namespace pEp {
namespace JNIAdapter {
jclass findClass(JNIEnv *env, const char *classname)
@ -302,7 +310,15 @@ namespace pEp {
if (!ts)
return (jobject) NULL;
time_t t = timegm(ts);
LOGD("/* Seconds (0-60) */ FROM :%d", ts->tm_sec);
LOGD("/* Minutes (0-59) */ :%d", ts->tm_min);
LOGD("/* Hours (0-23) */ :%d", ts->tm_hour);
LOGD("/* Day of the month (1-31) */:%d", ts->tm_mday);
LOGD("/* Month (0-11) */ :%d", ts->tm_mon);
LOGD("/* Year - 1900 */ :%d", ts->tm_year);
time64_t t = timegm64(ts)*1000;
LOGD( "TimeGM returns : %lld", t);
jclass clazz = findClass(env, "java/util/Date");
jmethodID constructor = env->GetMethodID(clazz, "<init>", "(J)V");
assert(constructor);
@ -314,8 +330,26 @@ namespace pEp {
if (!date)
return NULL;
time_t t = (time_t) callLongMethod(env, date, "getTime");
return new_timestamp(t);
jlong t = callLongMethod(env, date, "getTime");
LOGD( "Set Time to : %lld", t);
timestamp *ts = (timestamp*)calloc(1, sizeof(timestamp));
assert(ts);
if (ts == NULL)
return NULL;
if (t){
time64_t clock = t/1000;
gmtime64_r(&clock, ts);
LOGD("/* Seconds (0-60) */ TO :%d", ts->tm_sec);
LOGD("/* Minutes (0-59) */ :%d", ts->tm_min);
LOGD("/* Hours (0-23) */ :%d", ts->tm_hour);
LOGD("/* Day of the month (1-31) */:%d", ts->tm_mday);
LOGD("/* Month (0-11) */ :%d", ts->tm_mon);
LOGD("/* Year - 1900 */ :%d", ts->tm_year);
}
return ts;
}
static void _setStringField(JNIEnv *env, const char *classname,

Loading…
Cancel
Save