summaryrefslogtreecommitdiffstats
path: root/web_src
diff options
context:
space:
mode:
authorGiteabot <teabot@gitea.io>2024-05-06 10:01:22 +0800
committerGitHub <noreply@github.com>2024-05-06 10:01:22 +0800
commit2252a7bf84c26aee0dfa1b1b826dba148f507a3a (patch)
tree0823c5e892fac0e2ad8b36ce6efb2ce3f305b9f5 /web_src
parentbb7150c30d51fb0f14c98ad519af73717ddd3044 (diff)
downloadgitea-2252a7bf84c26aee0dfa1b1b826dba148f507a3a.tar.gz
gitea-2252a7bf84c26aee0dfa1b1b826dba148f507a3a.zip
Have time.js use UTC-related getters/setters (#30857) (#30869)
Backport #30857 by kemzeb Co-authored-by: Kemal Zebari <60799661+kemzeb@users.noreply.github.com> Co-authored-by: Sam Fisher <fisher@3echelon.local>
Diffstat (limited to 'web_src')
-rw-r--r--web_src/js/components/RepoCodeFrequency.vue2
-rw-r--r--web_src/js/components/RepoContributors.vue2
-rw-r--r--web_src/js/components/RepoRecentCommits.vue2
-rw-r--r--web_src/js/utils/time.js33
4 files changed, 22 insertions, 17 deletions
diff --git a/web_src/js/components/RepoCodeFrequency.vue b/web_src/js/components/RepoCodeFrequency.vue
index adce431264..1d40d6d417 100644
--- a/web_src/js/components/RepoCodeFrequency.vue
+++ b/web_src/js/components/RepoCodeFrequency.vue
@@ -67,7 +67,7 @@ export default {
const weekValues = Object.values(this.data);
const start = weekValues[0].week;
const end = firstStartDateAfterDate(new Date());
- const startDays = startDaysBetween(new Date(start), new Date(end));
+ const startDays = startDaysBetween(start, end);
this.data = fillEmptyStartDaysWithZeroes(startDays, this.data);
this.errorText = '';
} else {
diff --git a/web_src/js/components/RepoContributors.vue b/web_src/js/components/RepoContributors.vue
index 2347c41ae4..f7b05831e0 100644
--- a/web_src/js/components/RepoContributors.vue
+++ b/web_src/js/components/RepoContributors.vue
@@ -114,7 +114,7 @@ export default {
const weekValues = Object.values(total.weeks);
this.xAxisStart = weekValues[0].week;
this.xAxisEnd = firstStartDateAfterDate(new Date());
- const startDays = startDaysBetween(new Date(this.xAxisStart), new Date(this.xAxisEnd));
+ const startDays = startDaysBetween(this.xAxisStart, this.xAxisEnd);
total.weeks = fillEmptyStartDaysWithZeroes(startDays, total.weeks);
this.xAxisMin = this.xAxisStart;
this.xAxisMax = this.xAxisEnd;
diff --git a/web_src/js/components/RepoRecentCommits.vue b/web_src/js/components/RepoRecentCommits.vue
index 502af533da..8759978e78 100644
--- a/web_src/js/components/RepoRecentCommits.vue
+++ b/web_src/js/components/RepoRecentCommits.vue
@@ -62,7 +62,7 @@ export default {
const data = await response.json();
const start = Object.values(data)[0].week;
const end = firstStartDateAfterDate(new Date());
- const startDays = startDaysBetween(new Date(start), new Date(end));
+ const startDays = startDaysBetween(start, end);
this.data = fillEmptyStartDaysWithZeroes(startDays, data).slice(-52);
this.errorText = '';
} else {
diff --git a/web_src/js/utils/time.js b/web_src/js/utils/time.js
index 1848792c98..7c7eabd1a3 100644
--- a/web_src/js/utils/time.js
+++ b/web_src/js/utils/time.js
@@ -1,25 +1,30 @@
import dayjs from 'dayjs';
+import utc from 'dayjs/plugin/utc.js';
import {getCurrentLocale} from '../utils.js';
-// Returns an array of millisecond-timestamps of start-of-week days (Sundays)
+dayjs.extend(utc);
+
+/**
+ * Returns an array of millisecond-timestamps of start-of-week days (Sundays)
+ *
+ * @param startConfig The start date. Can take any type that `Date` accepts.
+ * @param endConfig The end date. Can take any type that `Date` accepts.
+ */
export function startDaysBetween(startDate, endDate) {
+ const start = dayjs.utc(startDate);
+ const end = dayjs.utc(endDate);
+
+ let current = start;
+
// Ensure the start date is a Sunday
- while (startDate.getDay() !== 0) {
- startDate.setDate(startDate.getDate() + 1);
+ while (current.day() !== 0) {
+ current = current.add(1, 'day');
}
- const start = dayjs(startDate);
- const end = dayjs(endDate);
const startDays = [];
-
- let current = start;
while (current.isBefore(end)) {
startDays.push(current.valueOf());
- // we are adding 7 * 24 hours instead of 1 week because we don't want
- // date library to use local time zone to calculate 1 week from now.
- // local time zone is problematic because of daylight saving time (dst)
- // used on some countries
- current = current.add(7 * 24, 'hour');
+ current = current.add(1, 'week');
}
return startDays;
@@ -29,10 +34,10 @@ export function firstStartDateAfterDate(inputDate) {
if (!(inputDate instanceof Date)) {
throw new Error('Invalid date');
}
- const dayOfWeek = inputDate.getDay();
+ const dayOfWeek = inputDate.getUTCDay();
const daysUntilSunday = 7 - dayOfWeek;
const resultDate = new Date(inputDate.getTime());
- resultDate.setDate(resultDate.getDate() + daysUntilSunday);
+ resultDate.setUTCDate(resultDate.getUTCDate() + daysUntilSunday);
return resultDate.valueOf();
}