Testing Time Values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff B.

    Testing Time Values

    Hello,

    Has anyone ran across an efficient algorithm for determining if a DateTime
    or TimeSpan value falls within two other DateTime/TimeSpan values?

    For example, if I have a start time of 09:00 and an end time of 21:00 and I
    want to test if 16:00 is between them, this is fairly straightforward .
    However, if my start time is 21:00 and my end time is 09:00 and I want to
    test if 04:00 is between them, then it's not quite as straightforward .

    I don't want to take the actual dates into account, only the times.

    Any ideas?

    --- Thanks, Jeff


  • Kevin Yu [MSFT]

    #2
    RE: Testing Time Values

    Hi Jeff,

    First of all, I would like to confirm my understanding of your issue. From
    your description, I understand that you need to check if a certain DateTime
    is in a certain range. If there is any misunderstandin g, please feel free
    to let me know.

    There is no direct method for us to call in the .NET framework. Since it
    has no date information in it, I think it's hard to write such a method.
    For example, we need to check if a time is between 21:00 and 9:00. Now we
    pass in 6:00. But how do we know it is the 6:00 of the first day or the
    next day?

    Kevin Yu
    =======
    "This posting is provided "AS IS" with no warranties, and confers no
    rights."

    Comment

    • Jeff B.

      #3
      Re: Testing Time Values

      > For example, we need to check if a time is between 21:00 and 9:00. Now we[color=blue]
      > pass in 6:00. But how do we know it is the 6:00 of the first day or the
      > next day?[/color]

      Kevin,

      As far as my needs go, I'm not concerned about which day it is. In the
      example you state above, if 06:00 falls "anywhere" between 21:00 and 09:00,
      which it does, then I would want a result of true to come back. If I were
      testing to see if 06:00 falls "anywhere" between 09:00 and 21:00 (the
      reverse of the example above) then I would want false to come back since
      06:00 does not fall anywhere between 9 in the morning and 9 in the evening.

      Does this make more sense?

      --- Thanks, Jeff


      Comment

      • Boaz Ben-Porat

        #4
        Re: Testing Time Values

        Hi Jeff

        Try this. It works for me

        // Check if a given TimeSpan DateTime is between two other given TimeSpans

        private bool IsTimeBetween(T imeSpan tsToTest, TimeSpan tsStart, TimeSpan
        tsEnd)
        {
        bool bRet = false;
        if (tsStart > tsEnd)
        {
        TimeSpan ts0 = new TimeSpan(0); // 00:00:00
        TimeSpan ts1 = new TimeSpan(TimeSp an.TicksPerDay-1); // 23:59:59.xxx
        bRet = (IsTimeBetween( tsToTest, tsStart, ts1) ||
        (IsTimeBetween( tsToTest, ts0, tsEnd)));
        }
        else
        {
        if ((tsToTest >= tsStart) && (tsToTest <= tsEnd))
        {
        bRet = true;
        }
        }
        return bRet;
        }

        // Check if time part of a given DateTime is between two given TimeSpans
        private bool IsTimeBetween(D ateTime dtToTest, TimeSpan tsStart, TimeSpan
        tsEnd)
        {
        return IsTimeBetween(d tToTest.TimeOfD ay, tsStart, tsEnd);
        }

        Boaz Ben-Porat
        Milstone Systems

        "Jeff B." <jsb@community. nospam> wrote in message
        news:ePDFDijGFH A.3068@tk2msftn gp13.phx.gbl...[color=blue]
        > Hello,
        >
        > Has anyone ran across an efficient algorithm for determining if a DateTime
        > or TimeSpan value falls within two other DateTime/TimeSpan values?
        >
        > For example, if I have a start time of 09:00 and an end time of 21:00 and
        > I want to test if 16:00 is between them, this is fairly straightforward .
        > However, if my start time is 21:00 and my end time is 09:00 and I want to
        > test if 04:00 is between them, then it's not quite as straightforward .
        >
        > I don't want to take the actual dates into account, only the times.
        >
        > Any ideas?
        >
        > --- Thanks, Jeff
        >
        >[/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Testing Time Values

          Jeff B. <jsb@community. nospam> wrote:[color=blue][color=green]
          > > For example, we need to check if a time is between 21:00 and 9:00. Now we
          > > pass in 6:00. But how do we know it is the 6:00 of the first day or the
          > > next day?[/color]
          >
          > As far as my needs go, I'm not concerned about which day it is. In the
          > example you state above, if 06:00 falls "anywhere" between 21:00 and 09:00,
          > which it does, then I would want a result of true to come back. If I were
          > testing to see if 06:00 falls "anywhere" between 09:00 and 21:00 (the
          > reverse of the example above) then I would want false to come back since
          > 06:00 does not fall anywhere between 9 in the morning and 9 in the evening.
          >
          > Does this make more sense?[/color]

          Not really, because it falls between 9 in the morning and 9 on the
          following evening. I think you need a more precise definition of
          exactly what you mean. Once you've got a precise definition, turning it
          into code should be simple.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Jeff B.

            #6
            Re: Testing Time Values

            > Not really, because it falls between 9 in the morning and 9 on the[color=blue]
            > following evening. I think you need a more precise definition of
            > exactly what you mean. Once you've got a precise definition, turning it
            > into code should be simple.[/color]

            Ok, to be more precise, if I were testing to see if 06:00 falls "anywhere"
            between 09:00 and 21:00 I am talking about the _first_ occurrence of 21:00
            following 09:00. In other words, if the timespan being tested falls between
            the starting timespan and the first occurrence of the ending timespan
            (whether it happens to fall on the first day or next day) then I want a
            method that returns true; else I want it to return false.

            I hope this makes more sense.

            --- Thanks, Jeff


            Comment

            • Jeff B.

              #7
              Re: Testing Time Values

              Boaz,

              This algorithm works great. It's along the same lines of what I was
              attempting but a lot simpler and cleaner. Thanks for the help.

              --- Jeff
              [color=blue]
              > Hi Jeff
              >
              > Try this. It works for me
              >
              > // Check if a given TimeSpan DateTime is between two other given TimeSpans
              >
              > private bool IsTimeBetween(T imeSpan tsToTest, TimeSpan tsStart, TimeSpan
              > tsEnd)
              > {
              > bool bRet = false;
              > if (tsStart > tsEnd)
              > {
              > TimeSpan ts0 = new TimeSpan(0); // 00:00:00
              > TimeSpan ts1 = new TimeSpan(TimeSp an.TicksPerDay-1); //
              > 23:59:59.xxx
              > bRet = (IsTimeBetween( tsToTest, tsStart, ts1) ||
              > (IsTimeBetween( tsToTest, ts0, tsEnd)));
              > }
              > else
              > {
              > if ((tsToTest >= tsStart) && (tsToTest <= tsEnd))
              > {
              > bRet = true;
              > }
              > }
              > return bRet;
              > }
              >
              > // Check if time part of a given DateTime is between two given TimeSpans
              > private bool IsTimeBetween(D ateTime dtToTest, TimeSpan tsStart, TimeSpan
              > tsEnd)
              > {
              > return IsTimeBetween(d tToTest.TimeOfD ay, tsStart, tsEnd);
              > }
              >
              > Boaz Ben-Porat
              > Milstone Systems[/color]


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Testing Time Values

                Jeff B. <jsb@community. nospam> wrote:[color=blue][color=green]
                > > Not really, because it falls between 9 in the morning and 9 on the
                > > following evening. I think you need a more precise definition of
                > > exactly what you mean. Once you've got a precise definition, turning it
                > > into code should be simple.[/color]
                >
                > Ok, to be more precise, if I were testing to see if 06:00 falls "anywhere"
                > between 09:00 and 21:00 I am talking about the _first_ occurrence of 21:00
                > following 09:00. In other words, if the timespan being tested falls between
                > the starting timespan and the first occurrence of the ending timespan
                > (whether it happens to fall on the first day or next day) then I want a
                > method that returns true; else I want it to return false.
                >
                > I hope this makes more sense.[/color]

                Right. In that case, I think you need to:

                a) Take the start time and the end time, constructing DateTimes for
                both of them on the same day
                b) If the end time is currently before the start time, add 24 hours
                c) See if the test time is between start time and end time

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Testing Time Values

                  Boaz Ben-Porat <bbp@milestone. dk> wrote:[color=blue]
                  > Try this. It works for me[/color]

                  <snip>

                  I think that can be made a bit more readable, with the same interface:

                  static bool IsTimeBetween (TimeSpan tsToTest,
                  TimeSpan tsStart,
                  TimeSpan tsEnd)
                  {
                  DateTime day = DateTime.Today;

                  DateTime start = day+tsStart;
                  DateTime end = day+tsEnd;
                  DateTime test = day+tsTest;

                  if (start > end)
                  {
                  end = end.AddDays (1);
                  }
                  return (test >= start && test <= end);
                  }

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  Working...