Составить описание класса для представления даты - C# (201449)

Узнай цену своей работы

Формулировка задачи:

Составить описание класса для представления даты. Предусмотреть возможности установки даты и изменения ее отдельных полей (год, месяц, день) с проверкой__ допустимости вводимых значений. В случае недопустимых значений полей выбрасываются исключения. Создать методы изменения даты на заданное количество дней, месяцев и лет. Написать программу, демонстрирующую все разработанные элементы класса. Программу написать на C#

Решение задачи: «Составить описание класса для представления даты»

textual
Листинг программы
// Type: System.DateTime
// Assembly: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll
 
using System.Globalization;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Security;
 
namespace System
{
    /// <summary>
    /// Represents an instant in time, typically expressed as a date and time of day.
    /// </summary>
    /// <filterpriority>1</filterpriority>
    [Serializable]
    [StructLayout(LayoutKind.Auto)]
    public struct DateTime
    {
        private static readonly int[] DaysToMonth365 = new int[13]
                                                           {
                                                               0,
                                                               31,
                                                               59,
                                                               90,
                                                               120,
                                                               151,
                                                               181,
                                                               212,
                                                               243,
                                                               273,
                                                               304,
                                                               334,
                                                               365
                                                           };
 
        private static readonly int[] DaysToMonth366 = new int[13]
                                                           {
                                                               0,
                                                               31,
                                                               60,
                                                               91,
                                                               121,
                                                               152,
                                                               182,
                                                               213,
                                                               244,
                                                               274,
                                                               305,
                                                               335,
                                                               366
                                                           };
 
        /// <summary>
        /// Represents the smallest possible value of <see cref="T:System.DateTime"/>. This field is read-only.
        /// </summary>
        /// <filterpriority>1</filterpriority>
        public static readonly DateTime MinValue = new DateTime(0L, DateTimeKind.Unspecified);
 
        /// <summary>
        /// Represents the largest possible value of <see cref="T:System.DateTime"/>. This field is read-only.
        /// </summary>
        /// <exception cref="T:System.ArgumentOutOfRangeException"><see cref="F:System.DateTime.MaxValue"/> is outside the range of the current culture's default calendar or of a specified culture's default calendar.</exception><filterpriority>1</filterpriority>
        public static readonly DateTime MaxValue = new DateTime(3155378975999999999L, DateTimeKind.Unspecified);
 
        internal const long MinTicks = 0L;
        internal const long MaxTicks = 3155378975999999999L;
        private readonly ulong dateData;
 
