GetYear() public method

public GetYear ( System.DateTime time ) : int
time System.DateTime
return int
Exemplo n.º 1
0
 public static string GregToHijri(DateTime dtTime)
 {
     if (dtTime == DateTime.MinValue)
         return "";
     UmAlQuraCalendar calHijri = new UmAlQuraCalendar();
     string weekday = Convert.ToString(calHijri.GetDayOfWeek(dtTime));
     int CurrDayOfMonth = calHijri.GetDayOfMonth(dtTime);
     int CurrMonth = calHijri.GetMonth(dtTime);
     int Curryear = calHijri.GetYear(dtTime);
     return CurrDayOfMonth.ToString() + "/" + CurrMonth.ToString() + "/" + Curryear.ToString();
 }
Exemplo n.º 2
0
        public void CanUseDateInCalendar()
        {
            var dt = new Date(2015, 1, 1);

            var calendar = new UmAlQuraCalendar();
            var year = calendar.GetYear(dt);
            var month = calendar.GetMonth(dt);
            var day = calendar.GetDayOfMonth(dt);

            Assert.Equal(1436, year);
            Assert.Equal(3, month);
            Assert.Equal(10, day);
        }
        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);
        }
Exemplo n.º 5
0
 public static string GregToHijriCompleteWithDayEng(DateTime dtTime)
 {
     if (dtTime == DateTime.MinValue)
         return "";
     UmAlQuraCalendar calHijri = new UmAlQuraCalendar();
     GregorianCalendar calEng = new GregorianCalendar();
     CultureInfo info = new CultureInfo("ar-sa");
     info.DateTimeFormat.Calendar = calHijri;
     string weekday = Convert.ToString(calEng.GetDayOfWeek(dtTime));
     int CurrDayOfMonth = calHijri.GetDayOfMonth(dtTime);
     int CurrMonth = calHijri.GetMonth(dtTime);
     int Curryear = calHijri.GetYear(dtTime);
     return string.Format("{3}/{2}/{1}  h ", weekday, CurrDayOfMonth.ToString(), CurrMonth.ToString(), Curryear.ToString());
 }
Exemplo n.º 6
0
 public static string GregToHijriTime(DateTime dtTime)
 {
     if (dtTime == DateTime.MinValue)
         return "";
     UmAlQuraCalendar calHijri = new UmAlQuraCalendar();
     CultureInfo info = new CultureInfo("ar-sa");
     info.DateTimeFormat.Calendar = calHijri;
     string weekday = Convert.ToString(calHijri.GetDayOfWeek(dtTime));
     int CurrDayOfMonth = calHijri.GetDayOfMonth(dtTime);
     int CurrMonth = calHijri.GetMonth(dtTime);
     int Curryear = calHijri.GetYear(dtTime);
     return dtTime.ToString("hh:mm tt", info);
 }