Tuesday, December 14, 2010

Studying F# : Local Calendars

One of the most useful features in .NET/CLR is supporting not only Gregorian calendar but also many local calendars. For me, it is awesome that there is the Japanese Calendar (in Japanese : 和暦 / pronounce "wareki").

For instance, Era 3 (Shouwa) ends at 1989/01/07 (Shouwa 64) and Era 4 (Heisei) starts at 1989/01/08 (Heisei 1). Of course, F# (on .NET/CLR) supports these like below:
> let wareki = new System.Globalization.JapaneseCalendar();;

val wareki : System.Globalization.JapaneseCalendar

> wareki.GetYear(new System.DateTime(1989, 1, 7));;
val it : int = 64
> wareki.GetEra(new System.DateTime(1989, 1, 7));;
val it : int = 3
> wareki.GetYear(new System.DateTime(1989, 1, 8));;
val it : int = 1
> wareki.GetEra(new System.DateTime(1989, 1, 8));;
val it : int = 4

And when System.DateTime is initialized with the Japanese Calendar, its Era is indicated for 4 (Heisei). For example, This year is Heisei 22 in the Japanese Calendar:
> new System.DateTime(22, 12, 14, wareki);;
val it : System.DateTime = 2010/12/14 0:00:00 {Date = 2010/12/14 0:00:00;
                                               Day = 14;
                                               DayOfWeek = Tuesday;
                                               DayOfYear = 348;
                                               Hour = 0;
                                               Kind = Unspecified;
                                               Millisecond = 0;
                                               Minute = 0;
                                               Month = 12;
                                               Second = 0;
                                               Ticks = 634278816000000000L;
                                               TimeOfDay = 00:00:00;
                                               Year = 2010;}

No comments:

Post a Comment