[kronolith] [patch] error in weeknumber by goto-button
Jason Rust
jrust at rustyparts.com
Wed Dec 24 13:42:55 PST 2003
> There is an error in the goto-button. The weeknumber isn't correct in the new
> year. It begins with 52 instead of 1.
After some research it turns out determining the week number is harder
than it looks. After trying several ideas I found on the web I finally
found one which works consistently (tested several years and everything
looks good). This also patches open_calendar.js which had the same
function (even though it doesn't look to be used anywhere).
-Jason
-------------- next part --------------
Index: templates/javascript/open_calendar.js
===================================================================
RCS file: /repository/horde/templates/javascript/open_calendar.js,v
retrieving revision 1.13
diff -r1.13 open_calendar.js
4a5,7
> // help from http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=38BA9E6E.5EDD4737%40rvjintl.com
> var P3D = 259200000
> var P7D = 604800000
9,21c12,16
< var firstday = new Date(year, 0, 1);
< var firstwday = 8 - firstday.getDay();
< if (firstwday == 8) {
< firstwday = 1;
< } else if (firstwday > 4) {
< firstwday -= 7;
< }
<
< var dayofyear = (d - firstday.getTime() + 86400000) / 86400000;
< if (dayofyear < firstwday) {
< return weekOfYear(new Date(year - 1, 12, 31));
< } else {
< return Math.floor((dayofyear - firstwday) / 7) + 1;
---
> var s = Math.floor((Date.UTC(year, d.getMonth(), d.getDate()) + P3D) / P7D);
> tmp = new Date(s * P7D);
> var weekyear = tmp.getYear();
> if (weekyear < 1900) {
> weekyear += 1900;
22a18
> return 1 + s - Math.floor((Date.UTC(weekyear, 0, 4) + P3D) / P7D)
Index: kronolith/templates/javascript/goto.inc
===================================================================
RCS file: /repository/kronolith/templates/javascript/goto.inc,v
retrieving revision 1.10
diff -r1.10 goto.inc
3c3
< function weekOfYear(d)
---
> function weekOfYear(d, returnVal)
4a5,7
> // help from http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=38BA9E6E.5EDD4737%40rvjintl.com
> var P3D = 259200000
> var P7D = 604800000
9,14c12,16
< var firstday = new Date(year, 0, 1);
< var firstwday = 8 - firstday.getDay();
< if (firstwday == 8) {
< firstwday = 1;
< } else if (firstwday > 4) {
< firstwday -= 7;
---
> var s = Math.floor((Date.UTC(year, d.getMonth(), d.getDate()) + P3D) / P7D);
> tmp = new Date(s * P7D);
> var weekyear = tmp.getYear();
> if (weekyear < 1900) {
> weekyear += 1900;
16,19c18,19
<
< var dayofyear = (d - firstday.getTime() + 86400000) / 86400000;
< if (dayofyear < firstwday) {
< return weekOfYear(new Date(year - 1, 12, 31));
---
> if (returnVal == 'year') {
> return weekyear;
21c21
< return Math.floor((dayofyear - firstwday) / 7) + 1;
---
> return 1 + s - Math.floor((Date.UTC(weekyear, 0, 4) + P3D) / P7D)
239c239,240
< week = weekOfYear(new Date(year, month, i));
---
> week = weekOfYear(new Date(year, month, i), 'week');
> weekYear = weekOfYear(new Date(year, month, i), 'year');
245c246
< link.href += '<?php echo ini_get('arg_separator.output') ?>year=' + year + '<?php echo ini_get('arg_separator.output') ?>week=' + week;
---
> link.href += '<?php echo ini_get('arg_separator.output') ?>year=' + weekYear + '<?php echo ini_get('arg_separator.output') ?>week=' + week;
247c248
< link.href += '?year=' + year + '<?php echo ini_get('arg_separator.output') ?>week=' + week;
---
> link.href += '?year=' + weekYear + '<?php echo ini_get('arg_separator.output') ?>week=' + week;
More information about the kronolith
mailing list