DateTime.FromFileTimeUtc should return a DateTime with DateTimeKind.Utc. Fixes #2936.
[mono.git] / mcs / class / corlib / System / DateTime.cs
index 0a81391957cc0cc1bda97f4915f60fc7afdebb64..07b247897c1616259461078bb01dc897185c1e03 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System.Collections;
+using System.Collections.Generic;
 using System.Globalization;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Text;
+using System.Runtime.Serialization;
 
 namespace System
 {
@@ -44,11 +45,18 @@ namespace System
        /// 
        [Serializable]
        [StructLayout (LayoutKind.Auto)]
-       public struct DateTime : IFormattable, IConvertible, IComparable, IComparable<DateTime>, IEquatable <DateTime>
+       public struct DateTime : IFormattable, IConvertible, IComparable, ISerializable, IComparable<DateTime>, IEquatable <DateTime>
        {
-               private TimeSpan ticks;
-
-               DateTimeKind kind;
+               //
+               // Encodes the DateTime in 64 bits, top two bits contain the DateTimeKind,
+               // the rest contains the 62 bit value for the ticks.   This reduces the
+               // memory usage from 16 to 8 bytes, see bug: 592221.   This also fixes the
+               // 622127 issue and simplifies the code in reflection.c to encode DateTimes
+               //
+               long encoded;
+               const long TicksMask = 0x3fffffffffffffff;
+               const long KindMask = unchecked ((long) 0xc000000000000000);
+               const int KindShift = 62;
 
                private const int dp400 = 146097;
                private const int dp100 = 36524;
@@ -73,16 +81,23 @@ namespace System
                private const double OAMinValue = -657435.0d;
                private const double OAMaxValue = 2958466.0d;
 
-               public static readonly DateTime MaxValue = new DateTime (false, new TimeSpan (MAX_VALUE_TICKS));
-               public static readonly DateTime MinValue = new DateTime (false, new TimeSpan (0));
+               public static readonly DateTime MaxValue = new DateTime (3155378975999999999);
+               public static readonly DateTime MinValue = new DateTime (0);
 
                // DateTime.Parse patterns
                // Patterns are divided to date and time patterns. The algorithm will
                // try combinations of these patterns. The algorithm also looks for
                // day of the week, AM/PM GMT and Z independently of the patterns.
                private static readonly string[] ParseTimeFormats = new string [] {
+                       "H:m:s.fff zzz",
                        "H:m:s.fffffffzzz",
                        "H:m:s.fffffff",
+                       "H:m:s.ffffff",
+                       "H:m:s.fffff",
+                       "H:m:s.ffff",
+                       "H:m:s.fff",
+                       "H:m:s.ff",
+                       "H:m:s.f",
                        "H:m:s tt zzz",
                        "H:m:szzz",
                        "H:m:s",
@@ -172,6 +187,16 @@ namespace System
                        "d/yy/MMM",
                        "yy/d/MMM",
                };
+               
+               private static readonly string[] ParseGenericYearMonthDayFormats = new string [] {
+                       "yyyy/M/dT",
+                       "yyyy/M/d",
+                       "M/yyyy/dT",
+                       "M/yyyy/d",
+                       "yyyy'\u5E74'M'\u6708'd'\u65E5",
+                       "yyyy'-'M'-'dT",
+                       "yyyy'-'M'-'d",
+               };
 
                // Patterns influenced by the MonthDayPattern in DateTimeFormatInfo.
                // Note that these patterns cannot be followed by the time.
@@ -215,7 +240,7 @@ namespace System
                        int M =1;
 
                        int[] days = daysmonth;
-                       int totaldays = this.ticks.Days;
+                       int totaldays = (int) ((encoded & TicksMask) / TimeSpan.TicksPerDay);
 
                        num400 = (totaldays / dp400);
                        totaldays -=  num400 * dp400;
@@ -251,6 +276,11 @@ namespace System
                        return totaldays +1; 
                }
 
+               static void InvalidTickValue (long ticks)
+               {
+                       string msg = Locale.GetText ("Value {0} is outside the valid range [0,{1}].", ticks, MAX_VALUE_TICKS);
+                       throw new ArgumentOutOfRangeException ("ticks", msg);
+               }
 
                // Constructors
                
@@ -260,13 +290,9 @@ namespace System
                /// 
                public DateTime (long ticks)
                {
-                       this.ticks = new TimeSpan (ticks);
-                       if (ticks < MinValue.Ticks || ticks > MaxValue.Ticks) {
-                               string msg = Locale.GetText ("Value {0} is outside the valid range [{1},{2}].", 
-                                       ticks, MinValue.Ticks, MaxValue.Ticks);
-                               throw new ArgumentOutOfRangeException ("ticks", msg);
-                       }
-                       kind = DateTimeKind.Unspecified;
+                       if (ticks < 0 || ticks > MAX_VALUE_TICKS)
+                               InvalidTickValue (ticks);
+                       encoded = ticks;
                }
 
                public DateTime (int year, int month, int day)
@@ -276,20 +302,18 @@ namespace System
                        : this (year, month, day, hour, minute, second, 0)      {}
 
                public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond)
