Ejemplo n.º 1
0
        public static int ClosesInHowManyMinutes(this Model.OpeningHours model,
                                                 DateTime?now = null)
        {
            if (model == null)
            {
                return(-1);               // Default to store closed
            }
            if (!now.HasValue)
            {
                now = DateTime.Now;
            }

            // Check to see if store is open today at all
            var todaysOpeningHours = model.TodaysOpeningHours(now);

            if (todaysOpeningHours == null || !todaysOpeningHours.IsOpen)
            {
                return(-1); // store closed
            }

            // Check to see if store is 24 hour
            if (todaysOpeningHours.IsOpen24Hours())
            {
                return(1440); // 24 hours
            }

            // Check to see if store has opened yet
            var timeOfDay = now.Value.TimeOfDay;

            if (timeOfDay < todaysOpeningHours.Opens)
            {
                return(-2); // store not opened yet
            }

            // Check to see when store closes
            var midnight    = new TimeSpan(0, 0, 0);
            var closingTime = todaysOpeningHours.Closes;

            if (closingTime == midnight)
            {
                closingTime = new TimeSpan(23, 59, 59);
            }

            var closesIn = (int)(closingTime - timeOfDay).TotalMinutes;

            return(closesIn > 0 ? closesIn : -3);
        }