Skip to content

Commit

Permalink
fix(logger): invalid time zone environment variables leads to error (#…
Browse files Browse the repository at this point in the history
…2865)

Co-authored-by: Andrea Amorosi <dreamorosi@gmail.com>
  • Loading branch information
daschaa and dreamorosi authored Aug 1, 2024
1 parent 08a1d4d commit d55465f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
5 changes: 4 additions & 1 deletion packages/logger/src/formatter/LogFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ abstract class LogFormatter implements LogFormatterInterface {
*/
#getDateFormatter = (timeZone: string): Intl.DateTimeFormat => {
const twoDigitFormatOption = '2-digit';
const validTimeZone = Intl.supportedValuesOf('timeZone').includes(timeZone)
? timeZone
: 'UTC';

return new Intl.DateTimeFormat('en', {
year: 'numeric',
Expand All @@ -120,7 +123,7 @@ abstract class LogFormatter implements LogFormatterInterface {
minute: twoDigitFormatOption,
second: twoDigitFormatOption,
hour12: false,
timeZone,
timeZone: validTimeZone,
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ describe('Class: PowertoolsLogFormatter', () => {
test('it formats the timestamp to ISO 8601, accounting for the `America/New_York` timezone offset', () => {
// Prepare
process.env.TZ = 'America/New_York';
/*
/*
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
The positive value indicates that `America/New_York` is behind UTC.
*/
Expand All @@ -341,7 +341,7 @@ describe('Class: PowertoolsLogFormatter', () => {
process.env.TZ = 'America/New_York';
const mockDate = new Date('2016-06-20T12:08:10.910Z');
jest.useFakeTimers().setSystemTime(mockDate);
/*
/*
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
The positive value indicates that `America/New_York` is behind UTC.
*/
Expand All @@ -362,7 +362,7 @@ describe('Class: PowertoolsLogFormatter', () => {
process.env.TZ = 'America/New_York';
const mockDate = new Date('2016-06-20T00:08:10.910Z');
jest.useFakeTimers().setSystemTime(mockDate);
/*
/*
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
The positive value indicates that `America/New_York` is behind UTC.
*/
Expand All @@ -381,7 +381,7 @@ describe('Class: PowertoolsLogFormatter', () => {
test('if `envVarsService` is not set, ensures timestamp is formatted to `UTC` even with `America/New_York` timezone', () => {
// Prepare
process.env.TZ = 'America/New_York';
/*
/*
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
The positive value indicates that `America/New_York` is behind UTC.
*/
Expand Down Expand Up @@ -471,6 +471,22 @@ describe('Class: PowertoolsLogFormatter', () => {
// Assess
expect(timestamp).toEqual('2016-06-20T12:08:10.000Z');
});

test('it is using UTC timezone when env var is set to :/etc/localtime', () => {
// Prepare
process.env.TZ = ':/etc/localtime';

jest.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(0);
const formatter = new PowertoolsLogFormatter({
envVarsService: new EnvironmentVariablesService(),
});

// Act
const timestamp = formatter.formatTimestamp(new Date());

// Assess
expect(timestamp).toEqual('2016-06-20T12:08:10.000+00:00');
});
});

describe('Method: getCodeLocation', () => {
Expand Down

0 comments on commit d55465f

Please sign in to comment.