New tests.
[mono.git] / mcs / class / corlib / System / DateTime.cs
index 286ad09106d5e1ff07fac42968af9e96585d078c..94c8a73a63577e7ab524aa04daeba40c501d7734 100644 (file)
@@ -34,6 +34,7 @@ using System.Globalization;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Text;
+using System.Runtime.Serialization;
 
 namespace System
 {
@@ -44,16 +45,19 @@ namespace System
        /// 
        [Serializable]
        [StructLayout (LayoutKind.Auto)]
-       public struct DateTime : IFormattable, IConvertible, IComparable
-#if NET_2_0
-               , IComparable<DateTime>, IEquatable <DateTime>
-#endif
+       public struct DateTime : IFormattable, IConvertible, IComparable, ISerializable, IComparable<DateTime>, IEquatable <DateTime>
        {
+#if MONOTOUCH
+               static DateTime () {
+                       if (MonoTouchAOTHelper.FalseFlag) {
+                               var comparer = new System.Collections.Generic.GenericComparer <DateTime> ();
+                               var eqcomparer = new System.Collections.Generic.GenericEqualityComparer <DateTime> ();
+                       }
+               }
+#endif
                private TimeSpan ticks;
 
-#if NET_2_0
                DateTimeKind kind;
-#endif
 
                private const int dp400 = 146097;
                private const int dp100 = 36524;
@@ -88,6 +92,7 @@ namespace System
                private static readonly string[] ParseTimeFormats = new string [] {
                        "H:m:s.fffffffzzz",
                        "H:m:s.fffffff",
+                       "H:m:s tt zzz",
                        "H:m:szzz",
                        "H:m:s",
                        "H:mzzz",
@@ -109,13 +114,9 @@ namespace System
                        "M/yyyy/dT",
                        "yyyy'\u5E74'M'\u6708'd'\u65E5",
 
-#if NET_2_0
+
                        "yyyy/d/MMMM",
                        "yyyy/MMM/d",
-#else
-                       "yyyy/MMMM/d",
-                       "yyyy/d/MMM",
-#endif
                        "d/MMMM/yyyy",
                        "MMM/d/yyyy",
                        "d/yyyy/MMMM",
@@ -190,11 +191,7 @@ namespace System
                };
                private static readonly string[] DayMonthShortFormats = new string [] {
                        "d/MMMM",
-#if NET_2_0
                        "MMM/yy",
-#else // In .Net 1.0 Feb 03 is always Feb 3rd (and not Feb 2003)
-                       "MMM/d",
-#endif
                        "yyyy/MMMM",
                };
 
@@ -278,9 +275,7 @@ namespace System
                                        ticks, MinValue.Ticks, MaxValue.Ticks);
                                throw new ArgumentOutOfRangeException ("ticks", msg);
                        }
-#if NET_2_0
                        kind = DateTimeKind.Unspecified;
-#endif
                }
 
                public DateTime (int year, int month, int day)
@@ -303,9 +298,7 @@ namespace System
 
                        ticks = new TimeSpan (AbsoluteDays(year,month,day), hour, minute, second, millisecond);
 
-#if NET_2_0
                        kind = DateTimeKind.Unspecified;
-#endif
                }
 
                public DateTime (int year, int month, int day, Calendar calendar)
@@ -323,9 +316,7 @@ namespace System
                        if (calendar == null)
                                throw new ArgumentNullException ("calendar");
                        ticks = calendar.ToDateTime (year, month, day, hour, minute, second, millisecond).ticks;
-#if NET_2_0
                        kind = DateTimeKind.Unspecified;
-#endif
                }
 
                internal DateTime (bool check, TimeSpan value)
@@ -335,12 +326,9 @@ namespace System
 
                        ticks = value;
 
-#if NET_2_0
                        kind = DateTimeKind.Unspecified;
-#endif
                }
 