-                       {
-                       if ( year < 1 || year > 9999 || 
-                               month < 1 || month >12  ||
-                               day < 1 || day > DaysInMonth(year, month) ||
-                               hour < 0 || hour > 23 ||
-                               minute < 0 || minute > 59 ||
-                               second < 0 || second > 59 ||
-                               millisecond < 0 || millisecond > 999)
+               {
+                       if (year < 1 || year > 9999 || 
+                           month < 1 || month >12  ||
+                           day < 1 || day > DaysInMonth(year, month) ||
+                           hour < 0 || hour > 23 ||
+                           minute < 0 || minute > 59 ||
+                           second < 0 || second > 59 ||
+                           millisecond < 0 || millisecond > 999)
                                throw new ArgumentOutOfRangeException ("Parameters describe an " +
-                                                                       "unrepresentable DateTime.");
-
-                       ticks = new TimeSpan (AbsoluteDays(year,month,day), hour, minute, second, millisecond);
+                                                                      "unrepresentable DateTime.");
 
-                       kind = DateTimeKind.Unspecified;
+                       encoded = new TimeSpan (AbsoluteDays (year,month,day), hour, minute, second, millisecond).Ticks;
                }
 
                public DateTime (int year, int month, int day, Calendar calendar)
@@ -306,129 +330,120 @@ namespace System
                {
                        if (calendar == null)
                                throw new ArgumentNullException ("calendar");
-                       ticks = calendar.ToDateTime (year, month, day, hour, minute, second, millisecond).ticks;
-                       kind = DateTimeKind.Unspecified;
+                       encoded = calendar.ToDateTime (year, month, day, hour, minute, second, millisecond).encoded;
                }
 
-               internal DateTime (bool check, TimeSpan value)
+               public DateTime (long ticks, DateTimeKind kind) 
                {
-                       if (check && (value.Ticks < MinValue.Ticks || value.Ticks > MaxValue.Ticks))
-                               throw new ArgumentOutOfRangeException ();
-
-                       ticks = value;
-
-                       kind = DateTimeKind.Unspecified;
-               }
+                       if (ticks < 0 || ticks > MAX_VALUE_TICKS)
+                               InvalidTickValue (ticks);
+                       if (kind < 0 || kind > DateTimeKind.Local)
+                               throw new ArgumentException ("Invalid DateTimeKind value.", "kind");
 
