Convert calendars to each other Use NodaTime libra

2019-05-22 15:31发布

问题:

I use Noda Time to handle DateTime issues, I read almost all of the documents but I have some problem to convert calendars to each other,

  1. First, how many calendars are supported by Noda?

    As CalendarSystem says there is just 4 calendars:

    a)CopticCalendar / b)GregorianCalendar / c)IslamicCalendar / d)JulianCalendar

    so how about other calendar systems?

  2. I use Persian Calendar aka Jalali Calendar, So is there any way to use this calendar in NodaTime? And how Convert it to Gregorian Calendar?

  3. The final goal for me is to convert both Time(in special Zone) and Date to UTC Time Zone And Georgian Calendar. For example Now(yyyy/MM/dd - HH:mm:ss) in Asia/Tehran time Zone And Persian Calendar is: (1391/06/30 - 10:01:15) and Now in Georgian Calendar and UTC is (2012/09/20 - 05:01:15), so what is your suggestion? The time individually not matter at all, but the Date, I don't have any idea about it(In NodaTime), and is there any way to convert both of these in one process or method?

回答1:

  1. Yes, currently those are the only calendar systems we support.

  2. No, we don't have any support for the Persian Calendar at the moment. You can file a feature request, but we're more likely to be interested in getting 1.0 out of the door to start with, I'm afraid.

  3. It's not clear what you mean by "special zone" - but it sounds like again, you'd need the Persian calendar for this to really work. If .NET supports the Persian calendar, you could parse it to a DateTime first, and then convert that to a Noda Time type... I'm sorry that we don't support the Persian calendar at the moment, and I'd like to be able to support the BCL calendars via wrapping types (as we do for TimeZoneInfo) but that won't be any time soon, I'm afraid.



回答2:

For the sake of others searching for similar keywords, support for Persian calendar has been added (I'm currently using v1.3).

Some calendars have more settings, so this example is simplified:

    var now = DateTime.Now;
    var nodaNow = new NodaTime.LocalDate(now.Year, now.Month, now.Day);
    var cal = nodaNow.WithCalendar(CalendarSystem.GetPersianCalendar());

    Console.WriteLine(cal.ToString());