Passing and Displaying Timestamps

6 posts / 0 new
Last post
Offline
Last seen: 6 years 3 weeks ago
Joined: 10/12/2018
Posts: 7
Passing and Displaying Timestamps

I am currently working on a project that deals with the Publisher and Subscriber. Using Java as the coding language, I want to pass the value of the timestamp on the publisher side and also receive the timestamp at the Subscriber side. 

How if I invoke this method:  <writer>.register_instance_w_timestamp(instance, s_tstamp) ; 

Will it work fine? How will I pass the timestamp here as the argument and how will I get it displayed in the Subscriber output? Is there any need to convert its datatype?

Also what will be the procedure to set the current system clock time as the timestamp rather giving my own timestamp?

Thanking in anticipation!

Gerardo Pardo's picture
Offline
Last seen: 1 week 1 day ago
Joined: 06/02/2010
Posts: 602

Normally you do not need to pass your own timestamp. Whenever you call the operation on the DataWriter (e.g. register_instance() or writer()) the Connext library will get the current time automatically and put it as part of the Sample that is sent to the DataReader(s).

The only reason to call the  _w_timestamp()  operations is if you want to suppy a differerent timestamp from the current one in the system. This is not a common scenario.

Either way the source timestamp is available when you receive the sample. This can be found in the source_timestamp field of the SampleInfo.

You do not need to convert the formats. The application handles the time stamp using the Time_t class.  This will be automatically marshalled/demarshalled by DDS. 

One thing to keep in mind is that the time origin for the Time_t   is represented by the reserved value TIME_ZERO and corresponds to the UNIX prime epoch 0h, 1 January 1970. Unfortunately this is not clearly documented in the API docs.

 

 

Offline
Last seen: 6 years 3 weeks ago
Joined: 10/12/2018
Posts: 7

Thanks for replying. But I am not able to figure out how to retrieve this timestamp from SampleInfo? What will be the code required for it? 

Consider if I am using the write() method, how should I get the value of the source timestamp on publisher side and reception timestamp on the Subscriber side? 

Can I please get a code snippet that implements the retrieval of timestamps from the SampleInfo.

Offline
Last seen: 6 years 3 weeks ago
Joined: 10/12/2018
Posts: 7

In Subcriber file, I have added this line: 

double mytime = _infoSeq.get(i).reception_timestamp.sec + (_infoSeq.get(i).reception_timestamp.nanosec/NANOSECOND);

System.out.println(mytime);

Also added the following line before the main method:

static double NANOSECOND = 1000000000.0;

 

The output of the subsciber file includes timestamp in this format:

1.5397802978029897E9

How to convert it into mm:ss format? or is there any other way to print the timestamp in the output? 

 

Fernando Garcia's picture
Offline
Last seen: 3 months 3 weeks ago
Joined: 05/18/2011
Posts: 200
Hi Syeda,
 
You can convert the Time_t into a string as follows:
 
import java.time.Instant;

// ...

for(int i = 0; i < _dataSeq.size(); ++i) {
    SampleInfo info = (SampleInfo)_infoSeq.get(i);
    Instant st = Instant.ofEpochSecond(
            info.source_timestamp.sec,
            info.source_timestamp.nanosec);

    System.out.println("Source Timestamp (in ISO8601 format): "
            + st.toString());
    // ...
}
The resulting string representation in ISO 8601 would be something like the following:
Source Timestamp (in ISO8601 format): 2018-10-17T15:30:56.812084998Z
Note that you can introduce the timezone offset as part of your call to Instant.ofEpochSecond(), but that is a different matter.
 
Also, If you need a different string representation, you can always use a DateTimeFormatter.
 
Best regards,
Fernando.
Offline
Last seen: 6 years 3 weeks ago
Joined: 10/12/2018
Posts: 7

So nice of you!!

Thanks very much!! Now I got the time in a readable format.