为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
跃然一笑 2022-06-18 16:32:03
我有一个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;    }}
查看完整描述

1 回答

?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

时间跨度只给你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;


查看完整回答
反对 回复 2022-06-18
  • 1 回答
  • 0 关注
  • 142 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号