计算可配置小时之间的分钟数

我有一个Journey具有 aDateTime JourneyStartTime { get; set; }和 a的对象DateTime JourneyEndTime { get; set; }。我想计算旅程在晚上 10 点到早上 6 点之间花费的总分钟数(注意这个时间超过了午夜)。


我尝试使用 TimeSpans 来指示晚上 10 点和早上 6 点,但我不确定这是否是最好的数据类型。


此逻辑的域是基于保险的。X 公司想要对在 X - Y 小时之间驾驶的司机进行评分。这些时间应该是可配置的。这是一个场景:


同一天下午 5 点到 6 点之间进行旅程。公司 X Inurance 对晚上 10 点到早上 6 点之间的旅程感兴趣。该旅程在 X 公司感兴趣的时间段内花费了多少分钟?


上面的答案是: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;

    }

}


跃然一笑
浏览 144回答 1
1回答

一只甜甜圈

时间跨度只给你2之间的原始时间,DateTime's 所以我不得不改变你的Journey初始化,这样我就可以在同一天进行比较&nbsp; &nbsp;var shortSameDayJourney = new Journey&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;JourneyId = 1,&nbsp; &nbsp; &nbsp; &nbsp;// start of journey - 5pm - start&nbsp; &nbsp; &nbsp; &nbsp;JourneyStartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 17, 00, 00, DateTimeKind.Utc),&nbsp; &nbsp; &nbsp; &nbsp;// end of journey - 6pm - end&nbsp; &nbsp; &nbsp; &nbsp;JourneyEndTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 18, 00, 00, DateTimeKind.Utc)&nbsp; &nbsp; };&nbsp;&nbsp;同样的 InsurerTimePeriodScoreSetting&nbsp;var scoreTimePeriod = new InsurerTimePeriodScoreSetting&nbsp;{&nbsp; &nbsp; &nbsp;// start of insurer's time period. 18/12 22:00&nbsp; &nbsp; &nbsp; StartOfTimePeriod = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 22, 0, 0, DateTimeKind.Utc),&nbsp; &nbsp;// DateTime.Now + TimeSpan.FromHours(22),&nbsp; &nbsp; &nbsp;// end of insurer's time period. 19/12 6:00&nbsp; &nbsp; &nbsp;EndOfTimePeriod = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + 1, 6, 0, 0, DateTimeKind.Utc)&nbsp; // DateTime.Now + TimeSpan.FromHours(30)&nbsp;};现在您需要做的只是一个简单的检查 - 旅程时间是否介于InsurerTimePeriodScoreSettingif (JourneyStart >= insurerTimePeriod.StartOfTimePeriod && JourneyEnd <= insurerTimePeriod.EndOfTimePeriod){// your same calculation here}else&nbsp; &nbsp;return 0;
打开App,查看更多内容
随时随地看视频慕课网APP