[xslt] Bug in EXSLT date:week-in-year()

Hi there,

It's been a long time since I last worked on LibXML/LibXSLT/LibEXSLT, many people here might not know about meâ I am the man who drafted the extension framework and committed libexslt, something like 4 or 5 years agoâ

Today, I had to add week numbers to a DHTML "datepicker" (screenshot attached for curious ones) so I went on Gnome CVS to borrow my own "week-in-year" code. It actually happens it's buggy, and it seems nobody reported it.

Sorry not to post into Bugzilla nor patch into CVS directly but I don't remember my password nor the email address I used at that timeâ

When computing the week number, it always consider the year to begin on Monday, while it should use the "day in week" of January 1st. The fix is simple; instead of the following lines:

   fdiw = (_exsltDateDayInWeek(fdiy, dt->value.date.year) + 6) % 7;

   ret = DAY_IN_YEAR(dt->value.date.day, dt->value.date.mon,
                     dt->value.date.year) / 7;

Put these ones:

   fdiw = (_exsltDateDayInWeek(fdiy, dt->value.date.year) + 6) % 7;

   ret = (DAY_IN_YEAR(dt->value.date.day, dt->value.date.mon,
                     dt->value.date.year) + fdiw) / 7;

The only change is to adjust the DAY_IN_YEAR result by the "January 1st day-in-week" value before dividing by 7. That way, week numbers turn on the appropriate day (in 2005, January 9th is on week 1 while January 10th is on week 2; whereas without the fix, week 1 would have run from 1st through 6th and week 2 from 7th to 13th).

Note that the "ISO 8601 adjustment" part just after is still needed, as the "(DAY_IN_YEAR + fdiw) / 7" part will give you week numbers starting at 0: when January 1st falls on Friday, Saturday or Sunday, it belongs to week 0 (or the last week of the previous year, I don't really now about ISO8601 rules), but when it falls on Monday, Tuesday, Wednesday or Thursday, it belongs to week 1, and there's no week 0.

Please note that I didn't test the code above, but it's a port of my ECMAScript script which works (or at least seems to).

Long life to libxml/libxslt/libexslt !!!

Thomas Broyer
Atol Conseils et DÃveloppement

PNG image

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]