Skip to content
This repository was archived by the owner on Jul 25, 2022. It is now read-only.

Commit 72ac0ae

Browse files
committed
feat: prefer UTC dates
1 parent 5c35cb4 commit 72ac0ae

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
- Output logs to time-rotated log files.
1010
- Optionally gzip logs once they are no longer current.
11+
- Dates are expressed in UTC only.
1112
- Stream pipe functionality coming soon.
1213

1314
## Install

src/index.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ function strfdatetime (str: string, date?: Date) {
2626
}
2727

2828
const dateFormatValues: [string, RegExp, string][] = [
29-
[ DATE_FORMATS[0][0], DATE_FORMATS[0][1], date.getFullYear().toString() ],
30-
[ DATE_FORMATS[1][0], DATE_FORMATS[1][1], ("0" + date.getMonth()).slice(-2) ],
31-
[ DATE_FORMATS[2][0], DATE_FORMATS[2][1], ("0" + date.getDate()).slice(-2) ],
32-
[ DATE_FORMATS[3][0], DATE_FORMATS[3][1], ("0" + date.getHours()).slice(-2) ],
33-
[ DATE_FORMATS[4][0], DATE_FORMATS[4][1], ("0" + date.getMinutes()).slice(-2) ],
34-
[ DATE_FORMATS[5][0], DATE_FORMATS[5][1], ("0" + date.getSeconds()).slice(-2) ]
29+
[ DATE_FORMATS[0][0], DATE_FORMATS[0][1], date.getUTCFullYear().toString() ],
30+
[ DATE_FORMATS[1][0], DATE_FORMATS[1][1], ("0" + date.getUTCMonth()).slice(-2) ],
31+
[ DATE_FORMATS[2][0], DATE_FORMATS[2][1], ("0" + date.getUTCDate()).slice(-2) ],
32+
[ DATE_FORMATS[3][0], DATE_FORMATS[3][1], ("0" + date.getUTCHours()).slice(-2) ],
33+
[ DATE_FORMATS[4][0], DATE_FORMATS[4][1], ("0" + date.getUTCMinutes()).slice(-2) ],
34+
[ DATE_FORMATS[5][0], DATE_FORMATS[5][1], ("0" + date.getUTCSeconds()).slice(-2) ]
3535
]
3636

3737
for (let i = 0; i < dateFormatValues.length; i++) {
@@ -52,9 +52,9 @@ function createWritableStream (filePath: string) {
5252
function getCurrentDate () {
5353
const now = getNow()
5454

55-
return now.getFullYear() + "-" +
56-
("0" + now.getMonth()).slice(-2) + "-" +
57-
("0" + now.getDate()).slice(-2)
55+
return now.getUTCFullYear() + "-" +
56+
("0" + now.getUTCMonth()).slice(-2) + "-" +
57+
("0" + now.getUTCDate()).slice(-2)
5858
}
5959

6060
/**
@@ -88,9 +88,9 @@ export function Logger (logFilePath: string, separator = "|") {
8888
const now = getNow()
8989
const timestamp = Date.now()
9090

91-
const currentDate = now.getFullYear() + "-" +
92-
("0" + now.getMonth()).slice(-2) + "-" +
93-
("0" + now.getDate()).slice(-2)
91+
const currentDate = now.getUTCFullYear() + "-" +
92+
("0" + now.getUTCMonth()).slice(-2) + "-" +
93+
("0" + now.getUTCDate()).slice(-2)
9494

9595
// If dates are different proceed to perform following actions:
9696
// 1. Compress current log file
@@ -111,12 +111,7 @@ export function Logger (logFilePath: string, separator = "|") {
111111
currentLogFilePath = strfdatetime(logFilePath)
112112
stream = createWritableStream(currentLogFilePath)
113113

114-
const datetime = currentDate + " " +
115-
("0" + now.getHours()).slice(-2) + ":" +
116-
("0" + now.getMinutes()).slice(-2) + ":" +
117-
("0" + now.getSeconds()).slice(-2)
118-
119-
str = datetime + separator + timestamp + separator + str.trim() + "\n"
114+
str = now.toISOString() + separator + timestamp + separator + str.trim() + "\n"
120115

121116
// Note: respect backpressure and avoid memory issues using the 'drain' event
122117
const isWritted = stream.write(str)
@@ -133,12 +128,7 @@ export function Logger (logFilePath: string, separator = "|") {
133128

134129
input.pipe(zlib.createGzip()).pipe(output)
135130
} else {
136-
const datetime = currentDate + " " +
137-
("0" + now.getHours()).slice(-2) + ":" +
138-
("0" + now.getMinutes()).slice(-2) + ":" +
139-
("0" + now.getSeconds()).slice(-2)
140-
141-
str = datetime + separator + timestamp + separator + str.trim() + "\n"
131+
str = now.toISOString() + separator + timestamp + separator + str.trim() + "\n"
142132

143133
// Note: respect backpressure and avoid memory issues using the 'drain' event
144134
const isWritted = stream.write(str)

0 commit comments

Comments
 (0)