打包工作日的最佳方式是什么?

我有一种情况,我想添加几个小时到一个日期,并将新的日期包裹在工作日。我拼凑了一个函数来确定这个新的日期,但是要确保我没有忘记任何东西。

要添加的小时数称为“延迟”。它可能很容易成为函数的一个参数。

请发布任何建议。 [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

Note: This will probably not work if delay is more than 9 hours long. It should never change from 3, through.

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

0

3 答案

您应该为每个可以想到的情况编写一些自动化测试,然后开始进行更多头脑风暴,按照您的想法编写测试。这样,你可以确定它会起作用,并且如果你做进一步的改变将会继续工作。如果您喜欢结果,请查看测试驱动开发。

0
额外
这不是答案,但它是一个upvote。
额外 作者 Giulio Caccin,

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

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}
0
额外
我正在寻找一种方法来解决这个问题的模数运算。很酷的东西。
额外 作者 EndangeredMassa,

好的,这些怎么样?方法之间的差异应该说明一切。

此外,这是经过测试,尽可能我可以扔它。保修持续到...现在。

希望能帮助到你!

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
0
额外