private void WriteDates() { //the day value is used to write the dates on the calendar, the init value is used to tell us where //to start in the calendar int day = 1; int init = GetDayNumber(1); for (int i = init; i <= 6; i++) { //writes out the dates in the first row starting from the correct position tableLayoutPanel1.GetControlFromPosition(i, 1).Text = day.ToString(System.Threading.Thread.CurrentThread.CurrentUICulture); day += 1; } int daysInMonth; try { daysInMonth = UmAlQuraInstance.GetDaysInMonth(m_selectedYear, m_selectedMonth); } catch (ArgumentException exception) { MessageBox.Show(exception.Message); return; } for (int i = 2; i <= 5; i++) { for (int j = 0; j <= 6; j++) { //writes out the remaining dates in the calendar watching out for the number of day in the month*/ if (day <= daysInMonth) { tableLayoutPanel1.GetControlFromPosition(j, i).Text = day.ToString(System.Threading.Thread.CurrentThread.CurrentUICulture); day++; } } } // There is an extra day we need to write init = 0; string previousDay; while (daysInMonth >= day) { previousDay = tableLayoutPanel1.GetControlFromPosition(init, 5).Text; tableLayoutPanel1.GetControlFromPosition(init, 5).Text = previousDay + "/" + day.ToString(System.Threading.Thread.CurrentThread.CurrentUICulture); day++; init++; } }
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); }