refactor(server): 日期工具统一迁入 dayjs(utc 插件),移除手写 Intl/padStart 重复实现
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
|
||||
import dayjs from '../common/dayjs';
|
||||
import { Cron } from '@nestjs/schedule';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { In, LessThan, LessThanOrEqual, MoreThanOrEqual, Repository } from 'typeorm';
|
||||
@@ -283,18 +284,13 @@ export class AttendanceSettlementService {
|
||||
}
|
||||
|
||||
private getCourseClock(date: Date): { date: string; weekDay: number; minutes: number } {
|
||||
const parts = Object.fromEntries(
|
||||
new Intl.DateTimeFormat('en-CA', {
|
||||
timeZone: this.courseTimeZone,
|
||||
year: 'numeric', month: '2-digit', day: '2-digit', weekday: 'short',
|
||||
hour: '2-digit', minute: '2-digit', hourCycle: 'h23',
|
||||
}).formatToParts(date).filter((part) => part.type !== 'literal').map((part) => [part.type, part.value]),
|
||||
);
|
||||
const weekDays: Record<string, number> = { Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri: 5, Sat: 6, Sun: 7 };
|
||||
// Asia/Shanghai 无夏令时,固定 UTC+8 与 Intl 时区格式化等价
|
||||
const c = dayjs.utc(date).add(8, 'hour');
|
||||
const weekDay = c.day() === 0 ? 7 : c.day();
|
||||
return {
|
||||
date: `${parts.year}-${parts.month}-${parts.day}`,
|
||||
weekDay: weekDays[parts.weekday],
|
||||
minutes: Number(parts.hour) * 60 + Number(parts.minute),
|
||||
date: c.format('YYYY-MM-DD'),
|
||||
weekDay,
|
||||
minutes: c.hour() * 60 + c.minute(),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user