fix(加班): 统一日期选择与耗时口径
This commit is contained in:
@@ -39,9 +39,69 @@ export const OVERTIME_REASON_LABEL: Record<string, string> = {
|
||||
'reason-11': '返工修改',
|
||||
};
|
||||
|
||||
export function calcDuration(start: string, end: string): number {
|
||||
const s = new Date(start).getTime();
|
||||
const e = new Date(end).getTime();
|
||||
if (isNaN(s) || isNaN(e) || e <= s) return 0;
|
||||
return Math.round(((e - s) / (1000 * 60 * 60)) * 10) / 10;
|
||||
const MS_PER_HOUR = 60 * 60 * 1000;
|
||||
const STANDARD_START_HOUR = 9;
|
||||
const LUNCH_START_HOUR = 12;
|
||||
const LUNCH_END_HOUR = 13;
|
||||
const STANDARD_END_HOUR = 18;
|
||||
|
||||
function startOfDay(date: Date): Date {
|
||||
const day = new Date(date);
|
||||
day.setHours(0, 0, 0, 0);
|
||||
return day;
|
||||
}
|
||||
|
||||
function dayBound(day: Date, hour: number): Date {
|
||||
const result = new Date(day);
|
||||
result.setHours(hour, 0, 0, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
function isSameLocalDay(a: Date, b: Date): boolean {
|
||||
return a.getFullYear() === b.getFullYear()
|
||||
&& a.getMonth() === b.getMonth()
|
||||
&& a.getDate() === b.getDate();
|
||||
}
|
||||
|
||||
function overlapHours(start: Date, end: Date, segmentStart: Date, segmentEnd: Date): number {
|
||||
const overlapStart = Math.max(start.getTime(), segmentStart.getTime());
|
||||
const overlapEnd = Math.min(end.getTime(), segmentEnd.getTime());
|
||||
return Math.max(0, overlapEnd - overlapStart) / MS_PER_HOUR;
|
||||
}
|
||||
|
||||
function calcProjectDayHours(start: Date, end: Date): number {
|
||||
if (end.getTime() <= start.getTime()) return 0;
|
||||
const day = startOfDay(start);
|
||||
const gross = (end.getTime() - start.getTime()) / MS_PER_HOUR;
|
||||
const lunch = overlapHours(
|
||||
start,
|
||||
end,
|
||||
dayBound(day, LUNCH_START_HOUR),
|
||||
dayBound(day, LUNCH_END_HOUR),
|
||||
);
|
||||
return Math.max(0, gross - lunch);
|
||||
}
|
||||
|
||||
export function calcDuration(start: string, end: string): number {
|
||||
const startDate = new Date(start);
|
||||
const endDate = new Date(end);
|
||||
const s = startDate.getTime();
|
||||
const e = endDate.getTime();
|
||||
if (isNaN(s) || isNaN(e) || e <= s) return 0;
|
||||
|
||||
let total = 0;
|
||||
const cursor = startOfDay(startDate);
|
||||
const lastDay = startOfDay(endDate);
|
||||
|
||||
while (cursor.getTime() <= lastDay.getTime()) {
|
||||
const isFirst = isSameLocalDay(cursor, startDate);
|
||||
const isLast = isSameLocalDay(cursor, endDate);
|
||||
const dayStart = isFirst ? new Date(startDate) : dayBound(cursor, STANDARD_START_HOUR);
|
||||
const dayEnd = isLast ? new Date(endDate) : dayBound(cursor, STANDARD_END_HOUR);
|
||||
|
||||
total += calcProjectDayHours(dayStart, dayEnd);
|
||||
cursor.setDate(cursor.getDate() + 1);
|
||||
}
|
||||
|
||||
return Math.round(total * 10) / 10;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user