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
This commit is contained in:
parent
0f52c924e2
commit
ee79579b2e
@ -9,7 +9,6 @@ const Shell = imports.gi.Shell;
|
|||||||
const Gettext_gtk20 = imports.gettext.domain('gtk20');
|
const Gettext_gtk20 = imports.gettext.domain('gtk20');
|
||||||
|
|
||||||
const MSECS_IN_DAY = 24 * 60 * 60 * 1000;
|
const MSECS_IN_DAY = 24 * 60 * 60 * 1000;
|
||||||
const MSECS_IN_WEEK = MSECS_IN_DAY * 7;
|
|
||||||
const WEEKDATE_HEADER_WIDTH_DIGITS = 3;
|
const WEEKDATE_HEADER_WIDTH_DIGITS = 3;
|
||||||
const SHOW_WEEKDATE_KEY = 'show-weekdate';
|
const SHOW_WEEKDATE_KEY = 'show-weekdate';
|
||||||
|
|
||||||
@ -20,12 +19,20 @@ function _sameDay(dateA, dateB) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function _getCalendarWeekForDate(date) {
|
function _getCalendarWeekForDate(date) {
|
||||||
let startOfYear = new Date(date.getFullYear(), 0, 1);
|
// Based on the algorithms found here:
|
||||||
let sday = startOfYear.getDay();
|
// http://en.wikipedia.org/wiki/Talk:ISO_week_date
|
||||||
let offset = 4 + (7 * Math.floor(sday * (1/5))) - sday;
|
let midnightDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
|
||||||
let firstThursday = new Date(date.getFullYear(), 0, 1 + offset);
|
// Need to get Monday to be 1 ... Sunday to be 7
|
||||||
let weekOfYear = Math.ceil((date.getTime() - firstThursday.getTime()) / MSECS_IN_WEEK);
|
let dayOfWeek = 1 + ((midnightDate.getDay() + 6) % 7);
|
||||||
return weekOfYear;
|
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){
|
function _getDigitWidth(actor){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user