-#if NET_2_0
                public DateTime (long ticks, DateTimeKind kind) : this (ticks)
                {
                        CheckDateTimeKind (kind);
@@ -366,9 +354,27 @@ namespace System
                {
                        CheckDateTimeKind (kind);
                        this.kind = kind;
-               }                       
-#endif
+               }
 
+               //
+               // Not visible, but can be invoked during deserialization
+               //
+               DateTime (SerializationInfo info, StreamingContext context)
+               {
+                       if (info.HasKey ("dateData")){
+                               long dateData = info.GetInt64 ("dateData");
+                               kind = (DateTimeKind) (dateData >> 62);
+                               ticks = new TimeSpan (dateData & 0x3fffffffffffffff);
+                       } else if (info.HasKey ("ticks")){
+                               ticks = new TimeSpan (info.GetInt64 ("ticks"));
+                               kind = DateTimeKind.Unspecified;
+                       } else {
+                               kind = DateTimeKind.Unspecified;
+                               ticks = new TimeSpan (0);
+                       }
+               }
+               
+                             
                /* Properties  */
 
                public DateTime Date 
@@ -376,9 +382,7 @@ namespace System
                        get     
                        { 
                                DateTime ret = new DateTime (Year, Month, Day);
-#if NET_2_0
                                ret.kind = kind;
-#endif
                                return ret;
                        }
                }
@@ -486,9 +490,7 @@ namespace System
 
                                // This is boxed, so we avoid locking.
                                DateTime ret = dt + (TimeSpan) to_local_time_span_object;
-#if NET_2_0
                                ret.kind = DateTimeKind.Local;
-#endif
                                return ret;
                        }
                }
@@ -506,9 +508,7 @@ namespace System
                        get {
                                DateTime now = Now;
                                DateTime today = new DateTime (now.Year, now.Month, now.Day);
-#if NET_2_0
                                today.kind = now.kind;
-#endif
                                return today;
                        }
                }
@@ -516,11 +516,7 @@ namespace System
                public static DateTime UtcNow 
                {
                        get {
-#if NET_2_0
                                return new DateTime (GetNow (), DateTimeKind.Utc);
-#else
-                               return new DateTime (GetNow ());
-#endif
                        }
                }
 
@@ -532,22 +528,18 @@ namespace System
                        }
                }
 
-#if NET_2_0
                public DateTimeKind Kind {
                        get {
                                return kind;
                        }
                }
-#endif
 
                /* methods */
 
                public DateTime Add (TimeSpan value)
                {
                        DateTime ret = AddTicks (value.Ticks);
-#if NET_2_0
                        ret.kind = kind;
-#endif
                        return ret;
                }
 
@@ -562,9 +554,7 @@ namespace System
                                throw new ArgumentOutOfRangeException();
                        }
                        DateTime ret = new DateTime (value + ticks.Ticks);
-#if NET_2_0
                        ret.kind = kind;
-#endif
                        return ret;
                }
 
@@ -579,7 +569,7 @@ namespace System
                                        (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);
                }
@@ -625,9 +615,7 @@ namespace System
                                day = maxday;
 
                        temp = new DateTime (year, month, day);
-#if NET_2_0
                        temp.kind = kind;
-#endif
                        return  temp.Add (this.TimeOfDay);
                }
 
@@ -663,7 +651,6 @@ namespace System
                        return Compare (this, (DateTime) value);
                }
 
-#if NET_2_0
                public bool IsDaylightSavingTime ()
                {
                        if (kind == DateTimeKind.Utc)
@@ -710,19 +697,6 @@ namespace System
                        return new DateTime (value.Ticks, kind);
                }
 
-#else
-
-               internal long ToBinary ()
-               {
-                       return Ticks;
-               }
-
-               internal static DateTime FromBinary (long dateData)
-               {
-                       return new DateTime (dateData & 0x3fffffffffffffff);
-               }
-#endif
-
                public static int DaysInMonth (int year, int month)
                {
                        int[] days ;
@@ -758,7 +732,6 @@ namespace System
                        return new DateTime (w32file_epoch + fileTime).ToLocalTime ();
                }
 
