Evaluate the hour as T or F

Posted on 2006-04-19
Last Modified: 2010-04-05
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!!
Question by:digitalwav
    LVL 17

    Expert Comment

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

    I think this is what you mean, let me know if I'm on the wrong track
    LVL 17

    Accepted Solution

    Try this

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

    LVL 1

    Author Comment

    MOKULE- Perfect! Thank you!

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
    Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now