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

Evaluate the hour as T or F

This has been driving me crazy for months. I hate time calculations. I have simplified my situation to just use the hour in 24 hour format since I can't find a way to do this with the datetime data.

Here's the deal, I need a function/procedure that will evaluate 3 integers and return a t/f response.  The first two values are a start and end 'hour', the third is the current 'hour'.  For example, if the start is 21, end is 6 and the current is 3, I need a false response. If the current is 7 it should return true.

In other words, if the current hour falls on 21,22,23,0,1,2,3,4,5 the response should be false, if it falls on 6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 the response should be true.

To complicate things, the start can be zero with an end of 6. Anything between 0 and 6 needs to return false, anything greater than 6 would be true.

Make sense?

Any assistance would be appreciated!!
0
digitalwav
Asked:
digitalwav
1 Solution
 
TheRealLokiSenior DeveloperCommented:
If you are sure that the "End" integer is always going to be after the "Start" integer, then the following should work for you

// StartHour, EndHour and CurrentHourare 0 -23.
// EndHour is assumed to be after StartHour
function CheckTimeThing(StartHour, EndHour, CurrentHour: integer): boolean;
begin
    if EndHour < StartHour then EndHour := EndHour + 24;  // 21, 6 becomes 21, 30
    if CurrentHour < StartHour then CurrentHour := CurrentHour + 24; // 21, 30, 3 becomes 21, 30, 27
// 1, 10, 11 = 1, 10, 11 = true
// 21, 6, 3 = 21, 30, 27 = false
// 0, 6, 3 = 0, 6, 3 = false
// 0, 6, 23 = 0, 6, 23 = true
    result := ( (CurrentHour < StartHour) or (CurrentHour >= EndHour) );
end;

I think this is what you mean, let me know if I'm on the wrong track
0
 
mokuleCommented:
Try this

function CompareHours(StartHour, EndHour, CurrentHour: integer): boolean;
begin
  if StartHour <= EndHour then
    result := (CurrentHour < StartHour) or (CurrentHour >= EndHour)
  else
    result := not ((CurrentHour < EndHour) or (CurrentHour >= StartHour));
end;

0
 
digitalwavIT Infrastructure ManagerAuthor Commented:
MOKULE- Perfect! Thank you!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now