-#if NET_1_1
                public static DateTime FromFileTimeUtc (long fileTime) 
                {
                        if (fileTime < 0)
@@ -766,7 +739,6 @@ namespace System
 
                        return new DateTime (w32file_epoch + fileTime);
                }
-#endif
 
                public static DateTime FromOADate (double d)
                {
@@ -849,12 +821,10 @@ namespace System
                        return results;
                }
 
-#if NET_2_0
                private void CheckDateTimeKind (DateTimeKind kind) {
                        if ((kind != DateTimeKind.Unspecified) && (kind != DateTimeKind.Utc) && (kind != DateTimeKind.Local))
                                throw new ArgumentException ("Invalid DateTimeKind value.", "kind");
                }
-#endif
 
                public override int GetHashCode ()
                {
@@ -885,77 +855,108 @@ namespace System
 
                public static DateTime Parse (string s, IFormatProvider provider, DateTimeStyles styles)
                {
-                       
-                       const string formatExceptionMessage = "String was not recognized as a valid DateTime.";
-#if !NET_2_0
-                       const string argumentYearRangeExceptionMessage = "Valid values are between 1 and 9999, inclusive.";
-#endif
-                       
                        if (s == null)
                                throw new ArgumentNullException ("s");
+
+                       DateTime res;
+                       DateTimeOffset dto;
+                       Exception exception = null;
+                       if (!CoreParse (s, provider, styles, out res, out dto, true, ref exception))
+                               throw exception;
+                       
+                       return res;
+               }
+
+               const string formatExceptionMessage = "String was not recognized as a valid DateTime.";
+               
+               internal static bool CoreParse (string s, IFormatProvider provider, DateTimeStyles styles,
+                                             out DateTime result, out DateTimeOffset dto, bool setExceptionOnError, ref Exception exception)
+               {
+                       dto = new DateTimeOffset (0, TimeSpan.Zero);
+                       if (s == null || s.Length == 0) {
+                               if (setExceptionOnError)
+                                       exception = new FormatException (formatExceptionMessage);
+                               result = MinValue;
+                               return false;
+                       }
+
                        if (provider == null)
                                provider = CultureInfo.CurrentCulture;
                        DateTimeFormatInfo dfi = DateTimeFormatInfo.GetInstance (provider);
 
-                       bool longYear = false;
-                       DateTime result;
                        // Try first all the combinations of ParseAllDateFormats & ParseTimeFormats
-                       string[] allDateFormats = YearMonthDayFormats (dfi);
+                       string[] allDateFormats = YearMonthDayFormats (dfi, setExceptionOnError, ref exception);
+                       if (allDateFormats == null){
+                               result = MinValue;
+                               return false;
+                       }
+
+                       bool longYear = false;
                        for (int i = 0; i < allDateFormats.Length; i++) {
                                string firstPart = allDateFormats [i];
                                bool incompleteFormat = false;
-                               if (_DoParse (s, firstPart, "", false, out result, dfi, styles, true, ref incompleteFormat, ref longYear))
-                                       return result;
+                               if (_DoParse (s, firstPart, "", false, out result, out dto, dfi, styles, true, ref incompleteFormat, ref longYear))
+                                       return true;
+
                                if (!incompleteFormat)
                                        continue;
 
                                for (int j = 0; j < ParseTimeFormats.Length; j++) {
-                                       if (_DoParse (s, firstPart, ParseTimeFormats [j], false, out result, dfi, styles, true, ref incompleteFormat, ref longYear))
-                                               return result;
+                                       if (_DoParse (s, firstPart, ParseTimeFormats [j], false, out result, out dto, dfi, styles, true, ref incompleteFormat, ref longYear))
+                                               return true;
                                }
                        }
-                       string[] monthDayFormats = IsDayBeforeMonth (dfi) ? DayMonthShortFormats : MonthDayShortFormats;
+
+                       //
+                       // Month day formats
+                       //
+                       int dayIndex = dfi.MonthDayPattern.IndexOf('d');
+                       int monthIndex = dfi.MonthDayPattern.IndexOf('M');
+                       if (dayIndex == -1 || monthIndex == -1){
+                               result = MinValue;
+                               if (setExceptionOnError)
+                                       exception = new FormatException (Locale.GetText("Order of month and date is not defined by {0}", dfi.MonthDayPattern));
+                               return false;
+                       }
+                       bool is_day_before_month = dayIndex < monthIndex;
+                       string[] monthDayFormats = is_day_before_month ? DayMonthShortFormats : MonthDayShortFormats;
                        for (int i = 0; i < monthDayFormats.Length; i++) {
                                bool incompleteFormat = false;
-                               if (_DoParse (s, monthDayFormats[i], "", false, out result, dfi, styles, true, ref incompleteFormat, ref longYear))
-                                       return result;
+                               if (_DoParse (s, monthDayFormats[i], "", false, out result, out dto, dfi, styles, true, ref incompleteFormat, ref longYear))
+                                       return true;
                        }
+                       
                        for (int j = 0; j < ParseTimeFormats.Length; j++) {
                                string firstPart = ParseTimeFormats [j];
                                bool incompleteFormat = false;
-                               if (_DoParse (s, firstPart, "", false, out result, dfi, styles, false, ref incompleteFormat, ref longYear))
-                                       return result;
+                               if (_DoParse (s, firstPart, "", false, out result, out dto, dfi, styles, false, ref incompleteFormat, ref longYear))
+                                       return true;
                                if (!incompleteFormat)
                                        continue;
 
                                for (int i = 0; i < monthDayFormats.Length; i++) {
-                                       if (_DoParse (s, firstPart, monthDayFormats [i], false, out result, dfi, styles, false, ref incompleteFormat, ref longYear))
-                                               return result;
+                                       if (_DoParse (s, firstPart, monthDayFormats [i], false, out result, out dto, dfi, styles, false, ref incompleteFormat, ref longYear))
+                                               return true;
                                }
                                for (int i = 0; i < allDateFormats.Length; i++) {
                                        string dateFormat = allDateFormats [i];
                                        if (dateFormat[dateFormat.Length - 1] == 'T')
                                                continue; // T formats must be before the time part
-                                       if (_DoParse (s, firstPart, dateFormat, false, out result, dfi, styles, false, ref incompleteFormat, ref longYear))
-                                               return result;
+                                       if (_DoParse (s, firstPart, dateFormat, false, out result, out dto, dfi, styles, false, ref incompleteFormat, ref longYear))
+                                               return true;
                                }
                        }
 
                        // Try as a last resort all the patterns
-                       if (ParseExact (s, dfi.GetAllDateTimePatternsInternal (), dfi, styles, out result, false, ref longYear))
-                               return result;
-
-#if NET_2_0
-                       // .NET does not throw an ArgumentOutOfRangeException, but .NET 1.1 does.
-                       throw new FormatException (formatExceptionMessage);
-#else
-                       if (longYear) {
-                               throw new ArgumentOutOfRangeException ("year",
-                                       argumentYearRangeExceptionMessage);
-                       }
+                       if (ParseExact (s, dfi.GetAllDateTimePatternsInternal (), dfi, styles, out result, false, ref longYear, setExceptionOnError, ref exception))
+                               return true;
 
-                       throw new FormatException (formatExceptionMessage);
-#endif
+                       if (!setExceptionOnError)
+                               return false;
+                       
+                       // .NET 2.x does not throw an ArgumentOutOfRangeException, but .NET 1.1 does.
+                       exception = new FormatException (formatExceptionMessage);
+                       return false;
                }
 
                public static DateTime ParseExact (string s, string format, IFormatProvider provider)
@@ -963,39 +964,38 @@ namespace System
                        return ParseExact (s, format, provider, DateTimeStyles.None);
                }
 
