Public Class Form1
Private Sub DisplayShifts()
Dim startdate = New DateTime(2022, 12, 24)
Dim selectedDate = DateTimePicker1.Value
Dim shift = GetShift(startdate, selectedDate)
MsgBox(shift.ToString())
End Sub
Function GetShift(startDate As Date, selectedDate As Date) As ShiftKind
Dim days = (selectedDate - startDate).TotalDays
Dim intervals = days \ 18
Dim normalizedSelected = selectedDate.AddDays(intervals * -18)
days = (normalizedSelected - startDate).TotalDays
Select Case days
Case 0 To 4
Return ShiftKind.Afternoon
Case 6 To 10
Return ShiftKind.Morning
Case 12 To 16
Return ShiftKind.Night
End Select
Return ShiftKind.Off
End Function
Public Structure Shift
Public Property StartDate As Date
Public Property EndDate As Date
Public Property Kind As ShiftKind
Public Function Contains(value As Date) As Boolean
Return value >= StartDate AndAlso value <= EndDate
End Function
Public Overrides Function ToString() As String
Return $"{StartDate} - {EndDate} : {Kind}"
End Function
End Structure
Public Enum ShiftKind
Off = 1
Morning
Afternoon
Night
End Enum
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DisplayShifts()
End Sub
End Class
But I'm trying to replicate it for b4x but to be honest, I literally don't know where to start :\
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
Dim ShiftName As Map
ShiftName.Initialize
ShiftName.Put("m", "Morning 9am to 5pm")
ShiftName.Put("a", "Afternoon 5pm to 1am next day")
ShiftName.Put("n", "Night 1am to 9am")
ShiftName.Put("-", "-")
Dim ShiftPattern As String = "aaaaa-mmmmm-nnnnn-"
Dim ShiftDay As Int = 0 'whereabouts in ShiftPattern corresponds to:
DateTime.DateFormat = "yyyy-MM-dd"
Dim ShiftDate As Long = DateTime.DateParse("2022-12-01")
Dim OneDay As Period
OneDay.Initialize
OneDay.Days = 1
DateTime.DateFormat = "EEE dd MMM"
For DoDay = 1 To 28 '''365 / 4 + 1
Log(DateTime.Date(ShiftDate) & TAB & ShiftName.Get("" & ShiftPattern.CharAt(ShiftDay)))
ShiftDate = DateUtils.AddPeriod(ShiftDate, OneDay)
ShiftDay = (ShiftDay + 1) Mod ShiftPattern.Length
Next
End Sub
Logger connected to: TCL 5009A
--------- beginning of system
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Thu. 01 Dec. Afternoon 5pm to 1am next day
Fri. 02 Dec. Afternoon 5pm to 1am next day
Sat. 03 Dec. Afternoon 5pm to 1am next day
Sun. 04 Dec. Afternoon 5pm to 1am next day
Mon. 05 Dec. Afternoon 5pm to 1am next day
Tue. 06 Dec. -
Wed. 07 Dec. Morning 9am to 5pm
Thu. 08 Dec. Morning 9am to 5pm
Fri. 09 Dec. Morning 9am to 5pm
Sat. 10 Dec. Morning 9am to 5pm
Sun. 11 Dec. Morning 9am to 5pm
Mon. 12 Dec. -
Tue. 13 Dec. Night 1am to 9am
Wed. 14 Dec. Night 1am to 9am
Thu. 15 Dec. Night 1am to 9am
Fri. 16 Dec. Night 1am to 9am
Sat. 17 Dec. Night 1am to 9am
Sun. 18 Dec. -
Mon. 19 Dec. Afternoon 5pm to 1am next day
Tue. 20 Dec. Afternoon 5pm to 1am next day
Wed. 21 Dec. Afternoon 5pm to 1am next day
Thu. 22 Dec. Afternoon 5pm to 1am next day
Fri. 23 Dec. Afternoon 5pm to 1am next day
Sat. 24 Dec. -
Sun. 25 Dec. Morning 9am to 5pm
Mon. 26 Dec. Morning 9am to 5pm
Tue. 27 Dec. Morning 9am to 5pm
Wed. 28 Dec. Morning 9am to 5pm
** Activity (main) Resume **
Sub ConstructShiftSchedule(FromYear As Int, ToYear As Int, ShiftPattern As String, Jan1stPatternDay As Int) As Map
Dim ShiftSchedule As Map
ShiftSchedule.Initialize
Dim ShiftPatternDay As Int = Jan1stPatternDay
For Y = FromYear To ToYear
For M = 1 To 12
For D = 1 To NumDaysInMonth(Y, M)
ShiftSchedule.Put(Y & "-" & ZeroPadLeft(M, 2) & "-" & ZeroPadLeft(D, 2), ShiftPattern.CharAt(ShiftPatternDay).As(String))
ShiftPatternDay = (ShiftPatternDay + 1) Mod ShiftPattern.Length
Next
Next
Next
Return ShiftSchedule
End Sub
Sub NumDaysInMonth(Year As Int, Month As Int) As Int
Select Month
Case 9, 4, 6, 11 'thirty days hath september...
Return 30
Case 2 'except february
If (Year Mod 4) = 0 Then
Return 29
Else
Return 28
End If
Case Else 'and all the others
Return 31
End Select
End Sub
Sub ZeroPadLeft(S As String, L As Int) As String
Dim ZS As String = S
Do While ZS.Length < L
ZS = "0" & ZS
Loop
Return ZS
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
Dim ShiftName As Map
ShiftName.Initialize
ShiftName.Put("m", "Morning 9am to 5pm")
ShiftName.Put("a", "Afternoon 5pm to 1am next day")
ShiftName.Put("n", "Night 1am to 9am")
ShiftName.Put("-", "?")
ShiftName.Put("=", "? Party All Day ?")
Dim ShiftSchedule As Map = ConstructShiftSchedule(2020, 2026, "mmmmm-aaaaa=nnnnn-", 9)
For D = 10 To 31
Dim K As String = "2022-12-" & D
'''Log(K & TAB & ShiftSchedule.Get(K) & TAB & ShiftName.Get(ShiftSchedule.Get(K)))
Log( _
K & TAB & _
ShiftSchedule.Get(K) & TAB & _
ShiftName.Get(ShiftSchedule.Get(K)) _
)
Next
Next
End Sub
Logger connected to: TCL 5009A
--------- beginning of system
--------- beginning of main
2022-12-10 m Morning 9am to 5pm
2022-12-11 m Morning 9am to 5pm
2022-12-12 - ?
2022-12-13 a Afternoon 5pm to 1am next day
2022-12-14 a Afternoon 5pm to 1am next day
2022-12-15 a Afternoon 5pm to 1am next day
2022-12-16 a Afternoon 5pm to 1am next day
2022-12-17 a Afternoon 5pm to 1am next day
2022-12-18 = ? Party All Day ?
2022-12-19 n Night 1am to 9am
2022-12-20 n Night 1am to 9am
2022-12-21 n Night 1am to 9am
2022-12-22 n Night 1am to 9am
2022-12-23 n Night 1am to 9am
2022-12-24 - ?
2022-12-25 m Morning 9am to 5pm
2022-12-26 m Morning 9am to 5pm
2022-12-27 m Morning 9am to 5pm
2022-12-28 m Morning 9am to 5pm
2022-12-29 m Morning 9am to 5pm
2022-12-30 - ?
2022-12-31 a Afternoon 5pm to 1am next day
** Activity (main) Resume **
would it be possible to choose a single day from a calendar control and check which shift i ll be working on that day without have a range?
Dim K As String = Date_From_Calendar_Control 'must be in YYYY-MM-DD format precisely
Log("Date = " & K)
Log("Shift = " & ShiftSchedule.Get(K))
sure, the employer send us a pdf with our shiftsHow does the employer specify the alignment of the shift pattern to the calendar date?
Do they say "the first morning shift is/was on yyyy-mm-dd" ?
Or "the first day of 2020 was a third-of-five afternoon shift" ?
Or do they just provide a calendar, in which case: how would you like to specify it to the computer?
Just need one alignment point eg "arbitrary date = day of shift schedule".
which control are you using for calendar?conveniently ignoring the "without have a range" stipulation ? for a moment, thiswouldshould do the job:
B4X:Dim K As String = Date_From_Calendar_Control 'must be in YYYY-MM-DD format precisely Log("Date = " & K) Log("Shift = " & ShiftSchedule.Get(K))
Without the range, can still be done using DateUtils, but I get a bit nervous about what happens around daylight-saving changeover when everything shifts backward/forward by an hour (or whatever). Although I guess if we decreed a date to be "as-at-noon" rather than "as-at-midnight-ie-time-is-zero" then that'd be enough of a margin to eliminate those pesky edge cases.
which control are you using for calendar?
I found this but it is not in y-m-d
'say for Swiss National Day 1st August 2022:
'2022-08-01
ShiftSchedule.Put(Y & "-" & ZeroPadLeft(M, 2) & "-" & ZeroPadLeft(D, 2), ShiftPattern.CharAt(ShiftPatternDay).As(String))
'01-08-2022
ShiftSchedule.Put(ZeroPadLeft(D, 2) & "-" & ZeroPadLeft(M, 2) & "-" & Y, ShiftPattern.CharAt(ShiftPatternDay).As(String))
'08-01-2022
ShiftSchedule.Put(ZeroPadLeft(M, 2) & "-" & ZeroPadLeft(D, 2) & "-" & Y, ShiftPattern.CharAt(ShiftPatternDay).As(String))
'1/8/2022
ShiftSchedule.Put(D & "/" & M & "/" & Y, ShiftPattern.CharAt(ShiftPatternDay).As(String))
'8/1/2022
ShiftSchedule.Put(M & "/" & D & "/" & Y, ShiftPattern.CharAt(ShiftPatternDay).As(String))
I will definitely useI am not using a calendar, I'm just suggesting one way of translating dates to shifts.
What format would you like the date to be in?
Examples of different date formats:'say for Swiss National Day 1st August 2022: '2022-08-01 ShiftSchedule.Put(Y & "-" & ZeroPadLeft(M, 2) & "-" & ZeroPadLeft(D, 2), ShiftPattern.CharAt(ShiftPatternDay).As(String)) '01-08-2022 ShiftSchedule.Put(ZeroPadLeft(D, 2) & "-" & ZeroPadLeft(M, 2) & "-" & Y, ShiftPattern.CharAt(ShiftPatternDay).As(String)) '08-01-2022 ShiftSchedule.Put(ZeroPadLeft(M, 2) & "-" & ZeroPadLeft(D, 2) & "-" & Y, ShiftPattern.CharAt(ShiftPatternDay).As(String)) '1/8/2022 ShiftSchedule.Put(D & "/" & M & "/" & Y, ShiftPattern.CharAt(ShiftPatternDay).As(String)) '8/1/2022 ShiftSchedule.Put(M & "/" & D & "/" & Y, ShiftPattern.CharAt(ShiftPatternDay).As(String))
ShiftSchedule.Put(ZeroPadLeft(D, 2) & "-" & ZeroPadLeft(M, 2) & "-" & Y, ShiftPattern.CharAt(ShiftPatternDay).As(String))
thanks lord, but I only need a easy tool to know which shift I will be working on a select date. Employer send us 2 weeks shifts at the time, but I m not interested on knowing hours and place of work, rather if morning/afternoon or night following the 5+1 systemI could give you the link to my program for work shifts, it is able to manage all types of shifts with also the calculation of hours, it is able to manage shifts both based on simple codes "M" morning "A" afternoon etcc . and also shifts with start and end times in the format "HH:MM"
but my program shows the whole month both on the main page of the day and in a comfortable full screen widget
so it's not for you
Turni di lavoro - Apps on Google Play
Shifts, view your work shifts on the phone or tablet.play.google.com
I'm trying using the b4x view templatesI could give you the link to my program for work shifts, it is able to manage all types of shifts with also the calculation of hours, it is able to manage shifts both based on simple codes "M" morning "A" afternoon etcc . and also shifts with start and end times in the format "HH:MM"
but my program shows the whole month both on the main page of the day and in a comfortable full screen widget
so it's not for you
Turni di lavoro - Apps on Google Play
Shifts, view your work shifts on the phone or tablet.play.google.com
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region
'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip
Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private Button1 As B4XView
Dim dialog As B4XDialog
Private Base As B4XView
End Sub
Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
dialog.Initialize (Base)
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
End Sub
'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
Sub ConstructShiftSchedule(FromYear As Int, ToYear As Int, ShiftPattern As String, Jan1stPatternDay As Int) As Map
Dim ShiftSchedule As Map
ShiftSchedule.Initialize
Dim ShiftPatternDay As Int = Jan1stPatternDay
For Y = FromYear To ToYear
For M = 1 To 12
For D = 1 To NumDaysInMonth(Y, M)
ShiftSchedule.Put(ZeroPadLeft(M, 2) & "-" & ZeroPadLeft(D, 2) & "-" & Y, ShiftPattern.CharAt(ShiftPatternDay).As(String))
ShiftPatternDay = (ShiftPatternDay + 1) Mod ShiftPattern.Length
Next
Next
Next
Return ShiftSchedule
End Sub
Sub NumDaysInMonth(Year As Int, Month As Int) As Int
Select Month
Case 9, 4, 6, 11 'thirty days hath september...
Return 30
Case 2 'except february
If (Year Mod 4) = 0 Then
Return 29
Else
Return 28
End If
Case Else 'and all the others
Return 31
End Select
End Sub
Sub ZeroPadLeft(S As String, L As Int) As String
Dim ZS As String = S
Do While ZS.Length < L
ZS = "0" & ZS
Loop
Return ZS
End Sub
Private Sub Button1_Click
Dim calendario As B4XDateTemplate
Dim ShiftName As Map
ShiftName.Initialize
ShiftName.Put("m", "Morning 9am to 5pm")
ShiftName.Put("a", "Afternoon 5pm to 1am next day")
ShiftName.Put("n", "Night 1am to 9am")
ShiftName.Put("-", "?")
ShiftName.Put("=", "? Party All Day ?")
Dim ShiftSchedule As Map = ConstructShiftSchedule(2020, 2026, "mmmmm-aaaaa=nnnnn-", 9)
For D = 10 To 31
Wait For (dialog.ShowTemplate(calendario, "", "", "CANCEL")) Complete (Result As Int)
If Result = xui.DialogResponse_Positive Then
Dim K As String = DateTime.Date(calendario.Date)
Log("Date = " & K)
Log("Shift = " & ShiftSchedule.Get(K))
End If
Next
End Sub
Logger connected to: samsung SM-G985F
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Error occurred on line: 95 (B4XDialog)
java.lang.RuntimeException: Object should first be initialized (B4XView).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
at anywheresoftware.b4a.objects.B4XViewWrapper.getViewObject(B4XViewWrapper.java:104)
at anywheresoftware.b4a.objects.B4XViewWrapper.asPanelWrapper(B4XViewWrapper.java:107)
at anywheresoftware.b4a.objects.B4XViewWrapper.GetAllViewsRecursive(B4XViewWrapper.java:307)
at b4a.example.b4xdialog$ResumableSub_ShowCustom.resume(b4xdialog.java:976)
at b4a.example.b4xdialog._showcustom(b4xdialog.java:911)
at b4a.example.b4xdialog$ResumableSub_ShowTemplate.resume(b4xdialog.java:117)
at b4a.example.b4xdialog._showtemplate(b4xdialog.java:74)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7881)
at android.widget.TextView.performClick(TextView.java:16201)
at android.view.View.performClickInternal(View.java:7858)
at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
at android.view.View$PerformClick.run(View.java:30863)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8741)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
eventho I'm initializing that.. :\
Private Sub Button1_Click
Dim calendario As B4XDateTemplate
...
Wait For (dialog.ShowTemplate(calendario, "", "", "CANCEL")) Complete (Result As Int)
Dim calendario As B4XDateTemplate
calendario.initialize
Dim ShiftName As Map
ShiftName.Initialize
ShiftName.Put("m", "Morning 9am to 5pm")
ShiftName.Put("a", "Afternoon 5pm to 1am next day")
ShiftName.Put("n", "Night 1am to 9am")
ShiftName.Put("-", "?")
ShiftName.Put("=", "? Party All Day ?")
Dim ShiftSchedule As Map = ConstructShiftSchedule(2020, 2026, "mmmmm-aaaaa=nnnnn-", 9)
For D = 10 To 31
Wait For (dialog.ShowTemplate(calendario, "", "", "CANCEL")) Complete (Result As Int)
If Result = xui.DialogResponse_Positive Then
Dim K As String = DateTime.Date(calendario.Date)
Log("Date = " & K)
Log("Shift = " & ShiftSchedule.Get(K))
End If
Next
Date = 12/30/2022
Shift = null
Date = 12/31/2022
Shift = null
Date = 12/30/2022
Shift = null
Date = 12/31/2022
Shift = null
I will definitely use
B4X:ShiftSchedule.Put(ZeroPadLeft(D, 2) & "-" & ZeroPadLeft(M, 2) & "-" & Y, ShiftPattern.CharAt(ShiftPatternDay).As(String))
B4X:Date = 12/30/2022 Date = 12/31/2022 Date = 12/30/2022 Date = 12/31/2022
I will definitely use
ShiftSchedule.Put(ZeroPadLeft(D, 2) & "-" & ZeroPadLeft(M, 2) & "-" & Y, ShiftPattern.CharAt(ShiftPatternDay).As(String))
B4X:Dim K As String = Date_From_Calendar_Control 'must be in YYYY-MM-DD format precisely Log("Date = " & K) Log("Shift = " & ShiftSchedule.Get(K))
B4X:Date = 12/30/2022 Date = 12/31/2022 Date = 12/30/2022 Date = 12/31/2022
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?