package sync import ( "sub-cli/internal/model" ) // scaleTimeline scales a timeline to match a different number of entries func scaleTimeline(timeline []model.Timestamp, targetCount int) []model.Timestamp { if targetCount <= 0 || len(timeline) == 0 { return []model.Timestamp{} } result := make([]model.Timestamp, targetCount) if targetCount == 1 { result[0] = timeline[0] return result } sourceLength := len(timeline) // Handle simple case: same length if targetCount == sourceLength { copy(result, timeline) return result } // Handle case where target is longer than source // We need to interpolate timestamps between source entries for i := 0; i < targetCount; i++ { if sourceLength == 1 { // If source has only one entry, use it for all target entries result[i] = timeline[0] continue } // Calculate a floating-point position in the source timeline floatIndex := float64(i) * float64(sourceLength-1) / float64(targetCount-1) lowerIndex := int(floatIndex) upperIndex := lowerIndex + 1 // Handle boundary case if upperIndex >= sourceLength { upperIndex = sourceLength - 1 lowerIndex = upperIndex - 1 } // If indices are the same, just use the source timestamp if lowerIndex == upperIndex || lowerIndex < 0 { result[i] = timeline[upperIndex] } else { // Calculate the fraction between the lower and upper indices fraction := floatIndex - float64(lowerIndex) // Convert timestamps to milliseconds for interpolation lowerMS := timeline[lowerIndex].Hours*3600000 + timeline[lowerIndex].Minutes*60000 + timeline[lowerIndex].Seconds*1000 + timeline[lowerIndex].Milliseconds upperMS := timeline[upperIndex].Hours*3600000 + timeline[upperIndex].Minutes*60000 + timeline[upperIndex].Seconds*1000 + timeline[upperIndex].Milliseconds // Interpolate resultMS := int(float64(lowerMS) + fraction*float64(upperMS-lowerMS)) // Convert back to timestamp hours := resultMS / 3600000 resultMS %= 3600000 minutes := resultMS / 60000 resultMS %= 60000 seconds := resultMS / 1000 milliseconds := resultMS % 1000 result[i] = model.Timestamp{ Hours: hours, Minutes: minutes, Seconds: seconds, Milliseconds: milliseconds, } } } return result } // calculateDuration calculates the time difference between two timestamps func calculateDuration(start, end model.Timestamp) model.Timestamp { startMillis := start.Hours*3600000 + start.Minutes*60000 + start.Seconds*1000 + start.Milliseconds endMillis := end.Hours*3600000 + end.Minutes*60000 + end.Seconds*1000 + end.Milliseconds durationMillis := endMillis - startMillis if durationMillis < 0 { // Return zero duration if end is before start return model.Timestamp{ Hours: 0, Minutes: 0, Seconds: 0, Milliseconds: 0, } } hours := durationMillis / 3600000 durationMillis %= 3600000 minutes := durationMillis / 60000 durationMillis %= 60000 seconds := durationMillis / 1000 milliseconds := durationMillis % 1000 return model.Timestamp{ Hours: hours, Minutes: minutes, Seconds: seconds, Milliseconds: milliseconds, } } // addDuration adds a duration to a timestamp func addDuration(start, duration model.Timestamp) model.Timestamp { startMillis := start.Hours*3600000 + start.Minutes*60000 + start.Seconds*1000 + start.Milliseconds durationMillis := duration.Hours*3600000 + duration.Minutes*60000 + duration.Seconds*1000 + duration.Milliseconds totalMillis := startMillis + durationMillis hours := totalMillis / 3600000 totalMillis %= 3600000 minutes := totalMillis / 60000 totalMillis %= 60000 seconds := totalMillis / 1000 milliseconds := totalMillis % 1000 return model.Timestamp{ Hours: hours, Minutes: minutes, Seconds: seconds, Milliseconds: milliseconds, } }