公历日期转换为孟加拉日期的算法 [英] Algorithm for conversion of Gregorian date to Bengali Date

查看:130
本文介绍了公历日期转换为孟加拉日期的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索谷歌一点,找出将格里高利日期转换为孟加拉日期的逻辑。但到目前为止,我搜索过我找到了几种此类软件,但我需要这背后的逻辑或计算。是否有人足够善良分享我实施孟加拉日历的某种资源?提前感谢。 :)

I have searched Google a little bit to find out the logic of converting Gregorian date to Bengali date.However so far I searched I found several software of this kind but I need the logic or calculation behind this.Will someone be enough kind to share me some sort of resources of implementing Bengali Calendar?Thanks in advance. :)

推荐答案

哇!我太棒了!



.Net中没有孟加拉日历。我认为这是一个疏忽,所以我做了一个!!!



这是一个粗略但它的工作原理



我知道,对:我很恐怖'很棒!



用法:

Wow! I am sooo good to you!

There is no Bengali calendar in .Net. I think this is an oversight so I made one!!!

It's a rough cut but it works

I know, right: I'm frickin' AWESOME!

usage:
var thisDate = DateTime.Now;
BengaliCalendar bCal = new BengaliCalendar();

Console.WriteLine("Today in the Gregorian Calendar:  {0:dddd}, {0}", thisDate);
Console.WriteLine("Today in the Persian Calendar:    {0}, {1}/{2}/{3} {4}:{5}:{6}\n",
                          bCal.GetDayOfWeek(thisDate),
                          bCal.GetMonth(thisDate),
                          bCal.GetDayOfMonth(thisDate),
                          bCal.GetYear(thisDate),
                          bCal.GetHour(thisDate),
                          bCal.GetMinute(thisDate),
                          bCal.GetSecond(thisDate));





这是日历类:





Here is the calendar class:

internal class BengaliCalendar : Calendar
{
    /// <summary>Represents the current era. This field is constant.</summary>
    public const int ADEra = 1;
    internal const int DatePartYear = 0;
    internal const int DatePartDayOfYear = 1;
    internal const int DatePartMonth = 2;
    internal const int DatePartDay = 3;
    internal const int MaxYear = 9999;
    internal static readonly int[] DaysToMonth365 = new int[]
    {
        0,
        31,
        62,
        93,
        124,
        155,
        185,
        215,
        245,
        275,
        305,
        335,
        365
    };
    internal static readonly int[] DaysToMonth366 = new int[]
    {
        0,
        31,
        62,
        93,
        124,
        155,
        185,
        215,
        245,
        275,
        305,
        336,
        366
    };
    private static volatile Calendar s_defaultInstance;
    private const int DEFAULT_TWO_DIGIT_YEAR_MAX = 2029;
    /// <summary>Gets the earliest date and time supported by the <see cref="T:System.Globalization.GregorianCalendar" /> type.</summary>
    /// <returns>The earliest date and time supported by the <see cref="T:System.Globalization.GregorianCalendar" /> type, which is the first moment of January 1, 0001 C.E. and is equivalent to <see cref="F:System.DateTime.MinValue" />.</returns>
    [ComVisible(false)]
    public override DateTime MinSupportedDateTime
    {
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        get
        {
            return DateTime.MinValue;
        }
    }
    /// <summary>Gets the latest date and time supported by the <see cref="T:System.Globalization.GregorianCalendar" /> type.</summary>
    /// <returns>The latest date and time supported by the <see cref="T:System.Globalization.GregorianCalendar" /> type, which is the last moment of December 31, 9999 C.E. and is equivalent to <see cref="F:System.DateTime.MaxValue" />.</returns>
    [ComVisible(false)]
    public override DateTime MaxSupportedDateTime
    {
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        get
        {
            return DateTime.MaxValue;
        }
    }
    /// <summary>Gets a value that indicates whether the current calendar is solar-based, lunar-based, or a combination of both.</summary>
    /// <returns>A <see cref="F:System.Globalization.CalendarAlgorithmType.SolarCalendar" /> object.</returns>
    [ComVisible(false)]
    public override CalendarAlgorithmType AlgorithmType
    {
        get
        {
            return CalendarAlgorithmType.SolarCalendar;
        }
    }
    /// <summary>Gets the list of eras in the <see cref="T:System.Globalization.GregorianCalendar" />.</summary>
    /// <returns>An array of integers that represents the eras in the <see cref="T:System.Globalization.GregorianCalendar" />.</returns>
    public override int[] Eras
    {
        get
        {
            return new int[]
            {
                1
            };
        }
    }

    internal static Calendar GetDefaultInstance()
    {
        if (s_defaultInstance == null)
        {
            s_defaultInstance = new BengaliCalendar();
        }
        return s_defaultInstance;
    }

    internal virtual int GetDatePart(long ticks, int part)
    {
        DateTime bengaliDiff = new DateTime(594,4,14);
        ticks = ticks - bengaliDiff.Ticks;

        int i = (int)(ticks / 864000000000L);
        int num = i / 146097;
        i -= num * 146097;
        int num2 = i / 36524;
        if (num2 == 4)
        {
            num2 = 3;
        }
        i -= num2 * 36524;
        int num3 = i / 1461;
        i -= num3 * 1461;
        int num4 = i / 365;
        if (num4 == 4)
        {
            num4 = 3;
        }
        if (part == 0)
        {
            return num * 400 + num2 * 100 + num3 * 4 + num4 + 1;
        }
        i -= num4 * 365;
        if (part == 1)
        {
            return i + 1;
        }
        int[] array = (num4 == 3 && (num3 != 24 || num2 == 3)) ? DaysToMonth366 : DaysToMonth365;
        int num5 = i >> 6;
        while (i >= array[num5])
        {
            num5++;
        }
        if (part == 2)
        {
            return num5;
        }
        return i - array[num5 - 1] + 1;
    }
    internal static long GetAbsoluteDate(int year, int month, int day)
    {
        if (year >= 1 && year <= 9999 && month >= 1 && month <= 12)
        {
            int[] array = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? DaysToMonth366 : DaysToMonth365;
            if (day >= 1 && day <= array[month] - array[month - 1])
            {
                int num = year - 1;
                int num2 = num * 365 + num / 4 - num / 100 + num / 400 + array[month - 1] + day - 1;
                return (long)num2;
            }
        }
        throw new ArgumentOutOfRangeException(null, "ArgumentOutOfRange_BadYearMonthDay");
    }
    internal virtual long DateToTicks(int year, int month, int day)
    {
        return GetAbsoluteDate(year, month, day) * 864000000000L;
    }
    /// <summary>Returns a <see cref="T:System.DateTime" /> that is the specified number of months away from the specified <see cref="T:System.DateTime" />.</summary>
    /// <returns>The <see cref="T:System.DateTime" /> that results from adding the specified number of months to the specified <see cref="T:System.DateTime" />.</returns>
    /// <param name="time">The <see cref="T:System.DateTime" /> to which to add months. </param>
    /// <param name="months">The number of months to add. </param>
    /// <exception cref="T:System.ArgumentException">The resulting <see cref="T:System.DateTime" /> is outside the supported range. </exception>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    ///   <paramref name="months" /> is less than -120000.-or- <paramref name="months" /> is greater than 120000. </exception>
    public override DateTime AddMonths(DateTime time, int months)
    {
        if (months < -120000 || months > 120000)
        {
            throw new ArgumentOutOfRangeException("months", string.Format(CultureInfo.CurrentCulture,"ArgumentOutOfRange_Range", new object[]
            {
                -120000,
                120000
            }));
        }
        int num = this.GetDatePart(time.Ticks, 0);
        int num2 = this.GetDatePart(time.Ticks, 2);
        int num3 = this.GetDatePart(time.Ticks, 3);
        int num4 = num2 - 1 + months;
        if (num4 >= 0)
        {
            num2 = num4 % 12 + 1;
            num += num4 / 12;
        }
        else
        {
            num2 = 12 + (num4 + 1) % 12;
            num += (num4 - 11) / 12;
        }
        int[] array = (num % 4 == 0 && (num % 100 != 0 || num % 400 == 0)) ? DaysToMonth366 : DaysToMonth365;
        int num5 = array[num2] - array[num2 - 1];
        if (num3 > num5)
        {
            num3 = num5;
        }
        long ticks = this.DateToTicks(num, num2, num3) + time.Ticks % 864000000000L;
        //Calendar.CheckAddResult(ticks, this.MinSupportedDateTime, this.MaxSupportedDateTime);
        return new DateTime(ticks);
    }
    /// <summary>Returns a <see cref="T:System.DateTime" /> that is the specified number of years away from the specified <see cref="T:System.DateTime" />.</summary>
    /// <returns>The <see cref="T:System.DateTime" /> that results from adding the specified number of years to the specified <see cref="T:System.DateTime" />.</returns>
    /// <param name="time">The <see cref="T:System.DateTime" /> to which to add years. </param>
    /// <param name="years">The number of years to add. </param>
    /// <exception cref="T:System.ArgumentException">The resulting <see cref="T:System.DateTime" /> is outside the supported range. </exception>
    public override DateTime AddYears(DateTime time, int years)
    {
        return this.AddMonths(time, years * 12);
    }
    /// <summary>Returns the day of the month in the specified <see cref="T:System.DateTime" />.</summary>
    /// <returns>An integer from 1 to 31 that represents the day of the month in <paramref name="time" />.</returns>
    /// <param name="time">The <see cref="T:System.DateTime" /> to read. </param>
    public override int GetDayOfMonth(DateTime time)
    {
        return this.GetDatePart(time.Ticks, 3);
    }
    /// <summary>Returns the day of the week in the specified <see cref="T:System.DateTime" />.</summary>
    /// <returns>A <see cref="T:System.DayOfWeek" /> value that represents the day of the week in <paramref name="time" />.</returns>
    /// <param name="time">The <see cref="T:System.DateTime" /> to read. </param>
    public override DayOfWeek GetDayOfWeek(DateTime time)
    {
        return (DayOfWeek)((time.Ticks / 864000000000L + 1L) % 7);
    }
    /// <summary>Returns the day of the year in the specified <see cref="T:System.DateTime" />.</summary>
    /// <returns>An integer from 1 to 366 that represents the day of the year in <paramref name="time" />.</returns>
    /// <param name="time">The <see cref="T:System.DateTime" /> to read. </param>
    public override int GetDayOfYear(DateTime time)
    {
        return this.GetDatePart(time.Ticks, 1);
    }
    /// <summary>Returns the number of days in the specified month in the specified year in the specified era.</summary>
    /// <returns>The number of days in the specified month in the specified year in the specified era.</returns>
    /// <param name="year">An integer that represents the year. </param>
    /// <param name="month">An integer from 1 to 12 that represents the month. </param>
    /// <param name="era">An integer that represents the era. </param>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    ///   <paramref name="era" /> is outside the range supported by the calendar.-or- <paramref name="year" /> is outside the range supported by the calendar.-or- <paramref name="month" /> is outside the range supported by the calendar. </exception>
    public override int GetDaysInMonth(int year, int month, int era)
    {
        if (era != 0 && era != 1)
        {
            throw new ArgumentOutOfRangeException("era", "ArgumentOutOfRange_InvalidEraValue");
        }
        if (year < 1 || year > 9999)
        {
            throw new ArgumentOutOfRangeException("year", "ArgumentOutOfRange_Range");
        }
        if (month < 1 || month > 12)
        {
            throw new ArgumentOutOfRangeException("month", "ArgumentOutOfRange_Month");
        }
        int[] array = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? DaysToMonth366 : DaysToMonth365;
        return array[month] - array[month - 1];
    }
    /// <summary>Returns the number of days in the specified year in the specified era.</summary>
    /// <returns>The number of days in the specified year in the specified era.</returns>
    /// <param name="year">An integer that represents the year. </param>
    /// <param name="era">An integer that represents the era. </param>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    ///   <paramref name="era" /> is outside the range supported by the calendar.-or- <paramref name="year" /> is outside the range supported by the calendar.</exception>
    public override int GetDaysInYear(int year, int era)
    {
        if (era != 0 && era != 1)
        {
            throw new ArgumentOutOfRangeException("era", "ArgumentOutOfRange_InvalidEraValue");
        }
        if (year < 1 || year > 9999)
        {
            throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, "ArgumentOutOfRange_Range", new object[]
            {
                1,
                9999
            }));
        }
        if (year % 4 != 0 || (year % 100 == 0 && year % 400 != 0))
        {
            return 365;
        }
        return 366;
    }
    /// <summary>Returns the era in the specified <see cref="T:System.DateTime" />.</summary>
    /// <returns>An integer that represents the era in <paramref name="time" />.</returns>
    /// <param name="time">The <see cref="T:System.DateTime" /> to read. </param>
    public override int GetEra(DateTime time)
    {
        return 1;
    }
    /// <summary>Returns the month in the specified <see cref="T:System.DateTime" />.</summary>
    /// <returns>An integer from 1 to 12 that represents the month in <paramref name="time" />.</returns>
    /// <param name="time">The <see cref="T:System.DateTime" /> to read. </param>
    public override int GetMonth(DateTime time)
    {
        return this.GetDatePart(time.Ticks, 2);
    }
    /// <summary>Returns the number of months in the specified year in the specified era.</summary>
    /// <returns>The number of months in the specified year in the specified era.</returns>
    /// <param name="year">An integer that represents the year. </param>
    /// <param name="era">An integer that represents the era. </param>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    ///   <paramref name="era" /> is outside the range supported by the calendar.-or- <paramref name="year" /> is outside the range supported by the calendar. </exception>
    public override int GetMonthsInYear(int year, int era)
    {
        if (era != 0 && era != 1)
        {
            throw new ArgumentOutOfRangeException("era", "ArgumentOutOfRange_InvalidEraValue");
        }
        if (year >= 1 && year <= 9999)
        {
            return 12;
        }
        throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, "ArgumentOutOfRange_Range", new object[]
        {
            1,
            9999
        }));
    }
    /// <summary>Returns the year in the specified <see cref="T:System.DateTime" />.</summary>
    /// <returns>An integer that represents the year in <paramref name="time" />.</returns>
    /// <param name="time">The <see cref="T:System.DateTime" /> to read. </param>
    public override int GetYear(DateTime time)
    {
        return this.GetDatePart(time.Ticks, 0);
    }
    /// <summary>Determines whether the specified date in the specified era is a leap day.</summary>
    /// <returns>true if the specified day is a leap day; otherwise, false.</returns>
    /// <param name="year">An integer that represents the year. </param>
    /// <param name="month">An integer from 1 to 12 that represents the month. </param>
    /// <param name="day">An integer from 1 to 31 that represents the day. </param>
    /// <param name="era">An integer that represents the era. </param>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    ///   <paramref name="era" /> is outside the range supported by the calendar. -or- <paramref name="year" /> is outside the range supported by the calendar.-or- <paramref name="month" /> is outside the range supported by the calendar.-or- <paramref name="day" /> is outside the range supported by the calendar. </exception>
    public override bool IsLeapDay(int year, int month, int day, int era)
    {
        if (month < 1 || month > 12)
        {
            throw new ArgumentOutOfRangeException("month", "ArgumentOutOfRange_Range");
        }
        if (era != 0 && era != 1)
        {
            throw new ArgumentOutOfRangeException("era", "ArgumentOutOfRange_InvalidEraValue");
        }
        if (year < 1 || year > 9999)
        {
            throw new ArgumentOutOfRangeException("year", "ArgumentOutOfRange_Range");
        }
        if (day < 1 || day > this.GetDaysInMonth(year, month))
        {
            throw new ArgumentOutOfRangeException("day", "ArgumentOutOfRange_Range");
        }
        return this.IsLeapYear(year) && (month == 2 && day == 29);
    }
    /// <summary>Calculates the leap month for a specified year and era.</summary>
    /// <returns>The return value is always 0 because the <see cref="T:System.Globalization.GregorianCalendar" /> type does not support the notion of a leap month.</returns>
    /// <param name="year">A year.</param>
    /// <param name="era">An era. Specify either <see cref="F:System.Globalization.GregorianCalendar.ADEra" /> or <see cref="F:System.Globalization.Calendar.CurrentEra" />.</param>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    ///   <paramref name="year" /> is less than the Gregorian calendar year 1 or greater than the Gregorian calendar year 9999.-or-<paramref name="era" /> is not <see cref="F:System.Globalization.GregorianCalendar.ADEra" /> or <see cref="F:System.Globalization.Calendar.CurrentEra" />.</exception>
    [ComVisible(false)]
    public override int GetLeapMonth(int year, int era)
    {
        if (era != 0 && era != 1)
        {
            throw new ArgumentOutOfRangeException("era", "ArgumentOutOfRange_InvalidEraValue");
        }
        if (year < 1 || year > 9999)
        {
            throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, "ArgumentOutOfRange_Range", new object[]
            {
                1,
                9999
            }));
        }
        return 0;
    }
    /// <summary>Determines whether the specified month in the specified year in the specified era is a leap month.</summary>
    /// <returns>This method always returns false, unless overridden by a derived class.</returns>
    /// <param name="year">An integer that represents the year. </param>
    /// <param name="month">An integer from 1 to 12 that represents the month. </param>
    /// <param name="era">An integer that represents the era. </param>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    ///   <paramref name="era" /> is outside the range supported by the calendar.-or- <paramref name="year" /> is outside the range supported by the calendar.-or- <paramref name="month" /> is outside the range supported by the calendar. </exception>
    public override bool IsLeapMonth(int year, int month, int era)
    {
        if (era != 0 && era != 1)
        {
            throw new ArgumentOutOfRangeException("era", "ArgumentOutOfRange_InvalidEraValue");
        }
        if (year < 1 || year > 9999)
        {
            throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, "ArgumentOutOfRange_Range", new object[]
            {
                1,
                9999
            }));
        }
        if (month < 1 || month > 12)
        {
            throw new ArgumentOutOfRangeException("month", "ArgumentOutOfRange_Range");
        }
        return false;
    }
    /// <summary>Determines whether the specified year in the specified era is a leap year.</summary>
    /// <returns>true if the specified year is a leap year; otherwise, false.</returns>
    /// <param name="year">An integer that represents the year. </param>
    /// <param name="era">An integer that represents the era. </param>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    ///   <paramref name="era" /> is outside the range supported by the calendar.-or- <paramref name="year" /> is outside the range supported by the calendar. </exception>
    public override bool IsLeapYear(int year, int era)
    {
        if (era != 0 && era != 1)
        {
            throw new ArgumentOutOfRangeException("era", "ArgumentOutOfRange_InvalidEraValue");
        }
        if (year >= 1 && year <= 9999)
        {
            return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
        }
        throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, "ArgumentOutOfRange_Range", new object[]
        {
            1,
            9999
        }));
    }
    /// <summary>Returns a <see cref="T:System.DateTime" /> that is set to the specified date and time in the specified era.</summary>
    /// <returns>The <see cref="T:System.DateTime" /> that is set to the specified date and time in the current era.</returns>
    /// <param name="year">An integer that represents the year. </param>
    /// <param name="month">An integer from 1 to 12 that represents the month. </param>
    /// <param name="day">An integer from 1 to 31 that represents the day. </param>
    /// <param name="hour">An integer from 0 to 23 that represents the hour. </param>
    /// <param name="minute">An integer from 0 to 59 that represents the minute. </param>
    /// <param name="second">An integer from 0 to 59 that represents the second. </param>
    /// <param name="millisecond">An integer from 0 to 999 that represents the millisecond. </param>
    /// <param name="era">An integer that represents the era. </param>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    ///   <paramref name="era" /> is outside the range supported by the calendar.-or- <paramref name="year" /> is outside the range supported by the calendar.-or- <paramref name="month" /> is outside the range supported by the calendar.-or- <paramref name="day" /> is outside the range supported by the calendar.-or- <paramref name="hour" /> is less than zero or greater than 23.-or- <paramref name="minute" /> is less than zero or greater than 59.-or- <paramref name="second" /> is less than zero or greater than 59.-or- <paramref name="millisecond" /> is less than zero or greater than 999. </exception>
    public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
    {
        if (era == 0 || era == 1)
        {
            return new DateTime(year, month, day, hour, minute, second, millisecond);
        }
        throw new ArgumentOutOfRangeException("era", "ArgumentOutOfRange_InvalidEraValue");
    }
    /// <summary>Converts the specified year to a four-digit year by using the <see cref="P:System.Globalization.GregorianCalendar.TwoDigitYearMax" /> property to determine the appropriate century.</summary>
    /// <returns>An integer that contains the four-digit representation of <paramref name="year" />.</returns>
    /// <param name="year">A two-digit or four-digit integer that represents the year to convert. </param>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    ///   <paramref name="year" /> is outside the range supported by the calendar. </exception>
    public override int ToFourDigitYear(int year)
    {
        if (year < 0)
        {
            throw new ArgumentOutOfRangeException("year", "ArgumentOutOfRange_NeedNonNegNum");
        }
        if (year > 9999)
        {
            throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, "ArgumentOutOfRange_Range", new object[]
            {
                1,
                9999
            }));
        }
        return base.ToFourDigitYear(year);
    }


这篇关于公历日期转换为孟加拉日期的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