Java date to C++

G

Guest

Hello,

I need to send a Java date (milliseconds since the epoch 1970-01-01
00:00:00.000 GMT) through a socket to a C++ app and reconstruct exactly the
same date in the C++ app as in the Java app.

The date (actually the milliseconds since the epoch) is splitted in 8 bytes
and sent through the socket.

How can I recalculate the date in c++? Are there methods / classes for this
purpose in c++? And if not: how is the amount of milliseconds in Java
precisely calculated?

greetings,

Mike
 
V

Victor Bazarov

Mike said:
I need to send a Java date (milliseconds since the epoch 1970-01-01
00:00:00.000 GMT) through a socket to a C++ app and reconstruct
exactly the same date in the C++ app as in the Java app.

The date (actually the milliseconds since the epoch) is splitted in 8
bytes and sent through the socket.

How can I recalculate the date in c++? Are there methods / classes
for this purpose in c++?

Not in the Standard C++. But if leap seconds are not very important,
you should simply assume 1000 ms per sec, 60 sec per min, 60 mins per
hr, 24 hrs per day, 365 (or 366) days per year, I recon. Once you
have the GMT date, you can use 'mktime' and 'localtime' functions to
reconstruct the local time.
And if not: how is the amount of
milliseconds in Java precisely calculated?

Shouldn't you ask this in a Java newsgroup?

V
 
V

Victor Bazarov

Roedy said:
There is nothing to calculate. The only problem you might have is
endianness. To write a little-endian long see nio or
http://mindprod.com/products1.html#LEDATASTREAM

Perhaps your problem is reconstructing a long from 8 bytes in C?

(a) It's C++.
(b) 'long' is often only 4 bytes, it would be impossible to reconstruct
it in that case, don't you think?
Look at how LeDataInputStream does it in Java. The C code would be
very similar.


It's C++.

V
 
R

red floyd

Victor said:
Not in the Standard C++. But if leap seconds are not very important,
you should simply assume 1000 ms per sec, 60 sec per min, 60 mins per
hr, 24 hrs per day, 365 (or 366) days per year, I recon. Once you
have the GMT date, you can use 'mktime' and 'localtime' functions to
reconstruct the local time.

What's wrong with using some form of multi-precision integer class,
dividing by 1000, checking for overflow, and using the result as a
time_t? localtime() and gmtime() use secnods since epoch UTC.
 
M

Mike Wahler

red floyd said:
What's wrong with using some form of multi-precision integer class,
dividing by 1000, checking for overflow, and using the result as a time_t?
localtime() and gmtime() use secnods since epoch UTC.

I don't believe there's any requirement that 'time_t'
be an integer type, or that there is a conversion from
an integer type to a 'time_t'.

-Mike
 
V

Victor Bazarov

Mike said:
I don't believe there's any requirement that 'time_t'
be an integer type, or that there is a conversion from
an integer type to a 'time_t'.

That's true. There is no requirement that 'time_t' is in any way
related to January 1st, 1970, either.

V
 
B

Branimir Maksimovic

Hello,

I need to send a Java date (milliseconds since the epoch 1970-01-01
00:00:00.000 GMT) through a socket to a C++ app and reconstruct exactly
the same date in the C++ app as in the Java app.

The date (actually the milliseconds since the epoch) is splitted in 8
bytes and sent through the socket.

Is date big endian or little endian?signed or unsigned?
Is it send as two consecutive 4 byte integers or single one?
Do you use any standard api CDR/XDR for example?
How can I recalculate the date in c++? Are there methods / classes for
this purpose in c++?

If you know the details calculation is not a problem :)

Greetings, Bane.
 
J

jfbriere

No need for big calculations.
There are standard C functions that deal with it.
Take a look at:
http://www.delorie.com/gnu/docs/glibc/libc_435.html

You have to be careful with OS specifics:

1- Endianness: be sure that the 8 bytes from java (allways big endian)
will be
assign correctly in the c++ part.

2- The standard C functions use time_t, which is typically define as a
long (you must verify it in time.h -> sys/types.h) which is typically
32bits (depending on systems) which 4 bytes and NOT 8 bytes.

3- java.util.Date.getTime() returns a long which is the number of
milliseconds since the epoch.
On the other hand, the standard C functions use time_t which represents
the number of seconds since the epoch.
Epochs are the same in both cases.

Good luck!
 
P

Pep

Hello,

I need to send a Java date (milliseconds since the epoch 1970-01-01
00:00:00.000 GMT) through a socket to a C++ app and reconstruct exactly
the same date in the C++ app as in the Java app.

The date (actually the milliseconds since the epoch) is splitted in 8
bytes and sent through the socket.

How can I recalculate the date in c++? Are there methods / classes for
this purpose in c++? And if not: how is the amount of milliseconds in Java
precisely calculated?

greetings,

Mike

Use the getTimeInMillis method of the Calendar class to get the UTC which is
a long int, then send that to the C++ program.

In C++ use localtime to convert to a tm struct to be able to mess with the
individual time components.

Cheers,
Pep.
 
B

BigBrian

Use the getTimeInMillis method of the Calendar class to get the UTC which is
a long int, then send that to the C++ program.

Send this how? If you're sending the binary representation of this
long int over the network, you need to consider how the machine from
which it comes represents long ints. This may not be the same as the
receiving host.

-Brian
 
G

