Skip to content

feat: enhanced Unix timestamp detection with validation and edge case handling#1302

Open
suatkocar wants to merge 2 commits intopilotmoon:masterfrom
suatkocar:fix/unix-time-milliseconds-and-date-parsing
Open

feat: enhanced Unix timestamp detection with validation and edge case handling#1302
suatkocar wants to merge 2 commits intopilotmoon:masterfrom
suatkocar:fix/unix-time-milliseconds-and-date-parsing

Conversation

@suatkocar
Copy link

@suatkocar suatkocar commented Nov 4, 2025

Problem

  • Timestamps in milliseconds (like 1762265258239) were incorrectly parsed as seconds, causing wrong date conversions (e.g., showing year 57813 instead of 2025)
  • Ambiguous 11-12 digit timestamps (e.g., 762265258239) were incorrectly interpreted, leading to dates like year 26125
  • Results were not being copied to clipboard
  • No support for microseconds and nanoseconds timestamps

Solution

Enhanced timestamp detection algorithm based on industry standards (epochconverter.com, unixtimestamp.com):

  • Auto-detect timestamp format using digit count and validation:

    • ≤10 digits: seconds (Unix standard)
    • 11-12 digits: validation with date range check (1970-2300) to determine seconds vs milliseconds
    • 13 digits: milliseconds (JavaScript/MongoDB standard)
    • 14-15 digits: validation to determine milliseconds vs microseconds
    • 16 digits: microseconds
    • 19+ digits: nanoseconds
  • Smart validation for ambiguous cases:

    • Tests both interpretations (seconds/milliseconds or milliseconds/microseconds)
    • Validates date ranges (1970-2300) to prevent false positives
    • Uses magnitude heuristic for 11-12 digit timestamps (threshold: 1e10)
    • Always treats negative timestamps as seconds (pre-1970 dates)
  • Copy result to clipboard in addition to showing it

  • Error handling for invalid inputs

  • Addresses known pitfalls from naive approaches (see Pydantic issue #7940)

Testing

✅ Seconds timestamp: 15354387292018-08-28 06:45:29 UTC
✅ Milliseconds timestamp: 17622652582392025-11-04 14:07:32 UTC
✅ Ambiguous 12-digit: 7622652582391994-02-26 12:20:58 UTC (correctly detected as milliseconds)
✅ Negative timestamp: -11969-12-31 23:59:59 UTC
✅ Microseconds: 9466848000000002000-01-01 00:00:00 UTC
✅ Nanoseconds: 9466848000000000002000-01-01 00:00:00 UTC

Comprehensive testing: 61 edge cases covering epoch boundaries, negative timestamps, year boundaries, ambiguous digit counts, and real-world examples - all passing ✅

- Auto-detect if timestamp is in seconds or milliseconds (>= 1e12 = milliseconds)
- Add support for parsing date strings (including extended year format with +)
- Fix copy functionality - now copies result to clipboard
- Add error handling for invalid inputs
- Add comprehensive validation for ambiguous 11-12 digit timestamps (seconds vs milliseconds)
- Add validation for ambiguous 14-15 digit timestamps (milliseconds vs microseconds)
- Extend date range validation to 1970-2300 to handle edge cases
- Implement magnitude heuristic for 11-12 digit timestamps (threshold: 1e10)
- Handle negative timestamps correctly (always treat as seconds)
- Support microseconds (16 digits) and nanoseconds (19+ digits)
- Based on industry standards (epochconverter.com, unixtimestamp.com)
- Addresses known pitfalls from naive approaches (Pydantic issue #7940)

Tested with 61 edge cases covering:
- Epoch boundaries, negative timestamps, year boundaries
- Ambiguous digit counts, real-world examples
- All tests passing
@suatkocar suatkocar changed the title fix: support milliseconds timestamps and date string parsing feat: enhanced Unix timestamp detection with validation and edge case handling Nov 4, 2025
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