Skip to content

Commit

Permalink
fix(@axhxrx/date): fix bug caused by adding tests 😂
Browse files Browse the repository at this point in the history
  • Loading branch information
Bottie McBotface committed Jun 8, 2024
1 parent be4df47 commit 35ee17e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
21 changes: 21 additions & 0 deletions dateToIS08601WithTimeZoneOffset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,24 @@ Deno.test('format ignoring milliseconds', () =>
const result = dateToIS08601WithTimeZoneOffset(date, testTimezoneOffset);
assertEquals(result, '2023-10-01T21:00:00+09:00');
});

Deno.test('format a valid date with an overridden time zone offset', () =>
{
const date = new Date('2023-10-01T12:00:00Z');
const result = dateToIS08601WithTimeZoneOffset(date, 540);
assertEquals(result, '2023-10-01T03:00:00-09:00');
});

Deno.test('format a valid date with an overridden time zone offset into the previous day', () =>
{
const date = new Date('2023-10-01T01:00:00Z');
const result = dateToIS08601WithTimeZoneOffset(date, 540);
assertEquals(result, '2023-09-30T16:00:00-09:00');
});

Deno.test('format a valid date with an overridden time zone offset into the next day', () =>
{
const date = new Date('2023-10-01T23:00:00Z');
const result = dateToIS08601WithTimeZoneOffset(date, -480);
assertEquals(result, '2023-10-02T07:00:00+08:00');
});
17 changes: 10 additions & 7 deletions dateToIS08601WithTimeZoneOffset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export const dateToIS08601WithTimeZoneOffset = (

// Thanks, Obama! https://stackoverflow.com/questions/17415579/how-to-iso-8601-format-a-date-with-timezone-offset-in-javascript

const offset = timeZoneOffset ?? date.getTimezoneOffset();
const actualOffset = date.getTimezoneOffset();
const offset = timeZoneOffset ?? actualOffset;
const difference = offset - actualOffset;
const offsetDate = new Date(date.getTime() - (difference * 60 * 1000));

const tzo = -offset;
const dif = tzo >= 0 ? '+' : '-';
Expand All @@ -30,17 +33,17 @@ export const dateToIS08601WithTimeZoneOffset = (
};

return (
date.getFullYear()
offsetDate.getFullYear()
+ '-'
+ pad(date.getMonth() + 1)
+ pad(offsetDate.getMonth() + 1)
+ '-'
+ pad(date.getDate())
+ pad(offsetDate.getDate())
+ 'T'
+ pad(date.getHours())
+ pad(offsetDate.getHours())
+ ':'
+ pad(date.getMinutes())
+ pad(offsetDate.getMinutes())
+ ':'
+ pad(date.getSeconds())
+ pad(offsetDate.getSeconds())
+ dif
+ pad(tzo / 60)
+ ':'
Expand Down

0 comments on commit 35ee17e

Please sign in to comment.