From fc6290e5394287a13235720cd1cc5a0c2eca7f5a Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 20 Aug 2018 09:20:02 +0200 Subject: Display the week number --- views/calendar/qml/WeekView.qml | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/views/calendar/qml/WeekView.qml b/views/calendar/qml/WeekView.qml index 2d16b772..80e41bea 100644 --- a/views/calendar/qml/WeekView.qml +++ b/views/calendar/qml/WeekView.qml @@ -32,6 +32,37 @@ FocusScope { property date startDate: currentDate property var calendarFilter + /** + * Returns the week number for this date. dowOffset is the day of week the week + * "starts" on for your locale - it can be from 0 to 6. If dowOffset is 1 (Monday), + * the week returned is the ISO 8601 week number. + * @param int dowOffset + * @return int + */ + function getWeek(date, dowOffset) { + var newYear = new Date(date.getFullYear(),0,1); + var day = newYear.getDay() - dowOffset; //the day of week the year begins on + day = (day >= 0 ? day : day + 7); + var daynum = Math.floor((date.getTime() - newYear.getTime() - + (date.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1; + var weeknum; + //if the year starts before the middle of a week + if(day < 4) { + weeknum = Math.floor((daynum+day-1)/7) + 1; + if(weeknum > 52) { + nYear = new Date(date.getFullYear() + 1,0,1); + nday = nYear.getDay() - dowOffset; + nday = nday >= 0 ? nday : nday + 7; + /*if the next year starts before the middle of + the week, it is week #1 of that year*/ + weeknum = nday < 4 ? 1 : 53; + } + } + else { + weeknum = Math.floor((daynum+day-1)/7); + } + return weeknum; + } Item { anchors { top: parent.top @@ -42,6 +73,21 @@ FocusScope { width: root.dayWidth * root.daysToShow + Kube.Units.gridUnit * 2 height: root.height + Item { + id: weekNumber + anchors { + top: parent.top + left: parent.left + } + width: Kube.Units.gridUnit * 2 + height: Kube.Units.gridUnit * 2 + Label { + anchors.centerIn: parent + text: getWeek(startDate, 1) + font.bold: true + } + } + DayLabels { id: dayLabels anchors.top: parent.top -- cgit v1.2.3