Assembla home | Assembla project page
 

Changeset 205

Show
Ignore:
Timestamp:
06/15/08 18:12:36 (4 months ago)
Author:
snovik
Message:

New: Added implicit operator overload from Date to DateTime?. Now all Date types can be seamlessly used with any standard .NET library

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/QLNet/Time/Date.cs

    r136 r205  
    2626 
    2727        public Date() { }                                                       //! Default constructor returning a null date. 
    28         public Date(int serialNumber) 
    29         {                       //! Constructor taking a serial number as given by Excel. Serial numbers in Excel have a known problem with leap year 1900 
     28        //! Constructor taking a serial number as given by Excel.  
     29        // Serial numbers in Excel have a known problem with leap year 1900 
     30        public Date(int serialNumber) {                  
    3031            date = (new DateTime(1899, 12, 31)).AddDays(serialNumber - 1); 
    3132        } 
     
    3334        public Date(int d, int m, int y) :              //! More traditional constructor. 
    3435            this(new DateTime(y, m, d)) { } 
    35         public Date(DateTime d) 
    36         {                               //! System DateTime constructor 
     36        public Date(DateTime d) {                               //! System DateTime constructor 
    3737            date = d; 
    3838        } 
    3939 
    4040        public int serialNumber() { return (date - new DateTime(1899, 12, 31).Date).Days + 1; } 
     41        public int Day { get { return date.Day; } } 
     42        public int Month { get { return date.Month; } } 
     43        public int Year { get { return date.Year; } } 
     44        public int DayOfYear { get { return date.DayOfYear; } } 
     45        public int weekday() { return (int)date.DayOfWeek + 1; }       // QL compatible definition 
     46        public DayOfWeek DayOfWeek { get { return date.DayOfWeek; } } 
    4147 
     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 
    4296        public static int operator -(Date d1, Date d2) { return (d1.date - d2.date).Days; } 
    4397        public static Date operator +(Date d, int days) { DateTime t = d.date; return new Date(t.AddDays(days)); } 
     
    52106        public static Date Max(Date d1, Date d2) { return d1 > d2 ? d1 : d2; } 
    53107 
    54         public static bool operator ==(Date d1, Date d2) 
    55         { 
     108        // this is the overload for DateTime operations 
     109        public static implicit operator DateTime(Date d) { return d.date; } 
     110 
     111        public static bool operator ==(Date d1, Date d2) { 
    56112            return ((Object)d1 == null || (Object)d2 == null) ? 
    57113                   ((Object)d1 == null && (Object)d2 == null) : 
     
    64120        public static bool operator >=(Date d1, Date d2) { return (d1.date >= d2.date); } 
    65121 
    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         } 
    124122        public string ToLongDateString() { return date.ToLongDateString(); } 
    125123        public string ToShortDateString() { return date.ToShortDateString(); } 
     
    129127 
    130128        // IComparable interface 
    131         public int CompareTo(object obj) 
    132         { 
     129        public int CompareTo(object obj) { 
    133130            if (this < (Date)obj) 
    134131                return -1;