Guest

2- The standard C functions use time_t, which is typically define as a
long (you must verify it in time.h -> sys/types.h) which is typically
32bits (depending on systems) which 4 bytes and NOT 8 bytes.
....
On the other hand, the standard C functions use time_t which represents
the number of seconds since the epoch.
Epochs are the same in both cases.

Thank you (and the others) for replying.

Does this mean that it's not possible to use milliseconds in dates/times in
C/C++?

I need to perform an action (make a call to a dll method) at a precise time
in the C++ app. The user enter the precise time in the Java app, for example
2005-10-18 23:00:00.500, this is sent (the amount of ms since the epoch) to
the C++ app.
Is it possible to let the C++ app perform an action at that precise time
_including_ the milliseconds (a little inaccuracy is tolerable, the less the
better)? Are there (open source / free) C/C++ libraries for time-critical
operations?


Best regards,

Mike
 
V

Victor Bazarov

Mike said:
Thank you (and the others) for replying.

Does this mean that it's not possible to use milliseconds in dates/times in
C/C++?

I need to perform an action (make a call to a dll method) at a precise time
in the C++ app. The user enter the precise time in the Java app, for example
2005-10-18 23:00:00.500, this is sent (the amount of ms since the epoch) to
the C++ app.
Is it possible to let the C++ app perform an action at that precise time
_including_ the milliseconds (a little inaccuracy is tolerable, the less the
better)? Are there (open source / free) C/C++ libraries for time-critical
operations?

Time in C and C++ has one second resolution. The platform on which your
program is running may be (and most likely is) better. But to get that
functionality, you'd have to use platform-specific means. For example,
file time in Windows has sub-microsecond resolution, which does not
necessarily mean it's good for real-time work, though. All in all, you
are better off asking about those things in a newsgroup for your OS.

V
 
A

Alan Krueger

Mike said:
I need to send a Java date (milliseconds since the epoch 1970-01-01
00:00:00.000 GMT) through a socket to a C++ app and reconstruct exactly the
same date in the C++ app as in the Java app.

The date (actually the milliseconds since the epoch) is splitted in 8 bytes
and sent through the socket.

You didn't specify what format the C++ app used for timestamps. If
you're using a Unix-ish time_t format, that would appear to use the same
epoch as the Java millisecond timestamp. Dividing by 1000 to convert
milliseconds to seconds within that epoch would seem to be the simple
solution.

(Of course, this being Usenet, someone will now stand up and poke holes
in this.)
 
P

Phlip

Alan said:
You didn't specify what format the C++ app used for timestamps. If you're
using a Unix-ish time_t format, that would appear to use the same epoch as
the Java millisecond timestamp. Dividing by 1000 to convert milliseconds
to seconds within that epoch would seem to be the simple solution.

(Of course, this being Usenet, someone will now stand up and poke holes in
this.)

The C Standard does not specify the epoch for time_t is the dawn of 1970. I
worked with a Standard Compliant implementation where it was 1980. No idea
why (there's more to a C Standard Library than the letter of The Standard),
and our file formats had to adjust.
 
P

Pep

Hello,

I need to send a Java date (milliseconds since the epoch 1970-01-01
00:00:00.000 GMT) through a socket to a C++ app and reconstruct exactly
the same date in the C++ app as in the Java app.

The date (actually the milliseconds since the epoch) is splitted in 8
bytes and sent through the socket.

How can I recalculate the date in c++? Are there methods / classes for
this purpose in c++? And if not: how is the amount of milliseconds in Java
precisely calculated?

greetings,

Mike

This is an easy thing to do.

Use the Calender class getTimeInMillis method to return the UTC, which is a
long integer. Send that value to the c++ application which will be able to
manipulate it using the unix time functions.

See the mktime man page for more details on the c++ side of things.

Cheers,
Pep.
 
N

Nick Keighley

(e-mail address removed) wrote:

3- java.util.Date.getTime() returns a long which is the number of
milliseconds since the epoch.
On the other hand, the standard C functions use time_t which represents
the number of seconds since the epoch.
Epochs are the same in both cases.

I don't think the C standard (which the C++ standard points to)
defines either the time period or the epoch of time_t. Posix may.
 
P

P.Hill

Mike said:
Hello,

I need to send a Java date (milliseconds since the epoch 1970-01-01
00:00:00.000 GMT) through a socket to a C++ app and reconstruct exactly the
same date in the C++ app as in the Java app.

The date (actually the milliseconds since the epoch) is splitted in 8 bytes
and sent through the socket.

How can I recalculate the date in c++? Are there methods / classes for this
purpose in c++? And if not: how is the amount of milliseconds in Java
precisely calculated?

On the Java side either some bit of code set all of the various fields
in a Calendar and then a some code called myCal.getDate() or the code
set the time to right now when the code did a new Date() which calls
System.currentTimeMillis()

But the conversion from the local system representation to this Unix/C
like style may depend on the localtime zone because timezones may
include daylight savings changes before 1970 and daylight savings change
days include variable length days of 23 and 25 hours.

In c consider:

http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_19.html#SEC316
Function: struct tm * localtime (const time_t *time)
Function: time_t mktime (struct tm *brokentime)

And then pray that the timezones are all the same in Java and C
environments.

-Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,416
Messages
2,571,560
Members
48,797
Latest member
shadowoftheunknown

Latest Threads

Top