Skip to content

Commit 46506b5

Browse files
authored
Merge pull request microsoft#40095 from rhillefeld/master
Added zero-padding to timestamp output
2 parents 9c99870 + 4c5e463 commit 46506b5

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/compiler/core.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,18 +2250,26 @@ namespace ts {
22502250
}
22512251
}
22522252

2253-
export function padLeft(s: string, length: number) {
2254-
while (s.length < length) {
2255-
s = " " + s;
2256-
}
2257-
return s;
2258-
}
22592253

2260-
export function padRight(s: string, length: number) {
2261-
while (s.length < length) {
2262-
s = s + " ";
2263-
}
2254+
/**
2255+
* Returns string left-padded with spaces or zeros until it reaches the given length.
2256+
*
2257+
* @param s String to pad.
2258+
* @param length Final padded length. If less than or equal to 's.length', returns 's' unchanged.
2259+
* @param padString Character to use as padding (default " ").
2260+
*/
2261+
export function padLeft(s: string, length: number, padString: " " | "0" = " ") {
2262+
return length <= s.length ? s : padString.repeat(length - s.length) + s;
2263+
}
22642264

2265-
return s;
2265+
/**
2266+
* Returns string right-padded with spaces until it reaches the given length.
2267+
*
2268+
* @param s String to pad.
2269+
* @param length Final padded length. If less than or equal to 's.length', returns 's' unchanged.
2270+
* @param padString Character to use as padding (default " ").
2271+
*/
2272+
export function padRight(s: string, length: number, padString: " " = " ") {
2273+
return length <= s.length ? s : s + padString.repeat(length - s.length);
22662274
}
22672275
}

src/jsTyping/shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ namespace ts.server {
5757
export function nowString() {
5858
// E.g. "12:34:56.789"
5959
const d = new Date();
60-
return `${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}.${d.getMilliseconds()}`;
60+
return `${padLeft(d.getHours().toString(), 2, "0")}:${padLeft(d.getMinutes().toString(), 2, "0")}:${padLeft(d.getSeconds().toString(), 2, "0")}.${padLeft(d.getMilliseconds().toString(), 3, "0")}`;
6161
}
6262
}

0 commit comments

Comments
 (0)