Skip to content

Fix millisecond zero-padding in epoch_to_log_line_timestamp - #1028

Draft
rootkiller6788 wants to merge 1 commit into
google:masterfrom
rootkiller6788:fix-epoch-log-line-timestamp-ms-padding
Draft

Fix millisecond zero-padding in epoch_to_log_line_timestamp#1028
rootkiller6788 wants to merge 1 commit into
google:masterfrom
rootkiller6788:fix-epoch-log-line-timestamp-ms-padding

Conversation

@rootkiller6788

Copy link
Copy Markdown

Problem

mobly.logger.epoch_to_log_line_timestamp produced a non-zero-padded millisecond component:

return d.strftime('%m-%d %H:%M:%S.') + str(ms)

For epoch values whose millisecond remainder is below 100, str(ms) drops the leading zeros. For example epoch_to_log_line_timestamp(1469134262005, time_zone=datetime.timezone.utc) returned '07-21 20:51:02.5' instead of '07-21 20:51:02.005'.

This is inconsistent with the canonical log line timestamp format used everywhere else in the module: get_log_line_timestamp returns exactly three millisecond digits, logline_timestamp_re matches \d\d\d after the dot, and is_valid_logline_timestamp rejects anything else. Such malformed timestamps also compare incorrectly with logline_timestamp_comparator, which relies on fixed-width fields.

Fix

Zero-pad the millisecond component to three digits:

return d.strftime('%m-%d %H:%M:%S.') + f'{ms:03d}'

Test

Added test_epoch_to_log_line_timestamp_pads_milliseconds covering millisecond remainders of 0, 5, 50 and 99, asserting both the exact output and that is_valid_logline_timestamp accepts it.

python -m pytest tests/mobly/logger_test.py passes (27 passed).

epoch_to_log_line_timestamp concatenated the milliseconds field with
str(), which drops leading zeros for values below 100. The resulting
timestamp (e.g. '07-21 20:51:02.5') does not match the canonical log
line timestamp format, which always uses three millisecond digits, and
fails is_valid_logline_timestamp. Zero-pad the milliseconds to three
digits and add a regression test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant