在工作日内消磨时间的最佳方法是什么?

时间:2020-03-05 18:38:28  来源:igfitidea点击:

我遇到这样的情况,我想在日期中增加小时数,并在工作日周围设置新的日期。我拼凑了一个函数来确定这个新日期,但想确保我不会忘记任何东西。

要添加的小时数称为"延迟"。它可以很容易地成为函数的参数。

请发表任何建议。 [VB.NET警告]

Private Function GetDateRequired() As Date
    ''// A decimal representation of the current hour
    Dim hours As Decimal = Decimal.Parse(Date.Now.Hour) + (Decimal.Parse(Date.Now.Minute) / 60.0) 

    Dim delay As Decimal = 3.0           ''// delay in hours
    Dim endOfDay As Decimal = 12.0 + 5.0 ''// end of day, in hours
    Dim startOfDay As Decimal = 8.0      ''// start of day, in hours

    Dim newHour As Integer
    Dim newMinute As Integer

    Dim dateRequired As Date = Now
    Dim delta As Decimal = hours + delay

    ''// Wrap around to the next day, if necessary
    If delta > endOfDay Then
        delta = delta - endOfDay
        dateRequired = dateRequired.AddDays(1)

        newHour = Integer.Parse(Decimal.Truncate(delta))
        newMinute = Integer.Parse(Decimal.Truncate((delta - newHour) * 60))
        newHour = startOfDay + newHour
    Else
        newHour = Integer.Parse(Decimal.Truncate(delta))
        newMinute = Integer.Parse(Decimal.Truncate((delta - newHour) * 60))
    End If

    dateRequired = New Date(dateRequired.Year, dateRequired.Month, dateRequired.Day, newHour, newMinute, 0)

    Return dateRequired
End Sub

注意:如果延迟时间超过9小时,则此方法可能无效。它永远不会从3更改为。

编辑:
目标是找到将当前时间增加几个小时后得到的日期和时间。这用于确定提交截止日期的默认值。我想在当前时间增加3个小时,以获取到期日时间。但是,我不希望截止日期超过当天的下午5点。因此,我尝试将时间分为(今天,直到下午5点)和(明天,从上午8点开始),这样将3小时加到4pm会给我们带来19 am,因为今天和2结束时增加了1小时小时将添加到明天开始。

解决方案

回答

我们可能应该针对可以想到的每种情况编写一些自动化测试,然后开始集思广益,并根据自己的想法编写测试。这样,我们可以确定它可以正常工作,并且在进行进一步更改后仍可以继续工作。如果我们喜欢结果,请查找"测试驱动开发"。

回答

好吧,这些呢?两种方法之间的差异应说明一切。

另外,就我所能进行的测试。保修期持续到现在。

希望能帮助到你!

Module Module1

    Public Function IsInBusinessHours(ByVal d As Date) As Boolean
        Return Not (d.Hour < 8 OrElse d.Hour > 17 OrElse d.DayOfWeek = DayOfWeek.Saturday OrElse d.DayOfWeek = DayOfWeek.Sunday)
    End Function

    Public Function AddInBusinessHours(ByVal fromDate As Date, ByVal hours As Integer) As Date
        Dim work As Date = fromDate.AddHours(hours)
        While Not IsInBusinessHours(work)
            work = work.AddHours(1)
        End While
        Return work
    End Function

    Public Function LoopInBusinessHours(ByVal fromDate As Date, ByVal hours As Integer) As Date
        Dim work As Date = fromDate
        While hours > 0
            While hours > 0 AndAlso IsInBusinessHours(work)
                work = work.AddHours(1)
                hours -= 1
            End While
            While Not IsInBusinessHours(work)
                work = work.AddHours(1)
            End While
        End While
        Return work
    End Function

    Sub Main()
        Dim test As Date = New Date(2008, 8, 8, 15, 0, 0)
        Dim hours As Integer = 5
        Console.WriteLine("Date: " + test.ToString() + ", " + hours.ToString())
        Console.WriteLine("Just skipping: " + AddInBusinessHours(test, hours))
        Console.WriteLine("Looping: " + LoopInBusinessHours(test, hours))
        Console.ReadLine()
    End Sub

End Module

回答

我已经使用以下公式(伪代码)取得了一些成功:

now <- number of minutes since the work day started
delay <- number of minutes in the delay
day <- length of a work day in minutes

x <- (now + delay) / day {integer division}
y <- (now + delay) % day {modulo remainder}

return startoftoday + x {in days} + y {in minutes}