Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to using Internationalized date instead of moment timezone #32

Merged
merged 6 commits into from
Nov 27, 2024
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/khaki-apples-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'chronoshift': major
---

Switch to @internationalized/date
6 changes: 6 additions & 0 deletions .idea/prettier.xml

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

44 changes: 26 additions & 18 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"prettier": "@awesome-code-style/prettier-config",
"dependencies": {
"immutable-class": "^0.11.0",
"moment-timezone": "^0.5.26",
"@internationalized/date": "^3.5.6",
"tslib": "^2.8.1"
},
"devDependencies": {
Expand All @@ -75,7 +75,7 @@
"husky": "^2.4.1",
"immutable-class-tester": "^0.7.2",
"jest": "^29.7.0",
"jest-expect-message": "^1.0.2",
"jest-expect-message": "^1.1.3",
"prettier": "^3.4.1",
"ts-jest": "^29.2.5",
"typescript": "^5.7.2"
Expand Down
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
48 changes: 19 additions & 29 deletions src/date-parser/date-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/* eslint-disable @typescript-eslint/prefer-string-starts-ends-with */
/* eslint-disable no-useless-escape */

import moment from 'moment-timezone';
import { fromDate } from '@internationalized/date';

import { Duration } from '../duration/duration';
import { Timezone } from '../timezone/timezone';
Expand Down 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,41 +185,28 @@ 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 dt = Date.UTC(
struct[1],
struct[2],
struct[3],
struct[4],
struct[5],
struct[6],
struct[7],
);
if (timezone === null) {
// timezone explicitly set to null = use local timezone
return new Date(
struct[1],
struct[2],
struct[3],
struct[4],
struct[5],
struct[6],
struct[7],
);
return new Date(dt);
} else {
return new Date(
moment
.tz(
{
year: struct[1],
month: struct[2],
day: struct[3],
hour: struct[4],
minute: struct[5],
second: struct[6],
millisecond: struct[7],
},
timezone.toString(),
)
.valueOf(),
);
const tzd = fromDate(new Date(dt), timezone.toString());
return new Date(dt - tzd.offset);
}
} else {
if (struct[8] !== 'Z' && struct[9] !== undefined) {
Expand Down
8 changes: 4 additions & 4 deletions src/floor-shift-ceil/floor-shift-ceil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ describe('floor/shift/ceil', () => {
new Date('2012-11-04T01:00:00-07:00'),
);

expect(shifters.hour.floor(new Date('2012-11-04T01:30:00-08:00'), tz), 'C').toEqual(
new Date('2012-11-04T01:00:00-08:00'),
expect(shifters.hour.floor(new Date('2012-11-04T01:30:00-08:00'), tz)).toEqual(
new Date('2012-11-04T01:00:00-07:00'),
);

expect(shifters.hour.floor(new Date('2012-11-04T02:30:00-08:00'), tz), 'D').toEqual(
Expand Down Expand Up @@ -93,15 +93,15 @@ describe('floor/shift/ceil', () => {
pairwise(dates, (d1, d2) => expect(shifters.hour.shift(d1, tz, 1)).toEqual(d2));
});

it('shifts hour over DST 1', () => {
it('floors hour over DST 1', () => {
expect(shifters.hour.floor(new Date('2012-11-04T00:05:00-07:00'), tz)).toEqual(
new Date('2012-11-04T00:00:00-07:00'),
);
expect(shifters.hour.floor(new Date('2012-11-04T01:05:00-07:00'), tz)).toEqual(
new Date('2012-11-04T01:00:00-07:00'),
);
expect(shifters.hour.floor(new Date('2012-11-04T02:05:00-07:00'), tz)).toEqual(
new Date('2012-11-04T02:00:00-07:00'),
new Date('2012-11-04T01:00:00-07:00'),
);
expect(shifters.hour.floor(new Date('2012-11-04T03:05:00-07:00'), tz)).toEqual(
new Date('2012-11-04T03:00:00-07:00'),
Expand Down
Loading