[gnome-shell] Change the algorithm used to calculate week numbers



commit ee79579b2e1dc2469b5f7bda38f4ca6c1d61f4d7
Author: Mads Villadsen <maxx krakoa dk>
Date:   Sun May 30 23:17:40 2010 +0200

    Change the algorithm used to calculate week numbers
    
    The currently used algorithm returns incorrect results in some corner
    cases, so replace it with a better one.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=620125

 js/ui/calendar.js |   21 ++++++++++++++-------
 1 files changed, 14 insertions(+), 7 deletions(-)
---
diff --git a/js/ui/calendar.js b/js/ui/calendar.js
index 6afbc1c..02a264e 100644
--- a/js/ui/calendar.js
+++ b/js/ui/calendar.js
@@ -9,7 +9,6 @@ const Shell = imports.gi.Shell;
 const Gettext_gtk20 = imports.gettext.domain('gtk20');
 
 const MSECS_IN_DAY = 24 * 60 * 60 * 1000;
-const MSECS_IN_WEEK = MSECS_IN_DAY * 7;
 const WEEKDATE_HEADER_WIDTH_DIGITS = 3;
 const SHOW_WEEKDATE_KEY = 'show-weekdate';
 
@@ -20,12 +19,20 @@ function _sameDay(dateA, dateB) {
 }
 
 function _getCalendarWeekForDate(date) {
-    let startOfYear = new Date(date.getFullYear(), 0, 1);
-    let sday = startOfYear.getDay();
-    let offset = 4 + (7 * Math.floor(sday * (1/5))) - sday;
-    let firstThursday = new Date(date.getFullYear(), 0, 1 + offset);
-    let weekOfYear = Math.ceil((date.getTime() - firstThursday.getTime()) / MSECS_IN_WEEK);
-    return weekOfYear;
+    // Based on the algorithms found here:
+    // http://en.wikipedia.org/wiki/Talk:ISO_week_date
+    let midnightDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
+    // Need to get Monday to be 1 ... Sunday to be 7
+    let dayOfWeek = 1 + ((midnightDate.getDay() + 6) % 7);
+    let nearestThursday = new Date(midnightDate.getFullYear(), midnightDate.getMonth(),
+                                   midnightDate.getDate() + (4 - dayOfWeek));
+
+    let jan1st = new Date(nearestThursday.getFullYear(), 0, 1);
+    let diffDate = nearestThursday - jan1st;
+    let dayNumber = Math.floor(Math.abs(diffDate) / MSECS_IN_DAY);
+    let weekNumber = Math.floor(dayNumber / 7) + 1;
+
+    return weekNumber;
 }
 
 function _getDigitWidth(actor){



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