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
5 changes: 5 additions & 0 deletions .changeset/ninety-cycles-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'chronoshift': patch
---

Fix minute.round with non-integer TZ offsets
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/duration/duration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,31 @@ describe('Duration', () => {
);
});

it('works for PT20M with Asia/Kolkata (UTC+5:30)', () => {
const pt20m = new Duration('PT20M');
const kolkata = Timezone.fromJS('Asia/Kolkata');

// 12:00 UTC = 17:30 IST → floor to 17:20 IST = 11:50 UTC
expect(pt20m.floor(new Date('2025-11-27T12:00:00Z'), kolkata)).toEqual(
new Date('2025-11-27T11:50:00.000Z'),
);

// 12:25 UTC = 17:55 IST → floor to 17:40 IST = 12:10 UTC
expect(pt20m.floor(new Date('2025-11-27T12:25:00Z'), kolkata)).toEqual(
new Date('2025-11-27T12:10:00.000Z'),
);
});

it('works for PT5M with Asia/Kolkata (UTC+5:30)', () => {
const pt5m = new Duration('PT5M');
const kolkata = Timezone.fromJS('Asia/Kolkata');

// 12:02 UTC = 17:32 IST → floor to 17:30 IST = 12:00 UTC
expect(pt5m.floor(new Date('2025-11-27T12:02:00Z'), kolkata)).toEqual(
new Date('2025-11-27T12:00:00.000Z'),
);
});

it('works for P2H', () => {
const pt2h = new Duration('PT2H');
expect(pt2h.floor(new Date('2013-09-29T03:02:03.456-07:00'), TZ_LA)).toEqual(
Expand Down
23 changes: 23 additions & 0 deletions src/floor-shift-ceil/floor-shift-ceil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,27 @@ describe('floor/shift/ceil', () => {
year.shift(new Date('2010-01-01T00:00:00-08:00'), tz, 1),
);
});

it('rounds minutes with non-integer timezone offsets', () => {
const kolkata = Timezone.fromJS('Asia/Kolkata');
const kathmandu = Timezone.fromJS('Asia/Kathmandu');

// Asia/Kolkata (UTC+5:30) with 20 minute boundary
expect(shifters.minute.round(new Date('2025-11-27T12:00:00Z'), 20, kolkata)).toEqual(
new Date('2025-11-27T11:50:00.000Z'),
);
expect(shifters.minute.round(new Date('2025-11-27T12:25:00Z'), 20, kolkata)).toEqual(
new Date('2025-11-27T12:10:00.000Z'),
);

// Asia/Kolkata (UTC+5:30) with 5 minute boundary
expect(shifters.minute.round(new Date('2025-11-27T12:02:00Z'), 5, kolkata)).toEqual(
new Date('2025-11-27T12:00:00.000Z'),
);

// Asia/Kathmandu (UTC+5:45) with 20 minute boundary
expect(shifters.minute.round(new Date('2025-11-27T12:00:00Z'), 20, kathmandu)).toEqual(
new Date('2025-11-27T11:55:00.000Z'),
);
});
});
21 changes: 17 additions & 4 deletions src/floor-shift-ceil/floor-shift-ceil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ export const second = timeShifterFiller({
},
});

// Movement by minute is tz independent because in every timezone a minute is 60 seconds
function minuteMove(dt: Date, _tz: Timezone, step: number) {
dt = new Date(dt.valueOf());
dt.setUTCMinutes(dt.getUTCMinutes() + step);
return dt;
}

export const minute = timeShifterFiller({
canonicalLength: 60000,
siblings: 60,
Expand All @@ -88,10 +95,16 @@ export const minute = timeShifterFiller({
dt.setUTCSeconds(0, 0);
return dt;
},
round: (dt, roundTo, _tz) => {
const cur = dt.getUTCMinutes();
const adj = floorTo(cur, roundTo);
if (cur !== adj) dt.setUTCMinutes(adj);
round: (dt, roundTo, tz) => {
if (tz.isUTC()) {
const cur = dt.getUTCMinutes();
const adj = floorTo(cur, roundTo);
if (cur !== adj) dt.setUTCMinutes(adj);
} else {
const cur = fromDate(dt, tz.toString()).minute;
const adj = floorTo(cur, roundTo);
if (cur !== adj) return minuteMove(dt, tz, adj - cur);
}
return dt;
},
shift: (dt, _tz, step) => {
Expand Down