-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathdueDate.ts
More file actions
218 lines (189 loc) · 5.33 KB
/
dueDate.ts
File metadata and controls
218 lines (189 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import {
CalendarDate,
fromDate,
parseAbsolute,
parseDate,
parseDateTime,
ZonedDateTime,
} from "@internationalized/date";
import type { DueDate as ApiDueDate } from "@/api/domain/dueDate";
import type { Duration as ApiDuration } from "@/api/domain/task";
import { t } from "@/i18n";
import { locale } from "@/infra/locale";
import { now, timezone, today } from "@/infra/time";
export type DateInfo = {
raw: Date;
hasTime: boolean;
isOverdue: boolean;
isCurrentYear: boolean;
flag: "today" | "tomorrow" | "nextWeek" | "yesterday" | "lastWeek" | undefined;
};
export type DueDate = {
start: DateInfo;
end: DateInfo | undefined;
};
const parseDueDate = (dueDate: ApiDueDate, duration?: ApiDuration): DueDate => {
let start: ZonedDateTime | CalendarDate;
const hasTime = dueDate.date.includes("T");
if (hasTime) {
// If the datetime comes with a trailing Z, then the task has a fixed timezone. We respect
// this and convert it into their local timezone. Otherwise, it is a floating timezone and we
// simply parse it as a local datetime.
if (dueDate.date.endsWith("Z")) {
start = parseAbsolute(dueDate.date, timezone());
} else {
start = fromDate(parseDateTime(dueDate.date).toDate(timezone()), timezone());
}
} else {
start = parseDate(dueDate.date);
}
let end: ZonedDateTime | undefined;
if (duration !== undefined && start instanceof ZonedDateTime) {
switch (duration.unit) {
case "day":
end = start.add({
days: duration.amount,
});
break;
case "minute":
end = start.add({
minutes: duration.amount,
});
break;
default: {
const _: never = duration.unit;
}
}
}
return {
start: calculateDateInfo(start),
end: end !== undefined ? calculateDateInfo(end) : undefined,
};
};
const calculateDateInfo = (datetime: ZonedDateTime | CalendarDate): DateInfo => {
let hasTime = false;
let date: CalendarDate;
if (datetime instanceof ZonedDateTime) {
date = new CalendarDate(datetime.year, datetime.month, datetime.day);
hasTime = true;
} else {
date = datetime;
}
let flag: DateInfo["flag"];
if (date.compare(today()) === 0) {
flag = "today";
} else if (date.compare(today().add({ days: 1 })) === 0) {
flag = "tomorrow";
} else if (date.compare(today().add({ days: -1 })) === 0) {
flag = "yesterday";
} else if (date.compare(today().add({ days: -7 })) >= 0 && date.compare(today()) < 0) {
flag = "lastWeek";
} else if (date.compare(today().add({ days: 7 })) <= 0 && date.compare(today()) > 0) {
flag = "nextWeek";
}
return {
raw: datetime.toDate(timezone()),
hasTime,
isOverdue: datetime.compare(hasTime ? now() : today()) < 0,
isCurrentYear: datetime.year === today().year,
flag,
};
};
const getFormatter: (style: string) => Intl.DateTimeFormat = (() => {
const styles: Record<string, Intl.DateTimeFormatOptions> = {
time: {
timeStyle: "short",
},
weekday: {
weekday: "long",
},
date: {
month: "short",
day: "numeric",
},
dateWithYear: {
month: "short",
day: "numeric",
year: "numeric",
},
};
const cache: Record<string, Intl.DateTimeFormat> = {};
return (style: string): Intl.DateTimeFormat => {
if (cache[style]) {
return cache[style];
}
cache[style] = new Intl.DateTimeFormat(locale(), {
timeZone: timezone(),
...styles[style],
});
return cache[style];
};
})();
const formatDueDate = (due: DueDate): string => {
const date = formatDate(due.start);
if (!due.start.hasTime) {
return date;
}
const i18n = t().dates;
const time = getFormatter("time").format(due.start.raw);
if (due.end === undefined) {
return i18n.dateTime(date, time);
}
const endTime = getFormatter("time").format(due.end.raw);
if (isSameDay(due.start.raw, due.end.raw)) {
return i18n.dateTimeDuration(date, time, endTime);
}
const endDate = formatDate(due.end);
return i18n.dateTimeDurationDifferentDays(date, time, endDate, endTime);
};
const formatDate = (info: DateInfo): string => {
const i18n = t().dates;
switch (info.flag) {
case "today":
return i18n.today;
case "tomorrow":
return i18n.tomorrow;
case "yesterday":
return i18n.yesterday;
case "lastWeek":
return i18n.lastWeekday(getFormatter("weekday").format(info.raw));
case "nextWeek":
return getFormatter("weekday").format(info.raw);
default:
break;
}
if (!info.isCurrentYear) {
return getFormatter("dateWithYear").format(info.raw);
}
return getFormatter("date").format(info.raw);
};
const formatDueDateHeader = (due: DueDate): string => {
const parts = [
getFormatter("date").format(due.start.raw),
getFormatter("weekday").format(due.start.raw),
];
const i18n = t().dates;
switch (due.start.flag) {
case "today":
parts.push(i18n.today);
break;
case "tomorrow":
parts.push(i18n.tomorrow);
break;
default:
break;
}
return parts.join(" ‧ ");
};
export const DueDate = {
parse: parseDueDate,
format: formatDueDate,
formatHeader: formatDueDateHeader,
};
function isSameDay(a: Date, b: Date): boolean {
return (
a.getFullYear() === b.getFullYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
);
}