0

I have a use case that needs to determine if a task should be queued until a later date or submitted for processing immediately.

The logic for this is based upon a business "open/close" hours. The open/close hours are specified in their local time zone: Ex: Open 8:00 AM, Close 5:00 PM. Because 8:00 AM is a relative time, dependent on the specific day, their local timezone is stored separately.

Tasks are logged with a created property in the server time UTC. I then calculate an offset in number of hours by iterating through the number of hours in a week. Before this, I convert the server time from UTC to the clients TZ (so the comparison is done in local client time)

If the offset is 0 I can run the task, if the offset is > 0 I offset the server utc time by the number of hours returned. Is there a better way of doing this? This seems like a common problem that must have some better solution.

 Private Function CalcDelay() As Integer
    Dim d = _localTime '// Client local time currently
    Dim n = d.DayOfWeek()
    Dim now = d.Hour() & "." & d.Minute()
    Dim delayHours As Integer = 0
    Dim _day
    For i = 0 To 168
        d = _localTime.AddHours(i)
        n = d.DayOfWeek()
        now = d.Hour() & "." & d.Minute()
        _day = _bh.Hours(n)
        If (ConvertTime(now) >= ConvertTime(_bh.Hours(n).Open1c) And ConvertTime(now) < ConvertTime(_bh.Hours(n).Close1c)) _
            Or (ConvertTime(now) >= ConvertTime(_bh.Hours(n).Open2c) And ConvertTime(now) < ConvertTime(_bh.Hours(n).Close2c)) Then
            delayHours = i
            Exit For
        End If
    Next
    Return delayHours
End Function

ConvertTime will just grab the customers open/close times represented as HH.MM (08.00, 15.00) etc

DDulla
  • 659
  • 9
  • 20
  • 1
    The DateDiff function will save you a lot of grief. – SezMe May 29 '23 at 18:23
  • Incidentally, in that if statement, you should change each `And` to `AndAlso`, and the `Or` to `OrElse`: [What is the difference between And and AndAlso in VB.NET?](https://stackoverflow.com/q/302047/1115360) Oh, and should the loop run to 168 or to 167 - isn't +168 the first hour of the next week? – Andrew Morton May 30 '23 at 19:06

0 Answers0