        internal long InternalTicks
        {
            [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
            get { return (long) dateData & 4611686018427387903L; }
        }
 
        private ulong InternalKind
        {
            [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
            get { return dateData & 13835058055282163712UL; }
        }
 
        /// <summary>
        /// Gets the date component of this instance.
        /// </summary>
        /// 
        /// <returns>
        /// A new <see cref="T:System.DateTime"/> with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public DateTime Date
        {
            get
            {
                long internalTicks = InternalTicks;
                return new DateTime((ulong) (internalTicks - internalTicks%864000000000L) | InternalKind);
            }
        }
 
        /// <summary>
        /// Gets the day of the month represented by this instance.
        /// </summary>
        /// 
        /// <returns>
        /// The day component, expressed as a value between 1 and 31.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public int Day
        {
            get { return GetDatePart(3); }
        }
 
        /// <summary>
        /// Gets the day of the week represented by this instance.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.DayOfWeek"/> enumerated constant that indicates the day of the week of this <see cref="T:System.DateTime"/> value.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public DayOfWeek DayOfWeek
        {
            get { return (DayOfWeek) ((InternalTicks/864000000000L + 1L)%7L); }
        }
 
        /// <summary>
        /// Gets the day of the year represented by this instance.
        /// </summary>
        /// 
        /// <returns>
        /// The day of the year, expressed as a value between 1 and 366.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public int DayOfYear
        {
            get { return GetDatePart(1); }
        }
 
        /// <summary>
        /// Gets the hour component of the date represented by this instance.
        /// </summary>
        /// 
        /// <returns>
        /// The hour component, expressed as a value between 0 and 23.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public int Hour
        {
            get { return (int) (InternalTicks/36000000000L%24L); }
        }
 
        /// <summary>
        /// Gets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither.
        /// </summary>
        /// 
        /// <returns>
        /// One of the <see cref="T:System.DateTimeKind"/> values. The default is <see cref="F:System.DateTimeKind.Unspecified"/>.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public DateTimeKind Kind
        {
            get
            {
                switch (InternalKind)
                {
                    case 0UL:
                        return DateTimeKind.Unspecified;
                    case 4611686018427387904UL:
                        return DateTimeKind.Utc;
                    default:
                        return DateTimeKind.Local;
                }
            }
        }
 
        /// <summary>
        /// Gets the milliseconds component of the date represented by this instance.
        /// </summary>
        /// 
        /// <returns>
        /// The milliseconds component, expressed as a value between 0 and 999.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public int Millisecond
        {
            get { return (int) (InternalTicks/10000L%1000L); }
        }
 
        /// <summary>
        /// Gets the minute component of the date represented by this instance.
        /// </summary>
        /// 
        /// <returns>
        /// The minute component, expressed as a value between 0 and 59.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public int Minute
        {
            get { return (int) (InternalTicks/600000000L%60L); }
        }
 
        /// <summary>
        /// Gets the month component of the date represented by this instance.
        /// </summary>
        /// 
        /// <returns>
        /// The month component, expressed as a value between 1 and 12.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public int Month
        {
            get { return GetDatePart(2); }
        }
 
        /// <summary>
        /// Gets a <see cref="T:System.DateTime"/> object that is set to the current date and time on this computer, expressed as the local time.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.DateTime"/> whose value is the current local date and time.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public static DateTime Now
        {
            get
            {
                DateTime utcNow = UtcNow;
                const bool isAmbiguousLocalDst = false;
                long ticks2 = utcNow.Ticks;
                if (ticks2 > 3155378975999999999L)
                    return new DateTime(3155378975999999999L, DateTimeKind.Local);
                if (ticks2 < 0L)
                    return new DateTime(0L, DateTimeKind.Local);
                else
                    return new DateTime(ticks2, DateTimeKind.Local, isAmbiguousLocalDst);
            }
        }
 
        /// <summary>
        /// Gets a <see cref="T:System.DateTime"/> object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.DateTime"/> whose value is the current UTC date and time.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public static DateTime UtcNow
        {
            get { return new DateTime((ulong) (GetSystemTimeAsFileTime() + 504911232000000000L | 4611686018427387904L)); }
        }
 
        /// <summary>
        /// Gets the seconds component of the date represented by this instance.
        /// </summary>
        /// 
        /// <returns>
        /// The seconds, between 0 and 59.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public int Second
        {
            get { return (int) (InternalTicks/10000000L%60L); }
        }
 
        /// <summary>
        /// Gets the number of ticks that represent the date and time of this instance.
        /// </summary>
        /// 
        /// <returns>
        /// The number of ticks that represent the date and time of this instance. The value is between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public long Ticks
        {
            get { return InternalTicks; }
        }
 
        /// <summary>
        /// Gets the time of day for this instance.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.TimeSpan"/> that represents the fraction of the day that has elapsed since midnight.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public TimeSpan TimeOfDay
        {
            get { return new TimeSpan(InternalTicks%864000000000L); }
        }
 
        /// <summary>
        /// Gets the current date.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.DateTime"/> set to today's date, with the time component set to 00:00:00.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public static DateTime Today
        {
            get { return Now.Date; }
        }
 
        /// <summary>
        /// Gets the year component of the date represented by this instance.
        /// </summary>
        /// 
        /// <returns>
        /// The year, between 1 and 9999.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public int Year
        {
            get { return GetDatePart(0); }
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.DateTime"/> structure to a specified number of ticks.
        /// </summary>
        /// <param name="ticks">A date and time expressed in 100-nanosecond units. </param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="ticks"/> is less than <see cref="F:System.DateTime.MinValue"/> or greater than <see cref="F:System.DateTime.MaxValue"/>. </exception>
        public DateTime(long ticks)
        {
            if (ticks < 0L || ticks > 3155378975999999999L)
                throw new ArgumentOutOfRangeException("ticks");
            dateData = (ulong) ticks;
        }
 
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        private DateTime(ulong dateData)
        {
            this.dateData = dateData;
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.DateTime"/> structure to a specified number of ticks and to Coordinated Universal Time (UTC) or local time.
        /// </summary>
        /// <param name="ticks">A date and time expressed in 100-nanosecond units. </param><param name="kind">One of the enumeration values that indicates whether <paramref name="ticks"/> specifies a local time, Coordinated Universal Time (UTC), or neither.</param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="ticks"/> is less than <see cref="F:System.DateTime.MinValue"/> or greater than <see cref="F:System.DateTime.MaxValue"/>. </exception><exception cref="T:System.ArgumentException"><paramref name="kind"/> is not one of the <see cref="T:System.DateTimeKind"/> values.</exception>
        public DateTime(long ticks, DateTimeKind kind)
        {
            if (ticks < 0L || ticks > 3155378975999999999L)
                throw new ArgumentOutOfRangeException("ticks");
            if (kind < DateTimeKind.Unspecified || kind > DateTimeKind.Local)
                throw new ArgumentException("kind");
            dateData = (ulong) (ticks | (long) kind << 62);
        }
 
        internal DateTime(long ticks, DateTimeKind kind, bool isAmbiguousDst)
        {
            if (ticks < 0L || ticks > 3155378975999999999L)
                throw new ArgumentOutOfRangeException("ticks");
            dateData = (ulong) (ticks | (isAmbiguousDst ? -4611686018427387904L : long.MinValue));
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.DateTime"/> structure to the specified year, month, and day.
        /// </summary>
        /// <param name="year">The year (1 through 9999). </param><param name="month">The month (1 through 12). </param><param name="day">The day (1 through the number of days in <paramref name="month"/>). </param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="year"/> is less than 1 or greater than 9999.-or- <paramref name="month"/> is less than 1 or greater than 12.-or- <paramref name="day"/> is less than 1 or greater than the number of days in <paramref name="month"/>. </exception><exception cref="T:System.ArgumentException">The specified parameters evaluate to less than <see cref="F:System.DateTime.MinValue"/> or more than <see cref="F:System.DateTime.MaxValue"/>. </exception>
        public DateTime(int year, int month, int day)
        {
            dateData = (ulong) DateToTicks(year, month, day);
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.DateTime"/> structure to the specified year, month, and day for the specified calendar.
        /// </summary>
        /// <param name="year">The year (1 through the number of years in <paramref name="calendar"/>). </param><param name="month">The month (1 through the number of months in <paramref name="calendar"/>). </param><param name="day">The day (1 through the number of days in <paramref name="month"/>). </param><param name="calendar">The calendar that is used to interpret <paramref name="year"/>, <paramref name="month"/>, and <paramref name="day"/>. </param><exception cref="T:System.ArgumentNullException"><paramref name="calendar"/> is null.</exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="year"/> is not in the range supported by <paramref name="calendar"/>.-or- <paramref name="month"/> is less than 1 or greater than the number of months in <paramref name="calendar"/>.-or- <paramref name="day"/> is less than 1 or greater than the number of days in <paramref name="month"/>. </exception><exception cref="T:System.ArgumentException">The specified parameters evaluate to less than <see cref="F:System.DateTime.MinValue"/> or more than <see cref="F:System.DateTime.MaxValue"/>. </exception>
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public DateTime(int year, int month, int day, Calendar calendar)
        {
            this = new DateTime(year, month, day, 0, 0, 0, calendar);
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.DateTime"/> structure to the specified year, month, day, hour, minute, and second.
        /// </summary>
        /// <param name="year">The year (1 through 9999). </param><param name="month">The month (1 through 12). </param><param name="day">The day (1 through the number of days in <paramref name="month"/>). </param><param name="hour">The hours (0 through 23). </param><param name="minute">The minutes (0 through 59). </param><param name="second">The seconds (0 through 59). </param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="year"/> is less than 1 or greater than 9999. -or- <paramref name="month"/> is less than 1 or greater than 12. -or- <paramref name="day"/> is less than 1 or greater than the number of days in <paramref name="month"/>.-or- <paramref name="hour"/> is less than 0 or greater than 23. -or- <paramref name="minute"/> is less than 0 or greater than 59. -or- <paramref name="second"/> is less than 0 or greater than 59. </exception><exception cref="T:System.ArgumentException">The specified parameters evaluate to less than <see cref="F:System.DateTime.MinValue"/> or more than <see cref="F:System.DateTime.MaxValue"/>. </exception>
        public DateTime(int year, int month, int day, int hour, int minute, int second)
        {
            dateData = (ulong) (DateToTicks(year, month, day) + TimeToTicks(hour, minute, second));
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.DateTime"/> structure to the specified year, month, day, hour, minute, second, and Coordinated Universal Time (UTC) or local time.
        /// </summary>
        /// <param name="year">The year (1 through 9999). </param><param name="month">The month (1 through 12). </param><param name="day">The day (1 through the number of days in <paramref name="month"/>). </param><param name="hour">The hours (0 through 23). </param><param name="minute">The minutes (0 through 59). </param><param name="second">The seconds (0 through 59). </param><param name="kind">One of the enumeration values that indicates whether <paramref name="year"/>, <paramref name="month"/>, <paramref name="day"/>, <paramref name="hour"/>, <paramref name="minute"/> and <paramref name="second"/> specify a local time, Coordinated Universal Time (UTC), or neither.</param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="year"/> is less than 1 or greater than 9999. -or- <paramref name="month"/> is less than 1 or greater than 12. -or- <paramref name="day"/> is less than 1 or greater than the number of days in <paramref name="month"/>.-or- <paramref name="hour"/> is less than 0 or greater than 23. -or- <paramref name="minute"/> is less than 0 or greater than 59. -or- <paramref name="second"/> is less than 0 or greater than 59. </exception><exception cref="T:System.ArgumentException">The specified time parameters evaluate to less than <see cref="F:System.DateTime.MinValue"/> or more than <see cref="F:System.DateTime.MaxValue"/>. -or-<paramref name="kind"/> is not one of the <see cref="T:System.DateTimeKind"/> values.</exception>
        public DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind)
        {
            if (kind < DateTimeKind.Unspecified || kind > DateTimeKind.Local)
                throw new ArgumentException("kind");
            dateData = (ulong) (DateToTicks(year, month, day) + TimeToTicks(hour, minute, second) | (long) kind << 62);
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.DateTime"/> structure to the specified year, month, day, hour, minute, and second for the specified calendar.
        /// </summary>
        /// <param name="year">The year (1 through the number of years in <paramref name="calendar"/>). </param><param name="month">The month (1 through the number of months in <paramref name="calendar"/>). </param><param name="day">The day (1 through the number of days in <paramref name="month"/>). </param><param name="hour">The hours (0 through 23). </param><param name="minute">The minutes (0 through 59). </param><param name="second">The seconds (0 through 59). </param><param name="calendar">The calendar that is used to interpret <paramref name="year"/>, <paramref name="month"/>, and <paramref name="day"/>. </param><exception cref="T:System.ArgumentNullException"><paramref name="calendar"/> is null. </exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="year"/> is not in the range supported by <paramref name="calendar"/>.-or- <paramref name="month"/> is less than 1 or greater than the number of months in <paramref name="calendar"/>.-or- <paramref name="day"/> is less than 1 or greater than the number of days in <paramref name="month"/>.-or- <paramref name="hour"/> is less than 0 or greater than 23 -or- <paramref name="minute"/> is less than 0 or greater than 59. -or- <paramref name="second"/> is less than 0 or greater than 59. </exception><exception cref="T:System.ArgumentException">The specified parameters evaluate to less than <see cref="F:System.DateTime.MinValue"/> or more than <see cref="F:System.DateTime.MaxValue"/>. </exception>
        public DateTime(int year, int month, int day, int hour, int minute, int second, Calendar calendar)
        {
            if (calendar == null)
                throw new ArgumentNullException("calendar");
            dateData = (ulong) calendar.ToDateTime(year, month, day, hour, minute, second, 0).Ticks;
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.DateTime"/> structure to the specified year, month, day, hour, minute, second, and millisecond.
        /// </summary>
        /// <param name="year">The year (1 through 9999). </param><param name="month">The month (1 through 12). </param><param name="day">The day (1 through the number of days in <paramref name="month"/>). </param><param name="hour">The hours (0 through 23). </param><param name="minute">The minutes (0 through 59). </param><param name="second">The seconds (0 through 59). </param><param name="millisecond">The milliseconds (0 through 999). </param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="year"/> is less than 1 or greater than 9999.-or- <paramref name="month"/> is less than 1 or greater than 12.-or- <paramref name="day"/> is less than 1 or greater than the number of days in <paramref name="month"/>.-or- <paramref name="hour"/> is less than 0 or greater than 23.-or- <paramref name="minute"/> is less than 0 or greater than 59.-or- <paramref name="second"/> is less than 0 or greater than 59.-or- <paramref name="millisecond"/> is less than 0 or greater than 999. </exception><exception cref="T:System.ArgumentException">The specified parameters evaluate to less than <see cref="F:System.DateTime.MinValue"/> or more than <see cref="F:System.DateTime.MaxValue"/>. </exception>
        public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond)
        {
            if (millisecond < 0 || millisecond >= 1000)
            {
                throw new ArgumentOutOfRangeException("millisecond");
            }
            else
            {
                long num = DateToTicks(year, month, day) + TimeToTicks(hour, minute, second) + millisecond*10000L;
                if (num < 0L || num > 3155378975999999999L)
                    throw new ArgumentException();
                dateData = (ulong) num;
            }
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.DateTime"/> structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time.
        /// </summary>
        /// <param name="year">The year (1 through 9999). </param><param name="month">The month (1 through 12). </param><param name="day">The day (1 through the number of days in <paramref name="month"/>). </param><param name="hour">The hours (0 through 23). </param><param name="minute">The minutes (0 through 59). </param><param name="second">The seconds (0 through 59). </param><param name="millisecond">The milliseconds (0 through 999). </param><param name="kind">One of the enumeration values that indicates whether <paramref name="year"/>, <paramref name="month"/>, <paramref name="day"/>, <paramref name="hour"/>, <paramref name="minute"/>, <paramref name="second"/>, and <paramref name="millisecond"/> specify a local time, Coordinated Universal Time (UTC), or neither.</param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="year"/> is less than 1 or greater than 9999.-or- <paramref name="month"/> is less than 1 or greater than 12.-or- <paramref name="day"/> is less than 1 or greater than the number of days in <paramref name="month"/>.-or- <paramref name="hour"/> is less than 0 or greater than 23.-or- <paramref name="minute"/> is less than 0 or greater than 59.-or- <paramref name="second"/> is less than 0 or greater than 59.-or- <paramref name="millisecond"/> is less than 0 or greater than 999. </exception><exception cref="T:System.ArgumentException">The specified time parameters evaluate to less than <see cref="F:System.DateTime.MinValue"/> or more than <see cref="F:System.DateTime.MaxValue"/>. -or-<paramref name="kind"/> is not one of the <see cref="T:System.DateTimeKind"/> values.</exception>
        public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond,
                        DateTimeKind kind)
        {
            if (millisecond < 0 || millisecond >= 1000)
            {
                throw new ArgumentOutOfRangeException("millisecond");
            }
            else
            {
                if (kind < DateTimeKind.Unspecified || kind > DateTimeKind.Local)
                    throw new ArgumentException("kind");
                long num = DateToTicks(year, month, day) + TimeToTicks(hour, minute, second) + millisecond*10000L;
                if (num < 0L || num > 3155378975999999999L)
                    throw new ArgumentException();
                dateData = (ulong) (num | (long) kind << 62);
            }
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.DateTime"/> structure to the specified year, month, day, hour, minute, second, and millisecond for the specified calendar.
        /// </summary>
        /// <param name="year">The year (1 through the number of years in <paramref name="calendar"/>). </param><param name="month">The month (1 through the number of months in <paramref name="calendar"/>). </param><param name="day">The day (1 through the number of days in <paramref name="month"/>). </param><param name="hour">The hours (0 through 23). </param><param name="minute">The minutes (0 through 59). </param><param name="second">The seconds (0 through 59). </param><param name="millisecond">The milliseconds (0 through 999). </param><param name="calendar">The calendar that is used to interpret <paramref name="year"/>, <paramref name="month"/>, and <paramref name="day"/>.</param><exception cref="T:System.ArgumentNullException"><paramref name="calendar"/> is null. </exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="year"/> is not in the range supported by <paramref name="calendar"/>.-or- <paramref name="month"/> is less than 1 or greater than the number of months in <paramref name="calendar"/>.-or- <paramref name="day"/> is less than 1 or greater than the number of days in <paramref name="month"/>.-or- <paramref name="hour"/> is less than 0 or greater than 23.-or- <paramref name="minute"/> is less than 0 or greater than 59.-or- <paramref name="second"/> is less than 0 or greater than 59.-or- <paramref name="millisecond"/> is less than 0 or greater than 999. </exception><exception cref="T:System.ArgumentException">The specified parameters evaluate to less than <see cref="F:System.DateTime.MinValue"/> or more than <see cref="F:System.DateTime.MaxValue"/>. </exception>
        public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond,
                        Calendar calendar)
        {
            if (calendar == null)
                throw new ArgumentNullException("calendar");
            if (millisecond < 0 || millisecond >= 1000)
            {
                throw new ArgumentOutOfRangeException("millisecond");
            }
            else
            {
                long num = calendar.ToDateTime(year, month, day, hour, minute, second, 0).Ticks + millisecond*10000L;
                if (num < 0L || num > 3155378975999999999L)
                    throw new ArgumentException();
                dateData = (ulong) num;
            }
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.DateTime"/> structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time for the specified calendar.
        /// </summary>
        /// <param name="year">The year (1 through the number of years in <paramref name="calendar"/>). </param><param name="month">The month (1 through the number of months in <paramref name="calendar"/>). </param><param name="day">The day (1 through the number of days in <paramref name="month"/>). </param><param name="hour">The hours (0 through 23). </param><param name="minute">The minutes (0 through 59). </param><param name="second">The seconds (0 through 59). </param><param name="millisecond">The milliseconds (0 through 999). </param><param name="calendar">The calendar that is used to interpret <paramref name="year"/>, <paramref name="month"/>, and <paramref name="day"/>.</param><param name="kind">One of the enumeration values that indicates whether <paramref name="year"/>, <paramref name="month"/>, <paramref name="day"/>, <paramref name="hour"/>, <paramref name="minute"/>, <paramref name="second"/>, and <paramref name="millisecond"/> specify a local time, Coordinated Universal Time (UTC), or neither.</param><exception cref="T:System.ArgumentNullException"><paramref name="calendar"/> is null. </exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="year"/> is not in the range supported by <paramref name="calendar"/>.-or- <paramref name="month"/> is less than 1 or greater than the number of months in <paramref name="calendar"/>.-or- <paramref name="day"/> is less than 1 or greater than the number of days in <paramref name="month"/>.-or- <paramref name="hour"/> is less than 0 or greater than 23.-or- <paramref name="minute"/> is less than 0 or greater than 59.-or- <paramref name="second"/> is less than 0 or greater than 59.-or- <paramref name="millisecond"/> is less than 0 or greater than 999. </exception><exception cref="T:System.ArgumentException">The specified time parameters evaluate to less than <see cref="F:System.DateTime.MinValue"/> or more than <see cref="F:System.DateTime.MaxValue"/>. -or-<paramref name="kind"/> is not one of the <see cref="T:System.DateTimeKind"/> values.</exception>
        public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond,
                        Calendar calendar, DateTimeKind kind)
        {
            if (calendar == null)
                throw new ArgumentNullException("calendar");
            if (millisecond < 0 || millisecond >= 1000)
            {
                throw new ArgumentOutOfRangeException("millisecond");
            }
            else
            {
                if (kind < DateTimeKind.Unspecified || kind > DateTimeKind.Local)
                    throw new ArgumentException("kind");
                long num = calendar.ToDateTime(year, month, day, hour, minute, second, 0).Ticks + millisecond*10000L;
                if (num < 0L || num > 3155378975999999999L)
                    throw new ArgumentException();
                dateData = (ulong) (num | (long) kind << 62);
            }
        }
 
        private DateTime(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new ArgumentNullException("info");
            bool flag1 = false;
            bool flag2 = false;
            long num1 = 0L;
            ulong num2 = 0UL;
            SerializationInfoEnumerator enumerator = info.GetEnumerator();
            while (enumerator.MoveNext())
            {
                switch (enumerator.Name)
                {
                    case "ticks":
                        num1 = Convert.ToInt64(enumerator.Value, CultureInfo.InvariantCulture);
                        flag1 = true;
                        continue;
                    case "dateData":
                        num2 = Convert.ToUInt64(enumerator.Value, CultureInfo.InvariantCulture);
                        flag2 = true;
                        continue;
                    default:
                        continue;
                }
            }
            if (flag2)
            {
                dateData = num2;
            }
            else
            {
                if (!flag1)
                    throw new SerializationException();
                dateData = (ulong) num1;
            }
            long internalTicks = InternalTicks;
            if (internalTicks < 0L || internalTicks > 3155378975999999999L)
                throw new SerializationException();
        }
 
        /// <summary>
        /// Adds a specified time interval to a specified date and time, yielding a new date and time.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.DateTime"/> that is the sum of the values of <paramref name="d"/> and <paramref name="t"/>.
        /// </returns>
        /// <param name="d">A <see cref="T:System.DateTime"/>. </param><param name="t">A <see cref="T:System.TimeSpan"/>. </param><exception cref="T:System.ArgumentOutOfRangeException">The resulting <see cref="T:System.DateTime"/> is less than <see cref="F:System.DateTime.MinValue"/> or greater than <see cref="F:System.DateTime.MaxValue"/>. </exception><filterpriority>3</filterpriority>
        public static DateTime operator +(DateTime d, TimeSpan t)
        {
            long internalTicks = d.InternalTicks;
            long num = t.Ticks;
            if (num > 3155378975999999999L - internalTicks || num < -internalTicks)
                throw new ArgumentOutOfRangeException("t");
            else
                return new DateTime((ulong) (internalTicks + num) | d.InternalKind);
        }
 
        /// <summary>
        /// Subtracts a specified time interval from a specified date and time and returns a new date and time.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.DateTime"/> whose value is the value of <paramref name="d"/> minus the value of <paramref name="t"/>.
        /// </returns>
        /// <param name="d">A <see cref="T:System.DateTime"/>. </param><param name="t">A <see cref="T:System.TimeSpan"/>. </param><exception cref="T:System.ArgumentOutOfRangeException">The resulting <see cref="T:System.DateTime"/> is less than <see cref="F:System.DateTime.MinValue"/> or greater than <see cref="F:System.DateTime.MaxValue"/>. </exception><filterpriority>3</filterpriority>
        public static DateTime operator -(DateTime d, TimeSpan t)
        {
            long internalTicks = d.InternalTicks;
            long num = t.Ticks;
            if (internalTicks < num || internalTicks - 3155378975999999999L > num)
                throw new ArgumentOutOfRangeException("t");
            else
                return new DateTime((ulong) (internalTicks - num) | d.InternalKind);
        }
 
        /// <summary>
        /// Subtracts a specified date and time from another specified date and time and returns a time interval.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.TimeSpan"/> that is the time interval between <paramref name="d1"/> and <paramref name="d2"/>; that is, <paramref name="d1"/> minus <paramref name="d2"/>.
        /// </returns>
        /// <param name="d1">A <see cref="T:System.DateTime"/> (the minuend). </param><param name="d2">A <see cref="T:System.DateTime"/> (the subtrahend). </param><filterpriority>3</filterpriority>
        [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
        public static TimeSpan operator -(DateTime d1, DateTime d2)
        {
            return new TimeSpan(d1.InternalTicks - d2.InternalTicks);
        }
 
        /// <summary>
        /// Determines whether two specified instances of <see cref="T:System.DateTime"/> are equal.
        /// </summary>
        /// 
        /// <returns>
        /// true if <paramref name="d1"/> and <paramref name="d2"/> represent the same date and time; otherwise, false.
        /// </returns>
        /// <param name="d1">A <see cref="T:System.DateTime"/>. </param><param name="d2">A <see cref="T:System.DateTime"/>. </param><filterpriority>3</filterpriority>
        [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
        public static bool operator ==(DateTime d1, DateTime d2)
        {
            return d1.InternalTicks == d2.InternalTicks;
        }
 
        /// <summary>
        /// Determines whether two specified instances of <see cref="T:System.DateTime"/> are not equal.
        /// </summary>
        /// 
        /// <returns>
        /// true if <paramref name="d1"/> and <paramref name="d2"/> do not represent the same date and time; otherwise, false.
        /// </returns>
        /// <param name="d1">A <see cref="T:System.DateTime"/>. </param><param name="d2">A <see cref="T:System.DateTime"/>. </param><filterpriority>3</filterpriority>
        [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
        public static bool operator !=(DateTime d1, DateTime d2)
        {
            return d1.InternalTicks != d2.InternalTicks;
        }
 
        /// <summary>
        /// Determines whether one specified <see cref="T:System.DateTime"/> is less than another specified <see cref="T:System.DateTime"/>.
        /// </summary>
        /// 
        /// <returns>
        /// true if <paramref name="t1"/> is less than <paramref name="t2"/>; otherwise, false.
        /// </returns>
        /// <param name="t1">A <see cref="T:System.DateTime"/>. </param><param name="t2">A <see cref="T:System.DateTime"/>. </param><filterpriority>3</filterpriority>
        [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
        public static bool operator <(DateTime t1, DateTime t2)
        {
            return t1.InternalTicks < t2.InternalTicks;
        }
 
        /// <summary>
        /// Determines whether one specified <see cref="T:System.DateTime"/> is less than or equal to another specified <see cref="T:System.DateTime"/>.
        /// </summary>
        /// 
        /// <returns>
        /// true if <paramref name="t1"/> is less than or equal to <paramref name="t2"/>; otherwise, false.
        /// </returns>
        /// <param name="t1">A <see cref="T:System.DateTime"/>. </param><param name="t2">A <see cref="T:System.DateTime"/>. </param><filterpriority>3</filterpriority>
        [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
        public static bool operator <=(DateTime t1, DateTime t2)
        {
            return t1.InternalTicks <= t2.InternalTicks;
        }
 
        /// <summary>
        /// Determines whether one specified <see cref="T:System.DateTime"/> is greater than another specified <see cref="T:System.DateTime"/>.
        /// </summary>
        /// 
        /// <returns>
        /// true if <paramref name="t1"/> is greater than <paramref name="t2"/>; otherwise, false.
        /// </returns>
        /// <param name="t1">A <see cref="T:System.DateTime"/>. </param><param name="t2">A <see cref="T:System.DateTime"/>. </param><filterpriority>3</filterpriority>
        [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
        public static bool operator >(DateTime t1, DateTime t2)
        {
            return t1.InternalTicks > t2.InternalTicks;
        }
 
        /// <summary>
        /// Determines whether one specified <see cref="T:System.DateTime"/> is greater than or equal to another specified <see cref="T:System.DateTime"/>.
        /// </summary>
        /// 
        /// <returns>
        /// true if <paramref name="t1"/> is greater than or equal to <paramref name="t2"/>; otherwise, false.
        /// </returns>
        /// <param name="t1">A <see cref="T:System.DateTime"/>. </param><param name="t2">A <see cref="T:System.DateTime"/>. </param><filterpriority>3</filterpriority>
        [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
        public static bool operator >=(DateTime t1, DateTime t2)
        {
            return t1.InternalTicks >= t2.InternalTicks;
        }
 
        /// <summary>
        /// Returns a new <see cref="T:System.DateTime"/> that adds the value of the specified <see cref="T:System.TimeSpan"/> to the value of this instance.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.DateTime"/> whose value is the sum of the date and time represented by this instance and the time interval represented by <paramref name="value"/>.
        /// </returns>
        /// <param name="value">A <see cref="T:System.TimeSpan"/> object that represents a positive or negative time interval. </param><exception cref="T:System.ArgumentOutOfRangeException">The resulting <see cref="T:System.DateTime"/> is less than <see cref="F:System.DateTime.MinValue"/> or greater than <see cref="F:System.DateTime.MaxValue"/>. </exception><filterpriority>2</filterpriority>
        public DateTime Add(TimeSpan value)
        {
            return AddTicks(value.Ticks);
        }
 
        private DateTime Add(double value, int scale)
        {
            var num = (long) (value*scale + (value >= 0.0 ? 0.5 : -0.5));
            if (num <= -315537897600000L || num >= 315537897600000L)
                throw new ArgumentOutOfRangeException("value");
            else
                return AddTicks(num*10000L);
        }
 
        /// <summary>
        /// Returns a new <see cref="T:System.DateTime"/> that adds the specified number of days to the value of this instance.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.DateTime"/> whose value is the sum of the date and time represented by this instance and the number of days represented by <paramref name="value"/>.
        /// </returns>
        /// <param name="value">A number of whole and fractional days. The <paramref name="value"/> parameter can be negative or positive. </param><exception cref="T:System.ArgumentOutOfRangeException">The resulting <see cref="T:System.DateTime"/> is less than <see cref="F:System.DateTime.MinValue"/> or greater than <see cref="F:System.DateTime.MaxValue"/>. </exception><filterpriority>2</filterpriority>
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public DateTime AddDays(double value)
        {
            return Add(value, 86400000);
        }
 
        /// <summary>
        /// Returns a new <see cref="T:System.DateTime"/> that adds the specified number of hours to the value of this instance.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.DateTime"/> whose value is the sum of the date and time represented by this instance and the number of hours represented by <paramref name="value"/>.
        /// </returns>
        /// <param name="value">A number of whole and fractional hours. The <paramref name="value"/> parameter can be negative or positive. </param><exception cref="T:System.ArgumentOutOfRangeException">The resulting <see cref="T:System.DateTime"/> is less than <see cref="F:System.DateTime.MinValue"/> or greater than <see cref="F:System.DateTime.MaxValue"/>. </exception><filterpriority>2</filterpriority>
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public DateTime AddHours(double value)
        {
            return Add(value, 3600000);
        }
 
        /// <summary>
        /// Returns a new <see cref="T:System.DateTime"/> that adds the specified number of milliseconds to the value of this instance.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.DateTime"/> whose value is the sum of the date and time represented by this instance and the number of milliseconds represented by <paramref name="value"/>.
        /// </returns>
        /// <param name="value">A number of whole and fractional milliseconds. The <paramref name="value"/> parameter can be negative or positive. Note that this value is rounded to the nearest integer.</param><exception cref="T:System.ArgumentOutOfRangeException">The resulting <see cref="T:System.DateTime"/> is less than <see cref="F:System.DateTime.MinValue"/> or greater than <see cref="F:System.DateTime.MaxValue"/>. </exception><filterpriority>2</filterpriority>
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public DateTime AddMilliseconds(double value)
        {
            return Add(value, 1);
        }
 
        /// <summary>
        /// Returns a new <see cref="T:System.DateTime"/> that adds the specified number of minutes to the value of this instance.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.DateTime"/> whose value is the sum of the date and time represented by this instance and the number of minutes represented by <paramref name="value"/>.
        /// </returns>
        /// <param name="value">A number of whole and fractional minutes. The <paramref name="value"/> parameter can be negative or positive. </param><exception cref="T:System.ArgumentOutOfRangeException">The resulting <see cref="T:System.DateTime"/> is less than <see cref="F:System.DateTime.MinValue"/> or greater than <see cref="F:System.DateTime.MaxValue"/>. </exception><filterpriority>2</filterpriority>
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public DateTime AddMinutes(double value)
        {
            return Add(value, 60000);
        }
 
        /// <summary>
        /// Returns a new <see cref="T:System.DateTime"/> that adds the specified number of months to the value of this instance.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.DateTime"/> whose value is the sum of the date and time represented by this instance and <paramref name="months"/>.
        /// </returns>
        /// <param name="months">A number of months. The <paramref name="months"/> parameter can be negative or positive. </param><exception cref="T:System.ArgumentOutOfRangeException">The resulting <see cref="T:System.DateTime"/> is less than <see cref="F:System.DateTime.MinValue"/> or greater than <see cref="F:System.DateTime.MaxValue"/>.-or- <paramref name="months"/> is less than -120,000 or greater than 120,000. </exception><filterpriority>2</filterpriority>
        public DateTime AddMonths(int months)
        {
            if (months < -120000 || months > 120000)
                throw new ArgumentOutOfRangeException("months");
            int datePart1 = GetDatePart(0);
            int datePart2 = GetDatePart(2);
            int day = GetDatePart(3);
            int num1 = datePart2 - 1 + months;
            int month;
            int year;
            if (num1 >= 0)
            {
                month = num1%12 + 1;
                year = datePart1 + num1/12;
            }
            else
            {
                month = 12 + (num1 + 1)%12;
                year = datePart1 + (num1 - 11)/12;
            }
            if (year < 1 || year > 9999)
                throw new ArgumentOutOfRangeException("months");
            int num2 = DaysInMonth(year, month);
            if (day > num2)
                day = num2;
            return new DateTime((ulong) (DateToTicks(year, month, day) + InternalTicks%864000000000L) | InternalKind);
        }
 
        /// <summary>
        /// Returns a new <see cref="T:System.DateTime"/> that adds the specified number of seconds to the value of this instance.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.DateTime"/> whose value is the sum of the date and time represented by this instance and the number of seconds represented by <paramref name="value"/>.
        /// </returns>
        /// <param name="value">A number of whole and fractional seconds. The <paramref name="value"/> parameter can be negative or positive. </param><exception cref="T:System.ArgumentOutOfRangeException">The resulting <see cref="T:System.DateTime"/> is less than <see cref="F:System.DateTime.MinValue"/> or greater than <see cref="F:System.DateTime.MaxValue"/>. </exception><filterpriority>2</filterpriority>
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public DateTime AddSeconds(double value)
        {
            return Add(value, 1000);
        }
 
        /// <summary>
        /// Returns a new <see cref="T:System.DateTime"/> that adds the specified number of ticks to the value of this instance.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="T:System.DateTime"/> whose value is the sum of the date and time represented by this instance and the time represented by <paramref name="value"/>.
        /// </returns>
        /// <param name="value">A number of 100-nanosecond ticks. The <paramref name="value"/> parameter can be positive or negative. </param><exception cref="T:System.ArgumentOutOfRangeException">The resulting <see cre

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

14   голосов , оценка 3.857 из 5
Похожие ответы