-               private static bool IsDayBeforeMonth (DateTimeFormatInfo dfi)
-               {
-                       int dayIndex = dfi.MonthDayPattern.IndexOf('d');
-                       int monthIndex = dfi.MonthDayPattern.IndexOf('M');
-                       if (dayIndex == -1 || monthIndex == -1)
-                               throw new FormatException (Locale.GetText("Order of month and date is not defined by {0}", dfi.MonthDayPattern));
-
-                       return dayIndex < monthIndex;
-               }
-
-               private static string[] YearMonthDayFormats (DateTimeFormatInfo dfi)
+               private static string[] YearMonthDayFormats (DateTimeFormatInfo dfi, bool setExceptionOnError, ref Exception exc)
                {
                        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)
-                               throw new FormatException (Locale.GetText("Order of year, month and date is not defined by {0}", dfi.ShortDatePattern));
+                       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 (yearIndex < monthIndex)
                                if (monthIndex < dayIndex)
                                        return ParseYearMonthDayFormats;
                                else if (yearIndex < dayIndex)
                                        return ParseYearDayMonthFormats;
-                               else
+                               else {
                                        // The year cannot be between the date and the month
-                                       throw new FormatException (Locale.GetText("Order of date, year and month defined by {0} is not supported", dfi.ShortDatePattern));
+                                       if (setExceptionOnError)
+                                               exc = new FormatException (Locale.GetText("Order of date, year and month defined by {0} is not supported", dfi.ShortDatePattern));
+                                       return null;
+                               }
                        else if (dayIndex < monthIndex)
                                return ParseDayMonthYearFormats;
                        else if (dayIndex < yearIndex)
                                return ParseMonthDayYearFormats;
-                       else
+                       else {
                                // The year cannot be between the month and the date
-                               throw new FormatException (Locale.GetText("Order of month, year and date defined by {0} is not supported", dfi.ShortDatePattern));
+                               if (setExceptionOnError)
+                                       exc = new FormatException (Locale.GetText("Order of month, year and date defined by {0} is not supported", dfi.ShortDatePattern));
+                               return null;
+                       }
                }
 
                private static int _ParseNumber (string s, int valuePos,
@@ -1161,6 +1161,7 @@ namespace System
                                              string secondPart,
                                              bool exact,
                                              out DateTime result,
+                                             out DateTimeOffset dto,
                                              DateTimeFormatInfo dfi,
                                              DateTimeStyles style,
                                              bool firstPartIsDate,
@@ -1170,9 +1171,7 @@ namespace System
                        bool useutc = false;
                        bool use_invariant = false;
                        bool sloppy_parsing = false;
-#if !NET_2_0
-                       bool afterTimePart = firstPartIsDate && secondPart == "";
-#endif
+                       dto = new DateTimeOffset (0, TimeSpan.Zero);
                        bool flexibleTwoPartsParsing = !exact && secondPart != null;
                        incompleteFormat = false;
                        int valuePos = 0;
@@ -1186,6 +1185,9 @@ namespace System
                        if (format == null)
                                return false;
 
+                       if (s == null)
+                               return false;
+                               
                        if ((style & DateTimeStyles.AllowLeadingWhite) != 0) {
                                format = format.TrimStart (null);
 
@@ -1224,11 +1226,7 @@ namespace System
                                if (flexibleTwoPartsParsing && pos + num == 0)
                                {
                                        bool isLetter = IsLetter(s, valuePos);
-#if NET_2_0
                                        if (isLetter) {
-#else
-                                       if (afterTimePart && isLetter) {
-#endif
                                                if (s [valuePos] == 'Z')
                                                        num_parsed = 1;
                                                else
@@ -1282,10 +1280,6 @@ namespace System
                                                chars = format;
                                                len = chars.Length;
                                                isFirstPart = false;
-#if !NET_2_0
-                                               if (!firstPartIsDate || format == "")
-                                                       afterTimePart = true;
-#endif
                                                continue;
                                        }
                                        break;
@@ -1489,11 +1483,9 @@ namespace System
                                                return false;
 
                                        break;
-#if NET_2_0
                                case 'F':
                                        leading_zeros = false;
                                        goto case 'f';
-#endif
                                case 'f':
                                        if (num > 6 || fractionalSeconds != -1)
                                                return false;
@@ -1523,7 +1515,7 @@ namespace System
                                        else if (num == 1)
                                                tzoffset = _ParseNumber (s, valuePos, 1, 2, true, sloppy_parsing, out num_parsed);
                                        else {
-                                               tzoffset = _ParseNumber (s, valuePos, 1, 2, true, sloppy_parsing, out num_parsed);
+                                               tzoffset = _ParseNumber (s, valuePos, 1, 2, true, /*sloppy_parsing*/true, out num_parsed);
                                                valuePos += num_parsed;
                                                if (num_parsed < 0)
                                                        return false;
@@ -1542,7 +1534,6 @@ namespace System
                                                        num_parsed = 0;
                                        }
                                        break;
-#if NET_2_0
                                case 'K':
                                        if (s [valuePos] == 'Z') {
                                                valuePos++;
@@ -1575,7 +1566,7 @@ namespace System
                                                        return false;
                                        }
                                        break;
-#endif
+
                                // LAMESPEC: This should be part of UTCpattern
                                // string and thus should not be considered here.
                                //
@@ -1635,9 +1626,7 @@ namespace System
                                        switch (chars [pos]) {
                                        case 'm':
                                        case 's':
-#if NET_2_0
                                        case 'F':
-#endif
                                        case 'f':
                                        case 'z':
                                                if (s.Length > valuePos && s [valuePos] == 'Z' &&
@@ -1653,10 +1642,14 @@ namespace System
                                num = 0;
                        }
 
-#if NET_2_0
+                       if (pos + 1 < len && chars [pos] == '.' && chars [pos + 1] == 'F') {
+                               pos++;
+                               while (pos < len && chars [pos] == 'F') // '.FFF....' can be mapped to nothing. See bug #444103
+                                       pos++;
+                       }
                        while (pos < len && chars [pos] == 'K') // 'K' can be mapped to nothing
                                pos++;
-#endif
+
                        if (pos < len)
                                return false;
 
@@ -1729,30 +1722,36 @@ namespace System
                        if (dayofweek != -1 && dayofweek != (int) result.DayOfWeek)
                                return false;
 
-                       TimeSpan utcoffset;
-
-                       bool adjustToUniversal = (style & DateTimeStyles.AdjustToUniversal) != 0;
-                       
-                       if (tzsign != -1) {
+                       if (tzsign == -1) {
+                               if (result != DateTime.MinValue) {
+                                       try {
+                                               dto = new DateTimeOffset (result);
+                                       } catch { } // We handle this error in DateTimeOffset.Parse
+                               }
+                       } else {
                                if (tzoffmin == -1)
                                        tzoffmin = 0;
                                if (tzoffset == -1)
                                        tzoffset = 0;
-                               if (tzsign == 1)
+                               if (tzsign == 1) {
                                        tzoffset = -tzoffset;
-
-                               utcoffset = new TimeSpan (tzoffset, tzoffmin, 0);
-                               long newticks = (result.ticks - utcoffset).Ticks;
+                                       tzoffmin = -tzoffmin;
+                               }
+                               try {
+                                       dto = new DateTimeOffset (result, new TimeSpan (tzoffset, tzoffmin, 0));
+                               } catch {} // We handle this error in DateTimeOffset.Parse
+                       }
+                       bool adjustToUniversal = (style & DateTimeStyles.AdjustToUniversal) != 0;
+                       
+                       if (tzsign != -1) {
+                               long newticks = (result.ticks - dto.Offset).Ticks;
                                if (newticks < 0)
                                        newticks += TimeSpan.TicksPerDay;
                                result = new DateTime (false, new TimeSpan (newticks));
-#if NET_2_0
                                result.kind = DateTimeKind.Utc;
                                if ((style & DateTimeStyles.RoundtripKind) != 0)
                                        result = result.ToLocalTime ();
-#endif
                        }
-#if NET_2_0                                                    
                        else if (useutc || ((style & DateTimeStyles.AssumeUniversal) != 0))
                                result.kind = DateTimeKind.Utc;
                        else if ((style & DateTimeStyles.AssumeLocal) != 0)
@@ -1766,10 +1765,6 @@ namespace System
                                else if (adjustToLocal)
                                        result = result.ToLocalTime ();
                        }
-#else
-                       if (!adjustToUniversal && (useutc || tzsign != -1))
-                               result = result.ToLocalTime ();
-#endif
                        return true;
                }
 
@@ -1790,9 +1785,7 @@ namespace System
                                                   DateTimeStyles style)
                {
                        DateTimeFormatInfo dfi = DateTimeFormatInfo.GetInstance (provider);
-#if NET_2_0
                        CheckStyle (style);
-#endif
                        if (s == null)
                                throw new ArgumentNullException ("s");
                        if (formats == null)
@@ -1802,12 +1795,12 @@ namespace System
 
                        DateTime result;
                        bool longYear = false;
-                       if (!ParseExact (s, formats, dfi, style, out result, true, ref longYear))
-                               throw new FormatException ();
+                       Exception e = null;
+                       if (!ParseExact (s, formats, dfi, style, out result, true, ref longYear, true, ref e))
+                               throw e;
                        return result;
                }               
 
-#if NET_2_0
                private static void CheckStyle (DateTimeStyles style)
                {
                        if ( (style & DateTimeStyles.RoundtripKind) != 0)
@@ -1822,24 +1815,30 @@ namespace System
 
                public static bool TryParse (string s, out DateTime result)
                {
-                       try {
-                               result = Parse (s);
-                       } catch {
-                               result = MinValue;
-                               return false;
+                       if (s != null){
+                               try {
+                                       Exception exception = null;
+                                       DateTimeOffset dto;
+
+                                       return CoreParse (s, null, DateTimeStyles.AllowWhiteSpaces, out result, out dto, false, ref exception);
+                               } catch { }
                        }
-                       return true;
+                       result = MinValue;
+                       return false;
                }
                
                public static bool TryParse (string s, IFormatProvider provider, DateTimeStyles styles, out DateTime result)
                {
-                       try {
-                               result = Parse (s, provider, styles);
-                       } catch {
-                               result = MinValue;
-                               return false;
-                       }
-                       return true;
+                       if (s != null){
+                               try {
+                                       Exception exception = null;
+                                       DateTimeOffset dto;
+                                       
+                                       return CoreParse (s, provider, styles, out result, out dto, false, ref exception);
+                               } catch {}
+                       } 
+                       result = MinValue;
+                       return false;
                }
                
                public static bool TryParseExact (string s, string format,
@@ -1848,7 +1847,6 @@ namespace System
                                                  out DateTime result)
                {
                        string[] formats;
-
                        formats = new string [1];
                        formats[0] = format;
 
@@ -1860,16 +1858,22 @@ namespace System
                                                  DateTimeStyles style,
                                                  out DateTime result)
                {
-                       DateTimeFormatInfo dfi = DateTimeFormatInfo.GetInstance (provider);
+                       try {
+                               DateTimeFormatInfo dfi = DateTimeFormatInfo.GetInstance (provider);
 
-                       bool longYear = false;
-                       return ParseExact (s, formats, dfi, style, out result, true, ref longYear);
+                               bool longYear = false;
+                               Exception e = null;
+                               return ParseExact (s, formats, dfi, style, out result, true, ref longYear, false, ref e);
+                       } catch {
+                               result = MinValue;
+                               return false;
+                       }
                }
-#endif
 
                private static bool ParseExact (string s, string [] formats,
-                       DateTimeFormatInfo dfi, DateTimeStyles style, out DateTime ret,
-                       bool exact, ref bool longYear)
+                                               DateTimeFormatInfo dfi, DateTimeStyles style, out DateTime ret,
+                                               bool exact, ref bool longYear,
+                                               bool setExceptionOnError, ref Exception exception)
                {
                        int i;
                        bool incompleteFormat = false;
@@ -1878,13 +1882,17 @@ namespace System
                                DateTime result;
                                string format = formats[i];
                                if (format == null || format == String.Empty)
-                                       throw new FormatException ("Invalid Format String");
+                                       break;
 
-                               if (_DoParse (s, formats[i], null, exact, out result, dfi, style, false, ref incompleteFormat, ref longYear)) {
+                               DateTimeOffset dto;
+                               if (_DoParse (s, formats[i], null, exact, out result, out dto, dfi, style, false, ref incompleteFormat, ref longYear)) {
                                        ret = result;
                                        return true;
                                }
                        }
+
+                       if (setExceptionOnError)
+                               exception = new FormatException ("Invalid format string");
                        ret = DateTime.MinValue;
                        return false;
                }
@@ -1900,9 +1908,7 @@ namespace System
 
                        newticks = (new TimeSpan (ticks.Ticks)) - value;
                        DateTime ret = new DateTime (true,newticks);
-#if NET_2_0
                        ret.kind = kind;
-#endif
                        return ret;
                }
 
@@ -1917,7 +1923,6 @@ namespace System
                        return(universalTime.Ticks - w32file_epoch);
                }
 
-#if NET_1_1
                public long ToFileTimeUtc()
                {
                        if (Ticks < w32file_epoch) {
@@ -1926,7 +1931,6 @@ namespace System
                        
                        return (Ticks - w32file_epoch);
                }
-#endif
 
                public string ToLongDateString()
                {
@@ -2029,9 +2033,7 @@ namespace System
                public static DateTime operator +(DateTime d, TimeSpan t)
                {
                        DateTime ret = new DateTime (true, d.ticks + t);
-#if NET_2_0
                        ret.kind = d.kind;
-#endif
                        return ret;
                }
 
@@ -2073,9 +2075,7 @@ namespace System
                public static DateTime operator -(DateTime d,TimeSpan t)
                {
                        DateTime ret = new DateTime (true, d.ticks - t);
-#if NET_2_0
                        ret.kind = d.kind;
-#endif
                        return ret;
                }
 
@@ -2135,16 +2135,16 @@ namespace System
                        throw new InvalidCastException();
                }
 
-               object IConvertible.ToType (Type conversionType, IFormatProvider provider)
+               object IConvertible.ToType (Type targetType, IFormatProvider provider)
                {
-                       if (conversionType == null)
-                               throw new ArgumentNullException ("conversionType");
+                       if (targetType == null)
+                               throw new ArgumentNullException ("targetType");
 
-                       if (conversionType == typeof (DateTime))
+                       if (targetType == typeof (DateTime))
                                return this;
-                       else if (conversionType == typeof (String))
+                       else if (targetType == typeof (String))
                                return this.ToString (provider);
-                       else if (conversionType == typeof (Object))
+                       else if (targetType == typeof (Object))
                                return this;
                        else
                                throw new InvalidCastException();
@@ -2154,7 +2154,7 @@ namespace System
                {
                        throw new InvalidCastException();
                }
-               
+
                UInt32 IConvertible.ToUInt32(IFormatProvider provider)
                {
                        throw new InvalidCastException();
@@ -2164,5 +2164,15 @@ namespace System
                {
                        throw new InvalidCastException();
                }
+
+               void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
+               {
+                       long t = ticks.Ticks;
+                       info.AddValue ("ticks", t);
+
+                       // This is the new .NET format, encodes the kind on the top bits
+                       info.AddValue ("dateData", t | (((uint)kind) << 62));
+               }
+               
        }
 }