-               public DateTime (long ticks, DateTimeKind kind) : this (ticks)
-               {
-                       CheckDateTimeKind (kind);
-                       this.kind = kind;
+                       encoded = ((long)kind << KindShift) | ticks;
                }
 
                public DateTime (int year, int month, int day, int hour, int minute, int second, DateTimeKind kind)
                        : this (year, month, day, hour, minute, second)
                {
-                       CheckDateTimeKind (kind);
-                       this.kind = kind;
+                       if (kind < 0 || kind > DateTimeKind.Local)
+                               throw new ArgumentException ("Invalid DateTimeKind value.", "kind");
+                       encoded |= ((long)kind << KindShift);
                }
 
                public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind)
                        : this (year, month, day, hour, minute, second, millisecond)
                {
-                       CheckDateTimeKind (kind);
-                       this.kind = kind;
+                       if (kind < 0 || kind > DateTimeKind.Local)
+                               throw new ArgumentException ("Invalid DateTimeKind value.", "kind");
+                       encoded |= ((long)kind << KindShift);
                }
 
                public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar, DateTimeKind kind)
                        : this (year, month, day, hour, minute, second, millisecond, calendar)
                {
-                       CheckDateTimeKind (kind);
-                       this.kind = kind;
-               }                       
+                       if (kind < 0 || kind > DateTimeKind.Local)
+                               throw new ArgumentException ("Invalid DateTimeKind value.", "kind");
+                       encoded |= ((long)kind << KindShift);
+               }
 
+               //
+               // Not visible, but can be invoked during deserialization
+               //
+               DateTime (SerializationInfo info, StreamingContext context)
+               {
+                       if (info.HasKey ("dateData")){
+                               encoded = info.GetInt64 ("dateData");
+                       } else if (info.HasKey ("ticks")){
+                               encoded = info.GetInt64 ("ticks") & TicksMask;
+                       } else {
+                               encoded = 0;
+                       }
+               }
+               
+                             
                /* Properties  */
 
