Work Shift Algorithm

  • Thread starter Screaming Eagles 101
  • Start date
S

Screaming Eagles 101

Hi,
I'm looking for some code to find the right work shift according to a
specific date and time.

This one worked fine in VB6, but somewhere I do not succeed in converting it
to VB2005 code....


Public Function GetPloegLetter(dte As Date, tme As Date) As String
'dte is the date, tme is the time of the shift.
Dim strPloegLetters(3) As String
strPloegLetters(0) = "A"
strPloegLetters(1) = "D"
strPloegLetters(2) = "B"
strPloegLetters(3) = "C"
Dim i As Integer

'Get the right shift for the parsed date and time...
Select Case tme
Case "05:30:00" To "13:29:59"
'Process morningshift
i = (((dte + 1) \ 7) + 1) Mod 4
GetPloegLetter = strPloegLetters(i)

Case "13:30:00" To "21:29:59"
'Process afternoonshift
i = (((dte + 10) \ 7) + 1) Mod 4
GetPloegLetter = strPloegLetters(i)

Case "21:30:00" To "23:59:59"
'Process nightshift < midnight
i = ((dte - 2) \ 7) Mod 4
GetPloegLetter = strPloegLetters(i)

Case "00:00:00" To "05:29:59"
dte = dte - 1 ' we need the day before
'Process nightshift > midnight
i = ((dte - 2) \ 7) Mod 4
GetPloegLetter = strPloegLetters(i)
End Select
End Function



--
Filip
http://www.ww2airborne.net/
Official Site of the 101st Airborne - 463rd PFA
skype: airborne463pfa-fiwi
-------------------------------------------------
 
S

Stephany Young

First of all, Date is an alias for System.DateTime which holds a point in
time which of course has a date part and a time part. If you want to use one
part or the other you MUST 'screen' the unwanted part out.


The declaration of the array can be simplified like:

Dim strPloegLetters As String() = New String() {"A", "D", "B", "C"}

This will 'unclutter' your code.

To do the comparison against the time part as a string you need to do it
like:

Select Case tme.ToString("HH:mm:ss")
Case "05:30:00" To "13:29:59"

Otherwise you are not comparing apples with apples.

What is the rationale behind the way that i is calculated? I don't mean what
does it do. I mean what is the intent of the business rule.

By my reckoning you will get a different value for a given time today thna
you will get for the same time tomorrow.
 
S

Screaming Eagles 101

Thanks already for this.

It calculates which work shift (A,B,C,D) will be working on day x
in the morning, afternoon, nightshift at a certain time...

Example (I did not calculate, just explaining) :
Parsing 13th March 2007 as date, hour 17:15 as time,
will result in a letter A,B,C, or a D.
We know then that at that time for example the D-shift will be at work.

Our shifts work
in the morningshift from Friday to Thursday
in the afternoonshift from Wednesday to Tuesday
in the nightshift from Monday to Sunday.

Inbetween are the rest-days : for example last nightshift is Sunday and
one starts again on Wednesday in the afternoon shift,
last afternoon is on Tuesday and then starting in the morning shift on
Friday,
rest on Wednesday and Thursday...

--
Filip
http://www.ww2airborne.net/
Official Site of the 101st Airborne - 463rd PFA
skype: airborne463pfa-fiwi
-------------------------------------------------

Stephany Young said:
First of all, Date is an alias for System.DateTime which holds a point in
time which of course has a date part and a time part. If you want to use
one part or the other you MUST 'screen' the unwanted part out.


The declaration of the array can be simplified like:

Dim strPloegLetters As String() = New String() {"A", "D", "B", "C"}

This will 'unclutter' your code.

To do the comparison against the time part as a string you need to do it
like:

Select Case tme.ToString("HH:mm:ss")
Case "05:30:00" To "13:29:59"

Otherwise you are not comparing apples with apples.

What is the rationale behind the way that i is calculated? I don't mean
what does it do. I mean what is the intent of the business rule.

By my reckoning you will get a different value for a given time today thna
you will get for the same time tomorrow.
 
S

Stephany Young

Cool ... so that makes it:

Public Function GetPloegLetter(dt as DateTime) As String

Dim i As Integer

