loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Have time.js use UTC-related getters/setters (#30857)

Before this patch, we were using `Date` getter/setter methods that
worked with local time to get a list of Sundays that are in the range of
some start date and end date. The problem with this was that the Sundays
are in Unix epoch time and when we changed the "startDate" argument that
was passed to make sure it is on a Sunday, this change would be
reflected when we convert it to Unix epoch time. More specifically, I
observed that we may get different Unix epochs depending on your
timezone when the returned list should rather be timezone-agnostic.

This led to issues in US timezones that caused the contributor, code
frequency, and recent commit charts to not show any chart data. This fix
resolves this by using getter/setter methods that work with UTC since it
isn't dependent on timezones.

Fixes #30851.

---------

Co-authored-by: Sam Fisher <fisher@3echelon.local>
(cherry picked from commit 22c7b3a74459833b86783e84d4708c8934d34e58)

authored by

Kemal Zebari
Sam Fisher
and committed by
Earl Warren
16eb85ad 8672ad12

+22 -17
+1 -1
web_src/js/components/RepoCodeFrequency.vue
··· 67 67 const weekValues = Object.values(this.data); 68 68 const start = weekValues[0].week; 69 69 const end = firstStartDateAfterDate(new Date()); 70 - const startDays = startDaysBetween(new Date(start), new Date(end)); 70 + const startDays = startDaysBetween(start, end); 71 71 this.data = fillEmptyStartDaysWithZeroes(startDays, this.data); 72 72 this.errorText = ''; 73 73 } else {
+1 -1
web_src/js/components/RepoContributors.vue
··· 114 114 const weekValues = Object.values(total.weeks); 115 115 this.xAxisStart = weekValues[0].week; 116 116 this.xAxisEnd = firstStartDateAfterDate(new Date()); 117 - const startDays = startDaysBetween(new Date(this.xAxisStart), new Date(this.xAxisEnd)); 117 + const startDays = startDaysBetween(this.xAxisStart, this.xAxisEnd); 118 118 total.weeks = fillEmptyStartDaysWithZeroes(startDays, total.weeks); 119 119 this.xAxisMin = this.xAxisStart; 120 120 this.xAxisMax = this.xAxisEnd;
+1 -1
web_src/js/components/RepoRecentCommits.vue
··· 62 62 const data = await response.json(); 63 63 const start = Object.values(data)[0].week; 64 64 const end = firstStartDateAfterDate(new Date()); 65 - const startDays = startDaysBetween(new Date(start), new Date(end)); 65 + const startDays = startDaysBetween(start, end); 66 66 this.data = fillEmptyStartDaysWithZeroes(startDays, data).slice(-52); 67 67 this.errorText = ''; 68 68 } else {
+19 -14
web_src/js/utils/time.js
··· 1 1 import dayjs from 'dayjs'; 2 + import utc from 'dayjs/plugin/utc.js'; 2 3 import {getCurrentLocale} from '../utils.js'; 3 4 4 - // Returns an array of millisecond-timestamps of start-of-week days (Sundays) 5 + dayjs.extend(utc); 6 + 7 + /** 8 + * Returns an array of millisecond-timestamps of start-of-week days (Sundays) 9 + * 10 + * @param startConfig The start date. Can take any type that `Date` accepts. 11 + * @param endConfig The end date. Can take any type that `Date` accepts. 12 + */ 5 13 export function startDaysBetween(startDate, endDate) { 14 + const start = dayjs.utc(startDate); 15 + const end = dayjs.utc(endDate); 16 + 17 + let current = start; 18 + 6 19 // Ensure the start date is a Sunday 7 - while (startDate.getDay() !== 0) { 8 - startDate.setDate(startDate.getDate() + 1); 20 + while (current.day() !== 0) { 21 + current = current.add(1, 'day'); 9 22 } 10 23 11 - const start = dayjs(startDate); 12 - const end = dayjs(endDate); 13 24 const startDays = []; 14 - 15 - let current = start; 16 25 while (current.isBefore(end)) { 17 26 startDays.push(current.valueOf()); 18 - // we are adding 7 * 24 hours instead of 1 week because we don't want 19 - // date library to use local time zone to calculate 1 week from now. 20 - // local time zone is problematic because of daylight saving time (dst) 21 - // used on some countries 22 - current = current.add(7 * 24, 'hour'); 27 + current = current.add(1, 'week'); 23 28 } 24 29 25 30 return startDays; ··· 29 34 if (!(inputDate instanceof Date)) { 30 35 throw new Error('Invalid date'); 31 36 } 32 - const dayOfWeek = inputDate.getDay(); 37 + const dayOfWeek = inputDate.getUTCDay(); 33 38 const daysUntilSunday = 7 - dayOfWeek; 34 39 const resultDate = new Date(inputDate.getTime()); 35 - resultDate.setDate(resultDate.getDate() + daysUntilSunday); 40 + resultDate.setUTCDate(resultDate.getUTCDate() + daysUntilSunday); 36 41 return resultDate.valueOf(); 37 42 } 38 43