Skip to content

Commit 64dbabd

Browse files
committed
Use dayjs's utc plugin in startDaysBetween()
1 parent 4c6b196 commit 64dbabd

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

web_src/js/components/RepoCodeFrequency.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export default {
6767
const weekValues = Object.values(this.data);
6868
const start = weekValues[0].week;
6969
const end = firstStartDateAfterDate(new Date());
70-
const startDays = startDaysBetween(new Date(start), new Date(end));
70+
const startDays = startDaysBetween(start, end);
7171
this.data = fillEmptyStartDaysWithZeroes(startDays, this.data);
7272
this.errorText = '';
7373
} else {

web_src/js/components/RepoContributors.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export default {
114114
const weekValues = Object.values(total.weeks);
115115
this.xAxisStart = weekValues[0].week;
116116
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);
118118
total.weeks = fillEmptyStartDaysWithZeroes(startDays, total.weeks);
119119
this.xAxisMin = this.xAxisStart;
120120
this.xAxisMax = this.xAxisEnd;

web_src/js/components/RepoRecentCommits.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default {
6262
const data = await response.json();
6363
const start = Object.values(data)[0].week;
6464
const end = firstStartDateAfterDate(new Date());
65-
const startDays = startDaysBetween(new Date(start), new Date(end));
65+
const startDays = startDaysBetween(start, end);
6666
this.data = fillEmptyStartDaysWithZeroes(startDays, data).slice(-52);
6767
this.errorText = '';
6868
} else {

web_src/js/utils/time.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import dayjs from 'dayjs';
2+
import utc from 'dayjs/plugin/utc.js';
23
import {getCurrentLocale} from '../utils.js';
34

4-
// Returns an array of millisecond-timestamps of start-of-week days (Sundays)
5-
export function startDaysBetween(startDate, endDate) {
5+
dayjs.extend(utc);
6+
7+
/**
8+
* Returns an array of millisecond-timestamps of start-of-week days (Sundays)
9+
*
10+
* @param startConfig can take any type that `Date` accepts.
11+
* @param endConfig can take any type that `Date` accepts.
12+
*/
13+
export function startDaysBetween(startConfig, endConfig) {
14+
const start = dayjs.utc(startConfig);
15+
const end = dayjs.utc(endConfig);
16+
617
// Ensure the start date is a Sunday
7-
while (startDate.getUTCDay() !== 0) {
8-
startDate.setUTCDate(startDate.getUTCDate() + 1);
18+
while (start.day() !== 0) {
19+
start.add(1, 'day');
920
}
1021

11-
const start = dayjs(startDate);
12-
const end = dayjs(endDate);
1322
const startDays = [];
14-
1523
let current = start;
1624
while (current.isBefore(end)) {
1725
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');
26+
current = current.add(1, 'week');
2327
}
2428

2529
return startDays;

0 commit comments

Comments
 (0)