Skip to content

date_time_with_ms formats milliseconds incorrectly when nanoseconds have leading zeros #569

@ahuazuipiaoliang

Description

@ahuazuipiaoliang

Generated by Generative AI

GPT-5.5

Operating System:

ubuntu 22.04

ROS version or commit hash:

humble

RMW implementation (if applicable):

cyclone dds

RMW Configuration (if applicable):

No response

Client library (if applicable):

No response

'ros2 doctor --report' output

Summary

The {date_time_with_ms} logging format token appears to format the millisecond field incorrectly when the nanosecond part of the timestamp has leading zeros.

I checked the Humble implementation, and the Rolling implementation appears to use the same logic.

log example

the timestamp of the second log should be newer than the first log

[INFO 2026-05-27 10:32:52.860] ... [4724] frame_ts:lidar_ts:1779849172000
[INFO 2026-05-27 10:32:52.245] ... [4725] frame_ts:lidar_ts:1779849172100

Affected code

In rcutils_time_point_value_as_date_string():

uint64_t nanoseconds = abs_time_point % (1000u * 1000u * 1000u);
char nanoseconds_str[21];

rcutils_snprintf(nanoseconds_str, sizeof(nanoseconds_str), "%" PRIu64, nanoseconds);

rcutils_snprintf(
  &str[date_end_position], str_size - date_end_position, ".%.3s",
  nanoseconds_str)

### Steps to reproduce issue

export RCUTILS_CONSOLE_OUTPUT_FORMAT="[{severity} {date_time_with_ms}] [{name}@{function_name}:{line_number}]: {message}"

### Expected behavior

The nanosecond remainder should be zero-padded to 9 digits before extracting milliseconds, or milliseconds should be computed directly:
```c
uint64_t milliseconds = nanoseconds / (1000u * 1000u);

and formatted as:

".%03" PRIu64

Actual behavior

the ts of LiDAR are increasing but the ts of logger are decreasing;

[INFO 2026-05-27 10:32:52.860] ... [4724] frame_ts:lidar_ts:1779849172000
[INFO 2026-05-27 10:32:52.245] ... [4725] frame_ts:lidar_ts:1779849172100

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions