rayhunter icon indicating copy to clipboard operation
rayhunter copied to clipboard

Add time synchronization feature for devices without cellular connectivity

Open i-am-logger opened this issue 1 month ago • 2 comments

Summary

This PR implements a time delta/offset approach to fix incorrect timestamps on devices that cannot sync via NITZ (e.g., devices without active SIM cards). Instead of modifying the system clock (which requires root and may fail), we store a time offset in memory and apply it to all rayhunter-generated timestamps.

Addresses #121

Implementation Details

Backend

  • daemon/src/time_correction.rs: TimeCorrection module that stores offset in seconds and provides sync/get methods
  • API endpoints:
    • GET /api/time-correction - Get current offset and last sync time
    • POST /api/time-correction/sync - Sync time from browser timestamp
  • Manifest timestamps: Applied via apply_time_correction() in stats.rs
  • PCAP timestamps: Applied via write_gsmtap_message_with_datetime() in pcap.rs

Frontend

  • TimeSyncCard component: UI component for syncing time from browser
  • Displays current offset in human-readable format (hours/minutes/seconds)
  • Shows last sync timestamp
  • One-click sync button

How it works

  1. Browser sends current timestamp to /api/time-correction/sync
  2. Backend calculates offset: browser_time - system_time
  3. Offset stored in ServerState (in-memory, no persistence)
  4. All timestamps (manifest + PCAP) corrected when served via API
  5. Frontend displays current offset and allows re-sync

Benefits

  • ✅ No system clock modification (100% safe, no root required)
  • ✅ Works on locked-down devices where date -s fails
  • ✅ Timestamps remain consistent (no sudden jumps in files)
  • ✅ Easy to test on development machines

Testing

  • ✅ All 15 daemon unit tests passing (added 5 new tests)
  • ✅ Manually tested API endpoints
  • ✅ Verified manifest timestamps are corrected
  • ✅ Built successfully without warnings

Note on QMDL Files

QMDL files remain unmodified as they are raw binary diagnostic logs that should preserve data integrity. Only generated/served content (manifest entries, PCAP files) have corrected timestamps.

Open Question for Maintainers

Should we store the offset per-recording in the manifest to enable proper timestamp correlation between QMDL and PCAP files?

Currently, all recordings use the current global offset, which means:

  • If offset changes over time, old recordings will show different timestamps when re-downloaded
  • QMDL files have original timestamps while PCAP files have corrected timestamps
  • There's no easy way to know what offset was applied to a specific PCAP file

Potential solutions:

  1. Store offset_seconds in each ManifestEntry (preserves the offset used at recording time)
  2. Include offset metadata in PCAP file comments
  3. Keep current approach (simpler, assumes system clock was wrong the whole time)

Would appreciate guidance on which approach aligns better with the project goals.

Additional Commit

This PR also includes a devenv.nix setup commit for a reproducible development environment (similar to the PDS project structure). This is separate from the feature and can be reviewed independently or split into a separate PR if preferred.

🤖 Generated with Claude Code

i-am-logger avatar Nov 30 '25 21:11 i-am-logger

@i-am-logger gave it a quick look, code looks okay. here's three requests:

  • please remove the devenv stuff, it has nothign to do with this PR
  • if you are going to let agents review other agent's output, please do so on a fork rather than this PR. copilot reviewing cursor reviewing claude will generate notification emails for everybody.
  • some (very few) of the changes don't seem necessary, such as changing pcap.rs

untitaker avatar Nov 30 '25 21:11 untitaker

@untitaker, I removed the devenv commit and fixed copilot comments, sorry didn't realize it will auto-trigger.

The pcap.rs changes apply time correction to downloaded PCAP/ZIP files so they match the corrected timestamps shown in the UI. QMDL files stay raw since changing timestamps there would break checksums. Happy to remove the PCAP changes if you prefer, though it's an open question whether users need corrected timestamps in downloaded files.

i-am-logger avatar Dec 01 '25 00:12 i-am-logger