[kronolith] [patch] error in weeknumber by goto-button

Jason Rust jrust at rustyparts.com
Mon Dec 29 10:44:04 PST 2003


> This doesn't work for me - it produces incorrect results for the first week of
> 2004, and for the 1st week of Feb 2004.

Ok, trying again ;)  Specifically, checked week numbers for Dec., Jan. & Feb.
for a few years and they match the ISO week numbers given at
http://www.merlyn.demon.co.uk/week-cal.txt  Also tested this version in
konqueror and it works.

-Jason
-------------- next part --------------
Index: kronolith/templates/javascript/goto.inc
===================================================================
RCS file: /repository/kronolith/templates/javascript/goto.inc,v
retrieving revision 1.10
diff -u -r1.10 goto.inc
--- kronolith/templates/javascript/goto.inc	24 Oct 2003 21:41:04 -0000	1.10
+++ kronolith/templates/javascript/goto.inc	29 Dec 2003 18:32:39 -0000
@@ -2,24 +2,18 @@
 
 function weekOfYear(d)
 {
+    // adapted from http://www.merlyn.demon.co.uk/js-date7.htm#WkConv
+    var ms1d = 86400000, ms3d = 3 * ms1d, ms7d = 7 * ms1d;
     var year = d.getYear()
     if (year < 1900) {
         year += 1900;
     }
-    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 D3 = Date.UTC(year, d.getMonth(), d.getDate()) + ms3d;
+    var wk = Math.floor(D3 / ms7d);
+    with (new Date(wk * ms7d)) {
+        var yy = getUTCFullYear();
     }
+    return [yy, 1 + wk - Math.floor((Date.UTC(yy, 0, 4) + ms3d) / ms7d)]
 }
 
 function openGoto(timestamp)
@@ -217,7 +211,7 @@
     tbody.appendChild(row);
 
     // rows
-    var week, italic;
+    var weekInfo, italic;
     var count = 1;
     var today = new Date();
     var thisYear = today.getYear();
@@ -236,18 +230,18 @@
             }
             odd = !odd;
             cell = document.createElement('TD');
-            week = weekOfYear(new Date(year, month, i));
+            weekInfo = weekOfYear(new Date(year, month, i));
             italic = document.createElement('I');
             cell.appendChild(italic);
             link = document.createElement('A');
             link.href = '<?php echo Horde::applicationUrl('week.php') ?>';
             if (link.href.indexOf('?') != -1) {
-                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=' + weekInfo[0] + '<?php echo ini_get('arg_separator.output') ?>week=' + weekInfo[1];
             } else {
-                link.href += '?year=' + year + '<?php echo ini_get('arg_separator.output') ?>week=' + week;
+                link.href += '?year=' + weekInfo[0] + '<?php echo ini_get('arg_separator.output') ?>week=' + weekInfo[1];
             }
             italic.appendChild(link);
-            link.appendChild(document.createTextNode(week));
+            link.appendChild(document.createTextNode(weekInfo[1]));
             row.appendChild(cell);
         }
         if (i == 1) {
Index: templates/javascript/open_calendar.js
===================================================================
RCS file: /repository/horde/templates/javascript/open_calendar.js,v
retrieving revision 1.13
diff -u -r1.13 open_calendar.js
--- templates/javascript/open_calendar.js	22 Dec 2003 20:07:32 -0000	1.13
+++ templates/javascript/open_calendar.js	29 Dec 2003 18:32:39 -0000
@@ -2,24 +2,18 @@
 
 function weekOfYear(d)
 {
+    // adapted from http://www.merlyn.demon.co.uk/js-date7.htm#WkConv
+    var ms1d = 86400000, ms3d = 3 * ms1d, ms7d = 7 * ms1d;
     var year = d.getYear()
     if (year < 1900) {
         year += 1900;
     }
-    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 D3 = Date.UTC(year, d.getMonth(), d.getDate()) + ms3d;
+    var wk = Math.floor(D3 / ms7d);
+    with (new Date(wk * ms7d)) {
+        var yy = getUTCFullYear();
     }
+    return [yy, 1 + wk - Math.floor((Date.UTC(yy, 0, 4) + ms3d) / ms7d)]
 }
 
 function openCalendar(imgId, target, callback)


More information about the kronolith mailing list