I'm developing a scheduling application in CF. While doing so, I've
discovered that there are a variety of different methods for determining the
week number, and that depending on the locale, a week can begin on Saturday,
Sunday or Monday. The first week of the year can be determined in a variety
of ways, and can be numbered in a variety of ways, such that January 1 of a
given year might be in week 0, 1, 52 or 53. Complicating this somewhat is
that there is no clear indication which standard CF's inbuilt Week()
function follows.

All this might not seem so important to Americans, but here in Europe, week
numbers are often used in calenders and agendas. A question such as "Can we
arrange a meeting during week 43?" can be rather common. The question, for
me, was "What standard is followed in Europe to determine week numbers".

The closest thing I found to a universal standard is the ISO week
date system, and I decided that's good enough for me. Reading the technical
definition makes it sound pretty complex, but it can be reduced to "weeks
start on monday and the first week of the year is the first week with work
days in it, which effectively means the week that contains January 4".

There's a ISOWeek function on cflib that tries, but fails to generate the
correct ISO week number some years.

Long story short, I eventually solved this, as a last resort, by dropping
down to Java. I'm an absolute novice at Java and wanted to ask if anyone
could improve on the UDF I came up with before I submit it to CFLib ...

<cfscript>
function ISOWeek(inputDateObj) {
var inputDate = DateFormat(inputDateObj,"yyyy-mm-dd");
    var formatter =
CreateObject("java","java.text.SimpleDateFormat").init("yyyy-MM-dd");
var theDate = formatter.parse(inputDate);
c = CreateObject("java","java.util.Calendar").getInstance();
c.setTimeInMillis(theDate.getTime());
c.setFirstDayOfWeek(c.MONDAY);
c.setMinimalDaysInFirstWeek(4);
return c.get(c.WEEK_OF_YEAR);
}
</cfscript>

... using the following references and a good measure of fumbling around.

http://www.jarvana.com/jarvana/view/com/h2database/h2/1.2.128/h2-1.2.128-sources.jar!/org/h2/util/DateTimeIso8601Utils.java?format=ok
http://www.java2s.com/Tutorial/Java/0040__Data-Type/CreateajavautilDateObjectfromaYearMonthDayFormat.htm
http://www.coldfusionmuse.com/index.cfm/2009/2/23/Unpacking-Java-In-Coldfusion

I also want to suggest that Week() is internationalized appropriately in CF
as an enhancement request.

Thanks,

Nando

-- 
Nando M. Breiter
Aria Media
CP 234
6934 Bioggio
Switzerland

+41 91 606 6372

www.aria-media.com


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:340201
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm

Reply via email to