amd
amd

Reputation: 21442

Working with date in C#

I have many companies each one have a opening date and a closing date ex :

Company  Open   Close

A        08.00  22.00     (close in the **same** day)
B        10.00  02.00     (close in the **second** day)
C        14.00  03.00     (close in the **second** day)

On the web page the user must see a list of the companies and show the status of each one (open or close) depending on the current time (SERVER TIME)

THE PROBLEM : when the company open and close in the same day ( A ) it's easy to get the status in the following algorithm : isOpen = (NOW > OpenTime AND NOW < ClosingTime) , but if the company open in day 1 and close in day 2 ( B , C ) if the user check the page at 1.00 the company B will appear closed (because (1.00 > 10.00 AND 1.00 < 02.00) is False ) but in real time its opened.

How can I resolve this issue (when the closing time day is different than the opening time day)

Upvotes: 1

Views: 270

Answers (5)

phoog
phoog

Reputation: 43046

The expression you gave applies only when OpenTime is less than closing time, so:

isOpen = (OpenTime < ClosingTime AND NOW >= OpenTime AND NOW < ClosingTime) OR (NOW >= OpenTime OR NOW < ClosingTime)

Upvotes: 0

Nicholas Carey
Nicholas Carey

Reputation: 74197

Something like this will do you:

class Company
{
  public string   Name         { get ; set ; }

  // represents the time-of-day at which the company opens (e.g. 08:00, 14:00, etc.)
  public TimeSpan OpensAtTime  { get ; set ; }

  // represents the time-of-day at which the company closes (e.g.17:00, 08:00, etc. )
  public TimeSpan ClosesAtTime { get ; set ; }

  public bool IsOpen()
  {
    DateTime instant          = DateTime.Now ;
    DateTime Today            = instant.Date ;
    TimeSpan totalOpenHours   = ( this.ClosesAtTime - this.OpensAtTime ).Duration() ;
    DateTime openTimeToday    = Today + this.OpensAtTime ;
    DateTime closingTimeToday = openTimeToday + totalOpenHours ;
    bool     isOpen           = instant >= openTime && instant <= closingTime ;

    return isOpen ;
  }

}

Upvotes: 1

If I've understood it right there is the following algorithm:

When open time is less than close time: isOpen = now > openTime && now < closeTime.

Otherwise, isOpen = now > openTime || now < closeTime.

TimeSpan nowTime = TimeSpan.FromHours(11);
TimeSpan openTime = TimeSpan.FromHours(8);
TimeSpan closeTime = TimeSpan.FromHours(2);

bool isOpened = openTime < closeTime
                    ? (nowTime > openTime && nowTime < closeTime)
                    : (nowTime > openTime || nowTime < closeTime);

Note: TimeSpan struct has been used for time interval representation, but double may be used also (by simple replacing).

Upvotes: 1

Anthony Shaw
Anthony Shaw

Reputation: 8166

The easiest solution I can think of would be to check if the close time listed is less than the open time and add 24 hours, and check against that value

isOpen = (NOW > OpenTime AND NOW < (ClosingTime < OpeningTime ? ClosingTime + 24.00 : ClosingTime)

Upvotes: 4

Greg Hewgill
Greg Hewgill

Reputation: 992917

If the opening time is later than the closing time, then reverse your test.

Upvotes: 2

Related Questions