September 19, 2003

calendars: way more than you want to know

most cf developers can conceivably go thru their entire professional lives without ever thinking too much about calendars (dates are another matter of course). most folks are familiar, in an unconscious sort of way, with the gregorian calendar, its the standard for cf and javaexcept in a few locales like here in Thailand. the world, however, is a big place and there exists quite a few other calendar systems. a couple of days ago someone on cf-talk was asking about hebrew dates. my first thought about that was "is the beer cold?" next thought was about ibm's ICU4J lib which contains a good number of the more popular, non-gregorian calendar systems in use today. since i stuffed my greying head full of its calendar details, some of it was bound to leak out: the hebrew calendar is "lunisolar" (where i actually thought it was lunar, silly me). that gives it what some folks would call "a number of interesting properties" distinct from the gregorian calendar. months start on the day of each new moon (the iCU4J lib actually makes an approximation of this). the solar year (as everyone knows is 365.24 days) is not an even multiple of the lunar month (approximately 29.53 days), so an extra "leap month" needs to be inserted in 7 out of every 19 years (yes, i'd call that interesting). and just to make sure everybody's paying attention, the start of a year can be delayed by up to three days in order to prevent certain holidays from falling on the Sabbath (as well as to prevent illegal year lengths). as the final cherry on the ice cream, the lengths of certain months can vary depending on the number of days in the year. hurts my head too, so i figure let the brainiacs at IBM worry about this and just use the java lib's calendar classes. the problem was i normally used these ICU4J calendars in that locale (ie hebrew langauge w/hebrew calendar, arabic w/islamic calendar, etc.). but what was required for this problem was english transliteraions of hebrew dates. i started thinking about jumping thru all sorts of transliteration hoops when i noticed that the lib's calendar and dateFormat classes all took locales as options. that made things much easier, just had to pass in an english langauge locale and bob's your uncle (snippet from a CFC i built): // fire up the ICU4J & java objects we need aCalendar = createObject("java",""); aLocale = createObject("java","java.util.Locale"); aDateFormat = createObject("java",""); //american locale usLocale=aLocale.init("en","US"); //point the hebrew calendar at that locale hCalendar=aCalendar.init(usLocale); //CFC private method to figure epoch date thisJavaDate=getJavaTime(thisDate,tzOffset); //this will return date string based on hebrew calendar formatted for that locale hDate=aDateFormat.getDateInstance(hCalendar,tDateFormat,usLocale).format(thisJavaDate); //and cool & out return hDate; you can see a testbed for the whole CFC here. i'll post the CFC to the gallery. since i use this sort of thing quite a bit, i guess i'll tidy up and make CFCs for the other calendars that ICU4J supports.


Post a Comment

<< Home