ToDateTime() public method

public ToDateTime ( int year, int month, int day, int hour, int minute, int second, int millisecond, int era ) : System.DateTime
year int
month int
day int
hour int
minute int
second int
millisecond int
era int
return System.DateTime
示例#1
0
        public static DateTime?ToHijriDateTimeObject(this string date)
        {
            if (string.IsNullOrEmpty(date.Trim()))
            {
                return(null);
            }
            string[] DateParts = date.Split('/');
            var      umAlQura  = new System.Globalization.UmAlQuraCalendar();

            return(umAlQura.ToDateTime(int.Parse(DateParts[2]), int.Parse(DateParts[1]), int.Parse(DateParts[0]), 0, 0, 0, 0));
        }
        public void GenerateData()
        {
            var bclCalendar = new UmAlQuraCalendar();
            DateTime minDateTime = bclCalendar.MinSupportedDateTime;            

            // Work out the min and max supported years, ensuring that we support complete years.
            var minYear = bclCalendar.GetYear(minDateTime);
            if (bclCalendar.GetMonth(minDateTime) != 1 || bclCalendar.GetDayOfMonth(minDateTime) != 1)
            {
                minYear++;
            }

            DateTime maxDateTime = bclCalendar.MaxSupportedDateTime;
            var maxYear = bclCalendar.GetYear(maxDateTime);
            if (bclCalendar.GetMonth(maxDateTime) != 12 || bclCalendar.GetDayOfMonth(maxDateTime) != bclCalendar.GetDaysInMonth(maxYear, 12))
            {
                maxYear--;
            }

            // This is two elements longer than it needs to be, but it's simpler this way.
            var monthLengths = new ushort[maxYear - minYear + 3];
            for (int year = minYear; year <= maxYear; year++)
            {
                int yearIndex = year - minYear + 1;
                ushort monthBits = 0;
                for (int month = 1; month <= 12; month++)
                {
                    if (bclCalendar.GetDaysInMonth(year, month) == 30)
                    {
                        monthBits |= (ushort) (1 << month);
                    }
                }
                monthLengths[yearIndex] = monthBits;
            }
            byte[] data = monthLengths.SelectMany(value => new[] { (byte)(value >> 8), (byte)(value & 0xff) }).ToArray();

            // Assume every 10 years before minDateTime has exactly 3544 days... it doesn't matter whether or not that's
            // correct, but it gets roughly the right estimate. It doesn't matter that startOfMinYear isn't in UTC; we're only
            // taking the Ticks property, which doesn't take account of the Kind.
            DateTime startOfMinYear = bclCalendar.ToDateTime(minYear, 1, 1, 0, 0, 0, 0);
            var computedDaysAtStartOfMinYear = (int)((startOfMinYear.Ticks - NodaConstants.BclTicksAtUnixEpoch) / NodaConstants.TicksPerDay);

            Console.WriteLine($"private const int ComputedMinYear = {minYear};");
            Console.WriteLine($"private const int ComputedMaxYear = {maxYear};");
            Console.WriteLine($"private const int ComputedDaysAtStartOfMinYear = {computedDaysAtStartOfMinYear};");
            Console.WriteLine("private const string GeneratedData =");

            // Adapted from PersianCalendarSystemTest. If we do this any more, we should
            // put it somewhere common.
            var base64 = Convert.ToBase64String(data);
            var lineLength = 80;
            for (int start = 0; start < base64.Length; start += lineLength)
            {
                var line = base64.Substring(start, Math.Min(lineLength, base64.Length - start));
                var last = start + lineLength >= base64.Length;
                Console.WriteLine($"    \"{line}\"{(last ? ";" : " +")}");
            }
            Console.WriteLine();
        }
        static UmAlQuraYearMonthDayCalculator()
        {
            // Try to initialize. If anything fails, YearLengths will still be null, so IsSupported will return false.
            Calendar bclCalendar;
#if PCL
            // Can't refer to the BCL calendar by name, but it *might* be available anyway. Let's try to instantiate
            // it with reflection. If we can't, that's fair enough.
            try
            {
                var type = typeof(Calendar).Assembly.GetType("System.Globalization.UmAlQuraCalendar");
                if (type == null)
                {
                    return;
                }
                bclCalendar = (Calendar) Activator.CreateInstance(type);
            }
            catch
            {
                // Don't really care what went wrong here. We'll assume it's not supported.
                return;
            }
#else
            bclCalendar = new UmAlQuraCalendar();
#endif

            DateTime minDateTime = bclCalendar.MinSupportedDateTime;

            // Check if this looks like a sensible implementation, with a limited time range.
            // (The .NET implementation only runs from 1900-2077, for example.)
            // Mono is unfortunately broken - it advertises a large range, but then is inconsistent:
            // Year 2 (for example) either has 354 or 355 days depending on how you ask.
            if (minDateTime.Year < 1800 || minDateTime.Year > 3000)
            {
                YearLengths = null;
                MonthLengths = null;
                YearStartDays = null;
                return;
            }

            // Work out the min and max supported years, ensuring that we support complete years.
            ComputedMinYear = bclCalendar.GetYear(minDateTime);
            if (bclCalendar.GetMonth(minDateTime) != 1 || bclCalendar.GetDayOfMonth(minDateTime) != 1)
            {
                ComputedMinYear++;
            }

            DateTime maxDateTime = bclCalendar.MaxSupportedDateTime;
            ComputedMaxYear = bclCalendar.GetYear(maxDateTime);
            if (bclCalendar.GetMonth(maxDateTime) != 12 || bclCalendar.GetDayOfMonth(maxDateTime) != bclCalendar.GetDaysInMonth(ComputedMaxYear, 12))
            {
                ComputedMaxYear--;
            }

            // For year lengths, we need to handle 1 year beyond the boundaries, too.
            // We don't need MonthLengths to be quite as long, but it's simpler to be consistent.
            YearLengths = new int[ComputedMaxYear - ComputedMinYear + 3];
            YearStartDays = new int[ComputedMaxYear - ComputedMinYear + 3];
            MonthLengths = new int[ComputedMaxYear - ComputedMinYear + 3];
            int totalDays = 0;
            for (int year = ComputedMinYear; year <= ComputedMaxYear; year++)
            {
                int yearIndex = year - ComputedMinYear + 1;
                YearLengths[yearIndex] = bclCalendar.GetDaysInYear(year);
                YearStartDays[yearIndex] = totalDays;
                totalDays += YearLengths[yearIndex];
                int monthBits = 0;
                for (int month = 1; month <= 12; month++)
                {
                    if (bclCalendar.GetDaysInMonth(year, month) == 30)
                    {
                        monthBits |= 1 << month;
                    }
                }
                MonthLengths[yearIndex] = monthBits;
            }
            // Fill in the cache with dummy data for before/after the min/max year, pretending
            // that both of the "extra" years were 354 days long.
            YearStartDays[0] = -354;
            YearStartDays[YearStartDays.Length - 1] = totalDays;
            YearLengths[0] = 354;
            YearLengths[YearStartDays.Length - 1] = 354;

            // Assume every 10 years before minDateTime has exactly 3544 days... it doesn't matter whether or not that's
            // correct, but it gets roughly the right estimate. It doesn't matter that startOfMinYear isn't in UTC; we're only
            // taking the Ticks property, which doesn't take account of the Kind.
            DateTime startOfMinYear = bclCalendar.ToDateTime(ComputedMinYear, 1, 1, 0, 0, 0, 0);
            ComputedDaysAtStartOfMinYear = (int) ((startOfMinYear.Ticks - NodaConstants.BclTicksAtUnixEpoch) / NodaConstants.TicksPerDay);
            ComputedDaysAtStartOfYear1 = ComputedDaysAtStartOfMinYear + (int) (((1 - ComputedMinYear) / 10.0) * AverageDaysPer10Years);
        }