Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

hotfix: fix time format issue #4108

Merged
merged 3 commits into from
Aug 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions ts/nni_manager/common/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ export class Logger {
return;
}

// `time.toLocaleString('sv')` trick does not work for Windows
const isoTime = new Date(new Date().toLocaleString() + ' UTC').toISOString();
const time = isoTime.slice(0, 10) + ' ' + isoTime.slice(11, 19);
const zeroPad = (num: number): string => num.toString().padStart(2, '0');
const now = new Date();
const date = now.getFullYear() + '-' + zeroPad(now.getMonth() + 1) + '-' + zeroPad(now.getDate());
const time = zeroPad(now.getHours()) + ':' + zeroPad(now.getMinutes()) + ':' + zeroPad(now.getSeconds());
const datetime = date + ' ' + time;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really tedious. But I guess there's no better way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The only better way may be using a third-party strftime library.


const levelName = levelNames.has(level) ? levelNames.get(level) : level.toString();

const message = args.map(arg => (typeof arg === 'string' ? arg : util.inspect(arg))).join(' ');

const record = `[${time}] ${levelName} (${this.name}) ${message}\n`;
const record = `[${datetime}] ${levelName} (${this.name}) ${message}\n`;

if (logFile === undefined) {
console.log(record);
Expand Down