Skip to content

Commit c01f4fd

Browse files
committed
Add and reorganize tests, add change log entry, cut few bytes
- Made tests less specific - Added tests for positive fractional time zones - The trick with assigning NaN cut few bytes from the function 219 B -> 214 B
1 parent 9f4960d commit c01f4fd

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ This change log follows the format documented in [Keep a CHANGELOG].
88
[semantic versioning]: http://semver.org/
99
[keep a changelog]: http://keepachangelog.com/
1010

11+
## v1.0.3 - 2024-09-22
12+
13+
This is yet another critical bug-fix release. Thank you to all the people who sent PRs and reported their issues.
14+
15+
### Fixes
16+
17+
- [Fixes negative fractional time zones like `America/St_Johns`](https://github.com/date-fns/tz/pull/7). Kudos to [@allohamora](https://github.com/allohamora)!
18+
1119
## v1.0.2 - 2024-09-14
1220

1321
This release fixes a couple of critical bugs in the previous release.

src/tzOffset/index.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,9 @@ export function tzOffset(timeZone: string | undefined, date: Date): number {
2525
const offsetStr = format(date).slice(6);
2626
if (offsetStr in offsetCache) return offsetCache[offsetStr]!;
2727

28-
const [hours, minutes = 0] = offsetStr.split(":").map(Number);
29-
30-
// type-guard
31-
if (hours === undefined) {
32-
return NaN;
33-
}
34-
35-
return (offsetCache[offsetStr] = hours > 0 ? hours * 60 + minutes : hours * 60 - minutes);
28+
const [hours = NaN, minutes = 0] = offsetStr.split(":").map(Number);
29+
return (offsetCache[offsetStr] =
30+
hours > 0 ? hours * 60 + minutes : hours * 60 - minutes);
3631
} catch {
3732
return NaN;
3833
}

src/tzOffset/tests.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,24 @@ describe("tzOffset", () => {
3939
expect(tzOffset("Europe/London", date)).toBe(0);
4040
});
4141

42-
it('works with America/St_Johns for Daylight Saving Time', () => {
43-
const date = new Date("2023-03-15T18:00:00.000Z");
44-
expect(tzOffset("America/St_Johns", date)).toBe(-150);
45-
});
46-
47-
it('works America/St_Johns for Standard Time', () => {
48-
const date = new Date("2023-03-03T18:00:00.000Z");
49-
expect(tzOffset("America/St_Johns", date)).toBe(-210);
50-
});
51-
5242
it("returns NaN if the offset the date or time zone are invalid", () => {
5343
expect(tzOffset("Etc/Invalid", new Date("2020-01-15T00:00:00Z"))).toBe(NaN);
5444
expect(tzOffset("America/New_York", new Date(NaN))).toBe(NaN);
5545
});
46+
47+
describe("fractional time zones", () => {
48+
it("works negative fractional time zones", () => {
49+
const dst = new Date("2023-03-15T18:00:00.000Z");
50+
const date = new Date("2023-03-03T18:00:00.000Z");
51+
expect(tzOffset("America/St_Johns", dst)).toBe(-150);
52+
expect(tzOffset("America/St_Johns", date)).toBe(-210);
53+
});
54+
55+
it("works positive fractional time zones", () => {
56+
const dst = new Date("2024-04-06T16:00:00.000Z");
57+
const date = new Date("2024-04-06T16:30:00.000Z");
58+
expect(tzOffset("Australia/Adelaide", dst)).toBe(630);
59+
expect(tzOffset("Australia/Adelaide", date)).toBe(570);
60+
});
61+
});
5662
});

0 commit comments

Comments
 (0)