convert C++ miiliseconds into Java Date

I am monitoring some events on C++ side and I get the time the event generated in milliseconds. ( DWORD evTime).

So I want to send this to a Java method in milliseconds, so that I can construct a Timestamp object.

c++ code:

// DWORD evTime is time in milliseconds

jenv->CallStaticVoidMethod(jcls, jmethID,(jlong) evTime);

java Code:

protected static void createTimeStamp(long timeAsMillisecs){

Timestamp ts = new Timestamp( timeAsMillisecs);

}

This doesn't work and I get weird dates in 1969..I believe it has to do with casting unsigned long to jlong on C++ side. Any ideas of what's wrong?

I also tried converting it into a time_t and then sending it as jlong to Java as below:

time_t iTime= evTime;

time_t rawtime = time(&iTime);

jenv->CallStaticVoidMethod(jcls, jmethID,(jlong) rawTime);

java side:

Timestamp ts = new Timestamp( timeAsMillisecs*1000);

(since time_t returns seconds, but I'm multiplying with 1000) .

This gives me correct time but it's only in seconds(eg: 2007-05-7 15:00:10.0) but I need the milliseconds. Any help is appreciated. Thanks.

[1184 byte] By [kteegalaa] at [2007-11-27 3:37:08]
# 1

I think that the problem is with the way you get the milliseconds in C++. The Java Timestamp class expects to get the number of milliseconds passed since 1970. I don抰 know about a standard C++ function that allows you to get it. You can get the milliseconds from the C++ function clock() but it is not their number since 1970 but the number of clock ticks elapsed since the program was launched.

Using a combination of time() and clock() you can calculate the number of milliseconds passed since 1970 and create a Java Timestamp.

Daniel.Barkana at 2007-7-12 8:40:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
Thanks! I just found out that the milliseconds in C++ is the milliseconds that have elapsed since the system was started, up to 49.7 days. But still looking for a way to convert it atleast to a time string.
kteegalaa at 2007-7-12 8:40:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

In order to get the exact number on milliseconds in C++ you can do the following:

//on the beginning of the program execution, do:

int milliSecondOffset = 0;

long time = time();

while(time() == time){}// waiting to find the exact millisecond that time() switches

milliSecondOffset = clock%1000;//calculating the offset between clock() and time()

Now you should pass to Java time() clock() and milliSecondOffset. The following Java method should return the Timestamp:

Timestamp getTimestamp(long time, int clock, int milliSecondOffset)

{

return (new Timestamp((time * 1000) + (clock%1000) ?milliSecondOffset);

}

Daniel.Barkana at 2007-7-12 8:40:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
Thanks. I'm doing something similar on the Java side. I'm calculating the offset on the Java side (System.currentmilliseconds() - C++ clock ticks in millis). Then I add the offset to the milliseconds passed from C++.Working fine now :)
kteegalaa at 2007-7-12 8:40:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

Of course you realize that when the 49 days pass if the application hasn't been restarted that the duration will be reset to zero?

Could be me but if it was me I wouldn't consider relying on that. And certainly not relying on it as a timestamp.

Particularly when the are other C API methods which can give you a timestamp that doesn't rollover.

jschella at 2007-7-12 8:40:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6

Yes, I did think abt it. If it was up to me to create a timestamp, I would use a different way . But the problem is I can't create a timestamp by any other method..I just have to use what's given to me by the winEventProc hook function ( http://msdn2.microsoft.com/en-us/library/ms697317.aspx), which gives me the time an event is generated in milliseconds.

But I'm hoping that the rollover wouldn't be a problem, because I'll be setting the offset everytime before calling setting the event hook. There is ofcourse a chance that this wouldn't work, if :

1) The system has not been restaretdd in 49 days

2) the offset was created just a few milliseconds before the 49 day reset and the and the events start firing just after the 49-day reset.

Any other ideas are welcome though.

kteegalaa at 2007-7-12 8:40:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7

> But I'm hoping that the rollover wouldn't be a

> problem, because I'll be setting the offset everytime

> before calling setting the event hook. There is

> ofcourse a chance that this wouldn't work, if :

> 1) The system has not been restaretdd in 49 days

> 2) the offset was created just a few milliseconds

> before the 49 day reset and the and the events start

> firing just after the 49-day reset.

>

> Any other ideas are welcome though.

One idea is that it sounds to me like you are dealing solely with a duration and probably a short one at that.

So using Date is probably a bad idea.

Dealing with the rolloever is just a matter of comparing the start and stop values to verify that the stop value is greater than the start value.

jschella at 2007-7-12 8:40:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...