Skip to content

Commit d55465f

Browse files
daschaadreamorosi
andauthored
fix(logger): invalid time zone environment variables leads to error (#2865)
Co-authored-by: Andrea Amorosi <[email protected]>
1 parent 08a1d4d commit d55465f

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

Diff for: packages/logger/src/formatter/LogFormatter.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ abstract class LogFormatter implements LogFormatterInterface {
111111
*/
112112
#getDateFormatter = (timeZone: string): Intl.DateTimeFormat => {
113113
const twoDigitFormatOption = '2-digit';
114+
const validTimeZone = Intl.supportedValuesOf('timeZone').includes(timeZone)
115+
? timeZone
116+
: 'UTC';
114117

115118
return new Intl.DateTimeFormat('en', {
116119
year: 'numeric',
@@ -120,7 +123,7 @@ abstract class LogFormatter implements LogFormatterInterface {
120123
minute: twoDigitFormatOption,
121124
second: twoDigitFormatOption,
122125
hour12: false,
123-
timeZone,
126+
timeZone: validTimeZone,
124127
});
125128
};
126129

Diff for: packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ describe('Class: PowertoolsLogFormatter', () => {
320320
test('it formats the timestamp to ISO 8601, accounting for the `America/New_York` timezone offset', () => {
321321
// Prepare
322322
process.env.TZ = 'America/New_York';
323-
/*
323+
/*
324324
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
325325
The positive value indicates that `America/New_York` is behind UTC.
326326
*/
@@ -341,7 +341,7 @@ describe('Class: PowertoolsLogFormatter', () => {
341341
process.env.TZ = 'America/New_York';
342342
const mockDate = new Date('2016-06-20T12:08:10.910Z');
343343
jest.useFakeTimers().setSystemTime(mockDate);
344-
/*
344+
/*
345345
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
346346
The positive value indicates that `America/New_York` is behind UTC.
347347
*/
@@ -362,7 +362,7 @@ describe('Class: PowertoolsLogFormatter', () => {
362362
process.env.TZ = 'America/New_York';
363363
const mockDate = new Date('2016-06-20T00:08:10.910Z');
364364
jest.useFakeTimers().setSystemTime(mockDate);
365-
/*
365+
/*
366366
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
367367
The positive value indicates that `America/New_York` is behind UTC.
368368
*/
@@ -381,7 +381,7 @@ describe('Class: PowertoolsLogFormatter', () => {
381381
test('if `envVarsService` is not set, ensures timestamp is formatted to `UTC` even with `America/New_York` timezone', () => {
382382
// Prepare
383383
process.env.TZ = 'America/New_York';
384-
/*
384+
/*
385385
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
386386
The positive value indicates that `America/New_York` is behind UTC.
387387
*/
@@ -471,6 +471,22 @@ describe('Class: PowertoolsLogFormatter', () => {
471471
// Assess
472472
expect(timestamp).toEqual('2016-06-20T12:08:10.000Z');
473473
});
474+
475+
test('it is using UTC timezone when env var is set to :/etc/localtime', () => {
476+
// Prepare
477+
process.env.TZ = ':/etc/localtime';
478+
479+
jest.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(0);
480+
const formatter = new PowertoolsLogFormatter({
481+
envVarsService: new EnvironmentVariablesService(),
482+
});
483+
484+
// Act
485+
const timestamp = formatter.formatTimestamp(new Date());
486+
487+
// Assess
488+
expect(timestamp).toEqual('2016-06-20T12:08:10.000+00:00');
489+
});
474490
});
475491

476492
describe('Method: getCodeLocation', () => {

0 commit comments

Comments
 (0)