Name: Find the next scheduled time.
Description by example: You need to start a service each day at 5 am, 6:30 am and 8 pm. You need to find the ticks values of the next scheduled time, either today or tomorrow if it is after 8pm.
Description by example: You need to start a service each day at 5 am, 6:30 am and 8 pm. You need to find the ticks values of the next scheduled time, either today or tomorrow if it is after 8pm.
B4X:
Dim t As Long = FindNextTime(Array As Double(5, 6.5, 20))
Log($"Next time is: $DateTime{t}"$)
Sub FindNextTime(Times As List) As Long
Times.Sort(True)
For Each st As Double In Times
If SetHours(st) > DateTime.Now Then
Return SetHours(st)
End If
Next
Return DateTime.Add(SetHours(Times.Get(0)), 0, 0, 1)
End Sub
Sub SetHours(st As Double) As Long
Dim hours As Int = Floor(st)
Dim minutes As Int = Round(60 * (st - hours))
Return DateUtils.SetDateAndTime(DateTime.GetYear(DateTime.Now), _
DateTime.GetMonth(DateTime.Now), DateTime.GetDayOfMonth(DateTime.Now), hours, minutes, 0)
End Sub
Last edited: