DateTime.FromFileTimeUtc should return a DateTime with DateTimeKind.Utc. Fixes #2936.
[mono.git] / mcs / class / corlib / System / DateTime.cs
index 0e8ad29583c36b3c6f547e09b48be524156edbab..07b247897c1616259461078bb01dc897185c1e03 100644 (file)
@@ -29,7 +29,7 @@
 // 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;
@@ -53,7 +53,7 @@ namespace System
                // 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
                //
-               public long encoded;
+               long encoded;
                const long TicksMask = 0x3fffffffffffffff;
                const long KindMask = unchecked ((long) 0xc000000000000000);
                const int KindShift = 62;
@@ -187,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.
@@ -650,7 +660,7 @@ namespace System
 
                public static DateTime FromBinary (long dateData)
                {
-                       switch ((ulong)dateData >> 62) {
+                       switch ((ulong)dateData >> KindShift) {
                        case 1: // Utc
                                return new DateTime (dateData & TicksMask, DateTimeKind.Utc);
                        case 0: // Unspecified
@@ -705,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)
@@ -751,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 )
@@ -848,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;
@@ -927,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)
@@ -945,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;
@@ -955,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;
                        }
                }