mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.1 KiB
73 lines
2.1 KiB
9 months ago
|
import dayjs from 'dayjs';
|
||
7 months ago
|
import utc from 'dayjs/plugin/utc.js';
|
||
5 months ago
|
import {getCurrentLocale} from '../utils.ts';
|
||
9 months ago
|
|
||
7 months ago
|
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.
|
||
|
*/
|
||
9 months ago
|
export function startDaysBetween(startDate, endDate) {
|
||
7 months ago
|
const start = dayjs.utc(startDate);
|
||
|
const end = dayjs.utc(endDate);
|
||
|
|
||
|
let current = start;
|
||
|
|
||
9 months ago
|
// Ensure the start date is a Sunday
|
||
7 months ago
|
while (current.day() !== 0) {
|
||
|
current = current.add(1, 'day');
|
||
9 months ago
|
}
|
||
|
|
||
|
const startDays = [];
|
||
|
while (current.isBefore(end)) {
|
||
|
startDays.push(current.valueOf());
|
||
7 months ago
|
current = current.add(1, 'week');
|
||
9 months ago
|
}
|
||
|
|
||
|
return startDays;
|
||
|
}
|
||
|
|
||
|
export function firstStartDateAfterDate(inputDate) {
|
||
|
if (!(inputDate instanceof Date)) {
|
||
|
throw new Error('Invalid date');
|
||
|
}
|
||
7 months ago
|
const dayOfWeek = inputDate.getUTCDay();
|
||
9 months ago
|
const daysUntilSunday = 7 - dayOfWeek;
|
||
|
const resultDate = new Date(inputDate.getTime());
|
||
7 months ago
|
resultDate.setUTCDate(resultDate.getUTCDate() + daysUntilSunday);
|
||
9 months ago
|
return resultDate.valueOf();
|
||
|
}
|
||
|
|
||
|
export function fillEmptyStartDaysWithZeroes(startDays, data) {
|
||
|
const result = {};
|
||
|
|
||
|
for (const startDay of startDays) {
|
||
|
result[startDay] = data[startDay] || {'week': startDay, 'additions': 0, 'deletions': 0, 'commits': 0};
|
||
|
}
|
||
|
|
||
|
return Object.values(result);
|
||
|
}
|
||
9 months ago
|
|
||
|
let dateFormat;
|
||
|
|
||
|
// format a Date object to document's locale, but with 24h format from user's current locale because this
|
||
|
// option is a personal preference of the user, not something that the document's locale should dictate.
|
||
|
export function formatDatetime(date) {
|
||
|
if (!dateFormat) {
|
||
|
// TODO: replace `hour12` with `Intl.Locale.prototype.getHourCycles` once there is broad browser support
|
||
|
dateFormat = new Intl.DateTimeFormat(getCurrentLocale(), {
|
||
|
day: 'numeric',
|
||
|
month: 'short',
|
||
|
year: 'numeric',
|
||
|
hour: 'numeric',
|
||
|
hour12: !Number.isInteger(Number(new Intl.DateTimeFormat([], {hour: 'numeric'}).format())),
|
||
|
minute: '2-digit',
|
||
|
timeZoneName: 'short',
|
||
|
});
|
||
|
}
|
||
|
return dateFormat.format(date);
|
||
|
}
|