• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

Need a function, very hard task.


Hello, Experts!
I have
Run time:
7:00am – 9:00pm


Off time array:
Array_offtime_start(0) = 8:00am
Array_offtime_end(0) = 8:10am
…………………………………
Array_offtime_start(n) = 11:30am
Array_offtime_end(n) = 11:45am

Need a function to check if off time is inside or touching somehow my run time.  It can be only one element in off time array, can be none or can be n elements.

If run time IS inside of my runtime, some how I have to get back array or something with all my timeframes.

Thanks a lot for any help.
0
bogdem
Asked:
bogdem
  • 12
  • 7
  • 6
1 Solution
 
ctm5Commented:
I don't think it will all that hard.

You can use the DateDiff function to find out if the offtime_start and the offtime_end fall within the Run Time.

Dim withinstart as Long= DateDiff(runtime_start,array_offtime_start(0))
Dim withinend as Long= DateDiff(array_offtime_end(0), runtime_end)

If withinstart > -1 And withinend > -1 Then MsgBox("Break is within run time.")


Not sure what you mean by getting back an array of all the timeframes....

ctm5
0
 
ctm5Commented:
Oops...
You need to use the interval with DateDiff:

DateDiff(DateInterval.Minute, runtime_start,....)
0
 
mydasxCommented:
ok real quick...

you have 2 arrays for offtime a startingtime array and an ending time array is that accurate?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
mydasxCommented:
if this is true position n in array start and position n in array end will reprent a time span?
0
 
bogdemAuthor Commented:
In this example
Let say I have one run time
7:00am – 9:00pm

and two off time
Array_offtime_start(0) = 8:00am
Array_offtime_end(0) = 8:10am

Array_offtime_start(1) = 11:30am
Array_offtime_end(1) = 11:45am

I wish to have two arrays with all my timeframes:
Array_timeframe_start(0) = 7:00am
Array_timeframe_end(0) = 8:00am

Array_timeframe_start (1) = 8:00am
Array_timeframe_end (1) = 8:10am

Array_timeframe_start(2) = 8:10am
Array_timeframe_end(2) = 11:30am

Array_timeframe_start(3) = 11:30am
Array_timeframe_end(3) = 11:45am

Array_timeframe_start(4) = 11:45am
Array_timeframe_end(5) = 9:00pm

That’s what I’m after.
0
 
bogdemAuthor Commented:
sorry:
Array_timeframe_end(4) = 9:00pm
0
 
mydasxCommented:
i need you to answer my questions if you want me to help you.
if this is true position n in array start and position n in array end will reprent a time span?
0
 
mydasxCommented:
also are you using .net 1.1 or 2.0?
0
 
mydasxCommented:
assuming your are using 1.1... and the two arrays are syncronized.

first off having 2 arrays trying to store information for one concept is dirty.

You should have a class called Offtime and a class called TimeFrame which both inherit from a class that has a start time and an end time which are exposed through properties and you will need a method that checks to see if a passed in span overlaps w/ you..  You would then have a single array for each of your span children, which could be checked against each other for overlap

you could then do a for each on your arrays and check each element.

0
 
ctm5Commented:
So you can just expand on the DateDiff approach I outlined. You already have all the info you need.

Look at all of the start & stop times as before, and figure out if they are within the time you need to account for. Take the ones that are during the run time and sort them according to their start time. Then build an array:

run time start
off time start 1
off time start 2
.
.
.
off time start x
off time end x
run time end

You can sort the off times by looking at how far "past" the run time start they are.

ctm5
0
 
bogdemAuthor Commented:
I’m using .NET 2.0
I understand what you saying, but can you provide some code?
0
 
ctm5Commented:
Are you working with me or with mydasx?

ctm5
0
 
mydasxCommented:
jup im doing that for you now.
0
 
mydasxCommented:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim offTime As List(Of ExSpan) = New List(Of ExSpan)
        Dim timeSpan As List(Of ExSpan) = New List(Of ExSpan)
        Dim runTime As ExSpan = New ExSpan()
        runTime.StartTime = New DateTime(2006, 5, 24, 9, 0, 0)
        runTime.EndTime = New DateTime(2006, 5, 24, 11, 0, 0)

        'Populate your offTime and timeSpan lists here...

        For Each ex As ExSpan In offTime
            If (ex.OverLaps(runTime)) Then
                'DO SOMETHING
            End If
        Next

        For Each ex As ExSpan In timeSpan
            If (ex.OverLaps(runTime)) Then
                'DO SOMETHING ELSE
            End If
        Next
    End Sub
End Class

Public Class ExSpan
    Private mdtStartTime As DateTime
    Private mdtEndTime As DateTime
    Public Property StartTime() As DateTime
        Get
            Return Me.mdtStartTime
        End Get
        Set(ByVal value As DateTime)
            Me.mdtStartTime = value
        End Set
    End Property

    Public Property EndTime() As DateTime
        Get
            Return Me.mdtEndTime
        End Get
        Set(ByVal value As DateTime)
            Me.mdtEndTime = value
        End Set
    End Property

    Public Function OverLaps(ByRef ex As ExSpan) As Boolean
        Dim retVal As Boolean = False
        If (Me.TimeFoundInRange(Me.mdtStartTime, ex.StartTime, ex.EndTime)) Then
            retVal = True
        ElseIf (Me.TimeFoundInRange(Me.mdtEndTime, ex.StartTime, ex.EndTime)) Then
            retVal = True
        ElseIf (Me.TimeFoundInRange(ex.StartTime, Me.mdtStartTime, Me.mdtEndTime)) Then
            retVal = True
        ElseIf (Me.TimeFoundInRange(ex.EndTime, Me.mdtStartTime, Me.mdtEndTime)) Then
            retVal = True
        End If
        Return retVal
    End Function

    Public Function TimeFoundInRange(ByRef timeToCheck As DateTime, ByRef startTime As DateTime, ByRef endTime As DateTime) As Boolean
        Dim retVal As Boolean = False
        If ((timeToCheck.TimeOfDay >= startTime.TimeOfDay) And (timeToCheck.TimeOfDay <= endTime.TimeOfDay)) Then
            retVal = True
        End If
        Return retVal
    End Function
