Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@
"moment": "^2.22.2",
"object-assign": "^4.1.1",
"optimized": "^1.2.0",
"oxlint": "^1.11.1",
"oxlint": "1.11.1",
"time-require": "^0.1.2",
"tsd": "^0.28.1",
"tshy": "^3.0.2",
"tshy-after": "^1.0.0",
"typescript": "^5.2.2"
},
"overrides": {
"@arethetypeswrong/[email protected]": {
"fflate": "0.8.2"
}
},
"homepage": "https://github.com/node-modules/utility",
"repository": {
"type": "git",
Expand Down
16 changes: 7 additions & 9 deletions src/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ import { LRU } from 'ylru';
const lru = new LRU(1000);

export function resetTimezone(date: Date) {
let TIMEZONE: string = '';
const offsetInMinutes = date.getTimezoneOffset();
const _hourOffset: number = Math.floor(-offsetInMinutes / 60);
const _minuteOffset: number = Math.abs(offsetInMinutes % 60);

TIMEZONE += _hourOffset >= 0 ? '+' : '-';
TIMEZONE += `${String(Math.abs(_hourOffset)).padStart(2, '0')}${String(_minuteOffset).padStart(2, '0')}`;

return TIMEZONE;
const sign = offsetInMinutes <= 0 ? '+' : '-';
const absoluteOffset = Math.abs(offsetInMinutes);
const hours = Math.floor(absoluteOffset / 60);
const minutes = absoluteOffset % 60;
return `${sign}${String(hours).padStart(2, '0')}${String(minutes).padStart(2, '0')}`;
}

const MONTHS: Record<string, string> = {
Expand All @@ -33,7 +30,8 @@ const MONTHS: Record<string, string> = {
};

export function getTimezone(d: Date) {
const key = `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`;
// The offset can change within a day, including during a repeated local hour.
const key = d.getTimezoneOffset();
const timeZone = lru.get(key);
if (timeZone === undefined) {
// Cache for 24 hours
Expand Down
57 changes: 57 additions & 0 deletions test/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,63 @@ describe('test/date.test.ts', () => {
});

describe('accessLogDate()', () => {
describe('timezone offsets', () => {
let originalTimezone: string | undefined;

beforeEach(() => {
originalTimezone = process.env.TZ;
});

afterEach(() => {
if (originalTimezone === undefined) {
delete process.env.TZ;
} else {
process.env.TZ = originalTimezone;
}
});

const transitions = [
{
name: 'spring forward',
dates: [ '2026-03-08T06:59:59Z', '2026-03-08T07:00:00Z' ],
expected: [ '08/Mar/2026:01:59:59 -0500', '08/Mar/2026:03:00:00 -0400' ],
},
{
name: 'fall back',
dates: [ '2026-11-01T05:30:00Z', '2026-11-01T06:30:00Z' ],
expected: [ '01/Nov/2026:01:30:00 -0400', '01/Nov/2026:01:30:00 -0500' ],
},
];

for (const { name, dates, expected } of transitions) {
for (const order of [ [ 0, 1 ], [ 1, 0 ] ]) {
it(`should use each date's offset during ${name} in order ${order}`, () => {
process.env.TZ = 'America/New_York';
for (const index of order) {
const date = new Date(dates[index]);
assert.equal(utility.accessLogDate(date), expected[index]);
assert.equal(utility.getDateFromMilliseconds(date.getTime(), utility.DateFormat.DateTimeWithTimeZone), expected[index]);
}
});
}
}

const timezones = [
[ 'America/St_Johns', '15/Jan/2026:08:30:00 -0330' ],
[ 'Pacific/Marquesas', '15/Jan/2026:02:30:00 -0930' ],
[ 'Asia/Kolkata', '15/Jan/2026:17:30:00 +0530' ],
[ 'Asia/Kathmandu', '15/Jan/2026:17:45:00 +0545' ],
[ 'UTC', '15/Jan/2026:12:00:00 +0000' ],
];

for (const [ timezone, expected ] of timezones) {
it(`should format the offset in ${timezone}`, () => {
process.env.TZ = timezone;
assert.equal(utility.accessLogDate(new Date('2026-01-15T12:00:00Z')), expected);
});
}
});

it('accessLogDate() should return an access log format date string', () => {
// 16/Apr/2013:16:40:09 +0800
assert.match(utility.accessLogDate(new Date()), /^(0[1-9]|[12]\d|3[01])\/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\/\d{4}:\d{2}:\d{2}:\d{2} [+-](0[0-9]|1[0-3])\d{2}$/);
Expand Down
Loading