Skip to content

Commit d10afa6

Browse files
committed
✨ Add all day events & improve timezone handling
1 parent 5d15fa5 commit d10afa6

File tree

3 files changed

+88
-22
lines changed

3 files changed

+88
-22
lines changed

index.test.ts

+47-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,29 @@ test("generate a google link", () => {
77
duration: [2, "hour"]
88
});
99
expect(link).toBe(
10-
"https://calendar.google.com/calendar/render?action=TEMPLATE&text=Birthday%20party&details=&location=&trp=&dates=20191228T230000Z%2F20191229T010000Z"
10+
"https://calendar.google.com/calendar/render?action=TEMPLATE&text=Birthday%20party&details=&location=&trp=&dates=20191229T000000Z%2F20191229T020000Z"
11+
);
12+
});
13+
14+
test("generate a google link with time & timezone", () => {
15+
const link = google({
16+
title: "Birthday party",
17+
start: "2019-12-29T12:00:00.000+01:00",
18+
duration: [2, "hour"]
19+
});
20+
expect(link).toBe(
21+
"https://calendar.google.com/calendar/render?action=TEMPLATE&text=Birthday%20party&details=&location=&trp=&dates=20191229T110000Z%2F20191229T130000Z"
22+
);
23+
});
24+
25+
test("generate an all day google link", () => {
26+
const link = google({
27+
title: "Birthday party",
28+
start: "2019-12-29",
29+
allDay: true
30+
});
31+
expect(link).toBe(
32+
"https://calendar.google.com/calendar/render?action=TEMPLATE&text=Birthday%20party&details=&location=&trp=&dates=20191229%2F20191230"
1133
);
1234
});
1335

@@ -19,7 +41,7 @@ test("generate a google link with guests", () => {
1941
2042
});
2143
expect(link).toBe(
22-
"https://calendar.google.com/calendar/render?action=TEMPLATE&text=Birthday%20party&details=&location=&trp=&dates=20191228T230000Z%2F20191229T010000Z&add=hello%40example.com%2Canother%40example.com"
44+
"https://calendar.google.com/calendar/render?action=TEMPLATE&text=Birthday%20party&details=&location=&trp=&dates=20191229T000000Z%2F20191229T020000Z&add=hello%40example.com%2Canother%40example.com"
2345
);
2446
});
2547

@@ -30,7 +52,18 @@ test("generate a yahoo link", () => {
3052
duration: [2, "hour"]
3153
});
3254
expect(link).toBe(
33-
"https://calendar.yahoo.com/?v=60&title=Birthday%20party&st=20191229T000000&et=20191229T020000&desc=&in_loc="
55+
"https://calendar.yahoo.com/?v=60&title=Birthday%20party&st=20191229T000000Z&et=20191229T020000Z&desc=&in_loc="
56+
);
57+
});
58+
59+
test("generate an all day yahoo link", () => {
60+
const link = yahoo({
61+
title: "Birthday party",
62+
start: "2019-12-29",
63+
allDay: true
64+
});
65+
expect(link).toBe(
66+
"https://calendar.yahoo.com/?v=60&title=Birthday%20party&st=20191229&et=20191230&desc=&in_loc="
3467
);
3568
});
3669

@@ -44,3 +77,14 @@ test("generate a outlook link", () => {
4477
"https://outlook.live.com/owa/?path=%2Fcalendar%2Faction%2Fcompose&rru=addevent&startdt=20191229T000000&enddt=20191229T020000&subject=Birthday%20party&body=&location="
4578
);
4679
});
80+
81+
test("generate an all day outlook link", () => {
82+
const link = outlook({
83+
title: "Birthday party",
84+
start: "2019-12-29",
85+
allDay: true
86+
});
87+
expect(link).toBe(
88+
"https://outlook.live.com/owa/?path=%2Fcalendar%2Faction%2Fcompose&rru=addevent&startdt=20191229&enddt=20191230&subject=Birthday%20party&body=&location="
89+
);
90+
});

index.ts

