| | 48 | // static properties |
|---|
| | 49 | public static Date minDate() { return new Date(1, 1, 1901); } |
|---|
| | 50 | public static Date maxDate() { return new Date(31, 12, 2199); } |
|---|
| | 51 | public static Date Today { get { return new Date(DateTime.Today); } } |
|---|
| | 52 | public static bool IsLeapYear(int y) { return DateTime.IsLeapYear(y); } |
|---|
| | 53 | public static int DaysInMonth(int y, int m) { return DateTime.DaysInMonth(y, m); } |
|---|
| | 54 | |
|---|
| | 55 | public static Date endOfMonth(Date d) { return (d - d.Day + DaysInMonth(d.Year, d.Month)); } |
|---|
| | 56 | public static bool isEndOfMonth(Date d) { return (d.Day == DaysInMonth(d.Year, d.Month)); } |
|---|
| | 57 | |
|---|
| | 58 | //! next given weekday following or equal to the given date |
|---|
| | 59 | public static Date nextWeekday(Date d, DayOfWeek dayOfWeek) { |
|---|
| | 60 | int wd = dayOfWeek - d.DayOfWeek; |
|---|
| | 61 | return d + (wd >= 0 ? wd : (7 + wd)); |
|---|
| | 62 | } |
|---|
| | 63 | |
|---|
| | 64 | //! n-th given weekday in the given month and year, e.g., the 4th Thursday of March, 1998 was March 26th, 1998. |
|---|
| | 65 | public static Date nthWeekday(int nth, DayOfWeek dayOfWeek, int m, int y) { |
|---|
| | 66 | if (nth < 1 || nth > 5) |
|---|
| | 67 | throw new ArgumentException("Wrong n-th weekday in a given month/year: " + nth); |
|---|
| | 68 | DayOfWeek first = new DateTime(y, m, 1).DayOfWeek; |
|---|
| | 69 | int skip = nth - (dayOfWeek >= first ? 1 : 0); |
|---|
| | 70 | return new Date(1, m, y) + (int)(dayOfWeek - first + skip * 7); |
|---|
| | 71 | } |
|---|
| | 72 | |
|---|
| | 73 | public static int monthOffset(int m, bool leapYear) { |
|---|
| | 74 | int[] MonthOffset = { 0, 31, 59, 90, 120, 151, // Jan - Jun |
|---|
| | 75 | 181, 212, 243, 273, 304, 334, // Jun - Dec |
|---|
| | 76 | 365 // used in dayOfMonth to bracket day |
|---|
| | 77 | }; |
|---|
| | 78 | return (MonthOffset[m - 1] + ((leapYear && m > 1) ? 1 : 0)); |
|---|
| | 79 | } |
|---|
| | 80 | |
|---|
| | 81 | public static Date advance(Date d, int n, TimeUnit u) { |
|---|
| | 82 | switch (u) { |
|---|
| | 83 | case TimeUnit.Days: |
|---|
| | 84 | return d + n; |
|---|
| | 85 | case TimeUnit.Weeks: |
|---|
| | 86 | return d + 7 * n; |
|---|
| | 87 | case TimeUnit.Months: { DateTime t = d.date; return new Date(t.AddMonths(n)); } |
|---|
| | 88 | case TimeUnit.Years: { DateTime t = d.date; return new Date(t.AddYears(n)); } |
|---|
| | 89 | default: |
|---|
| | 90 | throw Error.UnknownTimeUnit(u); |
|---|
| | 91 | } |
|---|
| | 92 | } |
|---|
| | 93 | |
|---|
| | 94 | |
|---|
| | 95 | // operator overloads |
|---|
| 66 | | public int Day { get { return date.Day; } } |
|---|
| 67 | | public int Month { get { return date.Month; } } |
|---|
| 68 | | public int Year { get { return date.Year; } } |
|---|
| 69 | | public int DayOfYear { get { return date.DayOfYear; } } |
|---|
| 70 | | public int weekday() { return (int)date.DayOfWeek + 1; } // QL compatible definition |
|---|
| 71 | | public DayOfWeek DayOfWeek { get { return date.DayOfWeek; } } |
|---|
| 72 | | public static Date minDate() { return new Date(1, 1, 1901); } |
|---|
| 73 | | public static Date maxDate() { return new Date(31, 12, 2199); } |
|---|
| 74 | | public static Date Today { get { return new Date(DateTime.Today); } } |
|---|
| 75 | | |
|---|
| 76 | | public static bool IsLeapYear(int y) { return DateTime.IsLeapYear(y); } |
|---|
| 77 | | public static int DaysInMonth(int y, int m) { return DateTime.DaysInMonth(y, m); } |
|---|
| 78 | | |
|---|
| 79 | | //! next given weekday following or equal to the given date |
|---|
| 80 | | public Date nextWeekday(Date d, DayOfWeek dayOfWeek) |
|---|
| 81 | | { |
|---|
| 82 | | int wd = dayOfWeek - d.DayOfWeek; |
|---|
| 83 | | return d + (wd >= 0 ? wd : (7 + wd)); |
|---|
| 84 | | } |
|---|
| 85 | | //! n-th given weekday in the given month and year, e.g., the 4th Thursday of March, 1998 was March 26th, 1998. |
|---|
| 86 | | public static Date nthWeekday(int nth, DayOfWeek dayOfWeek, int m, int y) |
|---|
| 87 | | { |
|---|
| 88 | | if (nth < 1 || nth > 5) throw new ArgumentException("Wrong n-th weekday in a given month/year: " + nth); |
|---|
| 89 | | DayOfWeek first = new DateTime(y, m, 1).DayOfWeek; |
|---|
| 90 | | int skip = nth - (dayOfWeek >= first ? 1 : 0); |
|---|
| 91 | | return new Date(1, m, y) + (int)(dayOfWeek - first + skip * 7); |
|---|
| 92 | | } |
|---|
| 93 | | public static int monthOffset(int m, bool leapYear) |
|---|
| 94 | | { |
|---|
| 95 | | int[] MonthOffset = { |
|---|
| 96 | | 0, 31, 59, 90, 120, 151, // Jan - Jun |
|---|
| 97 | | 181, 212, 243, 273, 304, 334, // Jun - Dec |
|---|
| 98 | | 365 // used in dayOfMonth to bracket day |
|---|
| 99 | | }; |
|---|
| 100 | | return (MonthOffset[m - 1] + ((leapYear && m > 1) ? 1 : 0)); |
|---|
| 101 | | } |
|---|
| 102 | | public static Date endOfMonth(Date d) |
|---|
| 103 | | { |
|---|
| 104 | | return (d - d.Day + DaysInMonth(d.Year, d.Month)); |
|---|
| 105 | | } |
|---|
| 106 | | public static bool isEndOfMonth(Date d) { return (d.Day == DaysInMonth(d.Year, d.Month)); } |
|---|
| 107 | | |
|---|
| 108 | | public static Date advance(Date d, int n, TimeUnit u) |
|---|
| 109 | | { |
|---|
| 110 | | switch (u) |
|---|
| 111 | | { |
|---|
| 112 | | case TimeUnit.Days: |
|---|
| 113 | | return d + n; |
|---|
| 114 | | case TimeUnit.Weeks: |
|---|
| 115 | | return d + 7 * n; |
|---|
| 116 | | case TimeUnit.Months: |
|---|
| 117 | | { DateTime t = d.date; return new Date(t.AddMonths(n)); } |
|---|
| 118 | | case TimeUnit.Years: |
|---|
| 119 | | { DateTime t = d.date; return new Date(t.AddYears(n)); } |
|---|
| 120 | | default: |
|---|
| 121 | | throw Error.UnknownTimeUnit(u); |
|---|
| 122 | | } |
|---|
| 123 | | } |
|---|