计算可配置时间之间的分钟数
Calculate number of minutes between configurable hours
我有一个 Journey
object,它有一个 DateTime JourneyStartTime { get; set; }
和一个 DateTime JourneyEndTime { get; set; }
。我想计算晚上 10 点到早上 6 点之间的旅程总分钟数(注意这超过了午夜)。
我已经尝试使用 TimeSpans 来指示晚上 10 点和早上 6 点,但我不确定这是否是最好的数据类型。
此逻辑的域是基于保险的。 X 公司想要对在 X - Y 小时内开车的司机进行评分。这些时间应该是可配置的。这是一个场景:
A journey takes place on the same day between 5pm and 6pm. Company X Inurance is interested in journeys between 10pm and 6am. How many minutes did that journey spend in the time period that Company X is interested in?
上面的答案是:0,但我的代码给了 60 分钟(这里是 dotnetFiddle)。
这是代码。
代码
using System;
public class Program
{
public static void Main()
{
var shortSameDayJourney = new Journey {
JourneyId = 1,
// start of journey - 5pm - start
JourneyStartTime = new DateTime(2018, 12, 17, 17, 00, 00, DateTimeKind.Utc),
// end of journey - 6pm - end
JourneyEndTime = new DateTime(2018, 12, 17, 18, 00, 00, DateTimeKind.Utc)
};
var scoreTimePeriod = new InsurerTimePeriodScoreSetting {
// start of insurer's time period.
StartOfTimePeriod = DateTime.Now + TimeSpan.FromHours(22),
// end of insurer's time period.
EndOfTimePeriod = DateTime.Now + TimeSpan.FromHours(30)
};
var minutesInTimePeriod = getNumberOfMinutesThatJourneyWasInTimePeriod(shortSameDayJourney, scoreTimePeriod);
Console.WriteLine("Number of minutes the journey was within the time period the insurer had sepcified:");
Console.WriteLine(minutesInTimePeriod + " minutes");
}
public static double getNumberOfMinutesThatJourneyWasInTimePeriod(
Journey journey,
InsurerTimePeriodScoreSetting insurerTimePeriod) {
var JourneyStart = journey.JourneyStartTime;
var JourneyEnd = journey.JourneyEndTime;
var timeSpan = insurerTimePeriod.EndOfTimePeriod - insurerTimePeriod.StartOfTimePeriod;
var startDif = (JourneyStart - insurerTimePeriod.StartOfTimePeriod);
var endDif = (insurerTimePeriod.EndOfTimePeriod - JourneyEnd);
var time = timeSpan - startDif - endDif;
return time.TotalMinutes;
}
}
public class Journey {
public int JourneyId {get;set;}
// journey start date and time in UTC, comes form a tracking device on vehicle.
public DateTime JourneyStartTime {get;set;}
// journey end date and time in UTC, comes form a tracking device on vehicle.
public DateTime JourneyEndTime {get;set;}
}
public class InsurerTimePeriodScoreSetting {
public DateTime StartOfTimePeriod {get;set;}
public DateTime EndOfTimePeriod {get;set;}
}
Timespan 只给出 2 DateTime's
之间的原始时间
所以我不得不更改你的 Journey
初始化,这样我就可以在同一天进行比较
var shortSameDayJourney = new Journey
{
JourneyId = 1,
// start of journey - 5pm - start
JourneyStartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 17, 00, 00, DateTimeKind.Utc),
// end of journey - 6pm - end
JourneyEndTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 18, 00, 00, DateTimeKind.Utc)
};
InsurerTimePeriodScoreSetting
相同
var scoreTimePeriod = new InsurerTimePeriodScoreSetting
{
// start of insurer's time period. 18/12 22:00
StartOfTimePeriod = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 22, 0, 0, DateTimeKind.Utc), // DateTime.Now + TimeSpan.FromHours(22),
// end of insurer's time period. 19/12 6:00
EndOfTimePeriod = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + 1, 6, 0, 0, DateTimeKind.Utc) // DateTime.Now + TimeSpan.FromHours(30)
};
现在您需要做的只是一个简单的检查 - 旅程时间是否在 InsurerTimePeriodScoreSetting
之间
if (JourneyStart >= insurerTimePeriod.StartOfTimePeriod && JourneyEnd <= insurerTimePeriod.EndOfTimePeriod)
{
// your same calculation here
}
else
return 0;
我有一个 Journey
object,它有一个 DateTime JourneyStartTime { get; set; }
和一个 DateTime JourneyEndTime { get; set; }
。我想计算晚上 10 点到早上 6 点之间的旅程总分钟数(注意这超过了午夜)。
我已经尝试使用 TimeSpans 来指示晚上 10 点和早上 6 点,但我不确定这是否是最好的数据类型。
此逻辑的域是基于保险的。 X 公司想要对在 X - Y 小时内开车的司机进行评分。这些时间应该是可配置的。这是一个场景:
A journey takes place on the same day between 5pm and 6pm. Company X Inurance is interested in journeys between 10pm and 6am. How many minutes did that journey spend in the time period that Company X is interested in?
上面的答案是:0,但我的代码给了 60 分钟(这里是 dotnetFiddle)。
这是代码。
代码
using System;
public class Program
{
public static void Main()
{
var shortSameDayJourney = new Journey {
JourneyId = 1,
// start of journey - 5pm - start
JourneyStartTime = new DateTime(2018, 12, 17, 17, 00, 00, DateTimeKind.Utc),
// end of journey - 6pm - end
JourneyEndTime = new DateTime(2018, 12, 17, 18, 00, 00, DateTimeKind.Utc)
};
var scoreTimePeriod = new InsurerTimePeriodScoreSetting {
// start of insurer's time period.
StartOfTimePeriod = DateTime.Now + TimeSpan.FromHours(22),
// end of insurer's time period.
EndOfTimePeriod = DateTime.Now + TimeSpan.FromHours(30)
};
var minutesInTimePeriod = getNumberOfMinutesThatJourneyWasInTimePeriod(shortSameDayJourney, scoreTimePeriod);
Console.WriteLine("Number of minutes the journey was within the time period the insurer had sepcified:");
Console.WriteLine(minutesInTimePeriod + " minutes");
}
public static double getNumberOfMinutesThatJourneyWasInTimePeriod(
Journey journey,
InsurerTimePeriodScoreSetting insurerTimePeriod) {
var JourneyStart = journey.JourneyStartTime;
var JourneyEnd = journey.JourneyEndTime;
var timeSpan = insurerTimePeriod.EndOfTimePeriod - insurerTimePeriod.StartOfTimePeriod;
var startDif = (JourneyStart - insurerTimePeriod.StartOfTimePeriod);
var endDif = (insurerTimePeriod.EndOfTimePeriod - JourneyEnd);
var time = timeSpan - startDif - endDif;
return time.TotalMinutes;
}
}
public class Journey {
public int JourneyId {get;set;}
// journey start date and time in UTC, comes form a tracking device on vehicle.
public DateTime JourneyStartTime {get;set;}
// journey end date and time in UTC, comes form a tracking device on vehicle.
public DateTime JourneyEndTime {get;set;}
}
public class InsurerTimePeriodScoreSetting {
public DateTime StartOfTimePeriod {get;set;}
public DateTime EndOfTimePeriod {get;set;}
}
Timespan 只给出 2 DateTime's
之间的原始时间
所以我不得不更改你的 Journey
初始化,这样我就可以在同一天进行比较
var shortSameDayJourney = new Journey
{
JourneyId = 1,
// start of journey - 5pm - start
JourneyStartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 17, 00, 00, DateTimeKind.Utc),
// end of journey - 6pm - end
JourneyEndTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 18, 00, 00, DateTimeKind.Utc)
};
InsurerTimePeriodScoreSetting
var scoreTimePeriod = new InsurerTimePeriodScoreSetting
{
// start of insurer's time period. 18/12 22:00
StartOfTimePeriod = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 22, 0, 0, DateTimeKind.Utc), // DateTime.Now + TimeSpan.FromHours(22),
// end of insurer's time period. 19/12 6:00
EndOfTimePeriod = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + 1, 6, 0, 0, DateTimeKind.Utc) // DateTime.Now + TimeSpan.FromHours(30)
};
现在您需要做的只是一个简单的检查 - 旅程时间是否在 InsurerTimePeriodScoreSetting
if (JourneyStart >= insurerTimePeriod.StartOfTimePeriod && JourneyEnd <= insurerTimePeriod.EndOfTimePeriod)
{
// your same calculation here
}
else
return 0;