refactor: improve date handling and timezone consistency

- Add timezone support to database module (+08:00)
- Extract date formatting utilities to shared modules
- Standardize timezone handling across frontend and backend
- Improve date formatting consistency in UI components
- Refactor crawler page.goto options for better readability
This commit is contained in:
dmy
2026-01-15 16:17:41 +08:00
parent eba5c7e5c5
commit 5edebd9d55
20 changed files with 219 additions and 57 deletions

View File

@@ -3,6 +3,11 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository, LessThan } from 'typeorm';
import { BidItem } from '../entities/bid-item.entity';
import { CrawlInfoAdd } from '../../crawler/entities/crawl-info-add.entity';
import {
getDaysAgo,
setStartOfDay,
setEndOfDay,
} from '../../common/utils/timezone.util';
interface FindAllQuery {
page?: number;
@@ -73,8 +78,7 @@ export class BidsService {
}
async cleanOldData() {
const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
const thirtyDaysAgo = getDaysAgo(30);
return this.bidRepository.delete({
createdAt: LessThan(thirtyDaysAgo),
});
@@ -91,9 +95,7 @@ export class BidsService {
}
async getRecentBids() {
const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
thirtyDaysAgo.setHours(0, 0, 0, 0);
const thirtyDaysAgo = setStartOfDay(getDaysAgo(30));
return this.bidRepository
.createQueryBuilder('bid')
@@ -118,14 +120,12 @@ export class BidsService {
const qb = this.bidRepository.createQueryBuilder('bid');
if (startDate) {
const start = new Date(startDate);
start.setHours(0, 0, 0, 0);
const start = setStartOfDay(new Date(startDate));
qb.andWhere('bid.publishDate >= :startDate', { startDate: start });
}
if (endDate) {
const end = new Date(endDate);
end.setHours(23, 59, 59, 999);
const end = setEndOfDay(new Date(endDate));
qb.andWhere('bid.publishDate <= :endDate', { endDate: end });
}

View File

@@ -76,6 +76,10 @@ const errorLogTransport = new DailyRotateFile({
export const winstonLogger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
format: logFormat,
transports: [consoleTransport, appLogTransport as any, errorLogTransport as any],
transports: [
consoleTransport,
appLogTransport as any,
errorLogTransport as any,
],
exitOnError: false,
});

View File

@@ -0,0 +1,98 @@
/**
* 时区工具函数
* 统一处理东八区(Asia/Shanghai)时间相关的操作
*/
const TIMEZONE_OFFSET = 8 * 60 * 60 * 1000;
/**
* 获取当前时间的东八区Date对象
* @returns Date 当前时间的东八区表示
*/
export function getCurrentDateInTimezone(): Date {
const now = new Date();
const utc = now.getTime() + now.getTimezoneOffset() * 60 * 1000;
return new Date(utc + TIMEZONE_OFFSET);
}
/**
* 将任意Date对象转换为东八区时间
* @param date 原始Date对象
* @returns Date 转换后的东八区时间
*/
export function convertToTimezone(date: Date): Date {
const utc = date.getTime() + date.getTimezoneOffset() * 60 * 1000;
return new Date(utc + TIMEZONE_OFFSET);
}
/**
* 格式化日期为 YYYY-MM-DD 格式
* @param date Date对象
* @returns 格式化后的日期字符串
*/
export function formatDate(date: Date): string {
const timezoneDate = convertToTimezone(date);
const year = timezoneDate.getFullYear();
const month = String(timezoneDate.getMonth() + 1).padStart(2, '0');
const day = String(timezoneDate.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
/**
* 格式化日期时间为 YYYY-MM-DD HH:mm:ss 格式
* @param date Date对象
* @returns 格式化后的日期时间字符串
*/
export function formatDateTime(date: Date): string {
const timezoneDate = convertToTimezone(date);
const year = timezoneDate.getFullYear();
const month = String(timezoneDate.getMonth() + 1).padStart(2, '0');
const day = String(timezoneDate.getDate()).padStart(2, '0');
const hours = String(timezoneDate.getHours()).padStart(2, '0');
const minutes = String(timezoneDate.getMinutes()).padStart(2, '0');
const seconds = String(timezoneDate.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
/**
* 设置时间为当天的开始时间 (00:00:00.000)
* @param date Date对象
* @returns 设置后的Date对象
*/
export function setStartOfDay(date: Date): Date {
const timezoneDate = convertToTimezone(date);
timezoneDate.setHours(0, 0, 0, 0);
return timezoneDate;
}
/**
* 设置时间为当天的结束时间 (23:59:59.999)
* @param date Date对象
* @returns 设置后的Date对象
*/
export function setEndOfDay(date: Date): Date {
const timezoneDate = convertToTimezone(date);
timezoneDate.setHours(23, 59, 59, 999);
return timezoneDate;
}
/**
* 获取指定天数前的日期
* @param days 天数
* @returns 指定天数前的Date对象
*/
export function getDaysAgo(days: number): Date {
const date = getCurrentDateInTimezone();
date.setDate(date.getDate() - days);
return date;
}
/**
* 解析日期字符串为东八区Date对象
* @param dateStr 日期字符串 (支持 YYYY-MM-DD 或 YYYY-MM-DD HH:mm:ss 格式)
* @returns 解析后的Date对象
*/
export function parseDateString(dateStr: string): Date {
const date = new Date(dateStr);
return convertToTimezone(date);
}

View File

@@ -139,7 +139,10 @@ export const CdtCrawler = {
logger.log(`Navigating to ${this.url}...`);
await delayRetry(
async () => {
await page.goto(this.url, { waitUntil: 'networkidle2', timeout: 60000 });
await page.goto(this.url, {
waitUntil: 'networkidle2',
timeout: 60000,
});
},
3,
5000,

View File

@@ -142,7 +142,10 @@ export const CeicCrawler = {
logger.log(`Navigating to ${this.url}...`);
await delayRetry(
async () => {
await page.goto(this.url, { waitUntil: 'networkidle2', timeout: 60000 });
await page.goto(this.url, {
waitUntil: 'networkidle2',
timeout: 60000,
});
},
3,
5000,

View File

@@ -148,7 +148,10 @@ export const CgnpcCrawler = {
logger.log(`Navigating to ${this.url}...`);
await delayRetry(
async () => {
await page.goto(this.url, { waitUntil: 'networkidle2', timeout: 60000 });
await page.goto(this.url, {
waitUntil: 'networkidle2',
timeout: 60000,
});
},
3,
5000,

View File

@@ -134,7 +134,10 @@ export const ChdtpCrawler = {
logger.log(`Navigating to ${this.url}...`);
await delayRetry(
async () => {
await page.goto(this.url, { waitUntil: 'networkidle2', timeout: 60000 });
await page.goto(this.url, {
waitUntil: 'networkidle2',
timeout: 60000,
});
},
3,
5000,

View File

@@ -148,7 +148,10 @@ export const CnncecpCrawler = {
logger.log(`Navigating to ${this.url}...`);
await delayRetry(
async () => {
await page.goto(this.url, { waitUntil: 'networkidle2', timeout: 60000 });
await page.goto(this.url, {
waitUntil: 'networkidle2',
timeout: 60000,
});
},
3,
5000,

View File

@@ -148,7 +148,10 @@ export const CnoocCrawler = {
logger.log(`Navigating to ${this.url}...`);
await delayRetry(
async () => {
await page.goto(this.url, { waitUntil: 'networkidle2', timeout: 60000 });
await page.goto(this.url, {
waitUntil: 'networkidle2',
timeout: 60000,
});
},
3,
5000,

View File

@@ -148,7 +148,10 @@ export const EpsCrawler = {
logger.log(`Navigating to ${this.url}...`);
await delayRetry(
async () => {
await page.goto(this.url, { waitUntil: 'networkidle2', timeout: 60000 });
await page.goto(this.url, {
waitUntil: 'networkidle2',
timeout: 60000,
});
},
3,
5000,

View File

@@ -148,7 +148,10 @@ export const PowerbeijingCrawler = {
logger.log(`Navigating to ${this.url}...`);
await delayRetry(
async () => {
await page.goto(this.url, { waitUntil: 'networkidle2', timeout: 60000 });
await page.goto(this.url, {
waitUntil: 'networkidle2',
timeout: 60000,
});
},
3,
5000,

View File

@@ -148,7 +148,10 @@ export const SdiccCrawler = {
logger.log(`Navigating to ${this.url}...`);
await delayRetry(
async () => {
await page.goto(this.url, { waitUntil: 'networkidle2', timeout: 60000 });
await page.goto(this.url, {
waitUntil: 'networkidle2',
timeout: 60000,
});
},
3,
5000,

View File

@@ -142,7 +142,10 @@ export const SzecpCrawler = {
logger.log(`Navigating to ${this.url}...`);
await delayRetry(
async () => {
await page.goto(this.url, { waitUntil: 'networkidle2', timeout: 60000 });
await page.goto(this.url, {
waitUntil: 'networkidle2',
timeout: 60000,
});
},
3,
5000,

View File

@@ -20,6 +20,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
database: configService.get<string>('DATABASE_NAME', 'bidding'),
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
synchronize: false,
timezone: '+08:00',
}),
}),
],