From e2e9ebffaf7003482e0924368d5da4c3ee638016 Mon Sep 17 00:00:00 2001 From: Mantas Date: Fri, 18 Dec 2020 12:20:17 +0200 Subject: [PATCH] Fixes #3341 - Reports do not show week 53 --- app/assets/javascripts/app/controllers/report.coffee | 6 +++++- .../javascripts/app/lib/app_post/pretty_date.coffee | 12 ++++++++++++ public/assets/tests/ui.js | 7 +++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/app/controllers/report.coffee b/app/assets/javascripts/app/controllers/report.coffee index a55226285..eabcb9a0f 100644 --- a/app/assets/javascripts/app/controllers/report.coffee +++ b/app/assets/javascripts/app/controllers/report.coffee @@ -456,6 +456,7 @@ class TimePicker extends App.Controller selectTimeYear: (e) => e.preventDefault() @ui.params.year = $(e.target).data('type') + @_timeSlotPicker() $(e.target).parent().parent().find('li').removeClass('active') $(e.target).parent().addClass('active') App.Event.trigger('ui:report:rerender') @@ -523,7 +524,10 @@ class TimePicker extends App.Controller ] @timeRangeWeek = [] - for item in [1..52] + + numberOfWeeks = App.PrettyDate.getISOWeeks(@ui.params.year) + + for item in [1..numberOfWeeks] record = { display: item value: item diff --git a/app/assets/javascripts/app/lib/app_post/pretty_date.coffee b/app/assets/javascripts/app/lib/app_post/pretty_date.coffee index 670dce162..cc8c4f635 100644 --- a/app/assets/javascripts/app/lib/app_post/pretty_date.coffee +++ b/app/assets/javascripts/app/lib/app_post/pretty_date.coffee @@ -137,3 +137,15 @@ class App.PrettyDate while num.toString().length < digits num = '0' + num num + + @getISOWeeks: (year) -> + dayNumber = new Date("#{year}-01-01").getDay() + isLeap = new Date("#{year}-02-29").getMonth() == 1 + + + # check for a Jan 1 that's a Thursday or a leap year that has a + # Wednesday jan 1. Otherwise it's 52 + if dayNumber == 4 || isLeap && dayNumber == 3 + 53 + else + 52 diff --git a/public/assets/tests/ui.js b/public/assets/tests/ui.js index bcb87c1f7..f35f32fe7 100644 --- a/public/assets/tests/ui.js +++ b/public/assets/tests/ui.js @@ -368,3 +368,10 @@ test("check pretty date", function() { } }); + +test("PrettyDate getISOWeeks", function() { + equal(App.PrettyDate.getISOWeeks(2026), 53, '53 weeks') + equal(App.PrettyDate.getISOWeeks(2020), 53, 'leap year and 53 weeks') + equal(App.PrettyDate.getISOWeeks(2019), 52, '52 weeks') + equal(App.PrettyDate.getISOWeeks(2024), 52, 'leap year and 52 weeks') +})