System.Globalization.GregorianCalendar.GetDaysInYear C# (CSharp) Method

GetDaysInYear() public method

public GetDaysInYear ( int year, int era ) : int
year int
era int
return int
        public override int GetDaysInYear(int year, int era)
        {
            if (era == CurrentEra || era == ADEra) {
                if (year >= 1 && year <= MaxYear) {
                    return ((year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 366:365);
                }
                throw new ArgumentOutOfRangeException(
                            "year",
                            String.Format(
                                CultureInfo.CurrentCulture,
                                Environment.GetResourceString("ArgumentOutOfRange_Range"),
                                1,
                                MaxYear));
            }
            throw new ArgumentOutOfRangeException("era", Environment.GetResourceString("ArgumentOutOfRange_InvalidEraValue"));
        }

Usage Example

Example #1
0
        /// <summary>
        /// Returns false only if all fields for the range has availability set to false
        /// </summary>
        /// <remarks>
        /// This method is useful to checking un-availability, as it will return true if there is any availability in a date range
        /// </remarks>        
        /// <param name="availability">The record to check</param>
        /// <param name="fromDayOfYear">From which Day Of Year</param>
        /// <param name="toDayOfYear">OPTIONAL: to which Day Of Year, last day in sequence will not be checked will not be checked</param>
        /// <returns>True if any of the rooms in the range specified is available otherwise false</returns>
        public static bool AreAnyRoomsInRangeAvailable(RoomsAvailability availability, int fromDayOfYear, int? toDayOfYear = null)
        {
            // if the availability is null means that the room is assumed to be available 
            if(availability == null)
            {
                return true;
            }

            //because we have leap years we need to consider either 365 or 366 days
            if (!toDayOfYear.HasValue)
            {
                GregorianCalendar calendar = new GregorianCalendar();
                toDayOfYear = calendar.GetDaysInYear(availability.Year);
            }

            bool isAvailable = false;
            for (int d = fromDayOfYear; d < toDayOfYear; d++)
            {
                string propertyName = String.Format("IsDay{0:000}Available", d);
                PropertyInfo p = typeof(RoomsAvailability).GetProperty(propertyName);

                isAvailable = isAvailable | (bool)p.GetValue(availability, null);
            }
            return isAvailable;
        }
All Usage Examples Of System.Globalization.GregorianCalendar::GetDaysInYear