Select Case dt.ToString("HH:mm:ss")
Case "05:30:00" To "13:29:59"
'Process morningshift
i = dt.Date.AddDays(1) \ 7 + 1
Case "13:30:00" To "21:29:59"
' Process afternoonshift
i = dt.Date.AddDays(10) \ 7 + 1
Case "21:30:00" To "23:59:59"
' Process nightshift < midnight
i = dt.Date.AddDays(-2)
Case "00:00:00" To "05:29:59"
' Process nightshift > midnight
i = dt.Date.AddDays(-3)
End Select

Return (New String() {"A", "D", "B", "C"})((i \ 7) Mod 4)

End Function

Screaming Eagles 101 said:
Thanks already for this.

It calculates which work shift (A,B,C,D) will be working on day x
in the morning, afternoon, nightshift at a certain time...

Example (I did not calculate, just explaining) :
Parsing 13th March 2007 as date, hour 17:15 as time,
will result in a letter A,B,C, or a D.
We know then that at that time for example the D-shift will be at work.

Our shifts work
in the morningshift from Friday to Thursday
in the afternoonshift from Wednesday to Tuesday
in the nightshift from Monday to Sunday.

Inbetween are the rest-days : for example last nightshift is Sunday and
one starts again on Wednesday in the afternoon shift,
last afternoon is on Tuesday and then starting in the morning shift on
Friday,
rest on Wednesday and Thursday...

--
Filip
http://www.ww2airborne.net/
Official Site of the 101st Airborne - 463rd PFA
skype: airborne463pfa-fiwi
-------------------------------------------------
 
S

Screaming Eagles 101

Well thanks, you've been a great help, that's for sure !

There are 2 minor things which were to be adapted to let it work,
one was in the division by 7 and Mod 4, small change to have the correct
shift.

A second one is that VB2005 for a weird reason won't allow to divide a date
by a number anymore as was possible in VB6.
The number I got from that division : I found out it is the amount of days
between 31 December 1899 and the parsed date.
(a bit as Excel works) So, once found, it was easy to make a tiny new
Function to convert the date to a long-variable, and make the calculation
with the result.

Thanks again for helping !

This is the code fully and 110% working :)

Public Function GetPloegLetter(ByVal dt As DateTime) As String
Dim l As Long

Select Case dt.ToString("HH:mm:ss")
Case "05:30:00" To "13:29:59"
'Process morningshift
l = DateToDays(dt.Date, 1) \ 7 + 1
Case "13:30:00" To "21:29:59"
' Process afternoonshift
l = DateToDays(dt.Date, 10) \ 7 + 1
Case "21:30:00" To "23:59:59"
' Process nightshift < midnight
l = DateToDays(dt.Date, -2) \ 7
Case "00:00:00" To "05:29:59"
' Process nightshift > midnight
l = DateToDays(dt.Date, -3) \ 7
End Select

Return (New String() {"A", "D", "B", "C"})(l Mod 4)
End Function

Private Function DateToDays(ByVal dte As Date, ByVal i As Integer) As
Long
Dim dteOld As Date = CDate("31/12/1899")
DateToDays = DateDiff(DateInterval.Day, dteOld, dte.AddDays(i))
End Function


--
Filip
http://www.ww2airborne.net/
Official Site of the 101st Airborne - 463rd PFA
skype: airborne463pfa-fiwi
-------------------------------------------------
Stephany Young said:
Cool ... so that makes it:

Public Function GetPloegLetter(dt as DateTime) As String

Dim i As Integer

Select Case dt.ToString("HH:mm:ss")
Case "05:30:00" To "13:29:59"
'Process morningshift
i = dt.Date.AddDays(1) \ 7 + 1
Case "13:30:00" To "21:29:59"
' Process afternoonshift
i = dt.Date.AddDays(10) \ 7 + 1
Case "21:30:00" To "23:59:59"
' Process nightshift < midnight
i = dt.Date.AddDays(-2)
Case "00:00:00" To "05:29:59"
' Process nightshift > midnight
i = dt.Date.AddDays(-3)
End Select

Return (New String() {"A", "D", "B", "C"})((i \ 7) Mod 4)

End Function
 
S

susiedba

hey army fag

I would stay away from DOTNET

what Iraqis are doing to us army is just the same thing that MS did to
vb6 programmers

why would you continue giving them business?

**** MICROSOFT


WITH A SLEDGEHAMMER
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top