Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
HUAHUAI23 committed Jul 24, 2024
1 parent 1b177df commit c83e70f
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions web/src/pages/app/mods/StatusBar/LogsModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,40 @@ type Log = {

const MAX_RETRIES = 5;

const timestampRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/;
// eslint-disable-next-line no-control-regex
const timestampRegex = /\u001b\[90m(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z)/g;
// const timestampRegex = new RegExp(
// "\\u001b\\[90m(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z)",
// "g",
// );

function convertLogTimestamp(log: Log): Log {
const match = log.data.match(timestampRegex);
function formatToCustomDateString(date: Date): string {
const pad = (num: number) => String(num).padStart(2, "0");
const padMillis = (num: number) => String(num).padStart(3, "0");

const year = date.getFullYear();
const month = pad(date.getMonth() + 1); // Months are zero-indexed
const day = pad(date.getDate());
const hours = pad(date.getHours());
const minutes = pad(date.getMinutes());
const seconds = pad(date.getSeconds());
const milliseconds = padMillis(date.getMilliseconds());

const timeZoneOffset = -date.getTimezoneOffset();
const offsetHours = pad(Math.floor(Math.abs(timeZoneOffset) / 60));
const offsetMinutes = pad(Math.abs(timeZoneOffset) % 60);
const offsetSign = timeZoneOffset >= 0 ? "+" : "-";
const timeZone = `T${hours}:${minutes}:${seconds}.${milliseconds}${offsetSign}${offsetHours}:${offsetMinutes}`;

if (match) {
const timestamp = match[0];
const date = new Date(timestamp);
const clientTimeString = date.toString();
log.data = log.data.replace(timestamp, clientTimeString);
}
return `${year}-${month}-${day}${timeZone}`;
}

function convertLogTimestamp(log: Log): Log {
log.data = log.data.replace(timestampRegex, (match, p1) => {
const date = new Date(p1);
const clientTimeString = formatToCustomDateString(date); // 将时间转换为带时区的客户端时间字符串
return `\u001b[90m${clientTimeString}`;
});

return log;
}
Expand Down

0 comments on commit c83e70f

Please sign in to comment.