+40-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import dayjs from "dayjs";
2+
import utc from "dayjs/plugin/utc";
23
import { stringify } from "querystring";
34
import { CalendarEvent, Google, Outlook, Yahoo } from "./interfaces";
45

6+
dayjs.extend(utc);
7+
58
export const eventify = (event: CalendarEvent) => {
69
event.start = dayjs(event.start).toDate();
710
if (event.duration && event.duration.length && !event.end) {
@@ -11,32 +14,36 @@ export const eventify = (event: CalendarEvent) => {
1114
.add(duration, unit)
1215
.toDate();
1316
}
17+
if (event.allDay) {
18+
event.end = dayjs(event.start)
19+
.add(1, "day")
20+
.toDate();
21+
}
1422
return event;
1523
};
1624

25+
const formats = {
26+
dateTime: "YYYYMMDD[T]HHmmss",
27+
dateTimeUTC: "YYYYMMDD[T]HHmmss[Z]",
28+
allDay: "YYYYMMDD"
29+
};
30+
1731
export const google = (event: CalendarEvent) => {
1832
event = eventify(event);
19-
const startDate: string = dayjs(event.start)
20-
.toISOString()
21-
.replace(/-/g, "")
22-
.replace(/:/g, "")
23-
.replace(/\./g, "");
24-
const endDate: string = dayjs(event.end)
25-
.toISOString()
26-
.replace(/-/g, "")
27-
.replace(/:/g, "")
28-
.replace(/\./g, "");
33+
const format = event.allDay ? formats.allDay : formats.dateTimeUTC;
34+
const start: string = dayjs(event.start)
35+
.utc()
36+
.format(format);
37+
const end: string = dayjs(event.end)
38+
.utc()
39+
.format(format);
2940
const details: Google = {
3041
action: "TEMPLATE",
3142
text: event.title,
3243
details: event.description,
3344
location: event.location,
3445
trp: event.busy,
35-
dates:
36-
startDate.substring(0, startDate.length - 4) +
37-
"Z/" +
38-
endDate.substring(0, endDate.length - 4) +
39-
"Z"
46+
dates: start + "/" + end
4047
};
4148
if (event.guests && event.guests.length) {
4249
details.add = event.guests.join();
@@ -46,11 +53,18 @@ export const google = (event: CalendarEvent) => {
4653

4754
export const outlook = (event: CalendarEvent) => {
4855
event = eventify(event);
56+
const format = event.allDay ? formats.allDay : formats.dateTime;
57+
const start: string = dayjs(event.start)
58+
.utc()
59+
.format(format);
60+
const end: string = dayjs(event.end)
61+
.utc()
62+
.format(format);
4963
const details: Outlook = {
5064
path: "/calendar/action/compose",
5165
rru: "addevent",
52-
startdt: dayjs(event.start).format("YYYYMMDD[T]HHmmss"),
53-
enddt: dayjs(event.end).format("YYYYMMDD[T]HHmmss"),
66+
startdt: start,
67+
enddt: end,
5468
subject: event.title,
5569
body: event.description,
5670
location: event.location
@@ -60,11 +74,18 @@ export const outlook = (event: CalendarEvent) => {
6074

6175
export const yahoo = (event: CalendarEvent) => {
6276
event = eventify(event);
77+
const format = event.allDay ? formats.allDay : formats.dateTimeUTC;
78+
const start: string = dayjs(event.start)
79+
.utc()
80+
.format(format);
81+
const end: string = dayjs(event.end)
82+
.utc()
83+
.format(format);
6384
const details: Yahoo = {
6485
v: 60,
6586
title: event.title,
66-
st: dayjs(event.start).format("YYYYMMDD[T]HHmmss"),
67-
et: dayjs(event.end).format("YYYYMMDD[T]HHmmss"),
87+
st: start,
88+
et: end,
6889
desc: event.description,
6990
in_loc: event.location
7091
};

interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ interface CalendarEvent {
55
start: any;
66
end?: any;
77
duration?: [number, dayjs.UnitType];
8+
allDay?: boolean;
89
description?: string;
910
location?: string;
1011
busy?: boolean;

0 commit comments

Comments
 (0)