EDIT: according to the comment, the types really should be TimeSpan, not DateTime, and at that moment everything is simple.
When you ask what proportion of X belongs to Y, this is the main division, which is easily implemented on TimeSpan:
public static double Divide(TimeSpan dividend, TimeSpan divisor)
{
return (double) dividend.Ticks / (double) divisor.Ticks;
}
Code example:
using System;
using System.IO;
using System.Globalization;
using System.Linq;
class Test
{
static void Main()
{
TimeSpan x = new TimeSpan(0, 34, 23);
TimeSpan y = new TimeSpan(4, 12, 31);
Console.WriteLine(Divide(x, y));
}
public static double Divide(TimeSpan dividend, TimeSpan divisor)
{
return (double) dividend.Ticks / (double) divisor.Ticks;
}
}
source
share