End Class
0
 
mydasxCommented:
I asked if you are on 2.0 cause i use the generic list here, which is type safe and wonderful.
0
 
bogdemAuthor Commented:
I'm just answer the question.
I can do the logic, and if I could write a code by myself then I didn’t post the question here. I need help with coding part not with logic.
Thanks.

0
 
mydasxCommented:
BTW ctm5

datediff is dangerous when only dealing w/ time.  if a programmer forgets to populate the date or populates date some of the time but not all of the time, comparisons of time become hosed.  Its been my experience, that taking complexity out of the comparison is safer then taking the complexity out of the constructor.  Best case scenario both work, worst case scenario, mine still works.
0
 
mydasxCommented:
anytime you find yourself creating 2 arrays sitting next to each other to work on data that is needed in both, move to a wrapper.  Seems you did need a little help w/ the logic :)
0
 
ctm5Commented:
Best case scenario and worst case scenario, the programmer is smart enough to build in error checking, which, for the sake of simplicity here, I do not list.

It is not our role to hold someone's hand through every line of code. We are here to suggest approaches, explain concepts, etc. It is also not our role, I believe, to intentionally step all over each other. I hope bogdem can sort through our conflicting answers.

ctm5
0
 
bogdemAuthor Commented:
To mydasx
Sorry, can not get it.
I see the runtime, but how to set off time?
0
 
bogdemAuthor Commented:
I need help with both, in first with coding :)
0
 
ctm5Commented:
bogdem,
I am happy to help you, but I will let mydasx finish up. I will continue to monitor this question. If you want to get back to me, just post a message here.

ctm5
0
 
mydasxCommented:
Best case scenario and worst case scenario, the programmer is smart enough to build in error checking, which, for the sake of simplicity here, I do not list.

It is not our role to hold someone's hand through every line of code. We are here to suggest approaches, explain concepts, etc. It is also not our role, I believe, to intentionally step all over each other. I hope bogdem can sort through our conflicting answers.

Not going to get into a flame war here.  However, If you can trust every programmer that will ever touch your code, you live in a world very different from mine.

You set offtime as follows:

'Do this for each one of your timespans:

Dim x As ExSpan = New ExSpan()
x.StartTime = New DateTime(2006, 5, 24, <some hour>, <some minute>, <some second>)
x.EndTime = New DateTime(2006, 5, 24, <some hour>, <some minute>, <some second>)
offTime.Add(x)


0
 
bogdemAuthor Commented:
Hello, Experts!
Here is what I wrote by myself, just tack a look on it and tell me how stupid it is.
Of course all point goes to mydasx . Thanks.

I appreciate any comments about my code.


Public Class Form1

    Dim Run_time_Start As Date = #5/23/2006 7:00:00 AM#
    Dim Run_time_End As Date = #5/23/2006 7:00:00 PM#

    Dim Off_time_Start As New ArrayList
    Dim Off_time_End As New ArrayList

    Dim TimeFrame_Start As New ArrayList
    Dim TimeFrame_End As New ArrayList


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Off_time_Start.Add("5/23/2006 8:00:00 AM")
        Off_time_End.Add("5/23/2006 8:30:00 AM")

        Off_time_Start.Add("5/23/2006 9:00:00 PM")
        Off_time_End.Add("5/23/2006 9:30:00 PM")

        For i As Integer = 0 To Off_time_Start.Count - 1
            ' add off time to Off_time_Start
            Run_time_End = Run_time_End.AddMinutes(System.Convert.ToDateTime(Off_time_End(i)).Subtract(System.Convert.ToDateTime(Off_time_Start(i))).TotalMinutes)

            If i = 0 Then
                TimeFrame_Start.Add(Run_time_Start)
                TimeFrame_End.Add(Off_time_Start(i))

                TimeFrame_Start.Add(Off_time_Start(i))
                TimeFrame_End.Add(Off_time_End(i))
            Else
                TimeFrame_Start.Add(TimeFrame_End.Item(TimeFrame_End.Count - 1))
                TimeFrame_End.Add(Off_time_Start(i))

                TimeFrame_Start.Add(Off_time_Start(i))
                TimeFrame_End.Add(Off_time_End(i))
            End If

            ' Check if the Run_time_Stop is before next Off_time_Start
            ' And i is not last element right now
            If i + 1 <= Off_time_Start.Count - 1 Then
                If DateTime.Compare(Off_time_Start(i + 1), Run_time_End) = 1 Then
                    TimeFrame_Start.Add(Off_time_End(i))
                    TimeFrame_End.Add(Run_time_End)
                    Exit For
                End If
            End If

            If i = Off_time_Start.Count - 1 Then
                TimeFrame_Start.Add(Off_time_End(i))
                TimeFrame_End.Add(Run_time_End)
            End If

        Next


        For i As Integer = 0 To TimeFrame_Start.Count - 1
            Debug.WriteLine("From: " & TimeFrame_Start.Item(i) & " To: " & TimeFrame_End.Item(i))
        Next

    End Sub


End Class
0
 
mydasxCommented:
i still dont recomend using two lists to represnt a span
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 12
  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now