Skip to content

Commit

Permalink
Fixes node-modules#36: timezone wrong on Pacific Daylight Time
Browse files Browse the repository at this point in the history
  • Loading branch information
abu-sufyan1 committed Jan 4, 2025
1 parent 1057de1 commit e40e367
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
9 changes: 3 additions & 6 deletions src/date.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// only set once.
let TIMEZONE = '';
export function resetTimezone() {
TIMEZONE = '';
const date = new Date();
export function resetTimezone(date: Date) {
let TIMEZONE = '';
const offsetInMinutes = date.getTimezoneOffset();
const _hourOffset = Math.floor(-offsetInMinutes / 60);
const _minuteOffset = Math.abs(offsetInMinutes % 60);
Expand All @@ -12,7 +9,6 @@ export function resetTimezone() {

return TIMEZONE;
}
resetTimezone();

const MONTHS: Record<string, string> = {
'01': 'Jan',
Expand Down Expand Up @@ -60,6 +56,7 @@ export function accessLogDate(d?: Date): string {
// 16/Apr/2013:16:40:09 +0800
d = d || new Date();
const [ year, month, date, hours, minutes, seconds ] = getDateStringParts(d);
const TIMEZONE = resetTimezone(d);
return `${date}/${MONTHS[month]}/${year}:${hours}:${minutes}:${seconds} ${TIMEZONE}`;
}

Expand Down
2 changes: 1 addition & 1 deletion test/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ describe('test/date.test.ts', () => {
it('should return access log date format', () => {
const milliseconds = Date.now();
const result = utility.getDateFromMilliseconds(milliseconds, utility.DateFormat.DateTimeWithTimeZone);
assert.match(result, /^\d{2}\/[A-Za-z]{3}\/\d{4}:\d{2}:\d{2}:\d{2} \+\d{4}$/);
assert.match(result, /^\d{2}\/[A-Za-z]{3}\/\d{4}:\d{2}:\d{2}:\d{2} [+-]\d{4}$/);
});

it('should return log date format with milliseconds', () => {
Expand Down

0 comments on commit e40e367

Please sign in to comment.