Skip to content

parse timezone offsets in space-separated log timestamps - #32

Draft
HrachShah wants to merge 4 commits into
mainfrom
patch/parse-space-timestamp-offsets
Draft

parse timezone offsets in space-separated log timestamps#32
HrachShah wants to merge 4 commits into
mainfrom
patch/parse-space-timestamp-offsets

Conversation

@HrachShah

@HrachShah HrachShah commented Jul 31, 2026

Copy link
Copy Markdown
Owner

What does this change do?

Parse timezone offsets when log timestamps use a space between the date and time, including fractional seconds. The CLI now aligns aware and naive timestamps before applying time filters.

Why is this needed?

The timestamp matcher accepted these offsets, but the parser did not, and the CLI could compare an aware log timestamp with a naive filter boundary.

Testing

PYTHONPATH=src pytest -q tests/test_utils.py tests/test_parsers.py tests/test_analyzer.py tests/test_cli.py

50 passed.

Drafted with Zo Bot; reviewed by the repository owner.

Summary by Sourcery

Support timezone-aware and mixed-awareness timestamp handling in CLI time filtering and extend timestamp parsing for additional log formats.

New Features:

  • Allow timestamps with space-separated timezone offsets, including fractional seconds, to be parsed from log lines.
  • Align aware and naive timestamps in CLI time filtering to consistently apply start and end time boundaries.

Bug Fixes:

  • Ensure log entries without parseable timestamps are excluded when time filters are applied instead of bypassing the boundaries.

Tests:

  • Add unit tests covering mixed aware/naive timestamp comparisons, additional timestamp formats, and behavior when timestamps are missing.

@sourcery-ai

sourcery-ai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Reviewer's Guide

This PR extends timestamp parsing to handle timezone offsets in space-separated log timestamps (including fractional seconds) and updates CLI time filtering logic to correctly compare aware and naive timestamps, with new tests covering these behaviors.

Sequence diagram for CLI timestamp parsing and timezone alignment

sequenceDiagram
    actor User
    participant CLI
    participant parse_timestamp

    User->>CLI: run with start_time, end_time
    CLI->>CLI: read log line
    CLI->>parse_timestamp: parse_timestamp(line)
    parse_timestamp-->>CLI: timestamp

    alt timestamp is not None and timestamp.tzinfo is not None
        opt start_time is not None and start_time.tzinfo is None
            CLI->>CLI: start_time.replace(tzinfo=timestamp.tzinfo)
        end
        opt end_time is not None and end_time.tzinfo is None
            CLI->>CLI: end_time.replace(tzinfo=timestamp.tzinfo)
        end
    else timestamp is not None and timestamp.tzinfo is None
        opt start_time is not None and start_time.tzinfo is not None
            CLI->>CLI: timestamp.replace(tzinfo=start_time.tzinfo)
        end
        opt end_time is not None and end_time.tzinfo is not None
            CLI->>CLI: timestamp.replace(tzinfo=end_time.tzinfo)
        end
    end

    alt start_time is not None and (timestamp is None or timestamp < start_time)
        CLI->>CLI: skip line (before start_time)
    else end_time is not None and (timestamp is None or timestamp > end_time)
        CLI->>CLI: skip line (after end_time)
    else
        CLI->>CLI: parse and analyze line
    end
Loading

File-Level Changes

Change Details Files
Align timezone awareness between parsed log timestamps and CLI start/end time filters before comparison.
  • Wrap timestamp handling in _parse_file with logic that, when a parsed timestamp is aware, coerces naive start_time/end_time to that timestamp’s tzinfo.
  • When a parsed timestamp is naive but start_time or end_time is aware, assign the boundary’s tzinfo to the timestamp to make it comparable.
  • Adjust start_time and end_time filter conditions to treat missing timestamps as outside the filtered range.
src/log_analyzer_cli/cli.py
Expand datetime parsing to accept space-separated ISO-like timestamps with timezone offsets and refine parsing flow.
  • Add formats for space-separated timestamps with fractional seconds and timezone offsets, and for both space- and T-separated aware timestamps.
  • Reorder format list to attempt aware formats (with %z) first, then naive formats, to ensure correct tzinfo handling.
  • Change _try_parse_datetime to capture parsed datetime in a local variable before returning, making the try/except structure clearer.
src/log_analyzer_cli/utils.py
Add tests verifying timestamp parsing and filtering behavior for mixed awareness, additional date formats, and entries without timestamps.
  • Add tests to ensure filter_lines correctly handles combinations of naive and aware timestamps and boundaries.
  • Add tests to confirm parse_timestamp accepts slash-separated ISO-like timestamps and space-separated timestamps with timezone offsets (including fractional seconds).
  • Add a test ensuring entries without timestamps are excluded when time filters are applied.
tests/test_utils.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 40c15121-17fd-451c-8dde-b50811956e3c

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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