473,396 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Java date to C++

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
Oct 17 '05 #1
17 6092
On Tue, 18 Oct 2005 01:14:41 +0200, <Mike> wrote or quoted :
How can I recalculate the date in c++?

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?
Look at how LeDataInputStream does it in Java. The C code would be
very similar.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Oct 18 '05 #2
Mike wrote:
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
Oct 18 '05 #3
Roedy Green wrote:
On Tue, 18 Oct 2005 01:14:41 +0200, <Mike> wrote or quoted :
How can I recalculate the date in c++? 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
Oct 18 '05 #4
Victor Bazarov wrote:
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.
Oct 18 '05 #5

"red floyd" <no*****@here.dude> wrote in message
news:7u*****************@newssvr25.news.prodigy.ne t...
Victor Bazarov wrote:
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.


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
Oct 18 '05 #6
Mike Wahler wrote:
"red floyd" <no*****@here.dude> wrote in message
news:7u*****************@newssvr25.news.prodigy.ne t...
Victor Bazarov wrote:
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.


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
Oct 18 '05 #7

<Mike> wrote in message news:43***********************@news.xs4all.nl...
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.
Oct 18 '05 #8
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!

Oct 18 '05 #9
Pep
<Mike> wrote:
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.

Oct 18 '05 #10
> > 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

Oct 18 '05 #11
> 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
Oct 18 '05 #12
Mike wrote:
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?


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
Oct 18 '05 #13
Mike wrote:
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.)
Oct 19 '05 #14
Alan Krueger wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Oct 19 '05 #15
Pep
<Mike> wrote:
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.

Oct 19 '05 #16
jf******@gmail.com wrote:

<snip>
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.
--
Nick Keighley

Oct 19 '05 #17
Mike wrote:
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/texi...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
Oct 25 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ...
2
by: Michael | last post by:
Running DB2 v7 UDB ("DB2 v7.1.0.93", "n031208" and "WR21333") on Windows XP, I am unable to find out why the "Build for Debug" option within Stored Procedure Builder is not enabled on Java stored...
2
by: LandWolf | last post by:
Hi, I have a Web services write in Java and I want to write a client in C#. My web services work with Weather prevision. Hence, I set a date and my web services return the prevision for that date....
6
by: rohayre | last post by:
Im a long time java developer and actually have never done anything with java scripting. I'd like to write a short simple script for calculating a date in the future based on today's date and a...
0
by: TraceyAnnison | last post by:
I wonder if you can help me - I'm new to this, and working in a project that has already been configured to work with Axis. We have a Java project built with the Spring framework, in order to...
7
helpwithcode
by: helpwithcode | last post by:
Hi people, I am just learning java.I have been creating a project which involves JDBC Connectivity.I find that the statements, String string_dob=text_dob.getText(); //Converting string to...
3
by: Nick048 | last post by:
Hi to All, In order to create a Date Picker that I need to use in Oracle forms, I have modified some element in the source of original calendarPJC provided from Oracle. Following the...
3
by: Joshepmichel | last post by:
Please to help me to following problem I want to do this 1. create Table Name MEMBER on the Database Name "mytestdb", 2. Add the Values to the Table through the Key board Inputs during running...
19
by: robtyketto | last post by:
Greetings, I have the following code below which allows the date to be added via a JDBC connection as a STRING. The value of dateString is inserted into the MS ACCESS database. What is the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.