473,811 Members | 4,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
17 6131
> > 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.currentT imeMillis()

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
9879
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 ********************************** package Celcom.Client;
2
3153
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 procedures. It is enabled for SQL stored procedures. It is possible to "Build" and "Run" the Java SPs, it just isn't possible to click on the "Build for Debug" option. Thanks for any help in advance. Michael
2
2363
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. When the web service return the date, this date is wrong. Es: Choose date: 24-apr-2006 Responce of Web service: day: 23-apr-2006, condition: sunny, temperature: 28
6
14457
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 letter. Can I use javascripting to create a webpage to allow a user to enter a letter and then click a button to find a future calendar date? I'm just not sure how much user interaction scripting allows. Does java scripting allow buttons, textfields...
0
2393
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 provide webservices for access via SOAP. As I understand it (ie not very well!) Axis works to accept these SOAP messages from the client and transform them into calls on our Java methods, and do the reverse with the responses - cool! Which is excellent,...
7
5342
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 date System.out.println(string_dob); s.Info_DOB=Date.valueOf(string_dob); runs perfectly fine in the method insert() and throws up an illegal Exception in the method UPDATE.This is the error I get when I pass a date "1979-05-02"
3
1877
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 instruction I have compiled in the correct order the Java file. The first is CalendarWidgetEvent.java with this code: package oracle.forms.demos;
3
7703
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 the My Java Application.; Therefore I used this following codes, It Consists the seperate parts for the Raede and Member Class for the purposely I craetes. 3. I have problem to Parsing the values to SQL statement, that Consists on the Run Time...
19
20252
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 easiest method to add the current date/time (dd/mm/yy hh:mm:ss) via JDBC in DATE format i.e. convert STRING to DATE or format the DATE without changing its type and insert it. I'm a newbie to Java/Jsp too.
0
10656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10397
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10413
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9214
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5565
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3879
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.