Time is zero in the node constructor when using simulated time because the clock has not received the current time yet.
The clock gets the simulated time through a subscription to a /clock
topic. Subscription callbacks are called by the executor, but the node must be constructed before it can be added to an executor. That's why simulated time will always be zero in the constructor.
I'm guessing you'd like record the initial time when a node starts. I would recommend doing that by checking if the time is zero when the node starts, and if so register a time jump callback. When the new simulated time is received the time jump callback will be called and the initial time can be recorded.
You might also be interested in the Clock and Time ROS 2 design doc which describes how simulated time works.
In particular:
If time has not been set it will return zero if nothing has been received. A time value of zero should be considered an error meaning that time is uninitialized.
Originally posted by sloretz with karma: 3061 on 2020-07-10
This answer was ACCEPTED on the original site
Post score: 2
Original comments
Comment by avzmpy on 2020-07-14:
Thank you!