Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vogievetsky committed Nov 27, 2024
1 parent fc8861f commit f279f68
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
24 changes: 11 additions & 13 deletions src/date-parser/date-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,17 @@ describe('date parser', () => {
).toBeUndefined();
});

it('date-time (tz = America/Los_Angeles)', () => {
const tz = Timezone.fromJS('America/Los_Angeles');
it('date-time (tz = America/New_York)', () => {
const tz = Timezone.fromJS('America/New_York');

expect(parseISODate('2001-02-03T04:05', tz), '2001-02-03T04:05').toEqual(
new Date(Date.UTC(2001, 1, 3, 4 + 8, 5, 0, 0)),
new Date(Date.UTC(2001, 1, 3, 4 + 5, 5, 0, 0)),
);
expect(parseISODate('2001-02-03T04:05:06', tz), '2001-02-03T04:05:06').toEqual(
new Date(Date.UTC(2001, 1, 3, 4 + 8, 5, 6, 0)),
new Date(Date.UTC(2001, 1, 3, 4 + 5, 5, 6, 0)),
);
expect(parseISODate('2001-02-03T04:05:06.007', tz), '2001-02-03T04:05:06.007').toEqual(
new Date(Date.UTC(2001, 1, 3, 4 + 8, 5, 6, 7)),
new Date(Date.UTC(2001, 1, 3, 4 + 5, 5, 6, 7)),
);

expect(parseISODate('2001-02-03T04:05Z', tz), '2001-02-03T04:05Z').toEqual(
Expand All @@ -231,25 +231,23 @@ describe('date parser', () => {
});

it('date-time (tz = null / local)', () => {
const tz: any = null;

expect(parseISODate('2001-02-03T04:05', tz), '2001-02-03T04:05').toEqual(
expect(parseISODate('2001-02-03T04:05', null), '2001-02-03T04:05').toEqual(
new Date(2001, 1, 3, 4, 5, 0, 0),
);
expect(parseISODate('2001-02-03T04:05:06', tz), '2001-02-03T04:05:06').toEqual(
expect(parseISODate('2001-02-03T04:05:06', null), '2001-02-03T04:05:06').toEqual(
new Date(2001, 1, 3, 4, 5, 6, 0),
);
expect(parseISODate('2001-02-03T04:05:06.007', tz), '2001-02-03T04:05:06.007').toEqual(
expect(parseISODate('2001-02-03T04:05:06.007', null), '2001-02-03T04:05:06.007').toEqual(
new Date(2001, 1, 3, 4, 5, 6, 7),
);

expect(parseISODate('2001-02-03T04:05Z', tz), '2001-02-03T04:05Z').toEqual(
expect(parseISODate('2001-02-03T04:05Z', null), '2001-02-03T04:05Z').toEqual(
new Date(Date.UTC(2001, 1, 3, 4, 5, 0, 0)),
);
expect(parseISODate('2001-02-03T04:05:06Z', tz), '2001-02-03T04:05:06Z').toEqual(
expect(parseISODate('2001-02-03T04:05:06Z', null), '2001-02-03T04:05:06Z').toEqual(
new Date(Date.UTC(2001, 1, 3, 4, 5, 6, 0)),
);
expect(parseISODate('2001-02-03T04:05:06.007Z', tz), '2001-02-03T04:05:06.007Z').toEqual(
expect(parseISODate('2001-02-03T04:05:06.007Z', null), '2001-02-03T04:05:06.007Z').toEqual(
new Date(Date.UTC(2001, 1, 3, 4, 5, 6, 7)),
);
});
Expand Down
16 changes: 10 additions & 6 deletions src/date-parser/date-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ export function parseSQLDate(type: string, v: string): Date {

// Taken from: https://github.com/csnover/js-iso8601/blob/lax/iso8601.js
const numericKeys = [1, 4, 5, 6, 10, 11];
export function parseISODate(date: string, timezone = Timezone.UTC): Date | undefined {
export function parseISODate(
date: string,
timezone: Timezone | null = Timezone.UTC,
): Date | undefined {
let struct: any;
let minutesOffset = 0;

Expand Down Expand Up @@ -182,14 +185,14 @@ export function parseISODate(date: string, timezone = Timezone.UTC): Date | unde
struct[3] = +struct[3] || 1;

// allow arbitrary sub-second precision beyond milliseconds
struct[7] = struct[7] ? +(struct[7] + '00').substr(0, 3) : 0;
struct[7] = struct[7] ? +(struct[7] + '00').slice(0, 3) : 0;

if (
(struct[8] === undefined || struct[8] === '') &&
(struct[9] === undefined || struct[9] === '') &&
!Timezone.UTC.equals(timezone)
!Timezone.UTC.equals(timezone || undefined)
) {
const d = new Date(
const dt = Date.UTC(
struct[1],
struct[2],
struct[3],
Expand All @@ -200,9 +203,10 @@ export function parseISODate(date: string, timezone = Timezone.UTC): Date | unde
);
if (timezone === null) {
// timezone explicitly set to null = use local timezone
return d;
return new Date(dt);
} else {
return fromDate(d, timezone.toString()).toDate();
const tzd = fromDate(new Date(dt), timezone.toString());
return new Date(dt - tzd.offset);
}
} else {
if (struct[8] !== 'Z' && struct[9] !== undefined) {
Expand Down

0 comments on commit f279f68

Please sign in to comment.