-               public DateTime Date 
-               {
-                       get     
-                       { 
+               public DateTime Date {
+                       get { 
                                DateTime ret = new DateTime (Year, Month, Day);
-                               ret.kind = kind;
+                               ret.encoded |= encoded & KindMask;
                                return ret;
                        }
                }
 
-               public int Month 
-               {
-                       get     
-                       { 
-                               return FromTicks(Which.Month); 
+               public int Month {
+                       get { 
+                               return FromTicks (Which.Month); 
                        }
                }
 
-               public int Day
-               {
-                       get 
-                       { 
-                               return FromTicks(Which.Day); 
+               public int Day {
+                       get { 
+                               return FromTicks (Which.Day); 
                        }
                }
 
-               public DayOfWeek DayOfWeek 
-               {
-                       get 
-                       { 
-                               return ( (DayOfWeek) ((ticks.Days+1) % 7) ); 
+               public DayOfWeek DayOfWeek {
+                       get {
+                               return (DayOfWeek) ((((encoded & TicksMask)/TimeSpan.TicksPerDay)+1) % 7);
                        }
                }
 
-               public int DayOfYear 
-               {
-                       get 
-                       { 
-                               return FromTicks(Which.DayYear); 
+               public int DayOfYear {
+                       get { 
+                               return FromTicks (Which.DayYear); 
                        }
                }
 
-               public TimeSpan TimeOfDay 
-               {
-                       get     
-                       { 
-                               return new TimeSpan(ticks.Ticks % TimeSpan.TicksPerDay );
+               public TimeSpan TimeOfDay {
+                       get { 
+                               return new TimeSpan ((encoded & TicksMask) % TimeSpan.TicksPerDay);
                        }
                        
                }
 
-               public int Hour 
-               {
-                       get 
-                       { 
-                               return ticks.Hours;
+               public int Hour {
+                       get { 
+                               return (int) ((encoded & TicksMask) % TimeSpan.TicksPerDay / TimeSpan.TicksPerHour);
                        }
                }
 
-               public int Minute 
-               {
-                       get 
-                       { 
-                               return ticks.Minutes;
+               public int Minute {
+                       get { 
+                               return (int)  ((encoded & TicksMask) % TimeSpan.TicksPerHour / TimeSpan.TicksPerMinute);
                        }
                }
 
-               public int Second 
-               {
-                       get     
-                       { 
-                               return ticks.Seconds;
+               public int Second {
+                       get { 
+                               return (int) ((encoded & TicksMask) % TimeSpan.TicksPerMinute / TimeSpan.TicksPerSecond);
                        }
                }
 
-               public int Millisecond 
-               {
-                       get 
-                       { 
-                               return ticks.Milliseconds;
+               public int Millisecond {
+                       get { 
+                               return (int) ((encoded & TicksMask) % TimeSpan.TicksPerSecond / TimeSpan.TicksPerMillisecond);
                        }
                }
                
@@ -447,10 +462,8 @@ namespace System
                static object to_local_time_span_object;
                static long last_now;
                
-               public static DateTime Now 
-               {
-                       get     
-                       {
+               public static DateTime Now {
+                       get {
                                long now = GetNow ();
                                DateTime dt = new DateTime (now);
 
@@ -462,47 +475,41 @@ namespace System
 
                                // This is boxed, so we avoid locking.
                                DateTime ret = dt + (TimeSpan) to_local_time_span_object;
-                               ret.kind = DateTimeKind.Local;
+                               ret.encoded |= ((long)DateTimeKind.Local << KindShift);
                                return ret;
                        }
                }
 
-               public long Ticks
-               { 
-                       get     
-                       { 
-                               return ticks.Ticks;
+               public long Ticks { 
+                       get { 
+                               return encoded & TicksMask;
                        }
                }
        
-               public static DateTime Today 
-               {
+               public static DateTime Today {
                        get {
                                DateTime now = Now;
                                DateTime today = new DateTime (now.Year, now.Month, now.Day);
-                               today.kind = now.kind;
+                               today.encoded |= ((long)DateTimeKind.Local << KindShift);
                                return today;
                        }
                }
 
-               public static DateTime UtcNow 
-               {
+               public static DateTime UtcNow {
                        get {
                                return new DateTime (GetNow (), DateTimeKind.Utc);
                        }
                }
 
-               public int Year 
-               {
-                       get 
-                       { 
-                               return FromTicks(Which.Year); 
+               public int Year {
+                       get { 
+                               return FromTicks (Which.Year); 
                        }
                }
 
                public DateTimeKind Kind {
                        get {
-                               return kind;
+                               return (DateTimeKind) ((ulong)encoded >> KindShift);
                        }
                }
 
@@ -511,7 +518,6 @@ namespace System
                public DateTime Add (TimeSpan value)
                {
                        DateTime ret = AddTicks (value.Ticks);
-                       ret.kind = kind;
                        return ret;
                }
 
@@ -522,11 +528,12 @@ namespace System
                
                public DateTime AddTicks (long value)
                {
-                       if ((value + ticks.Ticks) > MAX_VALUE_TICKS || (value + ticks.Ticks) < 0) {
+                       long res = value + (encoded & TicksMask);
+                       if (res < 0 || res > MAX_VALUE_TICKS)
                                throw new ArgumentOutOfRangeException();
-                       }
-                       DateTime ret = new DateTime (value + ticks.Ticks);
-                       ret.kind = kind;
+
+                       DateTime ret = new DateTime (res);
+                       ret.encoded |= (encoded & KindMask);
                        return ret;
                }
 
@@ -538,10 +545,10 @@ namespace System
                public DateTime AddMilliseconds (double value)
                {
                        if ((value * TimeSpan.TicksPerMillisecond) > long.MaxValue ||
-                                       (value * TimeSpan.TicksPerMillisecond) < long.MinValue) {
+                           (value * TimeSpan.TicksPerMillisecond) < long.MinValue) {
                                throw new ArgumentOutOfRangeException();
                        }
-                       long msticks = (long) (value * TimeSpan.TicksPerMillisecond);
+                       long msticks = (long) Math.Round (value * TimeSpan.TicksPerMillisecond);
 
                        return AddTicks (msticks);
                }
@@ -587,7 +594,7 @@ namespace System
                                day = maxday;
 
                        temp = new DateTime (year, month, day);
-                       temp.kind = kind;
+                       temp.encoded |= encoded & KindMask;
                        return  temp.Add (this.TimeOfDay);
                }
 
@@ -603,9 +610,12 @@ namespace System
 
                public static int Compare (DateTime t1, DateTime t2)
                {
-                       if (t1.ticks < t2.ticks) 
+                       long t1t = t1.encoded & TicksMask;
+                       long t2t = t2.encoded & TicksMask;
+                       
+                       if (t1t < t2t) 
                                return -1;
-                       else if (t1.ticks > t2.ticks
+                       else if (t1t > t2t
                                return 1;
                        else
                                return 0;
@@ -625,7 +635,7 @@ namespace System
 
                public bool IsDaylightSavingTime ()
                {
-                       if (kind == DateTimeKind.Utc)
+                       if ((int)((ulong)encoded >> KindShift) == (int) DateTimeKind.Utc)
                                return false;
                        return TimeZone.CurrentTimeZone.IsDaylightSavingTime (this);
                }
@@ -637,30 +647,26 @@ namespace System
 
                public bool Equals (DateTime value)
                {
-                       return value.ticks == ticks;
+                       return (value.encoded & TicksMask) == (encoded & TicksMask);
                }
 
                public long ToBinary ()
                {
-                       switch (kind) {
-                       case DateTimeKind.Utc:
-                               return Ticks | 0x4000000000000000;
-                       case DateTimeKind.Local:
+                       if ((encoded & ((long)DateTimeKind.Local << KindShift)) != 0)
                                return (long) ((ulong) ToUniversalTime ().Ticks | 0x8000000000000000);
-                       default:
-                               return Ticks;
-                       }
+                       
+                       return encoded;
                }
 
                public static DateTime FromBinary (long dateData)
                {
-                       switch ((ulong)dateData >> 62) {
-                       case 1:
-                               return new DateTime (dateData ^ 0x4000000000000000, DateTimeKind.Utc);
-                       case 0:
+                       switch ((ulong)dateData >> KindShift) {
+                       case 1: // Utc
+                               return new DateTime (dateData & TicksMask, DateTimeKind.Utc);
+                       case 0: // Unspecified
                                return new DateTime (dateData, DateTimeKind.Unspecified);
-                       default:
-                               return new DateTime (dateData & 0x3fffffffffffffff, DateTimeKind.Utc).ToLocalTime ();
+                       default: // Local
+                               return new DateTime (dateData & TicksMask, DateTimeKind.Utc).ToLocalTime ();
                        }
                }
 
@@ -688,12 +694,12 @@ namespace System
                        if (!(value is System.DateTime))
                                return false;
 
-                       return ((DateTime) value).ticks == ticks;
+                       return (((DateTime) value).encoded & TicksMask) == (encoded & TicksMask);
                }
 
                public static bool Equals (DateTime t1, DateTime t2 )
                {
-                       return (t1.ticks == t2.ticks );
+                       return (t1.encoded & TicksMask) == (t2.encoded & TicksMask);
                }
 
                public static DateTime FromFileTime (long fileTime) 
@@ -709,7 +715,7 @@ namespace System
                        if (fileTime < 0)
                                throw new ArgumentOutOfRangeException ("fileTime", "< 0");
 
-                       return new DateTime (w32file_epoch + fileTime);
+                       return new DateTime (w32file_epoch + fileTime, DateTimeKind.Utc);
                }
 
                public static DateTime FromOADate (double d)
@@ -755,10 +761,10 @@ namespace System
                {
                        DateTimeFormatInfo info = (DateTimeFormatInfo) provider.GetFormat (typeof(DateTimeFormatInfo));
 //                     return GetDateTimeFormats (info.GetAllDateTimePatterns ());
-                       ArrayList al = new ArrayList ();
+                       var l = new List<string> ();
                        foreach (char c in "dDgGfFmMrRstTuUyY")
-                               al.AddRange (GetDateTimeFormats (c, info));
-                       return al.ToArray (typeof (string)) as string [];
+                               l.AddRange (GetDateTimeFormats (c, info));
+                       return l.ToArray ();
                }
 
                public string[] GetDateTimeFormats(char format,IFormatProvider provider )
@@ -793,14 +799,9 @@ namespace System
                        return results;
                }
 
-               private void CheckDateTimeKind (DateTimeKind kind) {
-                       if ((kind != DateTimeKind.Unspecified) && (kind != DateTimeKind.Utc) && (kind != DateTimeKind.Local))
-                               throw new ArgumentException ("Invalid DateTimeKind value.", "kind");
-               }
-
                public override int GetHashCode ()
                {
-                       return (int) ticks.Ticks;
+                       return (int) encoded;
                }
 
                public TypeCode GetTypeCode ()
@@ -857,7 +858,7 @@ namespace System
                        DateTimeFormatInfo dfi = DateTimeFormatInfo.GetInstance (provider);
 
                        // Try first all the combinations of ParseAllDateFormats & ParseTimeFormats
-                       string[] allDateFormats = YearMonthDayFormats (dfi, setExceptionOnError, ref exception);
+                       string[] allDateFormats = YearMonthDayFormats (dfi);
                        if (allDateFormats == null){
                                result = MinValue;
                                return false;
@@ -936,16 +937,13 @@ namespace System
                        return ParseExact (s, format, provider, DateTimeStyles.None);
                }
 
-               private static string[] YearMonthDayFormats (DateTimeFormatInfo dfi, bool setExceptionOnError, ref Exception exc)
+               private static string[] YearMonthDayFormats (DateTimeFormatInfo dfi)
                {
                        int dayIndex = dfi.ShortDatePattern.IndexOf('d');
                        int monthIndex = dfi.ShortDatePattern.IndexOf('M');
                        int yearIndex = dfi.ShortDatePattern.IndexOf('y');
-                       if (dayIndex == -1 || monthIndex == -1 || yearIndex == -1){
-                               if (setExceptionOnError)
-                                       exc = new FormatException (Locale.GetText("Order of year, month and date is not defined by {0}", dfi.ShortDatePattern));
-                               return null;
-                       }
+                       if (dayIndex == -1 || monthIndex == -1 || yearIndex == -1)
+                               return ParseGenericYearMonthDayFormats;
 
                        if (yearIndex < monthIndex)
                                if (monthIndex < dayIndex)
@@ -954,9 +952,7 @@ namespace System
                                        return ParseYearDayMonthFormats;
                                else {
                                        // The year cannot be between the date and the month
-                                       if (setExceptionOnError)
-                                               exc = new FormatException (Locale.GetText("Order of date, year and month defined by {0} is not supported", dfi.ShortDatePattern));
-                                       return null;
+                                       return ParseGenericYearMonthDayFormats;
                                }
                        else if (dayIndex < monthIndex)
                                return ParseDayMonthYearFormats;
@@ -964,9 +960,7 @@ namespace System
                                return ParseMonthDayYearFormats;
                        else {
                                // The year cannot be between the month and the date
-                               if (setExceptionOnError)
-                                       exc = new FormatException (Locale.GetText("Order of month, year and date defined by {0} is not supported", dfi.ShortDatePattern));
-                               return null;
+                               return ParseGenericYearMonthDayFormats;
                        }
                }
 
@@ -1157,6 +1151,9 @@ namespace System
                        if (format == null)
                                return false;
 
+                       if (s == null)
+                               return false;
+                               
                        if ((style & DateTimeStyles.AllowLeadingWhite) != 0) {
                                format = format.TrimStart (null);
 
@@ -1713,22 +1710,19 @@ namespace System
                        bool adjustToUniversal = (style & DateTimeStyles.AdjustToUniversal) != 0;
                        
                        if (tzsign != -1) {
-                               long newticks = (result.ticks - dto.Offset).Ticks;
+                               long newticks = (result - dto.Offset).Ticks;
                                if (newticks < 0)
                                        newticks += TimeSpan.TicksPerDay;
-                               result = new DateTime (false, new TimeSpan (newticks));
-                               result.kind = DateTimeKind.Utc;
+                               result = new DateTime (newticks, DateTimeKind.Utc);
                                if ((style & DateTimeStyles.RoundtripKind) != 0)
                                        result = result.ToLocalTime ();
-                       }
-                       else if (useutc || ((style & DateTimeStyles.AssumeUniversal) != 0))
-                               result.kind = DateTimeKind.Utc;
+                       } else if (useutc || ((style & DateTimeStyles.AssumeUniversal) != 0))
+                               result.encoded |= ((long) DateTimeKind.Utc << KindShift);
                        else if ((style & DateTimeStyles.AssumeLocal) != 0)
-                               result.kind = DateTimeKind.Local;                                               
+                               result.encoded |= ((long) DateTimeKind.Local << KindShift);
 
                        bool adjustToLocal = !adjustToUniversal && (style & DateTimeStyles.RoundtripKind) == 0;
-                       if (result.kind != DateTimeKind.Unspecified)
-                       {                               
+                       if ((DateTimeKind)(((ulong) result.encoded >> KindShift)) != DateTimeKind.Unspecified) {
                                if (adjustToUniversal)
                                        result = result.ToUniversalTime ();
                                else if (adjustToLocal)
@@ -1736,6 +1730,7 @@ namespace System
                        }
                        return true;
                }
+               
 
                public static DateTime ParseExact (string s, string format,
                                                   IFormatProvider provider, DateTimeStyles style)
@@ -1868,16 +1863,18 @@ namespace System
                
                public TimeSpan Subtract (DateTime value)
                {
-                       return new TimeSpan (ticks.Ticks) - value.ticks;
+                       return new TimeSpan (Ticks) - new TimeSpan (value.Ticks);
                }
 
                public DateTime Subtract(TimeSpan value)
                {
-                       TimeSpan newticks;
+                       long newticks;
 
-                       newticks = (new TimeSpan (ticks.Ticks)) - value;
-                       DateTime ret = new DateTime (true,newticks);
-                       ret.kind = kind;
+                       newticks = Ticks - value.Ticks;
+                       if (newticks < 0 || newticks > MAX_VALUE_TICKS)
+                               throw new ArgumentOutOfRangeException ();
+                       DateTime ret = new DateTime (newticks);
+                       ret.encoded |= (encoded & KindMask);
                        return ret;
                }
 
@@ -2001,105 +1998,117 @@ namespace System
 
                public static DateTime operator +(DateTime d, TimeSpan t)
                {
-                       DateTime ret = new DateTime (true, d.ticks + t);
-                       ret.kind = d.kind;
-                       return ret;
+                       try {
+                               long res = checked ((d.encoded & TicksMask) + t.Ticks);
+                               if (res < 0 || res > MAX_VALUE_TICKS){
+                                       throw new ArgumentOutOfRangeException ();
+                               }
+                               
+                               return new DateTime (res, d.Kind);
+                       } catch (OverflowException){
+                               throw new ArgumentOutOfRangeException ();
+                       }
                }
 
                public static bool operator ==(DateTime d1, DateTime d2)
                {
-                       return (d1.ticks == d2.ticks);
+                       return ((d1.encoded & TicksMask) == (d2.encoded & TicksMask));
                }
 
                public static bool operator >(DateTime t1,DateTime t2)
                {
-                       return (t1.ticks > t2.ticks);
+                       return ((t1.encoded & TicksMask) > (t2.encoded & TicksMask));
                }
 
                public static bool operator >=(DateTime t1,DateTime t2)
                {
-                       return (t1.ticks >= t2.ticks);
+                       return ((t1.encoded & TicksMask) >= (t2.encoded & TicksMask));
                }
 
                public static bool operator !=(DateTime d1, DateTime d2)
                {
-                       return (d1.ticks != d2.ticks);
+                       return ((d1.encoded & TicksMask) != (d2.encoded & TicksMask));
                }
 
-               public static bool operator <(DateTime t1,      DateTime t2)
+               public static bool operator <(DateTime t1, DateTime t2)
                {
-                       return (t1.ticks < t2.ticks );
+                       return ((t1.encoded & TicksMask) < (t2.encoded & TicksMask));
                }
 
-               public static bool operator <=(DateTime t1,DateTime t2)
+               public static bool operator <=(DateTime t1, DateTime t2)
                {
-                       return (t1.ticks <= t2.ticks);
+                       return ((t1.encoded & TicksMask) <= (t2.encoded & TicksMask));
                }
 
-               public static TimeSpan operator -(DateTime d1,DateTime d2)
+               public static TimeSpan operator -(DateTime d1, DateTime d2)
                {
-                       return new TimeSpan((d1.ticks - d2.ticks).Ticks);
+                       return new TimeSpan ((d1.encoded & TicksMask) - (d2.encoded & TicksMask));
                }
 
-               public static DateTime operator -(DateTime d,TimeSpan t)
+               public static DateTime operator -(DateTime d, TimeSpan t)
                {
-                       DateTime ret = new DateTime (true, d.ticks - t);
-                       ret.kind = d.kind;
-                       return ret;
+                       try {
+                               long res = checked ((d.encoded & TicksMask) - t.Ticks);
+                               if (res < 0 || res > MAX_VALUE_TICKS)
+                                       throw new ArgumentOutOfRangeException ();
+                               return new DateTime (res, d.Kind);
+                       } catch (OverflowException){
+                               throw new ArgumentOutOfRangeException ();
+                       }
                }
 
-               bool IConvertible.ToBoolean(IFormatProvider provider)
+               bool IConvertible.ToBoolean (IFormatProvider provider)
                {
                        throw new InvalidCastException();
                }
                
-               byte IConvertible.ToByte(IFormatProvider provider)
+               byte IConvertible.ToByte (IFormatProvider provider)
                {
                        throw new InvalidCastException();
 
                }
 
-               char IConvertible.ToChar(IFormatProvider provider)
+               char IConvertible.ToChar (IFormatProvider provider)
                {
                        throw new InvalidCastException();
                }
 
-               System.DateTime IConvertible.ToDateTime(IFormatProvider provider)
+               System.DateTime IConvertible.ToDateTime (IFormatProvider provider)
                {
                        return this;
                } 
                
-               decimal IConvertible.ToDecimal(IFormatProvider provider)
+               decimal IConvertible.ToDecimal (IFormatProvider provider)
                {
                         throw new InvalidCastException();
                }
 
-               double IConvertible.ToDouble(IFormatProvider provider)
+               double IConvertible.ToDouble (IFormatProvider provider)
                {
                        throw new InvalidCastException();
                }
 
-               Int16 IConvertible.ToInt16(IFormatProvider provider)
+               Int16 IConvertible.ToInt16 (IFormatProvider provider)
                {
                        throw new InvalidCastException();
                }
 
-               Int32 IConvertible.ToInt32(IFormatProvider provider)
+               Int32 IConvertible.ToInt32 (IFormatProvider provider)
                {
                        throw new InvalidCastException();
                }
 
-               Int64 IConvertible.ToInt64(IFormatProvider provider)
+               Int64 IConvertible.ToInt64 (IFormatProvider provider)
                {
                        throw new InvalidCastException();
                }
 
-               SByte IConvertible.ToSByte(IFormatProvider provider)
+               SByte IConvertible.ToSByte (IFormatProvider provider)
                {
                        throw new InvalidCastException();
                }
 
-               Single IConvertible.ToSingle(IFormatProvider provider)
+               Single IConvertible.ToSingle (IFormatProvider provider)
                {
                        throw new InvalidCastException();
                }
@@ -2119,19 +2128,37 @@ namespace System
                                throw new InvalidCastException();
                }
 
-               UInt16 IConvertible.ToUInt16(IFormatProvider provider)
+               UInt16 IConvertible.ToUInt16 (IFormatProvider provider)
                {
                        throw new InvalidCastException();
                }
 
-               UInt32 IConvertible.ToUInt32(IFormatProvider provider)
+               UInt32 IConvertible.ToUInt32 (IFormatProvider provider)
                {
                        throw new InvalidCastException();
                }
 
-               UInt64 IConvertible.ToUInt64(IFormatProvider provider)
+               UInt64 IConvertible.ToUInt64 (IFormatProvider provider)
                {
                        throw new InvalidCastException();
                }
+
+               void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
+               {
+                       long t = Ticks;
+                       info.AddValue ("ticks", t);
+
+                       // This is the new .NET format, encodes the kind on the top bits
+                       info.AddValue ("dateData", encoded);
+               }
+               
+#if MONOTOUCH
+               static DateTime () {
+                       if (MonoTouchAOTHelper.FalseFlag) {
+                               var comparer = new System.Collections.Generic.GenericComparer <DateTime> ();
+                               var eqcomparer = new System.Collections.Generic.GenericEqualityComparer <DateTime> ();
+                       }
+               }
+#endif
        }
 }