Solved

# Need a function, very hard task.

Posted on 2006-05-24
162 Views

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
Question by:bogdem

LVL 6

Expert Comment

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

LVL 6

Expert Comment

Oops...
You need to use the interval with DateDiff:

DateDiff(DateInterval.Minute, runtime_start,....)
0

LVL 5

Expert Comment

ok real quick...

you have 2 arrays for offtime a startingtime array and an ending time array is that accurate?
0

LVL 5

Expert Comment

if this is true position n in array start and position n in array end will reprent a time span?
0

Author Comment

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

Author Comment

sorry:
Array_timeframe_end(4) = 9:00pm
0

LVL 5

Expert Comment

if this is true position n in array start and position n in array end will reprent a time span?
0

LVL 5

Expert Comment

also are you using .net 1.1 or 2.0?
0

LVL 5

Expert Comment

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

LVL 6

Expert Comment

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

Author Comment

I’m using .NET 2.0
I understand what you saying, but can you provide some code?
0

LVL 6

Expert Comment

Are you working with me or with mydasx?

ctm5
0

LVL 5

Expert Comment

jup im doing that for you now.
0

LVL 5

Accepted Solution

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

LVL 5

Expert Comment

I asked if you are on 2.0 cause i use the generic list here, which is type safe and wonderful.
0

Author Comment

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

LVL 5

Expert Comment

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

LVL 5

Expert Comment

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

LVL 6

Expert Comment

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

Author Comment

To mydasx
Sorry, can not get it.
I see the runtime, but how to set off time?
0

Author Comment

I need help with both, in first with coding :)
0

LVL 6

Expert Comment

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

LVL 5

Expert Comment

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>)

0

Author Comment

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.

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

For i As Integer = 0 To Off_time_Start.Count - 1
' add off time to Off_time_Start

If i = 0 Then

Else

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
Exit For
End If
End If

If i = Off_time_Start.Count - 1 Then
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

LVL 5

Expert Comment

i still dont recomend using two lists to represnt a span
0

## Featured Post

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…