Skip to content

Commit e4a365e

Browse files
committed
wip
1 parent ac67fcb commit e4a365e

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

plugin/src/data/dueDate.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { DueDate as ApiDueDate } from "@/api/domain/dueDate";
2+
import type { Duration as ApiDuration } from "@/api/domain/task";
23
import { DueDate } from "@/data/dueDate";
34
import { CalendarDate, ZonedDateTime } from "@internationalized/date";
45
import { describe, expect, it, vi } from "vitest";
@@ -336,6 +337,28 @@ describe("parse", () => {
336337
}
337338
});
338339

340+
describe("parse with duration", () => {
341+
type Testcase = {
342+
description: string;
343+
input: {
344+
due: ApiDueDate;
345+
duration: ApiDuration;
346+
};
347+
expected: DueDate;
348+
};
349+
350+
// TODO: add test cases
351+
const testcases: Testcase[] = [];
352+
353+
for (const tc of testcases) {
354+
it(tc.description, () => {
355+
const output = DueDate.parse(tc.input.due, tc.input.duration);
356+
expect(output).toStrictEqual(tc.expected);
357+
});
358+
}
359+
});
360+
361+
// TODO: add test cases for dates with end.
339362
describe("format", () => {
340363
type Testcase = {
341364
description: string;

plugin/src/data/dueDate.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,24 @@ const getFormatter: (style: string) => Intl.DateTimeFormat = (() => {
130130
const formatDueDate = (due: DueDate): string => {
131131
const date = formatDate(due.start);
132132

133-
if (due.start.hasTime) {
134-
const i18n = t().dates;
135-
const time = getFormatter("time").format(due.start.raw);
133+
if (!due.start.hasTime) {
134+
return date;
135+
}
136136

137-
if (due.end !== undefined) {
138-
const endTime = getFormatter("time").format(due.end.raw);
139-
return i18n.dateTimeDuration(date, time, endTime);
140-
}
137+
const i18n = t().dates;
138+
const time = getFormatter("time").format(due.start.raw);
141139

140+
if (due.end === undefined) {
142141
return i18n.dateTime(date, time);
143142
}
144143

145-
return date;
144+
const endTime = getFormatter("time").format(due.end.raw);
145+
if (isSameDay(due.start.raw, due.end.raw)) {
146+
return i18n.dateTimeDuration(date, time, endTime);
147+
}
148+
149+
const endDate = formatDate(due.end);
150+
return i18n.dateTimeDurationDifferentDays(date, time, endDate, endTime);
146151
};
147152

148153
const formatDate = (info: DateInfo): string => {
@@ -193,3 +198,11 @@ export const DueDate = {
193198
format: formatDueDate,
194199
formatHeader: formatDueDateHeader,
195200
};
201+
202+
function isSameDay(a: Date, b: Date): boolean {
203+
return (
204+
a.getFullYear() === b.getFullYear() &&
205+
a.getMonth() === b.getMonth() &&
206+
a.getDate() === b.getDate()
207+
);
208+
}

plugin/src/i18n/langs/en.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,15 @@ export const en: Translations = {
212212
return `${date} at ${time}`;
213213
},
214214
dateTimeDuration: (date: string, startTime: string, endTime: string) => {
215-
return `${date} at ${startTime}-${endTime}`;
215+
return `${date} at ${startTime} - ${endTime}`;
216+
},
217+
dateTimeDurationDifferentDays: (
218+
startDate: string,
219+
startTime: string,
220+
endDate: string,
221+
endTime: string,
222+
): string => {
223+
return `${startDate} at ${startTime} - ${endDate} at ${endTime}`;
216224
},
217225
},
218226
};

plugin/src/i18n/translation.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,11 @@ export type Translations = {
193193
lastWeekday: (weekday: string) => string;
194194
dateTime: (date: string, time: string) => string;
195195
dateTimeDuration: (date: string, startTime: string, endTime: string) => string;
196+
dateTimeDurationDifferentDays: (
197+
startDate: string,
198+
startTime: string,
199+
endDate: string,
200+
endTime: string,
201+
) => string;
196202
};
197203
};

0 commit comments

Comments
 (0)