简体   繁体   English

如何使用模算术计算时间跨度?

[英]How to calculate timespans using modular arithmetic?

This question is basically about representing modular arithmetic concept in code and using the modulo sign.这个问题基本上是关于在代码中表示模算术概念和使用模符号。

So recently I did a popup for SCCM installation, which should give user time to install an app, the client requuirements were to show the user a clock with the amount of time left to the installation (or let user install now as well).所以最近我做了一个 SCCM 安装的弹出窗口,它应该给用户时间来安装一个应用程序,客户端的要求是向用户显示一个时钟,上面有安装剩余的时间(或者让用户现在安装)。 The amount of time on the clock that they wanted was 24 hours, plus whatever time is left to 4pm when the 24 hours have passed so:他们想要的时钟上的时间是 24 小时,加上 24 小时过去后剩下的时间到下午 4 点,所以:

program runs at 13:00 then the clock should display 24 +3 = 27 hours程序在 13:00 运行,那么时钟应该显示 24 +3 = 27 小时

when 16:00 it should be 24+24 which is 48 hours 16:00 时应该是 24+24,也就是 48 小时

when 22:00 it should be 24 + 18 which is 42 hours 22:00 时应该是 24 + 18,即 42 小时

Now I noticed that:现在我注意到:

13 + 27 = 40 13 + 27 = 40

16 + 24 = 40 16 + 24 = 40

22 + 18 = 40 22 + 18 = 40

40 Modulo 24 = 16 40 模 24 = 16

So basically if I subtract the current time from 40 then I will be left with the difference:所以基本上,如果我从 40 中减去当前时间,那么我将留下差异:

40 - 13 = 27 40 - 13 = 27

40 - 16 = 24 40 - 16 = 24

40 - 22 = 18 40 - 22 = 18

So what I did is this:所以我所做的是这样的:

//I need to make a timespan object which has 24 hours from current time + time left to the next 4pm

//The context is time to install, which user should see
Timespan TimeToInstall = new Timespan(23,59,59)

DateTime Now = DateTime.Now;
if (Now.Hour < 16)
{
    long TimeTo4 = (new TimeSpan(40, 0, 0).Ticks - Now.TimeOfDay.Ticks);
    TimeToInstall = TimeSpan.FromTicks(TimeTo4);
}
else
{

long TimeTo4 = (new TimeSpan(40, 0, 0).Ticks - Now.TimeOfDay.Ticks) + TimeGiven.Ticks;
TimeToInstall = TimeSpan.FromTicks(TimeTo4);

}

The problem with the above solution is that I know that it can be shorter, because when I subtract times times before 4 pm then I don't need to add 24 hours, if the run time is higher than or equal 4 pm then I need to add 24 hours.上述解决方案的问题是我知道它可以更短,因为当我在下午 4 点之前减去时间时,我不需要添加 24 小时,如果运行时间高于或等于下午 4 点,那么我需要添加 24 小时。 I have a rough idea how to refactor this in mathematics/pseudocode for example:我有一个粗略的想法如何在数学/伪代码中重构它,例如:

absolute(16-13) modulo 24 = 3绝对 (16-13) 模 24 = 3

absolute(16-16) modulo 24 = 0 (24)绝对 (16-16) 模 24 = 0 (24)

absolute(16-22) modulo 24 = 18绝对 (16-22) 模 24 = 18

The question is how to do it in C# code to refactor this code?问题是如何在C#代码中重构这段代码? Please use any language you like, but I would be greatfull for C# example.请使用您喜欢的任何语言,但我会非常喜欢 C# 示例。 Thank you guys谢谢你们

If you're looking for a shorter solution, how about this?如果您正在寻找更短的解决方案,这个怎么样?

public static int HoursUntilDueTime(DateTime time)
{
    DateTime dueTime = (time + TimeSpan.FromHours(8)).Date + TimeSpan.FromHours(24 + 16);
    return (int)(0.5 + (dueTime - time).TotalHours);
}

Or if you want to pass in the target time (date part is ignored):或者如果你想传入目标时间(日期部分被忽略):

public static int HoursUntilDueTime(DateTime currentTime, DateTime targetTime)
{
    DateTime dueTime = (currentTime + TimeSpan.FromHours(24 - targetTime.Hour)).Date + TimeSpan.FromHours(24 + targetTime.Hour);
    return (int)(0.5 + (dueTime - currentTime).TotalHours);
}

Test code:测试代码:

var targetTime = new DateTime(2000, 1, 1, 16, 00, 00);
Console.WriteLine(HoursUntilDueTime(new DateTime(2016, 1, 1, 13, 00, 00), targetTime));
Console.WriteLine(HoursUntilDueTime(new DateTime(2016, 1, 1, 16, 00, 00), targetTime));
Console.WriteLine(HoursUntilDueTime(new DateTime(2016, 1, 1, 22, 00, 00), targetTime));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM