mirror of
https://github.com/cjfranko/NTP-Timeturner.git
synced 2025-11-08 18:32:02 +00:00
fix: prevent float precision loss in timecode calculation
Co-authored-by: aider (gemini/gemini-2.5-pro-preview-05-06) <aider@aider.chat>
This commit is contained in:
parent
a8aaf861ef
commit
30bcfe61d7
1 changed files with 11 additions and 13 deletions
|
|
@ -39,19 +39,17 @@ pub fn ntp_service_toggle(start: bool) {
|
||||||
|
|
||||||
pub fn calculate_target_time(frame: &LtcFrame, config: &Config) -> DateTime<Local> {
|
pub fn calculate_target_time(frame: &LtcFrame, config: &Config) -> DateTime<Local> {
|
||||||
let today_local = Local::now().date_naive();
|
let today_local = Local::now().date_naive();
|
||||||
// Calculate total milliseconds including fractional frames
|
|
||||||
let total_ms = ((frame.hours as f64 * 3600.0 +
|
// Calculate millisecond component from frames with high precision, keeping H:M:S as integers.
|
||||||
frame.minutes as f64 * 60.0 +
|
let ms_from_frames = (frame.frames as f64 / frame.frame_rate * 1000.0).round() as u32;
|
||||||
frame.seconds as f64 +
|
|
||||||
frame.frames as f64 / frame.frame_rate) * 1000.0).round() as u32;
|
let timecode = NaiveTime::from_hms_milli_opt(
|
||||||
|
frame.hours,
|
||||||
let seconds = total_ms / 1000 % 60;
|
frame.minutes,
|
||||||
let minutes = (total_ms / 60000) % 60;
|
frame.seconds,
|
||||||
let hours = (total_ms / 3600000) % 24;
|
ms_from_frames,
|
||||||
let ms_component = total_ms % 1000;
|
)
|
||||||
|
.expect("Invalid LTC timecode");
|
||||||
let timecode = NaiveTime::from_hms_milli_opt(hours, minutes, seconds, ms_component)
|
|
||||||
.expect("Invalid LTC timecode");
|
|
||||||
|
|
||||||
let naive_dt = today_local.and_time(timecode);
|
let naive_dt = today_local.and_time(timecode);
|
||||||
let mut dt_local = Local
|
let mut dt_local = Local
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue