dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.16k stars 4.72k forks source link

Bug in System.Globalization.PersianCalendar #4009

Closed mattjohnsonpint closed 4 years ago

mattjohnsonpint commented 9 years ago
Console.WriteLine(new PersianCalendar().IsLeapYear(1));

Returns False on any machine with .NET Framework 4.6 CTP installed when targeting .NET 4.0 or higher.

Returns True when targeting .NET 3.5 or lower - or when targeting .NET 4.0+ on a machine without Framework 4.6.

mattjohnsonpint commented 9 years ago

Note, according to MSDN:

A leap year is a year that, when divided by 33, has a remainder of 1, 5, 9, 13, 17, 22, 26, or 30. For example, the year 1370 is a leap year because dividing it by 33 yields a remainder of 17. There are approximately eight leap years in every 33-year cycle.

ellismg commented 9 years ago

@tarekgh You might want to take a look at this.

tarekgh commented 9 years ago

We have changed the implementation of the Persian calendar to match the modern Iranian and Afghani calendar which is the Solar Hijri calendar. the calendar mainly used there. both old and new implementation will have same results for date conversions in the year ranges 1800 (Gregorian) to 2123 (Gregorian). also you'll notice change in the leap years too. we are going to fix the documentation too.

tarekgh commented 9 years ago

I forgot to mention Windows 10 also supported this calendar for some locales (like Iran) and used exactly same Solar Hijri calendar algorithm. so now Windows and .Net are fully consistent here

jskeet commented 9 years ago

So what does it do before 1800? As far as I'm aware, the regular Solar Hirji calendar used to be applied throughout history for PersianCalendar, i.e. the 33 year cycle of leap years 1, 5, 9, 13, 17, 22, 26, or 30 - which is what Wikipedia records as well in http://en.wikipedia.org/wiki/Iranian_calendars#Modern_calendar:_Solar_Hijri_.28SH.29

There's a second wikipedia article which may be relevant, however, indicating both an observational calendar and a "Solar Hijri algorithmic calendar" with cycles, grand cycles and great grand cycles...

Is there a good web source for what is implemented now? The latter wikipedia article lists several possibilities, by the looks of it. (For Noda Time, I'll need to work out whether to change our implementation or create a new calendar system with the same rules as the new one, but keep the existing implementation as well for those who want it...)

tarekgh commented 9 years ago

The accurate reference would be Calendrical Calculations book 3rd Edition by Nachum Dershowitz and Edward M. Reingold. Also the link http://en.wikipedia.org/wiki/Solar_Hijri_calendar can be helpful too giving general idea about the Solar Hijri calendar

jskeet commented 9 years ago

Thanks - I assume this is following the astronomical rather than the arithmetic form?

tarekgh commented 9 years ago

That is right. it is astronomical.

tarekgh commented 9 years ago

Please let me know if you have any other question at any time. @mj1856 please close the issue if there is any other question here.